首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得具有相同对象属性断言的OWLIndividual集?

如何获得具有相同对象属性断言的OWLIndividual集?
EN

Stack Overflow用户
提问于 2015-08-27 17:32:40
回答 1查看 411关注 0票数 0

我有一个本体,使用Protegé4.3.0创建,我将使用推理器来检索具有相同对象属性断言的OWLIndividual个体。

我读过this Q&A,但我想我应该修改建议的解决方案,因为我的问题略有不同,如下所述。

  • 本体论包含一组个体(老鼠、猫、狗),它们代表着某种动物。
  • 本体包含一组个体(mouseEyes、mouseEars、mouseLegs、catEyes、catEars、catLegs、dogEyes、dogEars、dogLegs),它们与不同的类(眼睛、耳朵、腿)相关联。
代码语言:javascript
复制
- The individuals _mouseEyes_, _catEyes_ and _dogEyes_ are associated to the class _Eyes_.
- The individuals _mouseEars_, _catEars_ and _dogEars_ are associated to the class _Ears_.
- The individuals _mouseLegs_, _catLegs_ and _dogLegs_ are associated to the class _Legs_.

  • 例如,单个catEyes具有object属性断言arePartOf,该断言与catEyes和Cat关联。其他个体之间的关系与此类似。

给定mouseEyes、mouseEars、mouseLegs、catEyes、catEars、catLegs、dogEyes、dogEars、dogLegs中的指定个体,我将检索具有相同对象属性断言的个体集。例如,如果指定的个体是catEyes,那么推理者应该检索catEyes、catEars、catLegs。

我怎么能用推理者来完成这件事?

EN

回答 1

Stack Overflow用户

发布于 2015-08-28 13:08:29

我发现getObjectPropertyValues中的OWLReasoner方法对于使用推理器解决问题中暴露的问题很有用。正如this comment中所指出的,SPARQL确实是查询模型的更好方法。

下面是我使用reasoner实现的解决方案。

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32255921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档