How to preserve namespace prefixes in a Jena TDB d

2019-07-26 07:46发布

问题:

I am using Jena TDB (1.1.1) to store a set of named graphs. Everything works fine but whenever I retrieve a named graph from the the dataset, all the namespace prefix information is lost. Is there a way to preserve the namespace prefixes in the original RDF graph.

Following code snippet shows the issue.

@Test
public void testPreserveNsPrefixes(){

    String modelText = "@prefix ro:    <http://purl.org/wf4ever/ro#> ." +
            "@prefix ore:   <http://www.openarchives.org/ore/terms/> ." +
            "@prefix ldp:   <http://www.w3.org/ns/ldp#> ." +
            "<http://example.org/ro> a ore:Aggregation , ro:ResearchObject , ldp:DirectContainer .\n" ;

    // Build the RDF graph
    InputStream stream = new ByteArrayInputStream(modelText.getBytes(StandardCharsets.UTF_8));
    Model model = ModelFactory.createDefaultModel();
    model.read(stream, null, "TURTLE");
    System.out.println("NS prefix count: " + model.getNsPrefixMap().size());

    //Create a dataset
    Dataset dataset = TDBFactory.createDataset("test");

    // Add the RDF graph to the dataset
    dataset.begin(ReadWrite.WRITE) ;
    try {
        dataset.addNamedModel("http://example.org/ro", model);
        dataset.commit() ;
    } finally {
        dataset.end() ;
    }

    //Read the RDF graph again
    dataset.begin(ReadWrite.READ);
    try{
        Model model2 = dataset.getNamedModel("http://example.org/ro");
        model2.write(System.out, "TURTLE");
        System.out.println("NS prefix count: " + model2.getNsPrefixMap().size());
    } finally {
        dataset.end();
    }
}

The output of this is:

    NS prefix count: 3
    <http://example.org/ro>
        a       <http://www.w3.org/ns/ldp#DirectContainer> ,
                <http://purl.org/wf4ever/ro#ResearchObject> ,
                <http://www.openarchives.org/ore/terms/Aggregation> .
    NS prefix count: 0

I think is related to this question though I don't think it is an exact duplicate.

回答1:

Just so that the answer isn't lost if the comments get deleted, AndyS mentioned that this was discussed on the Jena user's mailing list, and resolved in issue JENA-860, Adding a graph does not add prefixes.