2014-12-29 45 views
3

我在eclipse 3.4中使用OWL Api 4.0和Protege 4中的简单本体论。我有两个“Ward”和“Gaurdian”类。这些类的个体通过对象属性isWardOf关联。我怎样才能找回有关同一个人Gaurdian类的病房的个人。考虑如下图所示: -使用OWL API 4.0检索具有相同对象属性的OWL个体

enter image description here

我要检索的事实,彼得和Allice相关或兄弟姐妹,因为它们都连接到杰克。关于如何使用OWL API 4.0来实现这个任何粗略的线索。

我的完整的猫头鹰文件贴: -

<?xml version="1.0"?> 


<!DOCTYPE Ontology [ 
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" > 
<!ENTITY xml "http://www.w3.org/XML/1998/namespace" > 
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" > 
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" > 
]> 


<Ontology xmlns="http://www.w3.org/2002/07/owl#" 
xml:base="http://www.semanticweb.org/antonio/ontologies/2014/11/untitled-ontology-46" 
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema#" 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns:xml="http://www.w3.org/XML/1998/namespace" 
ontologyIRI="http://www.semanticweb.org/antonio/ontologies/2014/11/untitled-ontology- 
46"> 
<Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/> 
<Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/> 
<Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/> 
<Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/> 
<Declaration> 
    <Class IRI="#Gaurdian"/> 
</Declaration> 
<Declaration> 
    <Class IRI="#Ward"/> 
</Declaration> 
<Declaration> 
    <ObjectProperty IRI="#isWardOf"/> 
</Declaration> 
<Declaration> 
    <NamedIndividual IRI="#Allice"/> 
</Declaration> 
<Declaration> 
    <NamedIndividual IRI="#Amber"/> 
</Declaration> 
<Declaration> 
    <NamedIndividual IRI="#Jack"/> 
</Declaration> 
<Declaration> 
    <NamedIndividual IRI="#Paul"/> 
</Declaration> 
<Declaration> 
    <NamedIndividual IRI="#Peter"/> 
</Declaration> 
<ClassAssertion> 
    <Class IRI="#Ward"/> 
    <NamedIndividual IRI="#Allice"/> 
</ClassAssertion> 
<ClassAssertion> 
    <Class IRI="#Gaurdian"/> 
    <NamedIndividual IRI="#Amber"/> 
</ClassAssertion> 
<ClassAssertion> 
    <Class IRI="#Gaurdian"/> 
    <NamedIndividual IRI="#Jack"/> 
</ClassAssertion> 
<ClassAssertion> 
    <Class IRI="#Ward"/> 
    <NamedIndividual IRI="#Paul"/> 
</ClassAssertion> 
<ClassAssertion> 
    <Class IRI="#Ward"/> 
    <NamedIndividual IRI="#Peter"/> 
</ClassAssertion> 
<ObjectPropertyAssertion> 
    <ObjectProperty IRI="#isWardOf"/> 
    <NamedIndividual IRI="#Allice"/> 
    <NamedIndividual IRI="#Jack"/> 
</ObjectPropertyAssertion> 
<ObjectPropertyAssertion> 
    <ObjectProperty IRI="#isWardOf"/> 
    <NamedIndividual IRI="#Amber"/> 
    <NamedIndividual IRI="#Jack"/> 
</ObjectPropertyAssertion> 
<ObjectPropertyAssertion> 
    <ObjectProperty IRI="#isWardOf"/> 
    <NamedIndividual IRI="#Paul"/> 
    <NamedIndividual IRI="#Amber"/> 
</ObjectPropertyAssertion> 
<ObjectPropertyDomain> 
    <ObjectProperty IRI="#isWardOf"/> 
    <Class IRI="#Ward"/> 
    </ObjectPropertyDomain> 
    <ObjectPropertyRange> 
    <ObjectProperty IRI="#isWardOf"/> 
    <Class IRI="#Gaurdian"/> 
    </ObjectPropertyRange> 
    </Ontology> > 

回答

1

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

这个想法是获得每个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<OWLNamedIndividual> 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<OWLNamedIndividual> wards = reasoner.getInstances(wardsOfG, false).getFlattened(); 
    if (wards.size() > 1) { 
     // this set of wards is connected to the same gaurdian 
    } 
} 
+0

我已经实施了您建议的已删除跟踪版本。本体很大。它确实很昂贵,但因为没有其他建议。现在我会走这个。 – jaykio77

0
OWL的API文档中

还有对源代码的引用的粗糙指南教程here

一个测试的检索断言对一个对象属性,你应该能够适应你的需求:

@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); 
} 
相关问题