将jena模型转换为jena tdb

时间:2016-02-07 11:52:32

标签: java query-optimization rdf jena

我用这个java代码

在jena中创建了一个模型
    Model m = ModelFactory.createDefaultModel();
    Resource a1 = m.createResource("<http://Actor1>");
    Resource a2 = m.createResource("<http://Actor2>");
    Resource a3 = m.createResource("<http://Actor3>");
    Property p3 = m.createProperty("<http://MovieTitle>");

    Resource m1 = m.createResource("<http://Movie1>");
    m1.addProperty(p3, "<http://M1>");
    Resource m2 = m.createResource("<http://Movie2>");
    m2.addProperty(p3, "<http://M2>");
    Resource m3 = m.createResource("<http://Movie3>");
    m3.addProperty(p3, "<http://M3>");
    Resource m4 = m.createResource("<http://Movie4>");
    m4.addProperty(p3, "<http://M4>");

    Property p1 = m.createProperty("<http://dateOfbirth>");
    Property p2 = m.createProperty("<http://played>");

    m.add(a1,p1 , "1980");
    m.add(a2,p1 , "1981");
    m.add(a3,p1 , "1982");

    m.add(a1,p2 , m1);
    m.add(a1,p2 , m2);
    m.add(a1,p2 , m3);
    m.add(a1,p2 , m4);
    m.add(a2,p2 , m1);
    m.add(a2,p2 , m3);
    m.add(a3,p2 , m1);
    m.add(a3,p2 , m3);
    m.add(a3,p2 , m4);

现在我想将此模型转换为jena tdb以创建stats.opt文件,以优化在此模型上执行的查询。我知道可以使用命令行将rdf文件加载到tdb并创建stats.opt。但我想用我的代码来做。有办法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用 - &gt;在jena中创建tdb。

// First create an empty tdb 
Dataset tdb = TDBFactory.createDataset(path2NewTdb);

// Then get its Model
Model tdbModel = tdb.getDefaultModel();

// Then update tdbModel. For example use the code you wrote
// above or if you already have a model use tdbModel.add(anotherModel)
// After that make sure to close everything

tdbModel.close();
tdb.close();