1

I define an assembler file with name dataset2.ttl. The content of this file is:

@prefix tdb:     <http://jena.hpl.hp.com/2008/tdb#> .

@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .

@prefix ja:      <http://jena.hpl.hp.com/2005/11/Assembler#> .

[] ja:loadClass "com.hp.hpl.jena.tdb.TDB" .
tdb:DatasetTDB  rdfs:subClassOf  ja:RDFDataset .
tdb:GraphTDB    rdfs:subClassOf  ja:Model .
<#dataset> rdf:type         tdb:DatasetTDB ;
   tdb:location "DB" ;
   tdb:unionDefaultGraph true ;
   .

<#data1> rdf:type tdb:GraphTDB ;
    tdb:dataset <#dataset> ;
    tdb:graphName <http://example.org/data1> ;
    ja:content [ja:externalContent <file:///C:/Users/data/data1.ttl>;];
    .

The related jena code to create a datase is:

public class TDB {

public static void main(String[] args) {
    Dataset ds = null;
    try {
        ds = TDBFactory.assembleDataset("Dataset2.ttl");

        if(ds == null) {

            System.out.println("initial tdb failed");
        } else {
            System.out.println("Default Model:");

            Model model = ds.getDefaultModel();

            ds.begin(ReadWrite.WRITE);
            model.write(System.out, "TURTLE");
        }
    } finally {
        if(ds != null) {
            ds.close();
        }
    }
}

The content in data1.ttl is:

@prefix : <http://example.org/> .
@prefix foaf:   <http://xmlns.com/foaf/0.1/> .

:alice
a           foaf:Person ;
foaf:name   "Alice" ;
foaf:mbox   <mailto:alice@example.org> ;
foaf:knows  :bob ;
foaf:knows  :charlie ;
foaf:knows  :snoopy ;
.

:bob
foaf:name   "Bob" ;
foaf:knows  :charlie ;
.

:charlie
foaf:name   "Charlie" ;
foaf:knows  :alice ;
.

A dataset has been created using this code. However, the content in the file of "data1.ttl" has not been read into the model. What is the problem of my code?

WuZhu
  • 75
  • 6

3 Answers3

1

You also have

<#dataset> rdf:type         tdb:DatasetTDB ;
   tdb:location "DB" ;
   tdb:unionDefaultGraph true ;
   .

and

ds = TDBFactory.assembleDataset("Dataset2.ttl");

so you are asking Jena to assemble a dataset. That dataset will be <#dataset> (find by the type). It is not connected the graph you define so that's ignored; you can remove that part. Assembling the dataset is the way to do this.

You have tdb:unionDefaultGraph true so the default graph for query is the combination of all named graphs in the dataset.

Pick one out with model.getNamedModel.

If you use SPARQL, use the GRAPH keyword.

AndyS
  • 16,345
  • 17
  • 21
0

I would try validating your ttl file online to make sure they dataset2.ttl and data.ttl are both valid. I noticed you seem to add an extra semi-colon at the end when it's not needed (it should end with just a period).

try changing your line to this:

ja:content [ja:externalContent <file:///C:/Users/data/data1.ttl>] .

Omar Elabd
  • 1,914
  • 1
  • 18
  • 23
0
<#data1> rdf:type tdb:GraphTDB ;
    tdb:dataset <#dataset> ;
    tdb:graphName <http://example.org/data1> ;
    ja:content [ja:externalContent <file:///C:/Users/data/data1.ttl>;];
.

Note the tdb:GraphTDB which means attach to a graph in the database. It does not load data with ja:content.

As a persistent store, it is expected that the data is already loaded, e.g.by tdbloader, not every time the assembler is used.

AndyS
  • 16,345
  • 17
  • 21
  • Thanks for your answer. Is there any way to load data into TDB (not memory) using an assembler file? – WuZhu Feb 08 '17 at 09:36
  • Could you please give me an example that the loaded data is accord with the description in an assembler file. – WuZhu Feb 08 '17 at 10:32
  • It is expected that the data is already loaded. A persistent database is not for loading each time. Either use a memory graph and load it, or load the database first. – AndyS Feb 08 '17 at 18:31
  • Thanks for your answer. Currently the file has been loaded into a memory graph. Then I use a dataset to store those named graphs. However, when I query them using SPARQL named graph, the result contains all the info from the dataset, not limited in the specific named graph. – WuZhu Feb 09 '17 at 13:52