Exemple #1
0
    def _determine_table(self, triple):
        def data_table(datatype):
            return (get_data_table_name(datatype),
                    dict(**self.DATATYPES[self.DATA_TABLE_PREFIX], o=datatype))

        rel_table = (self.RELATIONSHIP_TABLE,
                     self.DATATYPES[self.RELATIONSHIP_TABLE])
        types_table = (self.TYPES_TABLE, self.DATATYPES[self.TYPES_TABLE])
        s, p, o = triple

        # object given
        if isinstance(o, rdflib.URIRef) and str(o).startswith(CUDS_IRI_PREFIX):
            return rel_table
        if isinstance(o, rdflib.URIRef):
            return types_table
        if isinstance(o, rdflib.Literal) and o.datatype:
            return data_table(o.datatype)
        # predicate given
        if p == rdflib.RDF.type:
            return types_table
        if p is not None:
            from osp.core.namespaces import from_iri
            predicate = from_iri(p)
            if isinstance(predicate, OntologyRelationship):
                return rel_table
            return data_table(predicate.datatype or rdflib.XSD.string)
Exemple #2
0
 def oclasses(self):
     """Get the ontology classes of this CUDS object."""
     result = list()
     for o in self._graph.objects(self.iri, RDF.type):
         r = from_iri(o, raise_error=False)
         if r is not None:
             result.append(r)
     return result
Exemple #3
0
 def oclasses(self):
     """Get the ontology classes of this CUDS object."""
     result = list()
     for s, p, o in self._graph.triples((self.iri, rdflib.RDF.type, None)):
         r = from_iri(o, raise_error=False)
         if r is not None:
             result.append(r)
     return result
Exemple #4
0
 def _getitem(self, uid):
     """Get the oclass of the object with the given UUID."""
     iri = iri_from_uid(uid)
     if (self.cuds_object.iri, self.rel.iri, iri) in self.graph:
         result = list()
         for _, _, o in self.graph.triples((iri, rdflib.RDF.type, None)):
             result.append(from_iri(o))
         return result
     raise KeyError(uid)
Exemple #5
0
 def get_attributes(self):
     """Get the attributes as a dictionary."""
     if self.session:
         self.session._notify_read(self)
     result = {}
     for s, p, o in self._graph.triples((self.iri, None, None)):
         obj = from_iri(p, raise_error=False)
         if isinstance(obj, OntologyAttribute):
             result[obj] = o.toPython()
     return result
Exemple #6
0
 def _iter(self):
     """Iterate over the dictionary."""
     predicates = set([
         p for _, p, _ in self.graph.triples((self.cuds_object.iri,
                                              None, None))
     ])
     for p in predicates:
         if (p, rdflib.RDF.type, rdflib.OWL.ObjectProperty) \
                 in _namespace_registry._graph:
             yield from_iri(p)
Exemple #7
0
 def get_attributes(self):
     """Get the attributes as a dictionary."""
     if self.session:
         self.session._notify_read(self)
     result = {}
     for s, p, o in self._graph.triples((self.iri, None, None)):
         obj = from_iri(p, raise_error=False)
         if isinstance(obj, OntologyAttribute):
             value = self._rdflib_5_inplace_modification_prevention_filter(
                 o.toPython(), obj)
             result[obj] = value
     return result
Exemple #8
0
 def _iter(self):
     """Iterate over the dictionary."""
     predicates = set(self.graph.predicates(self.cuds_object.iri, None))
     # Using set(..) instead of the iterator directly makes the code 2x
     #  faster.
     for p in predicates:
         try:
             obj = from_iri(p)
             if isinstance(obj, OntologyRelationship):
                 yield obj
         except KeyError:
             pass
Exemple #9
0
    def test_namespace_registry_from_iri(self):
        """Test getting namespaces from iri."""
        self.installer.install("city")
        ns_iri = rdflib.URIRef("http://www.osp-core.com/city#")
        city_iri = ns_iri + "City"
        hasPart_iri = ns_iri + "hasPart"
        self.modify_labels()

        c = self.namespace_registry.from_iri(rdflib_cuba.Entity)
        self.assertIsInstance(c, OntologyClass)
        self.assertEqual(c.namespace.get_name(), "cuba")
        self.assertEqual(c.name, "Entity")
        r = self.namespace_registry.from_iri(rdflib_cuba.relationship)
        self.assertIsInstance(r, OntologyRelationship)
        self.assertEqual(r.namespace.get_name(), "cuba")
        self.assertEqual(r.name, "relationship")
        a = self.namespace_registry.from_iri(rdflib_cuba.attribute)
        self.assertIsInstance(a, OntologyAttribute)
        self.assertEqual(a.namespace.get_name(), "cuba")
        self.assertEqual(a.name, "attribute")
        c = self.namespace_registry.from_iri(city_iri)
        self.assertIsInstance(c, OntologyClass)
        self.assertEqual(c.namespace.get_name(), "city")
        self.assertEqual(c.name, "City")
        r = self.namespace_registry.from_iri(hasPart_iri)
        self.assertIsInstance(r, OntologyRelationship)
        self.assertEqual(r.namespace.get_name(), "city")
        self.assertEqual(r.name, "hasPart")
        from osp.core.namespaces import from_iri
        import osp.core.namespaces
        old_ns_reg = osp.core.namespaces._namespace_registry
        try:
            osp.core.namespaces._namespace_registry = self.namespace_registry
            c = from_iri(rdflib_cuba.Entity)
            self.assertIsInstance(c, OntologyClass)
            self.assertEqual(c.namespace.get_name(), "cuba")
            self.assertEqual(c.name, "Entity")

            self.graph.add((ns_iri, rdflib_cuba._reference_by_label,
                            rdflib.Literal(True)))
            c = self.namespace_registry.from_iri(city_iri)
            self.assertIsInstance(c, OntologyClass)
            self.assertEqual(c.namespace.get_name(), "city")
            self.assertEqual(c.name, "City_T")
            r = self.namespace_registry.from_iri(hasPart_iri)
            self.assertIsInstance(r, OntologyRelationship)
            self.assertEqual(r.namespace.get_name(), "city")
            self.assertEqual(r.name, "hasPart_T")

            # undefined namespace
            self.graph.add(
                (rdflib.URIRef("a/b#c"), rdflib.RDF.type, rdflib.OWL.Class))
            self.graph.add(
                (rdflib.URIRef("d/e/f"), rdflib.RDF.type, rdflib.OWL.Class))
            a = from_iri("a/b#c")
            b = from_iri("d/e/f")
            self.assertIsInstance(a, OntologyClass)
            self.assertEqual(a.namespace.get_name(), "a/b#")
            self.assertEqual(a.name, "c")
            self.assertIsInstance(b, OntologyClass)
            self.assertEqual(b.namespace.get_name(), "d/e/")
            self.assertEqual(b.name, "f")
        finally:
            osp.core.namespaces._namespace_registry = old_ns_reg