Beispiel #1
0
    def test_get_neighbor_diff(self):
        """Test get_neighbor_diff method."""
        c1 = city.City(name="Paris")
        c2 = city.City(name="Berlin")
        c3 = city.City(name="London")
        n1 = city.Neighborhood(name="Zähringen")
        n2 = city.Neighborhood(name="Herdern")
        s1 = city.Street(name="Waldkircher Straße")
        s2 = city.Street(name="Habsburger Straße")
        s3 = city.Street(name="Lange Straße")

        n1.add(c1, c2, rel=city.isPartOf)
        n2.add(c2, c3, rel=city.isPartOf)
        n1.add(s1, s2)
        n2.add(s2, s3)

        self.assertEqual(set(get_neighbor_diff(n1, n2)),
                         {(c1.uid, city.isPartOf), (s1.uid, city.hasPart)})

        self.assertEqual(set(get_neighbor_diff(n2, n1)),
                         {(c3.uid, city.isPartOf), (s3.uid, city.hasPart)})

        self.assertEqual(
            set(get_neighbor_diff(n1, None)), {(c1.uid, city.isPartOf),
                                               (s1.uid, city.hasPart),
                                               (c2.uid, city.isPartOf),
                                               (s2.uid, city.hasPart)})

        self.assertEqual(set(get_neighbor_diff(None, n2)), set())
Beispiel #2
0
    def test_fix_old_neighbors(self):
        """Check if _fix_old_neighbors.

        - Deletes old children.
        - Adds connection to old parents.
        """
        c = city.City(name="Freiburg")

        with CoreSession() as session:
            wrapper = city.CityWrapper(session=session)
            cw = wrapper.add(c)
            n = city.Neighborhood(name="Zähringen")
            nw = cw.add(n)

            c = clone_cuds_object(c)
            c._session = session
            old_neighbor_diff = get_neighbor_diff(cw, c)
            old_neighbors = session.load(*[x[0] for x in old_neighbor_diff])
            Cuds._fix_old_neighbors(
                new_cuds_object=c,
                old_cuds_object=cw,
                old_neighbors=old_neighbors,
                old_neighbor_diff=old_neighbor_diff,
            )
        self.assertEqual(c.get(rel=city.isPartOf), [wrapper])
        self.assertEqual(c.get(rel=city.hasPart), [])
        self.assertEqual(nw.get(rel=city.isPartOf), [])
        self.assertEqual(wrapper.get(rel=city.hasPart), [c])
Beispiel #3
0
    def test_buffers(self):
        """Test if the buffers work correctly."""
        session = TestWrapperSession()
        self.assertEqual(
            session._buffers,
            [[dict(), dict(), dict()], [dict(), dict(), dict()]])

        w = city.CityWrapper(session=session)
        c = city.City(name="city 1")
        n = city.Neighborhood(name="neighborhood")
        cw = w.add(c)
        cw.add(n)
        cw.remove(n.uid)
        cw.name = "city 2"
        w.session.prune()

        self.assertEqual(session._buffers, [[{
            cw.uid: cw,
            w.uid: w
        }, dict(), dict()], [dict(), dict(), dict()]])

        w.session._reset_buffers(BufferContext.USER)
        c2 = city.City(name="city3")
        w.add(c2)
        cw2 = w.get(c2.uid)
        w.remove(cw.uid)
        w.session.prune()

        self.assertEqual(session._buffers, [[{
            cw2.uid: cw2
        }, {
            w.uid: w
        }, {
            cw.uid: cw
        }], [dict(), dict(), dict()]])
    def test_get_not_reachable(self):
        """Test the pruning method."""
        cities = list()
        for i in range(3):
            c = city.City(name="city %s" % i)
            cities.append(c)
            for j in range(2):
                n = city.Neighborhood(name="neighborhood %s %s" % (i, j))
                c.add(n)
                for k in range(2):
                    s = city.Street(name="street %s %s %s" % (i, j, k))
                    n.add(s)
        registry = cities[0].session._registry
        result = registry._get_not_reachable(cities[2].uid)
        self.assertEqual(
            set([k.name for k in result]),
            set([
                "city 0", "city 1", "neighborhood 0 0", "neighborhood 0 1",
                "neighborhood 1 0", "neighborhood 1 1", "street 0 0 0",
                "street 0 0 1", "street 0 1 0", "street 0 1 1", "street 1 0 0",
                "street 1 0 1", "street 1 1 0", "street 1 1 1"
            ]))

        roots = [
            n for n in cities[0].get() if n.name.startswith("neighborhood 0")
        ]
        registry.prune(*roots, rel=cuba.passiveRelationship)
        self.assertEqual(
            set([k.name for k in registry.values()]),
            set(["neighborhood 0 0", "neighborhood 0 1", "city 0"]))
