0

This is my first post to stack overflow so I request for an encouraging reply :) (bonus reputations)

I am trying to use SWRL to do some calculations for me. To imitate the problem, I have created a small ontology using protege 4.3. It has only two classes Parent and Son. Instances include 1 parent (John) and three sons (son1, son2, son3). John is linked with 3 sons using "hasSon" object property. Age of each son is mentioned using "hasAge" data-type property (integers).

Question-1: I need to first check that how many instances are linked with a given Parent(John) using hasSon property. How this can be achieved in SWRL?

Question-2: After knowing the number of Sons then I have to add their ages to get the total age of all the Sons again using SWRL?

For me, this require a loop like addition (a=a+b) but I dont know how this will work in SWRL. I have attached the OWL code for you. (Please note that in actual ontology the linked instances are not 3 but are varying and counting them is part of the problem) Thanks in advance

 <?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY parenttrial "http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#"
     xml:base="http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:parenttrial="http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#"
     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://www.semanticweb.org/admin/ontologies/2015/7/parenttrial"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Object Properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#hasSon -->

    <owl:ObjectProperty rdf:about="&parenttrial;hasSon">
        <rdfs:domain rdf:resource="&parenttrial;Parent"/>
        <rdfs:range rdf:resource="&parenttrial;Son"/>
    </owl:ObjectProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#hasAge -->

    <owl:DatatypeProperty rdf:about="&parenttrial;hasAge"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#Parent -->

    <owl:Class rdf:about="&parenttrial;Parent"/>



    <!-- http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#Son -->

    <owl:Class rdf:about="&parenttrial;Son"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#JohnF -->

    <owl:NamedIndividual rdf:about="&parenttrial;JohnF">
        <rdf:type rdf:resource="&parenttrial;Parent"/>
        <hasSon rdf:resource="&parenttrial;Son1"/>
        <hasSon rdf:resource="&parenttrial;Son2"/>
        <hasSon rdf:resource="&parenttrial;Son3"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#Son1 -->

    <owl:NamedIndividual rdf:about="&parenttrial;Son1">
        <rdf:type rdf:resource="&parenttrial;Son"/>
        <hasAge rdf:datatype="&xsd;integer">3</hasAge>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#Son2 -->

    <owl:NamedIndividual rdf:about="&parenttrial;Son2">
        <rdf:type rdf:resource="&parenttrial;Son"/>
        <hasAge rdf:datatype="&xsd;integer">4</hasAge>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/admin/ontologies/2015/7/parenttrial#Son3 -->

    <owl:NamedIndividual rdf:about="&parenttrial;Son3">
        <rdf:type rdf:resource="&parenttrial;Son"/>
        <hasAge rdf:datatype="&xsd;integer">5</hasAge>
    </owl:NamedIndividual>
</rdf:RDF>



<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->
  • Just a quick question for clarification, is it absolutely critical that you perform these tasks using SWRL? Or can you use an ontology framework like OWL API or Apache Jena? – Kunal Khaladkar Aug 27 '15 at 12:08

1 Answers1

1

Answer 1: There is no way through SWRL to check how many instances are connected to a certain property through SWRL. You are better off writing a sparql query with COUNT for this. Alternatively you can use an ontology framework and use an Iteratorto figure out the counts.

Answer 2: There is no way to loop a SWRL rule, perform an operation and return a value. SWRL rules are meant to add extra information about relations and not act as a programming language.

Solution: You are far better off using a ontology framework like Apache Jena or Owl api and writing a program to handle this instead of relying on SWRL. SWRL supports monotonic inferences only and thus cannot be used to loop over data in an ontology. Trying to do so will cause the rule to get executed infinitely.
Instead write a bit of code to do this. Refer to owl api or Jena ontology api and sparql in order to learn more on how to use these technologies.

Kunal Khaladkar
  • 493
  • 3
  • 16