0

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

4

2 回答 2

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();
        }
}
于 2014-12-24T09:43:28.147 回答
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 数据集最容易用 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");
}
于 2014-04-23T16:03:16.637 回答