Beispiel #5
0
 def test_delete_cuds_object(self):
     """Tests the pruning method."""
     with TestWrapperSession() as session:
         w = city.CityWrapper(session=session)
         cities = list()
         neighborhoods = list()
         for i in range(2):
             c = city.City(name="city %s" % i)
             for j in range(2):
                 n = city.Neighborhood(name="neighborhood %s %s" % (i, j))
                 c.add(n)
             cities.append(w.add(c))
             neighborhoods.extend(cities[-1].get())
         session._reset_buffers(BufferContext.USER)
         session.delete_cuds_object(cities[0])
         self.maxDiff = None
         self.assertEqual(
             session._buffers,
             [
                 [
                     {},
                     {
                         w.uid: w,
                         neighborhoods[0].uid: neighborhoods[0],
                         neighborhoods[1].uid: neighborhoods[1],
                     },
                     {cities[0].uid: cities[0]},
                 ],
                 [{}, {}, {}],
             ],
         )
         self.assertNotIn(cities[0], session._registry)
         self.assertRaises(AttributeError, getattr, cities[0], "name")
 def test_add_default(self):
     """Test the instantiation and type of the objects."""
     if not RUN_PERFORMANCE_TEST:
         return
     print("Test adding with the default relationship")
     for i in range(self.iterations):
         self.c.add(city.Neighborhood(name='neighborhood ' + str(i)))
Beispiel #7
0
    def test_iter(self):
        """Tests the iter() method when no ontology class is provided."""
        c = city.City(name="a city")
        n = city.Neighborhood(name="a neighborhood")
        p = city.Citizen(name="John Smith")
        q = city.Citizen(name="Jane Doe")
        c.add(n)
        c.add(p, q, rel=city.hasInhabitant)

        elements = set(list(c.iter()))
        self.assertEqual(elements, {n, p, q})

        # return_rel=True
        get_p_uid, get_p_rel = next(c.iter(p.uid, return_rel=True))
        self.assertEqual(get_p_uid, p)
        self.assertEqual(get_p_rel, city.hasInhabitant)
        result = c.iter(rel=city.encloses, return_rel=True)
        self.assertEqual(
            set(result),
            set([
                (p, city.hasInhabitant),
                (q, city.hasInhabitant),
                (n, city.hasPart),
            ]),
        )
    def test_prune(self):
        """Test the pruning method."""
        cities = list()
        for i in range(3):
            c = city.City(name="city %s" % i)
            cities.append(c)
            for j in range(2):
                n = city.Neighborhood(name="neighborhood %s %s" % (i, j))
                c.add(n)
                for k in range(2):
                    s = city.Street(name="street %s %s %s" % (i, j, k))
                    n.add(s)
        registry = cities[0].session._registry
        registry.prune(*[c.uid for c in cities[0:2]])
        self.assertEqual(
            set([k.name for k in registry.values()]),
            set([
                "city 0", "city 1", "neighborhood 0 0", "neighborhood 0 1",
                "neighborhood 1 0", "neighborhood 1 1", "street 0 0 0",
                "street 0 0 1", "street 0 1 0", "street 0 1 1", "street 1 0 0",
                "street 1 0 1", "street 1 1 0", "street 1 1 1"
            ]))

        root, = [n for n in cities[0].get() if n.name == "neighborhood 0 0"]
        registry.prune(root, rel=cuba.activeRelationship)
        self.assertEqual(
            set([k.name for k in registry.values()]),
            set(["neighborhood 0 0", "street 0 0 0", "street 0 0 1"]))
