def test_construct_remove_condition(self):
     """Test construction a remove condition."""
     # from rel table
     r = sorted(self.session._queries(
         pattern=(iri_from_uid(uuid.UUID(int=1)), city.hasInhabitant.iri,
                  iri_from_uid(uuid.UUID(int=2))),
         mode="delete"),
                key=lambda x: x[1])
     self.assertEqual(len(r), 1)
     self.assertEqual(r[0][1], RELATIONSHIP_TABLE)
     self.assertEqual(r[0][2], rdflib.XSD.integer)
     self.assertEqual(
         r[0][0],
         AndCondition(
             EqualsCondition(RELATIONSHIP_TABLE, "s", 1,
                             rdflib.XSD.integer),
             EqualsCondition(RELATIONSHIP_TABLE, "p", 42,
                             rdflib.XSD.integer),
             EqualsCondition(RELATIONSHIP_TABLE, "o", 2,
                             rdflib.XSD.integer)))
     # from types table
     r = sorted(self.session._queries(pattern=(iri_from_uid(
         uuid.UUID(int=1)), rdflib.RDF.type, city.City.iri),
                                      mode="delete"),
                key=lambda x: x[1])
     self.assertEqual(len(r), 1)
     self.assertEqual(r[0][1], TYPES_TABLE)
     self.assertEqual(r[0][2], rdflib.XSD.integer)
     self.assertEqual(
         r[0][0],
         AndCondition(
             EqualsCondition(TYPES_TABLE, "s", 1, rdflib.XSD.integer),
             EqualsCondition(TYPES_TABLE, "o", 42, rdflib.XSD.integer)))
Beispiel #2
0
 def _rows_to_triples(self, cursor, table_name, object_datatype):
     for row in cursor:
         s = rdflib.URIRef(iri_from_uid(row[0]))
         x = rdflib.URIRef(self._get_ns(row[1]) + row[2])
         if table_name == self.TYPES_TABLE:
             yield s, rdflib.RDF.type, x
         elif table_name == self.RELATIONSHIP_TABLE:
             o = rdflib.URIRef(iri_from_uid(row[3]))
             yield s, x, o
         else:
             yield s, x, rdflib.Literal(row[3], datatype=object_datatype)
 def _load_first_level(self):
     triple = (iri_from_uid(self.root), None, None)
     triple = next(self._substitute_root_iri([triple]))
     iris = {
         o
         for s, p, o in self._triples(triple)
         if isinstance(o, rdflib.URIRef) and str(o).startswith(
             CUDS_IRI_PREFIX) and uid_from_iri(o) != uuid.UUID(int=0)
     }
     iris.add(iri_from_uid(self.root))
     for iri in iris:
         self._load_by_iri(iri)
 def _substitute_zero_iri(self, triples):
     from osp.core.utils import CUDS_IRI_PREFIX
     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.UUID(
                 int=0) else x for x in triple)
 def test_get_values(self):
     """Test the get values method for adding triples."""
     v = self.session._get_values(
         (iri_from_uid(uuid.UUID(int=1)), city.hasInhabitant.iri,
          iri_from_uid(uuid.UUID(int=2))), RELATIONSHIP_TABLE)
     self.assertEqual(v, (1, 42, 2))
     v = self.session._get_values(
         (iri_from_uid(uuid.UUID(int=1)), city.coordinates.iri,
          rdflib.Literal(np.array([1, 2]),
                         datatype=rdflib_cuba["_datatypes/VECTOR-INT-2"])),
         data_tbl("VECTOR-INT-2"))
     np.testing.assert_equal(v, (1, 42, np.array([1, 2])))
     v = self.session._get_values(
         (iri_from_uid(uuid.UUID(int=1)), rdflib.XSD.type, city.City.iri),
         TYPES_TABLE)
     self.assertEqual(v, (1, 42))
