我有一个本体,使用protegOWLIndividual
个体。
我读了这篇问答,但我想我应该采用建议的解决方案,因为我的问题略有不同,如下所述。
本体包含一组代表某种动物的个体(Mouse、Cat、Dog)。
本体包含一组个体(mouseEyes、mouseEars、mouseLegs、catEyes、catEars、catLegs、dogEyes、dogEars、dogLegs),它们与不同的类(Eyes、Ears、Legs)相关联。
- 个体mouseEyes、catEyes和dogEyes关联到类Eyes。
- 个体mouseEars、catEars和dogEars关联到类Ears。
- 个体mouseLegs、catLegs和dogLegs关联到类Legs。
例如,单个catEyes具有对象属性断言arePartOf,它将catEyes和Cat关联。其他个体之间的关系也类似。
给定mouseEyes、mouseEars、mouseLegs、catEyes、catEars、catLegs、dogEyes、dogEars、dogLegs中的指定个体,我将检索具有相同对象属性断言的个体集。例如,如果指定的个体是catEyes,那么推理器应该检索catEyes、catEars、catLegs。
我如何使用推理器来完成这个?
我发现OWLReasoner
中的getObjectPropertyValues
方法对于使用推理器解决问题中暴露的问题是有用的。正如这条评论所指出的,SPARQL确实是查询模型的更好方法。
下面是我使用推理器实现的解决方案。
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;
}
}