Beispiel #9
0
 def test_branch(self):
     """Test the branch function."""
     x = branch(
         branch(city.City(name="Freiburg"),
                city.Citizen(name="Peter"),
                city.Citizen(name="Maria"),
                rel=city.hasInhabitant), city.Neighborhood(name="Herdern"),
         city.Neighborhood(name="Vauban"))
     self.assertEqual(x.name, "Freiburg")
     self.assertEqual({"Herdern", "Vauban"},
                      set(
                          map(lambda x: x.name,
                              x.get(oclass=city.Neighborhood))))
     self.assertEqual({"Peter", "Maria"},
                      set(
                          map(lambda x: x.name,
                              x.get(rel=city.hasInhabitant))))
 def fill_db(self, c, random_uid=True):
     """Fill the database with data."""
     for i in range(self.iterations):
         j = i * 9
         uids = iter([None for i in range(9)])
         if not random_uid:
             uids = iter(range(j * 9 + 1, (j + 1) * 9 + 1))
         c.add(city.Citizen(uid=next(uids)), rel=city.hasInhabitant)
         c.add(city.Citizen(uid=next(uids)), rel=city.encloses)
         c.add(city.Citizen(uid=next(uids)), rel=city.hasPart)
         c.add(city.Neighborhood(name="", uid=next(uids)),
               rel=city.hasInhabitant)
         c.add(city.Neighborhood(name="", uid=next(uids)),
               rel=city.encloses)
         c.add(city.Neighborhood(name="", uid=next(uids)), rel=city.hasPart)
         c.add(city.Street(name="", uid=next(uids)), rel=city.hasInhabitant)
         c.add(city.Street(name="", uid=next(uids)), rel=city.encloses)
         c = c.add(city.Street(name="", uid=next(uids)), rel=city.hasPart)
Beispiel #11
0
 def test_serialize(self):
     """Test the serialize function."""
     c = branch(
         city.City(name="Freiburg", uid=1),
         branch(city.Neighborhood(name="Littenweiler", uid=2),
                city.Street(name="Schwarzwaldstraße", uid=3)))
     self.maxDiff = None
     assertJsonLdEqual(self, json.loads(serialize(c)), CUDS_LIST)
     assertJsonLdEqual(self, serialize(c, json_dumps=False), CUDS_LIST)
Beispiel #12
0
def get_test_city():
    """Set up a test City for the tests."""
    c = city.City(name="Freiburg", coordinates=[1, 2])
    p1 = city.Citizen(name="Rainer")
    p2 = city.Citizen(name="Carlos")
    p3 = city.Citizen(name="Maria")
    n1 = city.Neighborhood(name="Zähringen", coordinates=[2, 3])
    n2 = city.Neighborhood(name="St. Georgen", coordinates=[3, 4])
    s1 = city.Street(name="Lange Straße", coordinates=[4, 5])

    c.add(p1, p2, p3, rel=city.hasInhabitant)
    p1.add(p3, rel=city.hasChild)
    p2.add(p3, rel=city.hasChild)
    c.add(n1, n2)
    n1.add(s1)
    n2.add(s1)
    s1.add(p2, p3, rel=city.hasInhabitant)
    return [c, p1, p2, p3, n1, n2, s1]
Beispiel #13
0
    def test_update_throws_exception(self):
        """Tests the update() method for unusual behaviors.

        - Update an element that wasn't added before
        """
        c = city.City(name="a city")
        n = city.Neighborhood(name="a neighborhood")
        c.add(n)
        p = city.Citizen()
        self.assertRaises(ValueError, c.update, p)
Beispiel #14
0
 def setUp(self):
     """Start the timer."""
     if not RUN_PERFORMANCE_TEST:
         return
     self.iterations = 100000
     self.c = city.City(name="A big city")
     for i in range(10):
         j = i * 9
         self.c.add(city.Citizen(uid=j + 0), rel=city.hasInhabitant)
         self.c.add(city.Citizen(uid=j + 1), rel=city.encloses)
         self.c.add(city.Citizen(uid=j + 2), rel=city.hasPart)
         self.c.add(city.Neighborhood(name="", uid=j + 3),
                    rel=city.hasInhabitant)
         self.c.add(city.Neighborhood(name="", uid=j + 4),
                    rel=city.encloses)
         self.c.add(city.Neighborhood(name="", uid=j + 5), rel=city.hasPart)
         self.c.add(city.Street(name="", uid=j + 6), rel=city.hasInhabitant)
         self.c.add(city.Street(name="", uid=j + 7), rel=city.encloses)
         self.c.add(city.Street(name="", uid=j + 8), rel=city.hasPart)
     self.start = time.time()
