Skip to content

Commit 9613f65

Browse files
committed
[doc/rdf] Add query example
1 parent 63e3765 commit 9613f65

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

doc/odmltordf.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,45 @@ The documents saved above can now be loaded into single graph::
274274
curr_graph.parse(file_B)
275275

276276

277+
The graph is now ready to accept simple SPARQL queries. Queries need the odML RDF namespace though to process the odml specific entries::
278+
279+
from odml.tools.rdf_converter import ODML_NS
280+
281+
from rdflib import Namespace, RDF, RDFS
282+
from rdflib.plugins.sparql import prepareQuery
283+
284+
# preparing the query namespace
285+
NAMESPACE_MAP = {"odml": Namespace(ODML_NS), "rdf": RDF, "rdfs": RDFS}
286+
287+
# preparing a query requesting the name of all sections in the graph
288+
q_string = "SELECT * WHERE {?s rdf:type odml:Section . ?s odml:hasName ?sec_name .}"
289+
sec_query = prepareQuery(q_string, initNs=NAMESPACE_MAP)
290+
291+
for row in curr_graph.query(sec_query):
292+
print("Section name: '%s'" % row.sec_name)
293+
294+
The query returns::
295+
296+
Section name: 'recording_A'
297+
Section name: 'recording_B'
298+
Section name: 'analysis_A'
299+
300+
301+
This query returns all sections from the first file, since reasoning is not yet enabled. This can be changed by adding reasioning to the query::
302+
303+
from owlrl import DeductiveClosure, RDFS_Semantics
304+
305+
DeductiveClosure(RDFS_Semantics).expand(curr_graph)
306+
307+
for row in curr_graph.query(sec_query):
308+
print("Section name: '%s'" % row.sec_name)
309+
310+
311+
This query now returns the sections from both files::
312+
313+
Section name: 'recording_B'
314+
Section name: 'recording_A'
315+
Section name: 'recording_protocol_B'
316+
Section name: 'recording_protocol_A'
317+
Section name: 'analysis_A'
318+

0 commit comments

Comments
 (0)