Reputation: 8310
I have an ontology, created using Protegé 4.3.0, and I would use the reasoner in order to retrieve the OWLIndividual
individuals with the same object property assertion.
I read this Q&A, but I guess I should adapt the proposed solution, because my problem is slightly different, as explained below.
The ontology contains a set of individuals (Mouse, Cat, Dog) which represent a certain kind of animal.
The ontology contains a set of individuals (mouseEyes, mouseEars, mouseLegs, catEyes, catEars, catLegs, dogEyes, dogEars, dogLegs) which are associated to different classes (Eyes, Ears, Legs).
For esample, the individual catEyes has the object property assertion arePartOf, which associates catEyes and Cat. The relationships between the other individuals are similar to this.
Given a specified individual among mouseEyes, mouseEars, mouseLegs, catEyes, catEars, catLegs, dogEyes, dogEars, dogLegs, I would retrieve the set of individuals with the same object property assertion. For esample, if the specified individual is catEyes, then the reasoner should retrieve catEyes, catEars, catLegs.
How could I use the reasoner to accomplish this?
Upvotes: 0
Views: 457
Reputation: 8310
I found that the method getObjectPropertyValues
in OWLReasoner
was useful in solving the problem exposed in the question using the reasoner. As pointed in this comment, it is true that SPARQL would be a better way to query the model.
The following is the solution I implemented using the reasoner.
import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyID;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.PrefixManager;
import org.semanticweb.owlapi.reasoner.InferenceType;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
public class Fauna {
private final static OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
private final static OWLDataFactory df = manager.getOWLDataFactory();
private final static OWLReasonerFactory rf = new StructuralReasonerFactory();
private final OWLOntology ontology;
private final OWLOntologyID id;
private final IRI iri;
private final PrefixManager pm;
private final OWLReasoner reasoner;
private final OWLObjectPropertyExpression arePartsOf;
private final TreeSet<OWLClass> clsParts = new TreeSet<>();
/**
*
* @param file
* @throws OWLOntologyCreationException
*/
public Fauna(File file) throws OWLOntologyCreationException {
ontology = manager.loadOntologyFromOntologyDocument(file);
id = ontology.getOntologyID();
iri = id.getOntologyIRI();
pm = new DefaultPrefixManager(iri.toString().concat("#"));
reasoner = rf.createReasoner(ontology);
reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_ASSERTIONS);
arePartsOf = df.getOWLObjectProperty(IRI.create(iri + "#arePartsOf"));
for (String s : new String[] { "Eyes", "Ears", "Legs" }) {
OWLClass cls = df.getOWLClass(":" + s, pm);
clsParts.add(cls);
}
}
/**
*
* @param individual
* @return
*/
private Set<OWLNamedIndividual> getPartsOfSameSpecies(OWLNamedIndividual individual) {
if (!ontology.containsIndividualInSignature(individual.getIRI())) {
return null;
}
Set<OWLClass> types = reasoner.getTypes(individual, true).getFlattened();
if (Collections.disjoint(clsParts, types)) {
return null;
}
Set<OWLNamedIndividual> values = reasoner.getObjectPropertyValues(individual, arePartsOf).getFlattened();
if (values == null || values.size() != 1) {
return null;
}
OWLNamedIndividual species = values.iterator().next();
return this.getParts(species);
}
/**
*
* @param species
* @return
*/
public Set<OWLNamedIndividual> getParts(OWLNamedIndividual species) {
HashSet<OWLNamedIndividual> individuals = new HashSet<>();
for (OWLClass cls : clsParts) {
for (OWLIndividual i : cls.getIndividuals(ontology)) {
OWLNamedIndividual part = i.asOWLNamedIndividual();
if (!reasoner.getObjectPropertyValues(part, arePartsOf).containsEntity(species)) {
continue;
}
individuals.add(part);
}
}
return individuals;
}
}
Upvotes: 0