Retrieve OWL Individuals with same Object Properties using OWL API 4.0

自作多情 提交于 2020-01-10 20:10:42

问题


I am using OWL Api 4.0 in eclipse 3.4 with a simple ontology in Protege 4. I have two classes "Ward" and "Gaurdian". Individuals of these classes are related by object property isWardOf. How can i retrieve the individuals of class Ward that are related to same individual of Class Gaurdian. consider the following figure:-

I want to retrieve the Fact that Peter and Allice are related or siblings as they both are connected to Jack. Any rough clue as to how to achieve this using OWL API 4.0.

My complete owl file is affixed:-

<?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:


Here is the simplest way I could think of. It involves reasoning with nominals, so it might be computationally expensive. However, if the ontology is not too big, this approach is feasible.

The idea is to get all the instances of every Gaurdian. Then for every such individual get all the individuals that are connected with it by isWard property. These sets will be what you are looking for, if their size is larger than 1 (if the set size is one, than there is only one Ward of a given Gaurdian). The OWL API code for this would be similar to:

// 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
    }
}



回答2:


in the OWL API documentation there's a reference to the source code for the Rough Guide tutorial here

One of the tests retrieves assertions for an object property you should be able to adapt it to your needs:

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


来源:https://stackoverflow.com/questions/27695956/retrieve-owl-individuals-with-same-object-properties-using-owl-api-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!