Search code examples
javaowlontologyowl-api

Get Property Value Intersection of two objects in an ontology using OWL API


I am new to OWL API and trying to experiment with it. I have created a simple ontology in protege and reciprocal application in eclipse using OWL API. Ontology structure is as follows:-

----------------------------------------------------------------------------------------  
 Class                Object       (Data Property)StringValue  ObjectProperty relatedTo    
----------------------------------------------------------------------------------------  
 WordString             WS1                    "One"                   DS1
                        WS2                    "Two"                   DS2
                        WS3                    "Three"                 DS3

 DigiString             DS1                    "1"  
                        DS2                    "2"  
                        DS3                    "3"  
----------------------------------------------------------------------------

Complete ontology is as follows:-

<rdf:RDF xmlns="http://localhost:3030/DigiWord.owl#"
 xml:base="http://localhost:3030/DigiWord.owl"
 xmlns:DigiWord="http://localhost:3030/DigiWord.owl#"
 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
 xmlns:owl="http://www.w3.org/2002/07/owl#"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="http://localhost:3030/DigiWord.owl"/>
<!-- 
// Object Properties
///////////////////////////////////////////////////////////////////////////////////////
 -->
<!-- http://localhost:3030/DigiWord.owl#relatedTo -->
<owl:ObjectProperty rdf:about="&DigiWord;relatedTo">
    <rdfs:domain rdf:resource="&DigiWord;DigiString"/>
    <rdfs:range rdf:resource="&DigiWord;WordString"/>
</owl:ObjectProperty>
<!-- 
//
// Data properties
///////////////////////////////////////////////////////////////////////////////////////
 -->
<!-- http://localhost:3030/DigiWord.owl#stringValue -->

<owl:DatatypeProperty rdf:about="&DigiWord;stringValue">
    <rdfs:domain rdf:resource="&DigiWord;DigiString"/>
    <rdfs:domain rdf:resource="&DigiWord;WordString"/>
    <rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<!-- 
// Classes
///////////////////////////////////////////////////////////////////////////////////////
 -->
<!-- http://localhost:3030/DigiWord.owl#DigiString -->
<owl:Class rdf:about="&DigiWord;DigiString"/>
<!-- http://localhost:3030/DigiWord.owl#WordString -->
<owl:Class rdf:about="&DigiWord;WordString"/>
<!-- 
// Individuals
///////////////////////////////////////////////////////////////////////////////////////
 -->
<!-- http://localhost:3030/DigiWord.owl#DS1 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS1">
    <rdf:type rdf:resource="&DigiWord;DigiString"/>
    <stringValue rdf:datatype="&xsd;string">1</stringValue>
    <relatedTo rdf:resource="&DigiWord;WS1"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#DS2 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS2">
    <rdf:type rdf:resource="&DigiWord;DigiString"/>
    <stringValue rdf:datatype="&xsd;string">2</stringValue>
    <relatedTo rdf:resource="&DigiWord;WS2"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#DS3 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS3">
    <rdf:type rdf:resource="&DigiWord;DigiString"/>
    <stringValue rdf:datatype="&xsd;string"></stringValue>
    <relatedTo rdf:resource="&DigiWord;WS3"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS1 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS1">
    <rdf:type rdf:resource="&DigiWord;WordString"/>
    <stringValue rdf:datatype="&xsd;string">One</stringValue>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS2 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS2">
    <rdf:type rdf:resource="&DigiWord;WordString"/>
    <stringValue rdf:datatype="&xsd;string">Two</stringValue>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS3 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS3">
    <rdf:type rdf:resource="&DigiWord;WordString"/>
    <stringValue rdf:datatype="&xsd;string">Three</stringValue>
</owl:NamedIndividual>
</rdf:RDF>

I want to retrieve Object WS1 for DS1, WS2 for DS2 i.e upon providing strings "1", "2" etc my code should retrieve "One" , "Two" respectively. I have not come across any relevant code of this type on net.Any help would be appreciated. Thanks in Advance.


Solution

  • The following example (OWLAPI 4) should help:

        OWLOntology o = ... your ontology here
        String ns = "http://localhost:3030/DigiWord.owl#";
        OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory();
        OWLObjectProperty relatedto = df.getOWLObjectProperty(IRI.create(ns + "relatedTo"));
        OWLDataProperty stringValue = df.getOWLDataProperty(IRI.create(ns + "stringValue"));
        SimpleRenderer renderer = new SimpleRenderer();
        for (OWLNamedIndividual i : o.getIndividualsInSignature()) {
            for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(i, stringValue, o)) {
                System.out.println(i + " has values " + renderer.render(lit));
            }
            for (OWLIndividual related : EntitySearcher.getObjectPropertyValues(i, relatedto, o)) {
                for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(related, stringValue, o)) {
                    System.out.println(i + " related to " + related + " which has values " + renderer.render(lit));
                }
            }
        }
    

    Edit: to find matches for your query:

        for (OWLNamedIndividual i : o.getIndividualsInSignature()) {
            for (OWLIndividual related : EntitySearcher.getObjectPropertyValues(i, relatedto, o)) {
                for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(related, stringValue, o)) {
                    if(lit.getLexicalForm().equals("1")){
                        for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(i, stringValue, o)) {
                            System.out.println(i + " has values " + renderer.render(lit));
                        }
                    }
                }
            }
        }