Beispiel #15
0
    def test_validate_tree_against_schema(self):
        """Test validation of CUDS tree against schema.yml."""
        schema_file = os.path.join(os.path.dirname(__file__),
                                   'test_validation_schema_city.yml')
        schema_file_with_missing_entity = os.path.join(
            os.path.dirname(__file__),
            'test_validation_schema_city_with_missing_entity.yml')
        schema_file_with_missing_relationship = os.path.join(
            os.path.dirname(__file__),
            'test_validation_schema_city_with_missing_relationship.yml')
        schema_file_with_optional_subtree = os.path.join(
            os.path.dirname(__file__),
            'test_validation_schema_city_with_optional_subtree.yml')

        c = city.City(name='freiburg')

        # empty city is not valid
        self.assertRaises(ConsistencyError, validate_tree_against_schema, c,
                          schema_file)

        # unless I do not specify relationships for it
        validate_tree_against_schema(c, schema_file_with_missing_relationship)

        # but it at least should be a city,
        # even when no relationships are defined
        wrong_object = cuba.File(path='some path')
        self.assertRaises(ConsistencyError, validate_tree_against_schema,
                          wrong_object, schema_file_with_missing_relationship)

        # with opional inhabitants an empty city is ok
        validate_tree_against_schema(c, schema_file_with_optional_subtree)

        # but the optional subtree should follow its own constraints
        # (here a citizen needs to work in a city)
        c.add(city.Citizen(name='peter'), rel=city.hasInhabitant)
        self.assertRaises(CardinalityError, validate_tree_against_schema, c,
                          schema_file_with_optional_subtree)

        c.add(city.Neighborhood(name='some hood'))
        c.add(city.Citizen(name='peter'), rel=city.hasInhabitant)

        # street of neighborhood violated
        self.assertRaises(CardinalityError, validate_tree_against_schema, c,
                          schema_file)

        c.get(oclass=city.Neighborhood)[0].add(city.Street(name='abc street'))

        # now the city is valid and validation should pass
        validate_tree_against_schema(c, schema_file)

        # entity that was defined is completely missing in cuds tree
        self.assertRaises(ConsistencyError, validate_tree_against_schema, c,
                          schema_file_with_missing_entity)
Beispiel #16
0
    def test_add_throws_exception(self):
        """Tests the add() method for unusual behaviors.

        - Adding an object that is already there
        - Adding an unsupported object
        """
        c = city.City(name="a city")
        n = city.Neighborhood(name="a neighborhood")

        c.add(n)
        self.assertRaises(ValueError, c.add, n)
        self.assertRaises(TypeError, c.add, "Not a CUDS objects")
Beispiel #17
0
 def test_rdf_import(self):
     """Test the import of RDF data."""
     with TestSession() as session:
         w = branch(
             city.CityWrapper(session=session),
             branch(city.City(name="Freiburg"),
                    city.Neighborhood(name="Littenweiler")))
         g = get_rdf_graph(session)
         c = w.get(rel=city.hasPart)[0]
         c.remove(rel=cuba.relationship)
         session._rdf_import(g)
         self.assertEqual(w.get(rel=city.hasPart), [c])
         self.assertEqual(c.get(rel=city.hasPart)[0].name, "Littenweiler")
Beispiel #18
0
    def test_get_throws_exception(self):
        """Tests the get() method for unusual behaviors.

        - Getting with a wrong type
        - Getting with a not allowed combination of arguments
        """
        c = city.City(name="a city")
        n = city.Neighborhood(name="a neighborhood")
        c.add(n)

        self.assertRaises(TypeError, c.get, "not a proper key")
        self.assertRaises(TypeError, c.get, n.uid, oclass=city.Neighborhood)
        self.assertRaises(ValueError, c.get, oclass=city.hasInhabitant)
        self.assertRaises(ValueError, c.get, rel=city.Citizen)
Beispiel #19
0
    def test_add(self):
        """Tests the standard, normal behavior of the add() method."""
        c = city.City(name="a city")
        n = city.Neighborhood(name="a neighborhood")
        p = city.Citizen()

        c.add(n)
        self.assertEqual(c.get(n.uid).uid, n.uid)

        # Test the inverse relationship
        get_inverse = n.get(rel=city.isPartOf)
        self.assertEqual(get_inverse, [c])

        c.add(p, rel=city.hasInhabitant)
        self.assertEqual(c.get(p.uid).uid, p.uid)
 def test_get_subtree(self):
     """Tests the get_subtree method."""
     c = city.City(name="a city")
     p = city.Citizen()
     n = city.Neighborhood(name="a neighborhood")
     s = city.Street(name="The street")
     c.add(p, rel=city.hasInhabitant)
     c.add(n)
     n.add(s)
     registry = c.session._registry
     self.assertEqual(registry.get_subtree(c.uid), set([c, p, n, s]))
     self.assertEqual(
         registry.get_subtree(c.uid, rel=cuba.activeRelationship),
         set([c, p, n, s]))
     self.assertEqual(registry.get_subtree(n.uid), set([c, p, n, s]))
     self.assertEqual(
         registry.get_subtree(n.uid, rel=cuba.activeRelationship),
         set([n, s]))
