我使用Jena TDB(1.1.1)存储一组命名图。一切正常,但每当我从数据集中检索命名图时,所有命名空间前缀信息都将丢失。有没有办法在原始RDF图中保留名称空间前缀。
以下代码段显示了该问题。
@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();
}
}
这个输出是:
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
我认为与this question有关,但我认为它并不完全相同。
答案 0 :(得分:0)
如果评论被删除,AndyS mentioned这是discussed on the Jena user's mailing list,并在问题JENA-860, Adding a graph does not add prefixes中解决,那么答案就不会丢失。< / p>