簡體   English   中英

使用OWL API 4.0檢索具有相同對象屬性的OWL個體

[英]Retrieve OWL Individuals with same Object Properties using OWL API 4.0

我在Eclipse 4中使用OWL Api 4.0,在Protege 4中使用簡單的本體。我有兩個類“Ward”和“Gaurdian”。 這些類的個體通過對象屬性isWardOf相關聯。 我如何檢索與類Gaurdian相關的Ward類的個體。 考慮下圖: -

在此輸入圖像描述

我想要檢索Peter和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> >

這是我能想到的最簡單的方法。 它涉及與名義推理,所以它可能在計算上很昂貴。 但是,如果本體不是太大,這種方法是可行的。

這個想法是為了獲得每個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
    }
}

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM