【问题标题】:empty ResultSet on second query (despite valid results with first)第二个查询时为空 ResultSet(尽管第一个查询结果有效)
【发布时间】:2013-11-13 09:08:11
【问题描述】:
public class MainClass {

public static void main(String[] args) {
    MainClass c = new MainClass();
    c.getType("m.012x34");
    System.out.println("---------------");
    MainClass c1 = new MainClass();
    c1.getType("m.012x34");
}

public String getType(String mid){      
String Type = null; 
try{
    InputStream inputStream = null;
    Resource resource = null;
    File automake_triples = new File(".\\automake_triples.ttl");

    ArrayList<String> products = new ArrayList<String>();
    InputStream in = new FileInputStream(automake_triples);

    final Model model = ModelFactory.createMemModelMaker().createDefaultModel();
        model.read(in ,null , "Turtle"); 
    in.close();

    String queryString = 
        "SELECT ?type" +
        " WHERE {" +
        " ?mid <type.type> ?type ." +
        " }";

    final Query query = QueryFactory.create(queryString);
    final ParameterizedSparqlString pss = new ParameterizedSparqlString(queryString);
    final QuerySolutionMap map = new QuerySolutionMap();
    final Resource res_mid = model.getResource(mid);    
    map.add("mid", (RDFNode) res_mid);
    pss.setParams(map);    

    System.out.println(pss);
    QueryExecution qe = QueryExecutionFactory.create(pss.toString(), model);            
        ResultSetFormatter.out( QueryExecutionFactory.create( pss.toString(), model ).execSelect() );

    qe.close();
    }catch(Exception ex){
    System.out.println(ex);
    ex.printStackTrace();
    }       
    return Type;
 }

}

Sample Input (In the ttl file):

<m.012x34>
      <automotive.company.make_s> <m.0h5wslk> ;
      <type.object.name> "Jaguar" ;
      <type.type> "automotive.company" .

<m.0ywc>
      <automotive.company.make_s> <m.0h5wtfh> , <m.06m97r> ;
      <type.object.name> "Aston Martin" ;
      <type.type> "automotive.company" .

Sample Output:

SELECT ?type WHERE { <m.012x34> <type.type> ?type .} 
------------------------ 
| type | 
======================== 
| "automotive.company" | 
------------------------
--------------- 
SELECT ?type WHERE 
{ <m.012x34> <type.type> ?type .} 
-------- 
| type | 
======== 
--------

执行此代码时,它会第一次返回正确的值。但是当getType 被第二次调用时它不会返回任何值,即使请求具有相同的值。似乎有一些我找不到的问题。有什么问题?

【问题讨论】:

  • 尽管有结果,但它是空的是什么意思。我明白为什么,如果您的数据与查询匹配,您会得到一个带有null 值的非空结果集。请显示您的输入数据和实际得到的输出。
  • 当我将您的代码加载到 Eclipse 中时,我注意到有许多不必要的东西。例如,query 从未使用过。将来,请尝试将代码简化为重现问题的最小示例。通常,这样做的过程会揭示问题所在。
  • 我正在尝试运行您的代码,但输出确实 not 看起来像您显示的输出。请逐字复制并粘贴您的输出。
  • 只是为了良好的实践,你应该考虑分解你的代码,以便每个函数只做一个“事情”,这将使你能够以指数速度更快地找出问题代码,并具有 self 的额外好处文档。

标签: java rdf sparql jena


【解决方案1】:

版本控制似乎有问题, 当我替换之前的 jena maven 依赖项时(下)

<dependency>
    <groupId>org.apache.jena</groupId>
    <artifactId>jena-arq</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>org.apache.jena</groupId>
    <artifactId>jena-core</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.jena</groupId>
    <artifactId>jena</artifactId>
    <version>2.11.0</version>
</dependency>

与,

<dependency>
    <groupId>org.apache.jena</groupId>
    <artifactId>apache-jena-libs</artifactId>
    <version>2.11.0</version>
</dependency>

代码似乎有效。希望它有用。干杯!

【讨论】:

  • 如果这是解决问题的原因,您能否在问题中也发布您原来的.pom,或者至少发布关于耶拿的sn-p。如果未来的用户遇到同样的问题,该信息将有所帮助(并且可能帮助他们首先找到它)。目前,除了您在 this comment 中发布的内容外,我们对您的“以前的 jena maven 依赖项”一无所知。
猜你喜欢
  • 2015-03-16
  • 2012-10-10
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
相关资源
最近更新 更多