0

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?


Thanks to the answer by @UninformedUser I now know that I must use the Snap SPARQL Query and not the 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?

yannis
  • 819
  • 1
  • 9
  • 26
  • 1
    DL Query Tab uses the OWL reasoner (if enabled), the SPARQL tab not. Snap SPARQL plugin does also use the OWL reasoner, but has some limitation w.r.t. SPARQL syntax. – UninformedUser Mar 22 '20 at 15:46
  • Thanks! How can I get Owlreader2 to use OWL reasoner? – yannis Mar 22 '20 at 18:22
  • I tried `sync_reasoner()` in the Python Owlreader2 code but nothing changed. – yannis Mar 22 '20 at 18:29
  • Within the Python's RDFlib collection of RDF tools is [OWL-RL](https://github.com/RDFLib/OWL-RL) which can do RDFS or RL reasoning but not DL. – Nicholas Car Mar 23 '20 at 00:39
  • The API is called "owlready2" and not "Owlreader2" - please always check your text before submitting it, otherwise things get confusing. – UninformedUser Mar 23 '20 at 07:54
  • And not that it matters, but why the heck are you writing `rdf-syntax:` - everybody is writing `rdf` - please try to stick to common standards. – UninformedUser Mar 23 '20 at 07:59
  • Regarding your question: I never used owlready2 in combination with rdflib. Have to try it first. – UninformedUser Mar 23 '20 at 08:00

1 Answers1

2

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 .
    }""")))
Nikolay Shebanov
  • 1,363
  • 19
  • 33
Jiba
  • 36
  • 1
  • Thank a lot! I have still a question: among the results I get `[[wildlife.Léo], [wildlife.lion1], [wildlife.Gigi], [wildlife.Giginou]]`, as far as I know there is no individual called `wildlife.lion1`, where does this come from? – yannis Mar 23 '20 at 09:49
  • 1
    I my example, lion1 comes from "lion()". When creating individuals, if their IRI is not explicitly given, Owlready generates a new IRI by taking the class IRI in lower case, and add a number (starting at 1 as here). – Jiba Mar 24 '20 at 13:14