diff --git a/osp/core/cuds.py b/osp/core/cuds.py index b1bbfd44..fe4eee1e 100644 --- a/osp/core/cuds.py +++ b/osp/core/cuds.py @@ -6,8 +6,8 @@ import logging from uuid import uuid4, UUID -from typing import Union, List, Iterator, Dict, Any, Optional, Tuple -from rdflib import URIRef, RDF, Graph, Literal +from typing import Union, List, Iterator, Dict, Any, Optional, Tuple, Iterable +from rdflib import URIRef, BNode, RDF, Graph, Literal from osp.core.namespaces import cuba, from_iri from osp.core.ontology.relationship import OntologyRelationship from osp.core.ontology.attribute import OntologyAttribute @@ -41,9 +41,11 @@ def __init__(self, session: Session = None, iri: URIRef = None, uid: Union[UUID, URIRef] = None, - # Create an empty CUDS and add the triples externally - # afterwards. - _from_triples: bool = False): + # Specify extra triples for the CUDS object. + extra_triples: Iterable[ + Tuple[Union[URIRef, BNode], + Union[URIRef, BNode], + Union[URIRef, BNode]]] = tuple()): """Initialize a CUDS object.""" # Set uid. This is a "user-facing" method, so strict types # checks are performed. @@ -71,7 +73,17 @@ def __init__(self, self._graph.add(( self.iri, RDF.type, oclass.iri )) - elif not _from_triples: + extra_oclass = False + for s, p, o in extra_triples: + if s != self.iri: + raise ValueError("Trying to add extra triples to a CUDS " + "object with a subject that does not match " + "the CUDS object's IRI.") + elif p == RDF.type: + extra_oclass = True + self._graph.add((s, p, o)) + oclass_assigned = bool(oclass) or extra_oclass + if not oclass_assigned: raise TypeError(f"No oclass associated with {self}! " f"Did you install the required ontology?") diff --git a/osp/core/utils/wrapper_development.py b/osp/core/utils/wrapper_development.py index 73fff972..9f51aa40 100644 --- a/osp/core/utils/wrapper_development.py +++ b/osp/core/utils/wrapper_development.py @@ -225,15 +225,17 @@ def create_from_triples(triples, neighbor_triples, session, 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, - _from_triples=True) + extra_triples=set(triples)) # add the triples - for triple in set(triples) | set(neighbor_triples): + for triple in set(neighbor_triples): session.graph.add(triple) if isinstance(session, WrapperSession): session._store_checks(cuds_object) diff --git a/tests/test_api_city.py b/tests/test_api_city.py index 3755891f..408c3573 100644 --- a/tests/test_api_city.py +++ b/tests/test_api_city.py @@ -673,11 +673,10 @@ def test_add_multi_session(self): def test_cuds_without_oclass(self): """Tries to create a cuds without oclass. - Should fail except if the argument _from_triples is set to True. + Should always fail. """ self.assertRaises(TypeError, Cuds, oclass=None, attributes={}) - self.assertTrue(Cuds(oclass=None, attributes={}, _from_triples=True)) if __name__ == '__main__':