簡體   English   中英

如何從Apache JENA TDB創建.owl文件

[英]How to create an .owl file from apache JENA TDB

我目前可以將.owl文件加載到數據集中,並將其提交給JENA TDB。 我該如何去另一個方向? 換句話說,如何獲取TDB中的所有信息並將其放入.owl文件(使用JENA api的rdf / xml)中?

您不會在此答案中有一些假設。

// 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數據集最容易以四邊形而不是三邊形表示。 如果要寫出包括命名圖在內的整個數據集,則需要使用另一種方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM