Skip to content

Commit b40ca7b

Browse files
committed
[doc/rdf] Cleanup code examples
1 parent d290cc6 commit b40ca7b

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

doc/rdf/rdf_generator.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
from rdflib import Graph, BNode, Literal, Namespace
1+
from rdflib import Graph, BNode, Literal
22
from rdflib.namespace import XSD
33

4-
odml = Namespace("http://g-node/odml#")
4+
from odml.tools.rdf_converter import ODML_NS
55

66
g = Graph()
77

88
doc = BNode("d1")
99
s1 = BNode("s1")
1010
p12 = BNode("p1")
1111

12-
g.add((doc, odml.version, Literal(1.1)))
13-
g.add((doc, odml.docversion, Literal(42)))
14-
g.add((doc, odml.author, Literal('D. N. Adams')))
15-
g.add((doc, odml.date, Literal('1979-10-12', datatype=XSD.date)))
16-
g.add((doc, odml.hasSection, s1))
12+
g.add((doc, ODML_NS.version, Literal(1.1)))
13+
g.add((doc, ODML_NS.docversion, Literal(42)))
14+
g.add((doc, ODML_NS.author, Literal('D. N. Adams')))
15+
g.add((doc, ODML_NS.date, Literal('1979-10-12', datatype=XSD.date)))
16+
g.add((doc, ODML_NS.hasSection, s1))
1717

18-
g.add((s1, odml.property, p12))
19-
g.add((s1, odml.type, Literal('crew')))
20-
g.add((s1, odml.description, Literal('Information on the crew')))
21-
g.add((s1, odml.name, Literal('TheCrew')))
18+
g.add((s1, ODML_NS.property, p12))
19+
g.add((s1, ODML_NS.type, Literal('crew')))
20+
g.add((s1, ODML_NS.description, Literal('Information on the crew')))
21+
g.add((s1, ODML_NS.name, Literal('TheCrew')))
2222

23-
g.add((p12, odml.hasValue, Literal('[Arthur Philip Dent,Zaphod Beeblebrox,Tricia Marie McMillan,Ford Prefect]')))
24-
g.add((p12, odml.description, Literal('List of crew members names')))
25-
g.add((p12, odml.dtype, Literal('person')))
26-
g.add((p12, odml.name, Literal('NameCrewMembers')))
23+
content = '[Arthur Philip Dent,Zaphod Beeblebrox,Tricia Marie McMillan,Ford Prefect]'
24+
g.add((p12, ODML_NS.hasValue, Literal(content)))
25+
g.add((p12, ODML_NS.description, Literal('List of crew members names')))
26+
g.add((p12, ODML_NS.dtype, Literal('person')))
27+
g.add((p12, ODML_NS.name, Literal('NameCrewMembers')))
2728

2829
res = g.serialize(format='application/rdf+xml').decode("utf-8")
2930
print(res)
3031

31-
f = open("generated_ex1.xml", "w")
32-
f.write(res)
33-
f.close()
32+
with open("generated_odml_rdf.xml", "w") as f:
33+
f.write(res)

doc/rdf/sparql_example_queries.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
from rdflib import Graph, Namespace, RDF
1+
from rdflib import Graph, RDF
22
from rdflib.plugins.sparql import prepareQuery
33

4-
resource = "./python-odml/doc/rdf/example_data/odml_RDF_example_A.ttl"
4+
from odml.tools.rdf_converter import ODML_NS
5+
6+
rdf_namespace = {"odml": ODML_NS, "rdf": RDF}
7+
8+
resource = "./odml_RDF_example_A.ttl"
59

610
g = Graph()
711
g.parse(resource, format='turtle')
@@ -18,8 +22,7 @@
1822
?p odml:hasUnit "%" .
1923
?v rdf:type rdf:Bag .
2024
?v rdf:li "20.0" .
21-
}""", initNs={"odml": Namespace("https://g-node.org/odml-rdf#"),
22-
"rdf": RDF})
25+
}""", initNs=rdf_namespace)
2326

2427
g = Graph()
2528
g.parse(resource, format='turtle')
@@ -46,8 +49,7 @@
4649
?p1 odml:hasName "CellType" .
4750
?p1 odml:hasValue ?v1 .
4851
?v1 rdf:li "P-unit" .
49-
}""", initNs={"odml": Namespace("https://g-node.org/odml-rdf#"),
50-
"rdf": RDF})
52+
}""", initNs=rdf_namespace)
5153

5254
# select d.* from dataset d, CellProperties s, EOD Frequency c where c.unit = 'Hz'
5355
g = Graph()
@@ -64,8 +66,7 @@
6466
?p odml:hasUnit "Hz" .
6567
?v rdf:type rdf:Bag .
6668
?v rdf:li ?value .
67-
}""", initNs={"odml": Namespace("https://g-node.org/odml-rdf#"),
68-
"rdf": RDF})
69+
}""", initNs=rdf_namespace)
6970

7071
print("q1")
7172
for row in g.query(q1):

0 commit comments

Comments
 (0)