Skip to content

Commit 6676a39

Browse files
committed
docs: add rdf example
1 parent 3442454 commit 6676a39

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

‎examples/generate_rdf.py‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import os
2+
from uuid import UUID, uuid5
3+
4+
import osw.model.entity as model
5+
from osw.core import OSW
6+
from osw.express import OswExpress
7+
from osw.utils.wiki import get_full_title
8+
from osw.wtsite import WtSite
9+
10+
# Create/update the password file under examples/accounts.pwd.yaml
11+
pwd_file_path = os.path.join(
12+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "accounts.pwd.yaml"
13+
)
14+
# pwd_file_path = "./accounts.pwd.yaml"
15+
osw_obj = OswExpress(
16+
domain="wiki-dev.open-semantic-lab.org", cred_filepath=pwd_file_path
17+
)
18+
19+
# load dependencies
20+
DEPENDENCIES = {
21+
"Tool": "Category:OSWe427aafafbac4262955b9f690a83405d",
22+
}
23+
osw_obj.install_dependencies(DEPENDENCIES, mode="append", policy="if-missing")
24+
25+
# generate a UUID namespace
26+
EXAMPLE_UUID_NAMESPACE = UUID("bd65611d-8669-4903-8a14-af88203acc38")
27+
28+
sensor1 = model.Device(
29+
uuid=uuid5(EXAMPLE_UUID_NAMESPACE, "Sensor1"),
30+
name="Sensor1",
31+
label=[model.Label(text="Sensor 1")],
32+
description=[model.Description(text="A sensor")],
33+
)
34+
35+
sensor2 = model.Device(
36+
uuid=uuid5(EXAMPLE_UUID_NAMESPACE, "Sensor2"),
37+
name="Sensor2",
38+
label=[model.Label(text="Sensor 2")],
39+
description=[model.Description(text="Another sensor")],
40+
)
41+
42+
example_machine = model.Device(
43+
uuid=uuid5(EXAMPLE_UUID_NAMESPACE, "ExampleMachine"),
44+
name="ExampleMachine",
45+
label=[model.Label(text="Example Machine")],
46+
# note: components are modelled as statements to define
47+
# the relationship between the machine and the components
48+
components=[
49+
model.Component(
50+
# note: uuid format needs to be fixed in schema
51+
uuid=str(uuid5(EXAMPLE_UUID_NAMESPACE, "sensor_at_position_1")),
52+
component_id="sensor_at_position_1",
53+
label=[model.Label(text="Sensor at position 1")],
54+
component_instance=get_full_title(sensor1),
55+
),
56+
model.Component(
57+
uuid=str(uuid5(EXAMPLE_UUID_NAMESPACE, "sensor_at_position_2")),
58+
component_id="sensor_at_position_2",
59+
label=[model.Label(text="Sensor at position 2")],
60+
component_instance=get_full_title(sensor2),
61+
),
62+
],
63+
)
64+
65+
result = osw_obj.export_jsonld(
66+
params=OSW.ExportJsonLdParams(
67+
entities=[example_machine, sensor1, sensor2],
68+
mode=OSW.JsonLdMode.expand,
69+
build_rdf_graph=True,
70+
context_loader_config=WtSite.JsonLdContextLoaderParams(
71+
prefer_external_vocal=False
72+
),
73+
)
74+
)
75+
graph = result.graph
76+
# print all triples in the graph
77+
qres = graph.query(
78+
"""
79+
SELECT ?s ?p ?o
80+
WHERE {
81+
?s ?p ?o .
82+
}
83+
"""
84+
)
85+
86+
print("\nAll triples in the graph:")
87+
for row in qres:
88+
print(row.s, row.p, row.o)
89+
90+
# query all components of example_machine
91+
qres = graph.query(
92+
"""
93+
SELECT ?component ?clabel
94+
WHERE {
95+
?s Property:HasStatement ?statement .
96+
?statement Property:HasProperty Property:HasPart .
97+
?statement Property:HasObject ?component .
98+
?component Property:HasLabel ?clabel .
99+
}
100+
"""
101+
)
102+
print("\nComponents of example_machine:")
103+
for row in qres:
104+
print(row.clabel)
105+
106+
print("\n\nDefine a custom context:")
107+
108+
result = osw_obj.export_jsonld(
109+
params=OSW.ExportJsonLdParams(
110+
entities=[example_machine, sensor1, sensor2],
111+
mode=OSW.JsonLdMode.expand,
112+
build_rdf_graph=True,
113+
context={
114+
"ex": "http://example.org/",
115+
"components": "@nest",
116+
"component_instance": {"@id": "ex:hasPart", "@type": "@id"},
117+
"name": {"@id": "ex:name"},
118+
},
119+
)
120+
)
121+
graph = result.graph
122+
# print all triples in the graph
123+
qres = graph.query(
124+
"""
125+
SELECT ?s ?p ?o
126+
WHERE {
127+
?s ?p ?o .
128+
}
129+
"""
130+
)
131+
print("\nAll triples in the graph:")
132+
for row in qres:
133+
print(row.s, row.p, row.o)
134+
135+
# query all components of example_machine
136+
qres = graph.query(
137+
"""
138+
SELECT ?clabel
139+
WHERE {
140+
?s <http://example.org/hasPart> ?component .
141+
?component <http://example.org/name> ?clabel
142+
}
143+
"""
144+
)
145+
print("\nComponents of example_machine:")
146+
for row in qres:
147+
print(row.clabel)
148+
149+
150+
print(
151+
"\n\ncreate a custom class with an additional property mapped in a context extension"
152+
)
153+
154+
155+
class MyCustomDeviceClass(model.Device):
156+
my_property: str
157+
158+
159+
my_custom_device = example_machine.cast(MyCustomDeviceClass, my_property="test")
160+
161+
result = osw_obj.export_jsonld(
162+
params=OSW.ExportJsonLdParams(
163+
entities=[my_custom_device, sensor1, sensor2],
164+
mode=OSW.JsonLdMode.expand,
165+
build_rdf_graph=True,
166+
context_loader_config=WtSite.JsonLdContextLoaderParams(
167+
prefer_external_vocal=False
168+
),
169+
additional_context={"my_property": "http://example.org/my_property"},
170+
)
171+
)
172+
173+
graph = result.graph
174+
# print all triples in the graph
175+
qres = graph.query(
176+
"""
177+
SELECT ?s ?p ?o
178+
WHERE {
179+
?s ?p ?o .
180+
}
181+
"""
182+
)
183+
print("\nAll triples in the graph:")
184+
for row in qres:
185+
print(row.s, row.p, row.o)

0 commit comments

Comments
 (0)