使用OWL API 4.0检索具有相同对象属性的OWL个体

我在Eclipse 4中使用OWL Api 4.0,在Protege 4中使用简单的本体。我有两个类“Ward”和“Gaurdian”。 这些类的个体通过对象属性isWardOf相关联。 我如何检索与类Gaurdian相关的Ward类的个体。 考虑下图: –

在此处输入图像描述

我想要检索Peter和Allice相关的事实或兄弟姐妹,因为他们都与杰克有关。 关于如何使用OWL API 4.0实现此目的的任何粗略线索。

我的完整猫头鹰文件贴有: –

 <!DOCTYPE Ontology [     ]>                                                                          > 

这是我能想到的最简单的方法。 它涉及与名义推理,所以它可能在计算上很昂贵。 但是,如果本体不是太大,这种方法是可行的。

这个想法是为了获得每个Gaurdian的所有实例。 然后,对于每个这样的个人,通过isWard属性获得与之相关的所有个体。 如果它们的大小大于1(如果设置的大小为1,那么这些集合将是您正在寻找的集合,而不是给定的Gaurdian只有一个Ward)。 用于此的OWL API代码类似于:

 // load an ontology OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntologyFromOntologyDocument(ONTOLOGY_IRI); OWLDataFactory df = manager.getOWLDataFactory(); // We need a reasoner to ask for individuals OWLReasoner reasoner = createReasoner(ontology); reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS); // get all the gaurdians in the ontology OWLClass gaurdian = df.getOWLClass(IRI.create("#Gaurdian")); Set gaurdians = reasoner.getInstances(gaurdian, false).getFlattened(); for (OWLNamedIndividual g : gaurdians) { // all wards of a given gaurdian g OWLObjectProperty isWardOf = df.getOWLObjectProperty(IRI.create("#isWardOf")); OWLClassExpression wardsOfG = df.getOWLObjectHasValue(isWardOf, g); // get all the wards related to a given gaurdian Set wards = reasoner.getInstances(wardsOfG, false).getFlattened(); if ( wards.size() > 1 ) { // this set of wards is connected to the same gaurdian } } 

在OWL API文档中,这里引用了粗糙指南教程的源代码

其中一个测试检索对象属性的断言,您应该能够根据需要调整它:

 @Test public void testIndividualAssertions() throws OWLException { OWLOntologyManager m = create(); OWLOntology o = m.createOntology(EXAMPLE_IRI); // We want to state that matthew has a father who is peter. OWLIndividual matthew = df.getOWLNamedIndividual(IRI.create(EXAMPLE_IRI + "#matthew")); OWLIndividual peter = df.getOWLNamedIndividual(IRI.create(EXAMPLE_IRI + "#peter")); // We need the hasFather property OWLObjectProperty hasFather = df.getOWLObjectProperty(IRI .create(EXAMPLE_IRI + "#hasFather")); // matthew --> hasFather --> peter OWLObjectPropertyAssertionAxiom assertion = df .getOWLObjectPropertyAssertionAxiom(hasFather, matthew, peter); // Finally, add the axiom to our ontology and save AddAxiom addAxiomChange = new AddAxiom(o, assertion); m.applyChange(addAxiomChange); // matthew is an instance of Person OWLClass personClass = df.getOWLClass(IRI.create(EXAMPLE_IRI + "#Person")); OWLClassAssertionAxiom ax = df.getOWLClassAssertionAxiom(personClass, matthew); // Add this axiom to our ontology - with a convenience method m.addAxiom(o, ax); }