Beispiel #6
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)
Beispiel #7
0
    def _setitem(self, uid, oclasses):
        """Add the UUID of a related CUDS object to the dictionary.

        Also add the oclass of the related CUDS object.
        """
        iri = iri_from_uid(uid)
        self.cuds_object._check_valid_add(oclasses, self.rel)
        self.graph.add((self.cuds_object.iri, self.rel.iri, iri))
        for oclass in oclasses:
            self.graph.add((iri, rdflib.RDF.type, oclass.iri))
    def _load_by_iri(self, iri):
        """Load the CUDS object wit the given IRI.

        Args:
            iri (rdflib.IRI): The IRI of the CUDS object to oad.

        Returns:
            Cuds - The CUDS object with the given IRI.
        """
        if iri == iri_from_uid(self.root):
            iri = iri_from_uid(uuid.UUID(int=0))
        triples, neighbor_triples = self._load_triples_for_iri(iri)

        triples = self._substitute_zero_iri(triples)
        neighbor_triples = self._substitute_zero_iri(neighbor_triples)

        return create_from_triples(triples=triples,
                                   neighbor_triples=neighbor_triples,
                                   session=self,
                                   fix_neighbors=False)
    def test_rows_to_triples(self):
        """Test transforming sql table rows to triples."""
        # relationship table
        cursor = iter([
            (uuid.UUID(int=1), 2, "hasInhabitant", uuid.UUID(int=2)),
            (uuid.UUID(int=1), 1, "activeRelationship", uuid.UUID(int=3))
        ])
        triples = list(
            self.session._rows_to_triples(cursor, RELATIONSHIP_TABLE,
                                          rdflib.XSD.integer))
        self.assertEqual(
            triples,
            [(iri_from_uid(uuid.UUID(int=1)), city.hasInhabitant.iri,
              iri_from_uid(uuid.UUID(int=2))),
             (iri_from_uid(uuid.UUID(int=1)), cuba.activeRelationship.iri,
              iri_from_uid(uuid.UUID(int=3)))])

        # types table
        cursor = iter([(uuid.UUID(int=1), 2, "City"),
                       (uuid.UUID(int=2), 1, "Entity")])
        triples = list(
            self.session._rows_to_triples(cursor, TYPES_TABLE,
                                          rdflib.XSD.integer))
        self.assertEqual(
            sorted(triples),
            sorted([(iri_from_uid(uuid.UUID(int=1)), rdflib.RDF.type,
                     city.City.iri),
                    (iri_from_uid(uuid.UUID(int=2)), rdflib.RDF.type,
                     cuba.Entity.iri)]))

        # data table
        cursor = iter([(uuid.UUID(int=1), 2, "coordinates", np.array([1, 2])),
                       (uuid.UUID(int=2), 1, "attribute", np.array([3, 4]))])
        triples = list(
            self.session._rows_to_triples(
                cursor, data_tbl("VECTOR-INT-2"),
                rdflib_cuba["_datatypes/VECTOR-INT-2"]))
        self.assertEqual(triples, [
            (iri_from_uid(uuid.UUID(int=1)), city.coordinates.iri,
             rdflib.Literal(np.array([1, 2]),
                            datatype=rdflib_cuba["_datatypes/VECTOR-INT-2"])),
            (iri_from_uid(uuid.UUID(int=2)), cuba.attribute.iri,
             rdflib.Literal(np.array([3, 4]),
                            datatype=rdflib_cuba["_datatypes/VECTOR-INT-2"]))
        ])
 def _load_from_backend(self, uids, expired=None):
     for uid in uids:
         iri = iri_from_uid(uid)
         yield self._load_by_iri(iri)
    def test_queries_subject_given(self):
        """Test computing the queries corresponding to a triple pattern."""
        r = sorted(
            self.session._queries(pattern=(iri_from_uid(uuid.UUID(int=1)),
                                           None, None)),
            key=lambda x: x[1])
        self.assertEqual(len(r), 4)
        self.assertEqual(r[0][1], data_tbl("VECTOR-2-2"))
        self.assertEqual(r[1][1], data_tbl("XSD_string"))
        self.assertEqual(r[2][1], RELATIONSHIP_TABLE)
        self.assertEqual(r[3][1], TYPES_TABLE)
        self.assertEqual(r[0][2], rdflib_cuba["_datatypes/VECTOR-2-2"])
        self.assertEqual(r[1][2], rdflib.XSD.string)
        self.assertEqual(r[2][2], rdflib.XSD.integer)
        self.assertEqual(r[3][2], rdflib.XSD.integer)
        self.assertEqual(r[0][0].order, ["ts", "tp", data_tbl("VECTOR-2-2")])

        # first query
        self.assertEqual(
            r[0][0]._columns, {
                "ts": ["uid"],
                "tp": ["ns_idx", "name"],
                data_tbl("VECTOR-2-2"): ["o___0", "o___1", "o___2", "o___3"]
            })
        self.assertEqual(
            r[0][0].condition,
            AndCondition(
                JoinCondition(data_tbl("VECTOR-2-2"), "s", "ts", "cuds_idx"),
                JoinCondition(data_tbl("VECTOR-2-2"), "p", "tp", "entity_idx"),
                EqualsCondition("ts", "uid", str(uuid.UUID(int=1)), "UUID")))
        self.assertEqual(
            r[0][0].datatypes, {
                data_tbl("VECTOR-2-2"): {
                    "o": rdflib_cuba["_datatypes/VECTOR-2-2"],
                    "o___0": rdflib.XSD.float,
                    "o___1": rdflib.XSD.float,
                    "o___2": rdflib.XSD.float,
                    "o___3": rdflib.XSD.float
                },
                "ts": {
                    "uid": "UUID",
                    "cuds_idx": rdflib.XSD.integer
                },
                "tp": {
                    "name": rdflib.XSD.string,
                    "ns_idx": rdflib.XSD.integer,
                    "entity_idx": rdflib.XSD.integer
                }
            })
        self.assertEqual(
            r[0][0].tables, {
                "ts": CUDS_TABLE,
                "tp": ENTITIES_TABLE,
                data_tbl("VECTOR-2-2"): data_tbl("VECTOR-2-2")
            })

        # second query
        self.assertEqual(r[1][0]._columns, {
            "ts": ["uid"],
            "tp": ["ns_idx", "name"],
            data_tbl("XSD_string"): ["o"]
        })
        self.assertEqual(
            r[1][0].condition,
            AndCondition(
                JoinCondition(data_tbl("XSD_string"), "s", "ts", "cuds_idx"),
                JoinCondition(data_tbl("XSD_string"), "p", "tp", "entity_idx"),
                EqualsCondition("ts", "uid", str(uuid.UUID(int=1)), "UUID")))
        self.assertEqual(
            r[1][0].datatypes, {
                data_tbl("XSD_string"): {
                    "o": rdflib.XSD.string
                },
                "ts": {
                    "uid": "UUID",
                    "cuds_idx": rdflib.XSD.integer
                },
                "tp": {
                    "name": rdflib.XSD.string,
                    "ns_idx": rdflib.XSD.integer,
                    "entity_idx": rdflib.XSD.integer
                }
            })
        self.assertEqual(
            r[1][0].tables, {
                "ts": CUDS_TABLE,
                "tp": ENTITIES_TABLE,
                data_tbl("XSD_string"): data_tbl("XSD_string")
            })

        # third query
        self.assertEqual(
            r[2][0]._columns, {
                "ts": ["uid"],
                "tp": ["ns_idx", "name"],
                RELATIONSHIP_TABLE: [],
                "to": ["uid"]
            })
        self.assertEqual(
            r[2][0].condition,
            AndCondition(
                JoinCondition(RELATIONSHIP_TABLE, "s", "ts", "cuds_idx"),
                JoinCondition(RELATIONSHIP_TABLE, "p", "tp", "entity_idx"),
                JoinCondition(RELATIONSHIP_TABLE, "o", "to", "cuds_idx"),
                EqualsCondition("ts", "uid", str(uuid.UUID(int=1)), "UUID")))
        self.assertEqual(
            r[2][0].datatypes, {
                "ts": {
                    "uid": "UUID",
                    "cuds_idx": rdflib.XSD.integer
                },
                "tp": {
                    "name": rdflib.XSD.string,
                    "ns_idx": rdflib.XSD.integer,
                    "entity_idx": rdflib.XSD.integer
                },
                "to": {
                    "uid": "UUID",
                    "cuds_idx": rdflib.XSD.integer
                },
                RELATIONSHIP_TABLE: {}
            })
        self.assertEqual(
            r[2][0].tables, {
                "ts": CUDS_TABLE,
                "tp": ENTITIES_TABLE,
                "to": CUDS_TABLE,
                RELATIONSHIP_TABLE: RELATIONSHIP_TABLE
            })

        # fourth query
        self.assertEqual(r[3][0]._columns, {
            "ts": ["uid"],
            TYPES_TABLE: [],
            "to": ["ns_idx", "name"]
        })
        self.assertEqual(
            r[3][0].condition,
            AndCondition(
                JoinCondition(TYPES_TABLE, "s", "ts", "cuds_idx"),
                JoinCondition(TYPES_TABLE, "o", "to", "entity_idx"),
                EqualsCondition("ts", "uid", str(uuid.UUID(int=1)), "UUID")))
        self.assertEqual(
            r[3][0].datatypes, {
                "ts": {
                    "uid": "UUID",
                    "cuds_idx": rdflib.XSD.integer
                },
                "to": {
                    "name": rdflib.XSD.string,
                    "ns_idx": rdflib.XSD.integer,
                    "entity_idx": rdflib.XSD.integer
                },
                TYPES_TABLE: {}
            })
        self.assertEqual(r[3][0].tables, {
            "ts": CUDS_TABLE,
            "to": ENTITIES_TABLE,
            TYPES_TABLE: TYPES_TABLE
        })
    def test_queries_object_given(self):
        """Test the _queries method when the object is given."""
        # type given
        r = sorted(self.session._queries(pattern=(None, None, city.City.iri)),
                   key=lambda x: x[1])
        self.assertEqual(len(r), 1)
        self.assertEqual(r[0][1:], (TYPES_TABLE, rdflib.XSD.integer))
        self.assertEqual(r[0][0]._columns, {
            "ts": ["uid"],
            "to": ["ns_idx", "name"],
            TYPES_TABLE: []
        })
        self.assertEqual(
            r[0][0].condition,
            AndCondition(
                JoinCondition(TYPES_TABLE, "s", "ts", "cuds_idx"),
                JoinCondition(TYPES_TABLE, "o", "to", "entity_idx"),
                EqualsCondition("to", "ns_idx", 2, rdflib.XSD.integer),
                EqualsCondition("to", "name", "City", rdflib.XSD.string)))
        self.assertEqual(
            r[0][0].datatypes, {
                "ts": {
                    "uid": "UUID",
                    "cuds_idx": rdflib.XSD.integer
                },
                "to": {
                    "name": rdflib.XSD.string,
                    "ns_idx": rdflib.XSD.integer,
                    "entity_idx": rdflib.XSD.integer
                },
                TYPES_TABLE: {}
            })
        self.assertEqual(r[0][0].tables, {
            "ts": CUDS_TABLE,
            "to": ENTITIES_TABLE,
            TYPES_TABLE: TYPES_TABLE
        })

        # UUID given
        r = sorted(
            self.session._queries(pattern=(None, None,
                                           iri_from_uid(uuid.UUID(int=1)))),
            key=lambda x: x[1])
        self.assertEqual(len(r), 1)
        self.assertEqual(r[0][1:], (RELATIONSHIP_TABLE, rdflib.XSD.integer))
        self.assertEqual(
            r[0][0]._columns, {
                "ts": ["uid"],
                "tp": ["ns_idx", "name"],
                RELATIONSHIP_TABLE: [],
                "to": ["uid"]
            })
        self.assertEqual(
            r[0][0].condition,
            AndCondition(
                JoinCondition(RELATIONSHIP_TABLE, "s", "ts", "cuds_idx"),
                JoinCondition(RELATIONSHIP_TABLE, "p", "tp", "entity_idx"),
                JoinCondition(RELATIONSHIP_TABLE, "o", "to", "cuds_idx"),
                EqualsCondition("to", "uid", str(uuid.UUID(int=1)), "UUID")))
        self.assertEqual(
            r[0][0].datatypes, {
                "ts": {
                    "uid": "UUID",
                    "cuds_idx": rdflib.XSD.integer
                },
                "tp": {
                    "name": rdflib.XSD.string,
                    "ns_idx": rdflib.XSD.integer,
                    "entity_idx": rdflib.XSD.integer
                },
                "to": {
                    "uid": "UUID",
                    "cuds_idx": rdflib.XSD.integer
                },
                RELATIONSHIP_TABLE: {}
            })
        self.assertEqual(
            r[0][0].tables, {
                "ts": CUDS_TABLE,
                "tp": ENTITIES_TABLE,
                "to": CUDS_TABLE,
                RELATIONSHIP_TABLE: RELATIONSHIP_TABLE
            })
Beispiel #13
0
 def iri(self):
     """Get the IRI of the CUDS object."""
     return iri_from_uid(self.uid)
Beispiel #14
0
 def _delitem(self, uid):
     """Delete an item from the dictionary."""
     iri = iri_from_uid(uid)
     self.graph.remove((self.cuds_object.iri, self.rel.iri, iri))