def _is_cuds_iri(self, iri): uid = uid_from_iri(rdflib.URIRef(iri)) return ( uid in self._registry.keys() or uid == uuid.UUID(int=0) or iri.startswith(CUDS_IRI_PREFIX) )
def _load_by_oclass(self, oclass): uids = { uid_from_iri(s) for s, _, _ in self._triples((None, RDF.type, oclass.iri)) } uids = {x if x != UUID(int=0) else self.root for x in uids} yield from self._load_from_backend(uids)
def _iter(self): """Iterate over the over the UUIDs of the related CUDS objects. Yields: UUID: The UUIDs of the CUDS object related with self.rel. """ for o in self.graph.objects(self.cuds_object.iri, self.rel.iri): yield uid_from_iri(o)
def __getitem__(self, variable_name): """Get the value of the given variable. Handles wrapper IRIs and datatype conversion. """ iri = self._get(variable_name) if (iri is not None and iri.startswith(CUDS_IRI_PREFIX) and uid_from_iri(iri) == uuid.UUID(int=0)): iri = iri_from_uid(self.session.root) return self._check_datatype(variable_name, iri)
def load_from_iri(self, *iris): """Load the cuds_objects with the given iris. Args: *iri (URIRef): The IRIs of the cuds_objects to load. Yields: Cuds: The fetched Cuds objects. """ return self.load(*[uid_from_iri(iri) for iri in iris])
def create_from_triples(triples, neighbor_triples, session, fix_neighbors=True): """Create a CUDS object from triples. Args: triples (List[Tuple]): The list of triples of the CUDS object to create. neighbor_triples (List[Tuple]): A list of important triples of neighbors, most importantly their types. session (Session): The session to create the CUDS object in. fix_neighbors (bool): Whether to remove the link from the old neighbors to this cuds object, defaults to True. """ from osp.core.cuds import Cuds from osp.core.session.wrapper_session import WrapperSession from osp.core.utils.general import uid_from_iri triples = list(triples) if not triples: return None uid = uid_from_iri(triples[0][0]) if isinstance(session, WrapperSession) and uid in session._expired: session._expired.remove(uid) # recycle old object if uid in session._registry: cuds_object = session._registry.get(uid) cuds_object.session._notify_read(cuds_object) if fix_neighbors: rels = set(cuds_object._neighbors.keys()) for rel in rels: cuds_object.remove(rel=rel) session.graph.remove((cuds_object.iri, None, None)) for triple in set(triples): session.graph.add(triple) else: # create new cuds_object = Cuds( attributes={}, oclass=None, session=session, uid=uid, extra_triples=set(triples), ) # add the triples for triple in set(neighbor_triples): session.graph.add(triple) if isinstance(session, WrapperSession): session._store_checks(cuds_object) cuds_object.session._notify_update(cuds_object) return cuds_object
def import_rdf(graph, session, buffer_context, return_uid=None): """Import RDF Graph to CUDS. Args: graph (rdflib.Graph): The graph to import. session (Session): The session to add the CUDS objects to. buffer_context (BufferContext): add the deserialized cuds objects to the selected buffers. return_uid (Union[UUID, URIRef]): Return only the object with the given uid. Raises: ValueError: Not allowed to deserialize with undefined buffer context. Returns: List[Cuds]: The deserialized CUDS objects. """ if not isinstance(buffer_context, BufferContext): raise ValueError( "Not allowed to deserialize CUDS object " "with undefined buffer_context" ) get_buffer_context_mngr(session, buffer_context) triples = (triple for triple in graph if _import_rdf_filter(triple)) triples = map(_import_rdf_custom_datatypes, triples) uid_triples = dict() for s, p, o in triples: 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)) result = list() for uid, t in uid_triples.items(): # Create entry in the registry x = create_from_triples(t, set(), session, fix_neighbors=False) if return_uid is None or uid == return_uid: result.append(x) return result if not return_uid else result[0]
def _substitute_zero_iri(self, triples): for triple in triples: yield tuple( iri_from_uid(self.root) if x is not None and x.startswith( CUDS_IRI_PREFIX) and uid_from_iri(x) == UUID(int=0) else x for x in triple)