SPARQL用于查询对象属性及其注释

时间:2016-11-20 14:51:48

标签: properties annotations sparql

我正在尝试了解语义Web技术,看它是否可以替换应用程序中的当前Relational模型。我正在使用protege来设计本体,并使用Jena来处理将在Apache TomEE上托管的应用程序。

我创建了一个简化的本体来解释我的查询。在people.owl(下面的乌龟)中只有一类人。 object属性hasChild将一个人链接到他的孩子,他们也是Person。现在我想知道一个人的孩子的顺序。 例如,个人约翰有两个孩子,其中安妮是第一个,杰克是第二个。所以对于john的hasChild对象属性,我添加了序列注释。序列是一个数字,表示孩子为该人出生的顺序。所以hasChild anne有序列1和hasChild jack有sequnce 2.同样jane的hasChild jack有序列1.

我必须编写的SPARQL是按照出生顺序获取给定人名的所有子项。因此,如果我查询“John Doe”的孩子,我应该

| sequence | childname |

| 1 | Anne Doe |

| 2 | Jack Doe |

以下是乌龟格式的本体论。

@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

到现在为止我有查询:

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

所以我有两个问题:

  • 我是否应该更改本体设计以将序列号分配给孩子?

  • 如果上述方法是正确的,我如何获得孩子的序列?

我在Jena用户列表上发布了相同的问题,并获得了使用RDF Collections的建议。但是从谷歌搜索中我发现集合不适用于OWL(因此也就是Protege)。

谢谢, 阿布舍克巴克

1 个答案:

答案 0 :(得分:1)

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