Query multiple models within same Dataset in TDB Jena

1.1k views Asked by At

I have an ontology (say dgo.owl) and another file containing individuals for same ontology stored in rdf format. I have stored both (ontology and instances/individual file) files in TDB under different named models (say dgo_ont and homedata).

Now, I want to fire different SPARQL queries on a combination of these created models. So the question is do I need to create one more model in TDB which will contain the triples of existing two named models or there is a mechanism in Jena such that I do not need to combine two existing named models and still I will get results. Till now, I tried with the following code:

1 static Dataset dataset;
2 static Model model;
3 public static void main(String[] args)
4 {
5   dataset = TDBFactory.createDataset("TDB_database/");
6   dataset.begin(ReadWrite.READ);
7   try{
8 model=dataset.getNamedModel("dgo_ont").add(dataset.getNamedModel("homedata"));
9   String qr=   SELECT *  Where    { ?s ?p ?o}; //for illustration
10    Query qy = QueryFactory.create(qr);
11    QueryExecution qe = QueryExecutionFactory.create(qy,model);
12    ResultSet rs= qe.execSelect();
13    ResultSetFormatter.out(System.out, rs, qy) ;
14    qe.close();
15    }
16   finally{
17      model.close();
18      dataset.end();       
19 }

The output obtained on running this program is:

Exception in thread "main" java.lang.NullPointerException
    at ac.iiitd.sparql.SparqlQuery.main(SparqlQuery.java:56)

In the main program line # 56 corresponds to the line # 17 of above script. But, I suspect line # 8 to be the root cause. This is because whenever I change this line to below code I used to get desired output.

         model=dataset.getNamedModel("dgo_ont");
               or
         model=dataset.getNamedModel("homedata");

So the whole problem is how to query the combination of named models in a given dataset.

1

There are 1 answers

1
AndyS On BEST ANSWER

You can make the default graph the union of the named graphs:

https://jena.apache.org/documentation/tdb/datasets.html

or a subset of the graphs (efficiency issues at very large scale):

https://jena.apache.org/documentation/tdb/dynamic_datasets.html