Cannot use prefixes in rdflib SPARQL

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Cannot use prefixes in rdflib SPARQL

Stefan
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?
Reply | Threaded
Open this post in threaded view
|

Re: Cannot use prefixes in rdflib SPARQL

Jiba
Administrator
Hi,

You are true, the namespace prefixing support is missing in the RDFlib store implementation. I added it in the development version, and you example now works as expected.

In addition, you can simply do :

graph.bind("ex", "http://www.semanticweb.org/stlu/ontologies/2019/1/untitled-ontology-117#")

instead of manually creating the namespace manager.

Best regards,
Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Cannot use prefixes in rdflib SPARQL

houzw
Can / how to set prefixes for generated ontology?

for example, prefix for "http://www.w3.org/ns/shacl#" is "sh", but in generated owl file is "shac"
Reply | Threaded
Open this post in threaded view
|

Re: Cannot use prefixes in rdflib SPARQL

Jiba
Administrator
You mean, in the ontology file generated by Owlready? There is currently no "pretty-printer" or possibility for customizing the file saving, I'm sorry.

Jiba