Repository navigation
Expand file tree
/
Copy pathgenerate_rdf.py
More file actions
186 lines (165 loc) · 4.87 KB
/
Copy pathgenerate_rdf.py
File metadata and controls
186 lines (165 loc) · 4.87 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
from uuid import UUID, uuid5
import osw.model.entity as model
from osw.core import OSW
from osw.express import OswExpress
from osw.utils.wiki import get_full_title
from osw.wtsite import WtSite
# Create/update the password file under examples/accounts.pwd.yaml
pwd_file_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "accounts.pwd.yaml"
)
# pwd_file_path = "./accounts.pwd.yaml"
osw_obj = OswExpress(
domain="wiki-dev.open-semantic-lab.org", cred_filepath=pwd_file_path
)
# load dependencies
DEPENDENCIES = {
"Tool": "Category:OSWe427aafafbac4262955b9f690a83405d",
"Device": "Category:OSWf0fe562f422d49c6877490b3dfee2f3f",
}
osw_obj.install_dependencies(DEPENDENCIES, mode="append", policy="if-missing")
# generate a UUID namespace
EXAMPLE_UUID_NAMESPACE = UUID("bd65611d-8669-4903-8a14-af88203acc38")
sensor1 = model.Device(
uuid=uuid5(EXAMPLE_UUID_NAMESPACE, "Sensor1"),
name="Sensor1",
label=[model.Label(text="Sensor 1")],
description=[model.Description(text="A sensor")],
)
sensor2 = model.Device(
uuid=uuid5(EXAMPLE_UUID_NAMESPACE, "Sensor2"),
name="Sensor2",
label=[model.Label(text="Sensor 2")],
description=[model.Description(text="Another sensor")],
)
example_machine = model.Device(
uuid=uuid5(EXAMPLE_UUID_NAMESPACE, "ExampleMachine"),
name="ExampleMachine",
label=[model.Label(text="Example Machine")],
# note: components are modelled as statements to define
# the relationship between the machine and the components
components=[
model.Component(
# note: uuid format needs to be fixed in schema
uuid=str(uuid5(EXAMPLE_UUID_NAMESPACE, "sensor_at_position_1")),
component_id="sensor_at_position_1",
label=[model.Label(text="Sensor at position 1")],
component_instance=get_full_title(sensor1),
),
model.Component(
uuid=str(uuid5(EXAMPLE_UUID_NAMESPACE, "sensor_at_position_2")),
component_id="sensor_at_position_2",
label=[model.Label(text="Sensor at position 2")],
component_instance=get_full_title(sensor2),
),
],
)
result = osw_obj.export_jsonld(
params=OSW.ExportJsonLdParams(
entities=[example_machine, sensor1, sensor2],
mode=OSW.JsonLdMode.expand,
build_rdf_graph=True,
context_loader_config=WtSite.JsonLdContextLoaderParams(
prefer_external_vocal=False
),
)
)
graph = result.graph
# print all triples in the graph
qres = graph.query(
"""
SELECT ?s ?p ?o
WHERE {
?s ?p ?o .
}
"""
)
print("\nAll triples in the graph:")
for row in qres:
print(row.s, row.p, row.o)
# query all components of example_machine
qres = graph.query(
"""
SELECT ?component ?clabel
WHERE {
?s Property:HasStatement ?statement .
?statement Property:HasProperty Property:HasPart .
?statement Property:HasObject ?component .
?component Property:HasLabel ?clabel .
}
"""
)
print("\nComponents of example_machine:")
for row in qres:
print(row.clabel)
print("\n\nDefine a custom context:")
result = osw_obj.export_jsonld(
params=OSW.ExportJsonLdParams(
entities=[example_machine, sensor1, sensor2],
mode=OSW.JsonLdMode.expand,
build_rdf_graph=True,
context={
"ex": "http://example.org/",
"components": "@nest",
"component_instance": {"@id": "ex:hasPart", "@type": "@id"},
"name": {"@id": "ex:name"},
},
)
)
graph = result.graph
# print all triples in the graph
qres = graph.query(
"""
SELECT ?s ?p ?o
WHERE {
?s ?p ?o .
}
"""
)
print("\nAll triples in the graph:")
for row in qres:
print(row.s, row.p, row.o)
# query all components of example_machine
qres = graph.query(
"""
SELECT ?clabel
WHERE {
?s <http://example.org/hasPart> ?component .
?component <http://example.org/name> ?clabel
}
"""
)
print("\nComponents of example_machine:")
for row in qres:
print(row.clabel)
print(
"\n\ncreate a custom class with an additional property mapped in a context extension"
)
class MyCustomDeviceClass(model.Device):
my_property: str
my_custom_device = example_machine.cast(MyCustomDeviceClass, my_property="test")
result = osw_obj.export_jsonld(
params=OSW.ExportJsonLdParams(
entities=[my_custom_device, sensor1, sensor2],
mode=OSW.JsonLdMode.expand,
build_rdf_graph=True,
context_loader_config=WtSite.JsonLdContextLoaderParams(
prefer_external_vocal=False
),
additional_context={"my_property": "http://example.org/my_property"},
)
)
graph = result.graph
# print all triples in the graph
qres = graph.query(
"""
SELECT ?s ?p ?o
WHERE {
?s ?p ?o .
}
"""
)
print("\nAll triples in the graph:")
for row in qres:
print(row.s, row.p, row.o)