def contract_vector_values(rows, query): """Contract vector values in a row of a database into one vector. Args: rows(Iterator[List[Any]]): The rows fetched from the database query(SqlQuery): The corresponding query- Returns: Iterator[List[Any]]: The rows with vectors being a single item in each row. """ rows = convert_values(rows, query) for row in rows: contracted_row = list() temp_vec = list() # collect the elements of the vectors here vector_datatype = None # iterate over the columns and look for vector columns for (t_alias, column), value in zip(query.columns, row): vector_datatype, is_vec_elem = handle_vector_item( column, value, query.datatypes[t_alias], temp_vec, vector_datatype, ) if is_vec_elem: continue if temp_vec: # add the vector to the result contracted_row.append(convert_to(temp_vec, vector_datatype)) temp_vec = list() vector_datatype, is_vec_elem = handle_vector_item( column, value, query.datatypes[t_alias], temp_vec, vector_datatype, ) if is_vec_elem: continue # non vectors are simply added to the result contracted_row.append(value) if temp_vec: # add the vector to the result contracted_row.append(convert_to(temp_vec, vector_datatype)) temp_vec = list() yield contracted_row
def convert_to_datatype(self, value): """Convert to the datatype of the value. Args: value(Any): The value to convert Returns: Any: The converted value """ return convert_to(value, self.datatype)
def convert_values(rows, query): """Convert the values in the database to the correct datatype. Args: rows(Iterator[Iterator[Any]]): The rows of the database. query(SqlQuery): The corresponding query. Yields: The rows with converted values. """ for row in rows: output = [] for value, (t_alias, column) in zip(row, query.columns): output.append(convert_to(value, query.datatypes[t_alias][column])) yield output
def create_recycle(oclass, kwargs, session, uid, fix_neighbors=True, _force=False): """Instantiate a cuds_object with a given session. If cuds_object with same uid is already in the session, this object will be reused. Args: oclass (Cuds): The OntologyClass of cuds_object to instantiate kwargs (Dict[str, Any]): The kwargs of the cuds_object session (Session): The session of the new Cuds object uid (Union[UUID, URIRef): The uid of the new Cuds object fix_neighbors (bool): Whether to remove the link from the old neighbors to this cuds object, defaults to True _force (bool): Skip sanity checks. Returns: Cuds: The created cuds object. """ from osp.core.cuds import Cuds from osp.core.session.wrapper_session import WrapperSession uid = convert_to(uid, "UID") 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) for rel in set(cuds_object._neighbors.keys()): if not fix_neighbors: del cuds_object._neighbors[rel] else: cuds_object.remove(rel=rel) change_oclass(cuds_object, oclass, kwargs, _force=_force) else: # create new if oclass is not None: cuds_object = oclass(uid=uid, session=session, **kwargs, _force=_force) else: cuds_object = Cuds(uid=uid, session=session, **kwargs) return cuds_object
def _to_cuds_object(json_obj, session, buffer_context, _force=False): """Transform a json serializable dict to a cuds_object. Args: json_obj (Dict[str, Any]): The json object to convert to a Cuds object. session (Session): The session to add the cuds object to. buffer_context (BufferContext): add the deserialized cuds object to the selected buffers. Returns: Cuds: The resulting cuds_object. """ if not isinstance(buffer_context, BufferContext): raise ValueError( "Not allowed to deserialize CUDS object " "with undefined buffer_context" ) with get_buffer_context_mngr(session, buffer_context): g = json_to_rdf(json_obj, rdflib.Graph()) try: this_s = next(s for s, p, _ in g if p != rdflib.RDF.type) except StopIteration: this_s = next(s for s, p, _ in g) triples, neighbor_triples = set(), set() for s, p, o in g: if s == this_s: # datatype conversion if ( isinstance(o, rdflib.Literal) and o.datatype and o.datatype in rdflib_cuba and "VECTOR" in o.datatype.toPython() ): o = rdflib.Literal( convert_to(ast.literal_eval(o.toPython()), o.datatype), datatype=o.datatype, lang=o.language, ) triples.add((s, p, o)) else: neighbor_triples.add((s, p, o)) cuds = create_from_triples( triples, neighbor_triples, session, fix_neighbors=False ) return cuds
def deserialize(json_obj, session, buffer_context, _force=False): """Deserialize a json object, instantiate the Cuds object in there. Args: json_obj (Union[Dict, List, str, None]): The json object to deserialize. session (Session): When creating a cuds_object, use this session. buffer_context (BufferContext): add the deserialized cuds objects to the selected buffers. Raises: ValueError: The json object could not be deserialized. Returns: Union[Cuds, UUID, List[Cuds], List[UUID], None]: The deserialized object. """ if json_obj is None: return None if isinstance(json_obj, (str, int, float)): return json_obj if ( isinstance(json_obj, list) and json_obj and isinstance(json_obj[0], dict) and "@id" in json_obj[0] ): return _to_cuds_object( json_obj, session, buffer_context, _force=_force ) if isinstance(json_obj, list): return [ deserialize(x, session, buffer_context, _force=_force) for x in json_obj ] if isinstance(json_obj, dict) and set(["UID"]) == set(json_obj.keys()): return convert_to(json_obj["UID"], "UID") if isinstance(json_obj, dict) and set(["ENTITY"]) == set(json_obj.keys()): return get_entity(json_obj["ENTITY"]) if isinstance(json_obj, dict): return { k: deserialize(v, session, buffer_context, _force=_force) for k, v in json_obj.items() } raise ValueError("Could not deserialize %s." % json_obj)
def __init__( self, attributes: Dict[OntologyAttribute, Any], oclass: OntologyEntity, session: Session = None, uid: uuid.UUID = None ): """Initialize a CUDS object. This method should not be called by the user directly. Instead use the __call__ magic method of OntologyClass. Construct the CUDS object. This will also register the CUDS objects in the corresponding session. Args: attributes (Dict[OntologyAttribute, Any]): Mapping from ontology attribute to specified value. oclass (OntologyEntity): The ontology class of the CUDS object. session (Session, optional): The session associated with the CUDS, if None is given it will be associated with the CoreSession. Defaults to None. uid (uuid.UUID, optional): A unique identifier. If None given, a random uid will be created. Defaults to None. Raises: ValueError: Uid of zero is not allowed. """ self._session = session or Cuds._session self._graph = rdflib.Graph() self.__uid = uuid.uuid4() if uid is None else convert_to(uid, "UUID") if self.__uid.int == 0: raise ValueError("Invalid UUID") for k, v in attributes.items(): self._graph.add(( self.iri, k.iri, rdflib.Literal(k.convert_to_datatype(v), datatype=k.datatype) )) if oclass: self._graph.add(( self.iri, rdflib.RDF.type, oclass.iri )) self.session._store(self)
def _import_rdf_custom_datatypes( triple: Tuple[Any, Any, Any] ) -> Tuple[Any, Any, Any]: """Auxiliary function for `import_rdf`. Handles custom datatypes in a triple (if any). """ s, p, o = triple # handle custom datatype: VECTORs if ( isinstance(o, rdflib.Literal) and o.datatype and o.datatype in rdflib_cuba and "VECTOR" in o.datatype.toPython() ): o = rdflib.Literal( convert_to(ast.literal_eval(o.toPython()), o.datatype), datatype=o.datatype, lang=o.language, ) return s, p, o