Here is a small ontology called wildlife.owl
, created by Protégé, in which I have classes animal
, carnivore
, herbivore
, lion
, giraffe
, and individuals Léo
(a lion
), Gigi
(a giraffe
) and Giginou
(also a giraffe
). In the ontology I only declare that lion ⊏ carnivore ⊏ animal
.
When I ask for the instances of animal
in the DL Query tab of Protégé, I get, among others, Léo
(which is a lion
, hence a carnivore
, hence an animal
).
But when I write the following SPARQ Query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX me: <file:wildlife.owl#>
SELECT ?b
WHERE { ?b rdf:type me:animal }
I don't get any instance. Same result when I replace me:animal
by me:carnivore
. Only when I replace it by me:lion
I get the desired result Léo
.
Why is DL Query doing the inference (allowing me to obtain Léo
as instance of the animal
class) and not SPARQL Query?
What can I do to get the same result in SPARQL Query?
My next question concerns Python: when I send the SPARQL Query using Owlready2 and RDFlib, again I get no result:
from owlready2 import *
from rdflib import *
onto = get_ontology("wildlife.owl").load()
sync_reasoner([onto])
graph = default_world.as_rdflib_graph()
print(list(graph.query_owlready("""
PREFIX rdf-syntax: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX me: <file:wildlife.owl#>
SELECT ?b WHERE {
?b rdf-syntax:type me:animal .
}""")))
How can I get this query use OWL Reasoner?
When calling the reasoner, Owlready does not retain trivia inferences, such as is-a transitivity (e.g. the fact that lion is an animal).
For trivial inferences, you should rather use SPARQL, as in the example below. The ?any_animal
variable contains all animal subclasses (including animal
itself), thank to the SubclassOf*
SPARQL syntax (the *
indicates transitivity). Then, we take any instance of ?any_animal
class.
from owlready2 import *
from rdflib import *
onto = get_ontology("http://test.org/wildlife.owl")
with onto:
class animal(Thing): pass
class carnivore(animal): pass
class lion(carnivore): pass
lion()
default_world.graph.dump()
graph = default_world.as_rdflib_graph()
print(list(graph.query_owlready("""
PREFIX rdf-syntax: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX me: <http://test.org/wildlife.owl#>
SELECT ?b WHERE {
?any_animal <http://www.w3.org/2000/01/rdf-schema#subClassOf>* me:animal .
?b rdf-syntax:type ?any_animal .
}""")))