I'm looking for a solution in order to "remove" the inference during a query launched on Sesame. I must use a "Native Java Store RDF Schema" repository and I have this query :
I have an instance, a NamedIndividual, and I want the uri of the class whose instance it is :
SELECT DISTINCT ?uri WHERE {
<http://www.semanticweb.org/ontotest#myInstance> rdf:type ?uri .
FILTER (?uri rdf:type owl:Class)
}
The problem is that I get severals URI (whom the good URI) instead of one because of the inference. I get the superclasses of the ontology then there is no link with the class of the instance.
How to obtain the right result without change the repository ?
Set the setIncludeInferred(false) on your Query object before executing it through the API to avoid using inferred statements.
It sounds like you're trying to return the most specific class(es) of which some individual is an instance. ThomasFrancart's answer explains how to disable inference, but you might not want to do that, because you might need inference to infer the most specific instance. For instance, you might have a class hierarchy with the relationship
SeniorCitizen ⊑ Person
Person ⊓ hasAge some integer[>= 60] ⊑ SeniorCitizen
and the data:
John rdf:type Person
John hasAge 62
Then if you disable inference, you won't be able to know that John is a SeniorCitizen, even though it's a more specific class than Person. It sounds like you actually want to keep inference enabled, but to only return the most specific class(es). You can do that with a query like this:
select ?class where {
:myInstance rdf:type ?class .
filter not exists {
?subclass rdfs:subClassOf* ?class .
:myInstance rdf:type ?subclass .
filter ( ?subclass != ?class )
}
}
This says to find those values of ?class
such that :myInstance
is an element of ?class
, but only where there's not ?subclass
that's a subclass of ?class
(other than ?class
itself) to which :myInstance
also belongs. Note that an instance can have multiple most specific classes though.
See:
- retrieving most specific classes of instances (your question is almost a duplicate of this one)
- How to get Least common subsumer in ontology using SPARQL Query? (not the same question, but the answer uses a similar technique)