我正在努力让我的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>
答案 0 :(得分:3)
这种情况发生的原因并不明显,但是关注您对命名空间的选择不当:file:/test#
。
file:
URI非常难以理解,通常执行得很差。特别是它们应该为本地文件启动file://hostname/
或(更常见)file:///
。
Jena的查询引擎尝试纠正此问题,但显然数据尚未已得到纠正。我们可以使用arq命令行工具看到这一点:
$ arq --data data.rdf --query query.rq --explain
...
15:12:43 INFO exec :: ALGEBRA
(project (?uri)
(bgp (triple ?uri <file:///test#hasFoot> ?foot)))
(请注意<file:///test#hasFoot>
)
更改数据和查询中的命名空间可以解决问题。
我建议避免使用file:
URI。如果您只是尝试尝试http://example.com/ns#
或类似的。
答案 1 :(得分:1)
您的自定义方案缺少权限,并且不是有效的IRI。
您可以使用jena中存在的IRIFactory
类型之一来测试您使用的IRI。例如,如果我通过以下方式运行您的一个IRI:
@Test
public void correctUri() {
final IRIFactory factory = IRIFactory.iriImplementation();
final IRI iri = factory.construct("file:/test#johnsFoot");
}
我收到以下异常,表明缺少IRI的权限部分,因此它不是IRI:
org.apache.jena.iri.impl.IRIImplException: <file:/test#johnsFoot> Code: 57/REQUIRED_COMPONENT_MISSING in AUTHORITY: A component that is required by the scheme is missing.
at org.apache.jena.iri.impl.AbsIRIFactoryImpl.throwAnyErrors(AbsIRIFactoryImpl.java:65)
at org.apache.jena.iri.impl.AbsIRIFactoryImpl.construct(AbsIRIFactoryImpl.java:48)
at SO_Test.correctUri(SO_Test.java:87)
更新您的本体以使用file://test#
而不是file:/test#
并运行以下查询:
@Test
public void getAllProperties2() {
final Query query = QueryFactory.create(
"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 * "+
"where { "+
"?uri ?prop ?subj"+
"} \n "
);
final QueryExecution exec = QueryExecutionFactory.create(query, model1);
final ResultSetRewindable results = ResultSetFactory.copyResults(exec.execSelect());
exec.close();
ResultSetFormatter.out(System.out, results, query);
results.reset();
}
获得您期望的输出结果。最值得注意的是,您要查看的前缀与您的元素相关联。输出如下:
---------------------------------------------------------
| uri | prop | subj |
=========================================================
| test:Hand | rdf:type | owl:Class |
| test:Foot3 | rdf:type | owl:NamedIndividual |
| test:Foot3 | rdf:type | test:Foot |
| test:Human | rdf:type | owl:Class |
| <file://test> | rdf:type | owl:Ontology |
| test:Hand2 | rdf:type | owl:NamedIndividual |
| test:Hand2 | rdf:type | test:Hand |
| test:Individuum | rdf:type | owl:Class |
| test:hasName | rdf:type | owl:DatatypeProperty |
| test:Foot2 | rdf:type | owl:NamedIndividual |
| test:Foot2 | rdf:type | test:Foot |
| test:Hand1 | rdf:type | owl:NamedIndividual |
| test:Hand1 | rdf:type | test:Hand |
| test:Foot1 | rdf:type | owl:NamedIndividual |
| test:Foot1 | rdf:type | test:Foot |
| test:hasAge | rdf:type | owl:DatatypeProperty |
| test:hasFoot | rdf:type | owl:ObjectProperty |
| test:hasHand | rdf:type | owl:ObjectProperty |
| test:Peter | rdf:type | owl:NamedIndividual |
| test:Peter | rdf:type | test:Individuum |
| test:Peter | test:hasAge | "98" |
| test:Peter | test:hasName | "Peter" |
| test:Peter | test:hasFoot | test:Foot5 |
| test:Peter | test:hasFoot | test:Foot6 |
| test:Peter | test:hasHand | test:Hand1 |
| test:Peter | test:hasHand | test:Hand2 |
| test:Foot6 | rdf:type | owl:NamedIndividual |
| test:Foot6 | rdf:type | test:Foot |
| test:Foot5 | rdf:type | owl:NamedIndividual |
| test:Foot5 | rdf:type | test:Foot |
| test:Doggy | rdf:type | owl:NamedIndividual |
| test:Doggy | rdf:type | test:Individuum |
| test:Doggy | test:hasName | "Doggy" |
| test:Doggy | test:hasAge | "7" |
| test:Doggy | test:hasFoot | test:Foot1 |
| test:Doggy | test:hasFoot | test:Foot2 |
| test:Doggy | test:hasFoot | test:Foot3 |
| test:Doggy | test:hasFoot | test:Foot4 |
| test:Foot4 | rdf:type | owl:NamedIndividual |
| test:Foot4 | rdf:type | test:Foot |
| test:Foot | rdf:type | owl:Class |
---------------------------------------------------------
使用以前的数据,以下查询会产生不同的结果:
@Test
public void getAllProperties() {
final Query query = QueryFactory.create(
"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 * "+
"where { "+
"?uri ?prop ?subj"+
"} \n "
);
final QueryExecution exec = QueryExecutionFactory.create(query, model0);
final ResultSetRewindable results = ResultSetFactory.copyResults(exec.execSelect());
exec.close();
ResultSetFormatter.out(System.out, results, query);
results.reset();
}
结果:
----------------------------------------------------------------------------
| uri | prop | subj |
============================================================================
| <file:/test#Hand> | rdf:type | owl:Class |
| <file:/test#Foot1> | rdf:type | owl:NamedIndividual |
| <file:/test#Foot1> | rdf:type | <file:/test#Foot> |
| <file:/test#hasFoot> | rdf:type | owl:ObjectProperty |
| <file:/test#Doggy> | rdf:type | owl:NamedIndividual |
| <file:/test#Doggy> | rdf:type | <file:/test#Individuum> |
| <file:/test#Doggy> | <file:/test#hasName> | "Doggy" |
| <file:/test#Doggy> | <file:/test#hasAge> | "7" |
| <file:/test#Doggy> | <file:/test#hasFoot> | <file:/test#Foot1> |
| <file:/test#Doggy> | <file:/test#hasFoot> | <file:/test#Foot2> |
| <file:/test#Doggy> | <file:/test#hasFoot> | <file:/test#Foot3> |
| <file:/test#Doggy> | <file:/test#hasFoot> | <file:/test#Foot4> |
| <file:/test#Foot6> | rdf:type | owl:NamedIndividual |
| <file:/test#Foot6> | rdf:type | <file:/test#Foot> |
| <file:/test#hasHand> | rdf:type | owl:ObjectProperty |
| <file:/test#hasName> | rdf:type | owl:DatatypeProperty |
| <file:/test> | rdf:type | owl:Ontology |
| <file:/test#Foot5> | rdf:type | owl:NamedIndividual |
| <file:/test#Foot5> | rdf:type | <file:/test#Foot> |
| <file:/test#Human> | rdf:type | owl:Class |
| <file:/test#Hand2> | rdf:type | owl:NamedIndividual |
| <file:/test#Hand2> | rdf:type | <file:/test#Hand> |
| <file:/test#Foot4> | rdf:type | owl:NamedIndividual |
| <file:/test#Foot4> | rdf:type | <file:/test#Foot> |
| <file:/test#hasAge> | rdf:type | owl:DatatypeProperty |
| <file:/test#Hand1> | rdf:type | owl:NamedIndividual |
| <file:/test#Hand1> | rdf:type | <file:/test#Hand> |
| <file:/test#Foot3> | rdf:type | owl:NamedIndividual |
| <file:/test#Foot3> | rdf:type | <file:/test#Foot> |
| <file:/test#Peter> | rdf:type | owl:NamedIndividual |
| <file:/test#Peter> | rdf:type | <file:/test#Individuum> |
| <file:/test#Peter> | <file:/test#hasAge> | "98" |
| <file:/test#Peter> | <file:/test#hasName> | "Peter" |
| <file:/test#Peter> | <file:/test#hasFoot> | <file:/test#Foot5> |
| <file:/test#Peter> | <file:/test#hasFoot> | <file:/test#Foot6> |
| <file:/test#Peter> | <file:/test#hasHand> | <file:/test#Hand1> |
| <file:/test#Peter> | <file:/test#hasHand> | <file:/test#Hand2> |
| <file:/test#Foot2> | rdf:type | owl:NamedIndividual |
| <file:/test#Foot2> | rdf:type | <file:/test#Foot> |
| <file:/test#Foot> | rdf:type | owl:Class |
| <file:/test#Individuum> | rdf:type | owl:Class |
----------------------------------------------------------------------------
似乎jena并没有在无效的IRI上强制执行qname / prefix标准。