0

I have a question.

I am trying to load my RDF files on Jena TDB.

I have written this code:

public void store(){
    String directory = "C:\\tdb";
    String source = "C:\\file1.rdf";
    String source1 = "C:\\file2.rdf";
    Dataset dataset = openTDB(directory);
    Model tdb = loadModel(source, dataset);
    dataset.addNamedModel("File1", tdb);

    Model tdb1 = loadModel(source1, dataset);
    dataset.addNamedModel("File2", tdb1);

    queryTDB(tdb, dataset);
    queryTDB(tdb1, dataset);

    tdb.close();
    tdb1.close();
    dataset.close();
}

public Dataset openTDB(String directory) {
    // open TDB dataset
    Dataset dataset = TDBFactory.createDataset(directory);
    return dataset;
}

public Model loadModel(String source, Dataset dataset) {

    Model tdb = dataset.getDefaultModel();
    FileManager.get().readModel( tdb, source, "RDF/XML" );
    return tdb;
}

In particular, I have two files and I want to load these files on Jena TDB. I have read on Internet that I can add a name to my models using "addNamedModel". In doing so, in the code above, I added the names "File1" and "File2".

Now, I want to query this dataset and I'm trying to write this code:

public void queryTDB(Model tdb, Dataset dataset) {

    String queryStr = "SELECT * { ?s ?p ?o }";

    Query query = QueryFactory.create(queryStr);
    QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
    /*Execute the Query*/
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.out(results) ;
    qexec.close();
}

This code works, but I would like to know how I can get the query results only for the model named "File1" (or "File2"). In fact, with the query written so, I get the results of both models.

How I can realize it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Musich87
  • 562
  • 1
  • 12
  • 31

1 Answers1

4

You're not using absolute IRIs to name you graphs, so I don't know offhand what your graph names will be. You can use a query like this to help find out what they are:

select * {
  graph ?g {  
    ?s ?p ?o
  }
}

Once you've done that, you can continue using the graph ?g { … } pattern, or use from named to specify a graph in your query:

select *
from named <name-of-graph>
{
  ?s ?p ?o
}

select * {
  graph <name-of-graph> {  
    ?s ?p ?o
  }
}

See 13.2 Specifying RDF Datasets and 13.3 Querying the Dataset for the full details and for more examples.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thanks. I have changed the name in "http://prova/File1" and "http://prova/File2". When I run select * { graph ?g { ?s ?p ?o } }, I get in column called "g" the follow values: "http://prova/File1" and "http://prova/File2". Then, when I run select * from named { ?s ?p ?o }, I get the results of both models. I get the same result when I run select * { graph { ?s ?p ?o } }. Why? – Musich87 Jul 16 '14 at 15:43
  • I think it's because you keep loading all your data into the default model, and then adding the default model as a named model. All your models end up being the same because of your loadModel implementation. – Joshua Taylor Jul 16 '14 at 15:56
  • 1
    Don't do: `Model tdb = dataset.getDefaultModel();` before `FileManager.get().readModel( tdb, source, "RDF/XML" );`. Do something like `Model model = ModelFactory.createDefaultModel();` instead. Then you're creating a fresh model, and there won't be conflicts when you add it as a named graph. – Joshua Taylor Jul 16 '14 at 17:04
  • Thanks Joshua. I have changed "Model tdb = dataset.getDefaultModel();" in "Model model = ModelFactory.createDefaultModel();". If I run this query: "String qiry = "select * {graph { ?s ?p ?o }}";" I get only query results of the model1 – Musich87 Jul 17 '14 at 06:45