我正试图让我的Ontology中的个人具有某种特性 . 我希望所有通过hasFoot属性链接到脚的个人 . 在过去我使用过Jena的迭代器,但现在我想使用SPARQL . 创建我的查询的Java代码:
String queryString =
"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 test: <file:/test#>"+
"select ?uri"+
"where { "+
"?uri test:hasFoot ?foot"+
"} \n ";
但是这个查询没有得到任何结果 . 当我查询所有三元组的属性时,我得到以下结果:
"select ?prop "+
"where { "+
"?uri ?prop ?subj"+
"} \n ";
<file:/test#hasFoot>
<file:/test#hasFoot>
<file:/test#hasFoot>
<file:/test#hasFoot>
<file:/test#hasAge>
<file:/test#hasName>
所以SPARQL查询显然正在运行 . 我甚至可以搜索rdf:types,所以rdf名称空间有效,只有我的命名空间(test)似乎不起作用 . 我也尝试写出整个属性名称(“file:/ test#hasFoot”)没有结果 . 有没有人知道我错过了什么?
这是我的本体论:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:test="file:/test#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
<rdf:Description rdf:about="file:/test#Foot2">
<rdf:type rdf:resource="file:/test#Foot"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Peter">
<test:hasHand rdf:resource="file:/test#Hand2"/>
<test:hasHand rdf:resource="file:/test#Hand1"/>
<test:hasFoot rdf:resource="file:/test#Foot6"/>
<test:hasFoot rdf:resource="file:/test#Foot5"/>
<test:hasName>Peter</test:hasName>
<test:hasAge>98</test:hasAge>
<rdf:type rdf:resource="file:/test#Individuum"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#hasName">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Foot1">
<rdf:type rdf:resource="file:/test#Foot"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Foot3">
<rdf:type rdf:resource="file:/test#Foot"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Hand">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#hasAge">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Human">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#hasFoot">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Doggy">
<test:hasFoot rdf:resource="file:/test#Foot4"/>
<test:hasFoot rdf:resource="file:/test#Foot3"/>
<test:hasFoot rdf:resource="file:/test#Foot2"/>
<test:hasFoot rdf:resource="file:/test#Foot1"/>
<test:hasAge>7</test:hasAge>
<test:hasName>Doggy</test:hasName>
<rdf:type rdf:resource="file:/test#Individuum"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Hand2">
<rdf:type rdf:resource="file:/test#Hand"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Foot6">
<rdf:type rdf:resource="file:/test#Foot"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#hasHand">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Individuum">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Foot">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Foot5">
<rdf:type rdf:resource="file:/test#Foot"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Hand1">
<rdf:type rdf:resource="file:/test#Hand"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
<rdf:Description rdf:about="file:/test#Foot4">
<rdf:type rdf:resource="file:/test#Foot"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</rdf:Description>
</rdf:RDF>
2 回答
您的自定义方案缺少权限,并且不是有效的IRI .
您可以使用jena中存在的
IRIFactory
类型之一来测试您使用的IRI . 例如,如果我通过以下方式运行您的一个IRI:我得到以下异常,表明缺少IRI的权限部分,因此它不是IRI:
更新本体以使用
file://test#
而不是file:/test#
并运行以下查询:结果是您期望的输出 . 最值得注意的是,您要查看的前缀与您的元素相关联 . 输出如下:
使用以前的数据,以下查询会产生不同的结果:
结果是:
似乎jena没有做出最好的猜测努力来强制执行无效IRI的qname / prefix标准 .
发生这种情况的原因并不明显,但是关于您对命名空间的选择不当:
file:/test#
.file:
URI非常难以理解,通常执行得很差 . 特别是它们应该为本地文件启动file://hostname/
或(更常见地)file:///
.Jena的查询引擎试图纠正这个问题,但显然数据尚未得到纠正 . 我们可以使用arq命令行工具看到这个:
(注意
<file:///test#hasFoot>
)更改数据和查询中的命名空间可以解决问题 .
我建议避免使用
file:
URI . 如果您只是尝试尝试http://example.com/ns#
或类似的 .