Search code examples
propertiesannotationssparql

SPARQL for querying object property and its annotations


I am trying to learn about semantic web technology to see if it can replace the current Relational model in an application. I am using protege to design the ontology and Jena for the application which will be hosted on Apache TomEE.

I have created a simplified ontology to explain the query I am having. In people.owl (turtle below) there is only one class People. The object property hasChild links a person individual to his children who are also Person. Now I want to know the order of the children for a person. For example, individual john has two children, with anne being first and jack being second. So to the hasChild object property of john I have added sequence annotation. The sequence is a number which tells the order in which the child was born for that person. So hasChild anne has sequence 1 and hasChild jack has sequnce 2. Similarly jane's hasChild jack has sequence 1.

The SPARQL I have to write is to get all the children of a given Person's name in the order they were born. So if I query children of "John Doe", I should get

|sequence | childname|

| 1 | Anne Doe |

| 2 | Jack Doe |

Below is the Ontology in turtle format.

@prefix : <http://www.semanticweb.org/abhishek/ontologies/people#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix people: <http://www.semanticweb.org/abhishek/ontologies/people#> .
@base <http://www.semanticweb.org/abhishek/ontologies/people> .

<http://www.semanticweb.org/abhishek/ontologies/people> rdf:type owl:Ontology .

###  http://www.semanticweb.org/abhishek/ontologies/people#sequence
people:sequence rdf:type owl:AnnotationProperty .


###  http://www.semanticweb.org/abhishek/ontologies/people#hasChild
people:hasChild rdf:type owl:ObjectProperty .


###  http://www.semanticweb.org/abhishek/ontologies/people#hasParent
people:hasParent rdf:type owl:ObjectProperty .


###  http://www.semanticweb.org/abhishek/ontologies/people#hasSpouse
people:hasSpouse rdf:type owl:ObjectProperty .


###  http://www.semanticweb.org/abhishek/ontologies/people#name
people:name rdf:type owl:DatatypeProperty .

###  http://www.semanticweb.org/abhishek/ontologies/people#Person
people:Person rdf:type owl:Class .

###  http://www.semanticweb.org/abhishek/ontologies/people#anne
people:anne rdf:type owl:NamedIndividual ,
                     people:Person ;
            people:name "Anne Doe"^^xsd:string .


###  http://www.semanticweb.org/abhishek/ontologies/people#jack
people:jack rdf:type owl:NamedIndividual ,
                     people:Person ;
            people:name "Jack Doe"^^xsd:string .


###  http://www.semanticweb.org/abhishek/ontologies/people#jane
people:jane rdf:type owl:NamedIndividual ,
                     people:Person ;
            people:hasChild people:jack ;
            people:hasSpouse people:john ;
            people:name "Jane Doe"^^xsd:string .

[ rdf:type owl:Axiom ;
   owl:annotatedSource people:jane ;
   owl:annotatedProperty people:hasChild ;
   owl:annotatedTarget people:jack ;
   people:sequence "1"^^xsd:int
 ] .


###  http://www.semanticweb.org/abhishek/ontologies/people#john
people:john rdf:type owl:NamedIndividual ,
                     people:Person ;
            people:hasChild people:anne ,
                            people:jack ;
            people:hasSpouse people:jane ;
            people:name "John Doe"^^xsd:string .

[ rdf:type owl:Axiom ;
   owl:annotatedSource people:john ;
   owl:annotatedProperty people:hasChild ;
   owl:annotatedTarget people:anne ;
   people:sequence "1"^^xsd:int
 ] .

[ rdf:type owl:Axiom ;
   owl:annotatedSource people:john ;
   owl:annotatedProperty people:hasChild ;
   owl:annotatedTarget people:jack ;
   people:sequence "2"^^xsd:int
 ] .


###  Generated by the OWL API (version 4.2.6.20160910-2108) https://github.com/owlcs/owlapi

I have the query till now:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ppl: <http://www.semanticweb.org/abhishek/ontologies/people#>
SELECT ?sequence ?childname 
    WHERE { ?person rdf:type ppl:Person .
        ?person ppl:name ?name .
        ?person ppl:hasChild ?child .
        #Get the ?sequence  for the current child. sequence is annotated to hasChild.
        ?child ppl:name ?childname .
        FILTER regex(?name, "John Doe") }

So I have two questions:

  • Should I change the ontology design to assign the sequence number to a child?

  • If the above approach is correct, how do I get the sequence for a child?

I posted the same question on Jena user list and got the recommendation to use RDF Collections. But from google search I found collections don't work with OWL (and hence Protege).

Thanks, Abhishek


Solution

  • PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX owl: <http://www.w3.org/2002/07/owl#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    PREFIX ppl: <http://www.semanticweb.org/abhishek/ontologies/people#>
    SELECT ?sequence ?childname 
        WHERE { ?person rdf:type ppl:Person .
            ?person ppl:name ?name .
            ?person ppl:hasChild ?child .
            #Get the ?sequence  for the current child. sequence is annotated to hasChild.
            ?child ppl:name ?childname .
            ?annotation owl:annotatedSource ?person ;
                        owl:annotatedTarget ?child ;
                        ppl:sequence ?sequence .       
            FILTER regex(?name, "John Doe") }