繁体   English   中英

使用时,Jena中的SPARQL查询缺少结果 <file:/test#> 字首?

[英]SPARQL queries in Jena missing results when using <file:/test#> prefix?

我正在尝试让本体中具有特定属性的个人。 我希望所有通过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命名空间有效,只有我的命名空间(测试)似乎不起作用。 我还尝试写出整个属性名称(“ 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>

发生这种情况的原因并不明显,但与您对命名空间的错误选择有关: 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#或类似方法。

您的自定义方案缺少权限,并且不是有效的IRI。

您可以使用IRIFactory (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               |
----------------------------------------------------------------------------

似乎耶拿并没有尽力在无效IRI上强制实施qname / prefix标准。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM