Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

java - empty ResultSet on second query (despite valid results with first)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

with,

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...