用于查询 object 属性 及其注释的 SPARQL
SPARQL for querying object property and its annotations
我正在尝试学习语义网技术,看看它是否可以替代应用程序中当前的关系模型。我正在使用 protege 为将托管在 Apache TomEE 上的应用程序设计 ontology 和 Jena。
我创建了一个简化的 ontology 来解释我的查询。在people.owl(下面的乌龟)中只有一个class人。 object 属性 hasChild 将一个人个人链接到他的 children,他们也是 Person。现在我想知道一个人的 children 的顺序。
例如,个人 john 有两个 children,anne 是第一个,jack 是第二个。所以对于 john 的 hasChild object 属性 我添加了序列注释。该序列是一个数字,它告诉该人 child 出生的顺序。所以 hasChild anne 的序列为 1,而 hasChild jack 的序列为 2。类似地,jane 的 hasChild jack 的序列为 1。
我必须编写的 SPARQL 是按照出生顺序获取给定 Person 姓名的所有 children。所以如果我查询children of "John Doe",我应该得到
|序列| child姓名|
| 1 |安妮·多伊 |
| 2 |无名氏 |
下面是海龟格式的Ontology。
@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") }
所以我有两个问题:
我是否应该更改 ontology 设计以将序列号分配给 child?
如果上述方法是正确的,我如何获得child的序列?
我在 Jena 用户列表上发布了同样的问题,并得到了使用 RDF 集合的建议。但是从 google 搜索我发现集合不适用于 OWL(因此 Protege)。
谢谢,
阿布舍克
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") }
我正在尝试学习语义网技术,看看它是否可以替代应用程序中当前的关系模型。我正在使用 protege 为将托管在 Apache TomEE 上的应用程序设计 ontology 和 Jena。
我创建了一个简化的 ontology 来解释我的查询。在people.owl(下面的乌龟)中只有一个class人。 object 属性 hasChild 将一个人个人链接到他的 children,他们也是 Person。现在我想知道一个人的 children 的顺序。 例如,个人 john 有两个 children,anne 是第一个,jack 是第二个。所以对于 john 的 hasChild object 属性 我添加了序列注释。该序列是一个数字,它告诉该人 child 出生的顺序。所以 hasChild anne 的序列为 1,而 hasChild jack 的序列为 2。类似地,jane 的 hasChild jack 的序列为 1。
我必须编写的 SPARQL 是按照出生顺序获取给定 Person 姓名的所有 children。所以如果我查询children of "John Doe",我应该得到
|序列| child姓名|
| 1 |安妮·多伊 |
| 2 |无名氏 |
下面是海龟格式的Ontology。
@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") }
所以我有两个问题:
我是否应该更改 ontology 设计以将序列号分配给 child?
如果上述方法是正确的,我如何获得child的序列?
我在 Jena 用户列表上发布了同样的问题,并得到了使用 RDF 集合的建议。但是从 google 搜索我发现集合不适用于 OWL(因此 Protege)。
谢谢, 阿布舍克
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") }