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

  • possible duplicate of [DBpedia Jena Query returning null](http://stackoverflow.com/questions/15663510/dbpedia-jena-query-returning-null) – Joshua Taylor Nov 13 '13 at 13:26
  • What do you mean that it's empty despite there being results. I can see why, if your data matches the query, you'd get a non-empty result set with `null` values. Please show your input data and the output that you're actually getting. – Joshua Taylor Nov 13 '13 at 13:39
  • 1
    As I'm loading your code into Eclipse, I notice that there are a number of unnecessary things. E.g., `query` is never used. In the future, please try to reduce the code to a _minimal_ example that reproduces the problem. Often times, the process of doing that will reveal what the problem is. – Joshua Taylor Nov 13 '13 at 20:59
  • I'm trying to run your code, but the output does _not_ look like the output that you've shown. Please copy and paste your output verbatim. – Joshua Taylor Nov 13 '13 at 21:11
  • I've reworked this into the code and output shown in http://pastebin.com/RdeHuvmj . Can you run it and see whether it works for you or not? I don't see what difference there would be, but it seems to work. If it doesn't work for you, perhaps we can address the issue in it. It doesn't require an external file, so it should be a bit easier to test. – Joshua Taylor Nov 13 '13 at 21:17
  • 1
    Just for good practice, you should consider breaking up you code so that each function does exactly one "thing", this will allow you to figure out the problem code exponentially faster and have the added benefit of self documentation. – MichaelTaylor3D Nov 14 '13 at 03:35

1 Answers1

0

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!

  • If this is what resolved the problem, could you post your original `.pom` in the question as well, or at least the snippet about Jena. That information will help if future users run into the same problem (and may help them find it in the first place). Right now, we don't know anything about your "previous jena maven dependency" except what you posted in [this comment](http://stackoverflow.com/questions/19949592/empty-resultset-on-second-query-despite-valid-results-with-first#comment29723950_19955257). – Joshua Taylor Nov 14 '13 at 12:18