如何在Jena TDB数据集中保留名称空间前缀? [英] How to preserve namespace prefixes in a Jena TDB dataset?

查看:95
本文介绍了如何在Jena TDB数据集中保留名称空间前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jena TDB(1.1.1)存储一组命名图.一切正常,但是每当我从数据集中检索一个命名图时,所有名称空间前缀信息都会丢失.有没有一种方法可以保留原始RDF图中的名称空间前缀.

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();
    }
}

此输出为:

    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.

推荐答案

请注意,如果注释被删除,答案不会丢失,提到,这是 JENA-中解决了860,添加图形不会添加前缀 .

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.

这篇关于如何在Jena TDB数据集中保留名称空间前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