如何从apache JENA TDB创建.owl文件

时间:2014-04-22 15:00:35

标签: rdf jena owl tdb

我现在可以将.owl文件加载到数据集中并将其提交给JENA TDB。我该怎么走另一个方向?换句话说,如何获取TDB中的所有信息并将其放入.owl文件(使用JENA api的rdf / xml)?

2 个答案:

答案 0 :(得分:0)

你并不是说这个答案中存在一些假设。

// Let's assume you pointed this at your existing TDB dataset on disk
final Dataset dataset = TDBFactory.createDataset(Files.createTempDirectory("ex0").toAbsolutePath().toString());

/* Your data is either in the default model of the dataset, or it's in some named  
 * graph. Let's assume that it's in a named graph with the name
 * 'urn:ex:your-graphs-iri'.
 */
// final Model yourData = dataset.getDefaultModel(); // If it were in the default
final Model yourData = dataset.getNamedModel("urn:ex:your-graphs-iri");

final Path tempFile = Files.createTempFile("ex1", ".owl");
try( final OutputStream out = Files.newOutputStream(tempFile, StandardOpenOption.CREATE_NEW) ) {
    yourData.write(out, null, "RDF/XML");
}

通常,您无法将整个数据集的内容表示为RDF / XML。原因是RDF Dataset最容易在Quads而不是Triples中表达。如果要写出整个数据集,包括命名图,那么您需要使用另一种方法:

final Dataset dataset = TDBFactory.createDataset(Files.createTempDirectory("ex0").toAbsolutePath().toString());
final Path tempFile = Files.createTempFile("ex1", ".quads");
try( final OutputStream out = Files.newOutputStream(tempFile, StandardOpenOption.CREATE_NEW) ) {
    RDFDataMgr.write(out, dataset, "NQUADS");
}

答案 1 :(得分:0)

public static void writeDatasetToFile(){
Dataset dataset = TDBFactory.createDataset("./Path/to/TDB");
Model model = dataset.getDefaultModel();
File file = new File("./path/of/file.owl");
        FileOutputStream os=null;
        try {
            os = new FileOutputStream(file);
            model.writeAll(os, "RDF/XML",null);//To write model with import closure
            model.write(os, "RDF/XML",null);//To write model without import closure
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}