Beispiel #21
0
    def test_fix_new_parents(self):
        """Check _fix_new_parent.

        Make sure the method:
        - Deletes connection to new parents not available in new session
        - Adds connection to new parents available in new session
        """
        n = city.Neighborhood(name="Zähringen")
        # parent in both sessions
        c1 = city.City(name="Berlin")
        # only parent in default session (available in both)
        c2 = city.City(name="Paris")
        n.add(c1, c2, rel=city.isPartOf)

        c3 = city.City(name="London")

        with CoreSession() as session:
            wrapper = city.CityWrapper(session=session)
            c1w, c2w = wrapper.add(c1, c2)
            nw = c2w.get(n.uid)
            nw.remove(c2.uid, rel=cuba.relationship)

            # only parent + available in default session
            n.add(c3, rel=city.isPartOf)

            n = clone_cuds_object(n)
            n._session = session
            new_parent_diff = get_neighbor_diff(n, nw, mode="non-active")
            new_parents = session.load(*[x[0] for x in new_parent_diff])

            missing = dict()
            Cuds._fix_new_parents(
                new_cuds_object=n,
                new_parents=new_parents,
                new_parent_diff=new_parent_diff,
                missing=missing,
            )

        self.assertEqual(
            set(n.get(rel=city.isPartOf)),
            {c1w, c2w, None},  # missing parent, should be in missing dict
        )
        self.assertEqual(missing, {c3.uid: [(n, city.isPartOf)]})
        self.assertEqual(c2w.get(rel=city.hasPart), [n])
Beispiel #22
0
    def test_update(self):
        """Tests the standard, normal behavior of the update() method."""
        c = city.City(name="a city")
        n = city.Neighborhood(name="a neighborhood")
        new_n = create_from_cuds_object(n, CoreSession())
        new_s = city.Street(name="a new street")
        new_n.add(new_s)
        c.add(n)

        old_neighborhood = c.get(n.uid)
        old_streets = old_neighborhood.get(oclass=city.Street)
        self.assertEqual(old_streets, [])

        c.update(new_n)

        new_neighborhood = c.get(n.uid)
        self.assertIs(new_neighborhood, n)
        new_streets = new_neighborhood.get(oclass=city.Street)
        self.assertEqual(new_streets, [new_s])

        self.assertRaises(ValueError, c.update, n)
Beispiel #23
0
    def test_remove_throws_exception(self):
        """Tests the remove() method for unusual behaviors.

        - Removing with a wrong key
        - Removing something non-existent
        - Removing with a not allowed argument combination
        """
        c = city.City(name="a city")
        n = city.Neighborhood(name="a neighborhood")

        # Wrong key
        self.assertRaises(TypeError, c.remove, "not a proper key")

        # Non-existent
        self.assertRaises(RuntimeError, c.remove, n.uid)
        self.assertRaises(RuntimeError, c.remove, rel=city.hasPart)
        self.assertRaises(RuntimeError, c.remove, oclass=city.Street)
        self.assertRaises(RuntimeError, c.remove, n.uid, rel=city.hasPart)

        # Wrong arguments
        self.assertRaises(TypeError, c.remove, n.uid, oclass=city.Street)
