|
Hi,
I'm trying to run SPARQL queries on my ontology, which works, but I cannot use prefixes for namespaces.
Trying to use rdflib.namespace.NamespaceManager.bind():
import owlready2
import rdflib
o = owlready2.get_ontology("file://<PATH>.owl").load()
graph = owlready2.default_world.as_rdflib_graph()
# Create namespace and namespace manager
ns = rdflib.namespace.Namespace("<NAMESPACE>")
ns_manager = rdflib.namespace.NamespaceManager(graph)
ns_manager.bind("ex", ns)
graph.namespace_manager = ns_manager # This line doesn't seem to do a difference, but it is included in the rdflib examples, so it is included here
print(ns_manager.store)
# prints: <owlready2.rdflib_store.TripleLiteRDFlibStore object at ...> # For a rdflib graph with the default store, this would be <rdflib.plugins.memory.IOMemory ...>
print([n for n in ns_manager.namespaces()])
# prints: [] (empty list)
# The reason this doesn't work is that TripleLiteRDFlibStore does not have a namespaces() function, so the inherited namespaces function of rdflib.store.Store is called instead. Unfortunately, the default implementation there returns nothing
q = graph.query("<some sort of SPARQL query using ex prefix>")
# Long error ending with "Unknown namespace prefix : ex"
# I don't know the specific reason this does not work as I do not understand the implementation of rdflib.graph.Graph.query(). However, I imagine the reason is similar to above
Trying to supply the namespaces explicitly each time:
import owlready2
import rdflib
o = owlready2.get_ontology("file://<PATH>.owl").load()
graph = owlready2.default_world.as_rdflib_graph()
ns = rdflib.namespace.Namespace("<NAMESPACE>")
q = graph.query("<some sort of SPARQL query using ex prefix>", initNs={"ex:ns})
print([row for row in q])
# Prints empty list
Note that queries work perfectly fine if I use whole namespaces instead of prefixes. However, it would be much more readable if I could prefixes somehow. Is there a way to do this with owlready2 right now?
|