在第二个查询上为空ResultSet(尽管第一个查询具有有效结果) [英] empty ResultSet on second query (despite valid results with first)

查看:108
本文介绍了在第二个查询上为空ResultSet(尽管第一个查询具有有效结果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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时,它也不会返回任何值.似乎有一些我找不到的问题.有什么问题吗?

When this code is executed, it returns the proper value for the first time. But it does not return any value when getType is called a second time, even if the request has the same value. There seems to be some problem that I can not find. What is the problem?

推荐答案

版本控制似乎存在问题, 当我替换以前的jena maven依赖关系(如下)时

There seems to be a problem with the versioning, when I replace the previous jena maven dependency (below)

<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>

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

the code seem to work. Hope its useful. Cheers!

这篇关于在第二个查询上为空ResultSet(尽管第一个查询具有有效结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