Beispiel #24
0
 def test_notify_delete_call(self):
     """Tests if notify_delete is called, when we call prune."""
     deleted = set()
     session = TestSession(notify_delete=lambda x: deleted.add(x))
     w = city.CityWrapper(session=session)
     cities = list()
     for i in range(3):
         c = city.City(name="city %s" % i)
         cw = w.add(c)
         cities.append(cw)
         for j in range(2):
             n = city.Neighborhood(name="neighborhood %s %s" % (i, j))
             cw.add(n)
             nw = cw.get(n.uid)
             for k in range(2):
                 s = city.Street(name="street %s %s %s" % (i, j, k))
                 nw.add(s)
     w.remove(cities[1].uid, cities[2].uid)
     expected_deletion = {
         x.uid
         for x in session._registry.values()
         if (hasattr(x, "name") and x.name in {
             "city 2", "neighborhood 2 0", "neighborhood 2 1",
             "street 2 0 0", "street 2 0 1", "street 2 1 0", "street 2 1 1",
             "city 1", "neighborhood 1 0", "neighborhood 1 1",
             "street 1 0 0", "street 1 0 1", "street 1 1 0", "street 1 1 1"
         })
     }
     session.prune(rel=None)
     self.assertEqual(
         set([
             "wrapper" if k.is_a(cuba.Wrapper) else k.name
             for k in session._registry.values()
         ]),
         set([
             "city 0", "neighborhood 0 0", "neighborhood 0 1",
             "street 0 0 0", "street 0 0 1", "street 0 1 0", "street 0 1 1",
             "wrapper"
         ]))
     self.assertEqual(set([d.uid for d in deleted]), expected_deletion)
Beispiel #25
0
    def test_get_subtree(self):
        """Tests the get_subtree method."""
        c = city.City(name="a city")
        p = city.Citizen()
        n = city.Neighborhood(name="a neighborhood")
        s = city.Street(name="The street")
        c.add(p, rel=city.hasInhabitant)
        c.add(n)
        n.add(s)
        registry = c.session._registry
        self.assertEqual(registry.get_subtree(c.uid), {c, p, n, s})
        self.assertEqual(
            registry.get_subtree(c.uid, rel=cuba.activeRelationship),
            {c, p, n, s},
        )
        self.assertEqual(registry.get_subtree(n.uid), {c, p, n, s})
        self.assertEqual(
            registry.get_subtree(n.uid, rel=cuba.activeRelationship), {n, s})

        c_o = city.City(name="other city")
        n.add(c_o, rel=city.isPartOf)
        self.assertEqual(registry.get_subtree(c.uid), {c, p, n, s, c_o})
        self.assertEqual(
            registry.get_subtree(c.uid, rel=cuba.activeRelationship),
            {c, p, n, s},
        )
        self.assertEqual(registry.get_subtree(n.uid), {c, p, n, s, c_o})
        self.assertEqual(
            registry.get_subtree(n.uid, rel=cuba.activeRelationship), {n, s})

        # Test whether cycles are a problem
        c_o.add(c, rel=cuba.relationship)
        self.assertEqual(registry.get_subtree(c_o.uid), {c, p, n, s, c_o})

        # Disconnected items
        c_f = city.City(name="far city")
        self.assertEqual(registry.get_subtree(c.uid), {c, p, n, s, c_o})
        self.assertEqual(registry.get_subtree(c_f.uid), {c_f})
Beispiel #26
0
    def test_application_json_doc_city(self):
        """Test importing the `application/ld+json` mime type from doc dict.

        This test uses a city ontology instead.
        """
        # Importing
        test_data_path = str(
            Path(__file__).parent / "test_importexport_city_import.json")
        with open(test_data_path, "r") as file:
            json_doc = json.loads(file.read())
        with CoreSession():
            cuds = import_cuds(json_doc, format="application/ld+json")
            self.assertTrue(cuds.is_a(city.Citizen))
            self.assertEqual(cuds.name, "Peter")
            self.assertEqual(cuds.age, 23)
            export_file = io.StringIO()
            export_cuds(cuds, file=export_file, format="application/ld+json")
            export_file.seek(0)
            assertJsonLdEqual(self, json_doc, json.loads(export_file.read()))
        # Exporting
        test_data_path = str(
            Path(__file__).parent / "test_importexport_city_export.json")
        with open(test_data_path, "r") as file:
            json_doc = json.loads(file.read())
        with CoreSession():
            c = branch(
                city.City(name="Freiburg", uid=1),
                branch(
                    city.Neighborhood(name="Littenweiler", uid=2),
                    city.Street(name="Schwarzwaldstraße", uid=3),
                ),
            )
            export_file = io.StringIO()
            export_cuds(c, file=export_file, format="application/ld+json")
            export_file.seek(0)
            assertJsonLdEqual(self, json.loads(export_file.read()), json_doc)
Beispiel #27
0
print("\nAdding p1 to c...")
c.add(p1, rel=city.hasInhabitant)
print("internal dict of c:", c._neighbors, "\n")

