Beispiel #1
0
 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.anyURI, city.City.iri),
         TYPES_TABLE,
     )
     # The next assertion should pass using any value for the predicate
     # in the previous function.
     self.assertEqual(v, (1, 42))
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)
Beispiel #3
0
 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 #4
0
 def _load_first_level(self):
     root_iri = iri_from_uid(self.root)
     zero_iri = iri_from_uid(UUID(int=0))
     triple = (root_iri, None, None)
     triple = next(self._substitute_root_iri([triple]))
     iris = {
         o
         for s, p, o in self._triples(triple) if isinstance(o, URIRef)
         and self._is_cuds_iri_ontology(o) and o != zero_iri
     }
     iris |= {root_iri}
     for _ in self._load_by_iri(*iris):
         pass  # Just exhaust the iterator so that CUDS are actually loaded.
Beispiel #5
0
    def test_load_by_iri(self):
        """Test if it is possible to load objects by their IRIs."""
        c = city.City(name="Freiburg")
        p1 = city.Citizen(name="Peter")
        p2 = city.Citizen(name="Anna")
        p3 = city.Citizen(name="Julia")
        c.add(p1, p2, p3, rel=city.hasInhabitant)
        p1.add(p3, rel=city.hasChild)
        p2.add(p3, rel=city.hasChild)

        with SqliteSession(DB) as session:
            wrapper = city.CityWrapper(session=session)
            wrapper.add(c)
            session.commit()

        with SqliteSession(DB) as session:
            wrapper = city.CityWrapper(session=session)
            cs = wrapper.get(c.uid)
            r = session.load_from_iri(cs.iri)
            self.assertIs(next(r), cs)
            r = session.load_from_iri(p1.iri, p2.iri, p3.iri)
            self.assertEqual(set(r), {p1, p2, p3})

        with SqliteSession(DB) as session:
            wrapper = city.CityWrapper(session=session)
            cs = wrapper.get(c.uid)
            r = session.load_from_iri(iri_from_uid(uuid.UUID(int=1)))
            self.assertEqual(set(r), {None})
Beispiel #6
0
 def _contains(self, item):
     """Checks if an item belongs to the dictionary."""
     return (
         self.cuds_object.iri,
         self.rel.iri,
         iri_from_uid(item),
     ) in self.graph
Beispiel #7
0
 def get_cuds_idx_0_1(self, uid):
     """Get CUDS index when migrating from v0 to v1."""
     cuds_iri = str(iri_from_uid(uid))
     if cuds_iri not in self.cuds:
         self.cuds[cuds_iri] = self.session._do_db_insert(
             "OSP_V1_CUDS", ["uid"], [str(uid)], {"uid": "UID"})
     cuds_idx = self.cuds[cuds_iri]
     return cuds_idx
Beispiel #8
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 #9
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))
Beispiel #10
0
    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)
Beispiel #11
0
    def _load_by_iri(self, *iris: URIRef):
        """Load the CUDS objects with the given IRIs.

        Args:
            iris: The IRIs of the CUDS objects to oad.

        Returns:
            Cuds - The CUDS object with the given IRI.
        """
        root_iri = iri_from_uid(self.root)
        zero_iri = iri_from_uid(UUID(int=0))
        iris = map(lambda x: x if x != root_iri else zero_iri, iris)

        for triples, neighbor_triples in self._load_triples_for_iris(*iris):
            triples = self._substitute_zero_iri(triples)
            neighbor_triples = self._substitute_zero_iri(neighbor_triples)
            yield create_from_triples(
                triples=triples,
                neighbor_triples=neighbor_triples,
                session=self,
                fix_neighbors=False,
            )
Beispiel #12
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))
Beispiel #13
0
 def _load_from_backend(self, uids, expired=None):
     yield from self._load_by_iri(*(iri_from_uid(uid) for uid in uids))
Beispiel #14
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)
Beispiel #15
0
    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": "UID", "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)), "UID"),
            ),
        )
        self.assertEqual(
            r[0][0].datatypes,
            {
                "ts": {"uid": "UID", "cuds_idx": rdflib.XSD.integer},
                "tp": {
                    "name": rdflib.XSD.string,
                    "ns_idx": rdflib.XSD.integer,
                    "entity_idx": rdflib.XSD.integer,
                },
                "to": {"uid": "UID", "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 #16
0
    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"],
                    ),
                ),
            ],
        )
Beispiel #17
0
    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)), "UID"),
            ),
        )
        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": "UID", "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)), "UID"),
            ),
        )
        self.assertEqual(
            r[1][0].datatypes,
            {
                data_tbl("XSD_string"): {"o": rdflib.XSD.string},
                "ts": {"uid": "UID", "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)), "UID"),
            ),
        )
        self.assertEqual(
            r[2][0].datatypes,
            {
                "ts": {"uid": "UID", "cuds_idx": rdflib.XSD.integer},
                "tp": {
                    "name": rdflib.XSD.string,
                    "ns_idx": rdflib.XSD.integer,
                    "entity_idx": rdflib.XSD.integer,
                },
                "to": {"uid": "UID", "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)), "UID"),
            ),
        )
        self.assertEqual(
            r[3][0].datatypes,
            {
                "ts": {"uid": "UID", "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},
        )