Skip to content

Commit

Permalink
Stop assigning an UID to imported CUDS with custom IRIs (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
kysrpex authored Feb 25, 2022
1 parent 635eb42 commit 272329f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 36 deletions.
4 changes: 2 additions & 2 deletions osp/core/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from abc import ABC, abstractmethod
from osp.core.session.registry import Registry
from osp.core.session.result import returns_query_result
from osp.core.utils.general import uid_from_general_iri
from osp.core.utils.general import uid_from_iri


class Session(ABC):
Expand Down Expand Up @@ -66,7 +66,7 @@ def load_from_iri(self, *iris):
Yields:
Cuds: The fetched Cuds objects.
"""
return self.load(*[uid_from_general_iri(iri, self.graph)[0]
return self.load(*[uid_from_iri(iri)
for iri in iris])

@returns_query_result
Expand Down
7 changes: 2 additions & 5 deletions osp/core/session/transport/transport_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _silent_warn(*args, **kwargs) -> None:
from osp.core.ontology.entity import OntologyEntity
from osp.core.session.buffers import BufferContext, get_buffer_context_mngr
from osp.core.utils.wrapper_development import create_from_triples
from osp.core.utils.general import uid_from_general_iri
from osp.core.utils.general import uid_from_iri
from osp.core.ontology.cuba import rdflib_cuba

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -436,10 +436,7 @@ def import_rdf(graph, session, buffer_context, return_uid=None):
triples = map(_import_rdf_custom_datatypes, triples)
uid_triples = dict()
for s, p, o in triples:
if isinstance(o, rdflib.URIRef) \
and p not in (rdflib.RDF.type, rdflib.OWL.sameAs):
_, o = uid_from_general_iri(o, session.graph)
s_uid, s = uid_from_general_iri(s, session.graph)
s_uid = uid_from_iri(s)
session.graph.add((s, p, o))
uid_triples[s_uid] = uid_triples.get(s_uid, set())
uid_triples[s_uid].add((s, p, o))
Expand Down
29 changes: 0 additions & 29 deletions osp/core/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,35 +318,6 @@ def uid_from_iri(iri):
return iri


def uid_from_general_iri(iri, graph, _visited=frozenset()):
"""Get a UUID from a general (not containing a UUID) IRI.
Args:
iri (UriRef): The IRI to convert to UUID.
graph (Graph): The rdflib Graph to look for different IRIs for the
same individual.
_visited (Frozenset): Used for recursive calls.
Returns:
Tuple[UUID, URIRef]: The UUID and an IRI containing this UUID.
"""
if str(iri).startswith(CUDS_IRI_PREFIX):
return uid_from_iri(iri), iri

for _, _, x in graph.triples((iri, OWL.sameAs, None)):
if x not in _visited:
return uid_from_general_iri(x, graph, _visited | {iri})
for x, _, _ in graph.triples((None, OWL.sameAs, iri)):
if x not in _visited:
return uid_from_general_iri(x, graph, _visited | {iri})
uid = uuid.uuid4()
new_iri = iri_from_uid(uid)
# The order is important.
# (iri, OWL.sameAs, iri_new) would produce new CUDS.
graph.add((new_iri, OWL.sameAs, iri))
return uid, new_iri


def get_custom_datatypes():
"""Get the set of all custom datatypes used in the ontology.
Expand Down

0 comments on commit 272329f

Please sign in to comment.