print("Adding p2 to c...")
c.add(p2, rel=city.hasInhabitant)
print("internal dict of c:", c._neighbors, "\n")

print("\nElements in c:")
for el in c.iter():
    print("  uid: " + str(el.uid))

print("\nGetting p1 from c:")
print(c.get(p1.uid))

print("\nGetting city.Citizen from c:")
print(c.get(oclass=city.Citizen))

print("\n Remove p1:")
c.remove(p1.uid)
print("internal dict of c:", c._neighbors, "\n")

print("\nAdding neighborhoods to Cuds object in a loop:")
for i in range(6):
    print("Added neighborhood %s" % i)
    c.add(city.Neighborhood(name="neighborhood %s" % i))
print("internal dict of c:", c._neighbors, "\n")

print("Trying out the `is_a` method trivially with the new neighborhoods.")
print(all(n.is_a(city.Neighborhood) for n in c.get(oclass=city.Neighborhood)))
Beispiel #28
0
uuid_re = re.compile(
    r".*(http://www\.osp-core\.com/cuds#"
    r"([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}"
    r"-[a-z0-9]{4}-[a-z0-9]{12})).*"
)

# Create CUDS structure
c = branch(
    branch(
        city.City(name="Freiburg"),
        city.City(name="Pablo"),
        city.City(name="Yoav"),
        rel=city.hasInhabitant,
    ),
    city.Neighborhood(name="Stühlinger"),
    city.Neighborhood(name="Herdern"),
)

# Export from Core Session
export_cuds(path="test.rdf", format="ttl")

# Check output
with open("test.rdf", encoding="utf-8") as f:
    print("Exported from Core Session")
    for line in f:
        print("\t", line.strip())

# Export from a Wrapper session
with SqliteSession(path="test.db") as session:
    w = city.CityWrapper(session=session)
        "examples/transport_session_example.py",
        "server"]
try:
    p = subprocess.Popen(args)
except FileNotFoundError:
    args[0] = "python"
    p = subprocess.Popen(args)
time.sleep(5)

try:
    # Construct the Datastructure.
    c = city.City(name="Freiburg")
    p1 = city.Citizen(name="Peter")
    p2 = city.Citizen(name="Hans")
    p3 = city.Citizen(name="Michel")
    n = city.Neighborhood(name="Zähringen")
    s = city.Street(name="Le street")
    b = city.Building(name="Theater")
    a = city.Address(postalCode=79123, name='Le street', number=12)
    c.add(p1, p2, p3, rel=city.hasInhabitant)
    c.add(n).add(s).add(b).add(a)

    print("Connect to DB via transport session")
    with TransportSessionClient(
        SqliteSession, "ws://localhost:8688", path="test.db"
    ) as session:
        wrapper = city.CityWrapper(session=session)
        wrapper.add(c)
        wrapper.session.commit()

    print("Reconnect and check if data is still there")
host = input("Host: ")
port = int(input("Port [5432]: ") or 5432)
postgres_url = 'postgresql://%s:%s@%s:%s/%s' % (user, pwd, host, port, db_name)

# Let's build an EMMO compatible city!
emmo_town = city.City(name='EMMO town')

emmo_town.add(city.Citizen(name='Emanuele Ghedini'), rel=city.hasInhabitant)
emmo_town.add(city.Citizen(name='Adham Hashibon'), rel=city.hasInhabitant)
emmo_town.add(city.Citizen(name='Jesper Friis'),
              city.Citizen(name='Gerhard Goldbeck'),
              city.Citizen(name='Georg Schmitz'),
              city.Citizen(name='Anne de Baas'),
              rel=city.hasInhabitant)

emmo_town.add(city.Neighborhood(name="Ontology"))
emmo_town.add(city.Neighborhood(name="User cases"))

ontology_uid = None
for neighborhood in emmo_town.get(oclass=city.Neighborhood):
    if neighborhood.name == "Ontology":
        ontology_uid = neighborhood.uid
        neighborhood.add(city.Street(name="Relationships"), rel=city.hasPart)
        neighborhood.add(city.Street(name="Entities"), rel=city.hasPart)

onto = emmo_town.get(ontology_uid)

# We can go through inverse relationships
print(onto.get(rel=city.isPartOf)[0].name + ' is my city!')

# Working with a DB-wrapper: Store in the DB.