/
api_example.py
69 lines (54 loc) · 2.09 KB
/
api_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""An example explaining the API of CUDS objects."""
# Please install the city ontology: $pico install city
# If you did not install the city ontology
# (pico install city),
# you have to execute these commands first:
# from osp.core import Parser
# p = Parser()
# p.parse("city")
from osp.core.namespaces import city
print("Creating a City object, c...")
c = city.City(name="Freiburg", coordinates=[47, 7])
print(" uid of c: " + str(c.uid))
print(" IRI of c: " + str(c.iri))
print(" oclass of c: " + str(c.oclass) + "\n")
print("Creating Citizen objects, p1, p2...")
p1 = city.Citizen(name="Peter")
p2 = city.Citizen(name="Anne")
print(" uid of p1: " + str(p1.uid))
print(" IRI of p1: " + str(p1.iri))
print(" oclass of p1: " + str(p1.oclass) + "\n")
print(" uid of p2: " + str(p2.uid))
print(" IRI of p2: " + str(p2.iri))
print(" oclass of p2: " + str(p2.oclass) + "\n")
print("Checking attributes of the CUDS objects...")
print(f"Name of c: {c.name}. Coordinates of c: {c.coordinates}.")
print("Name of p1: " + str(p1.name))
print("Name of p2: " + str(p2.name))
print("\nChanging the attribute values of the CUDS objects...")
print(f"Change the name of {p1.name}.")
p1.name = "Bob"
print(f"Name of p1: {p1.name}.")
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)))