0

I can currently load an .owl file into a dataset and commit it to JENA TDB. How can I go the other direction? In other words, how can I get all the information in the TDB and put it in an .owl file (rdf/xml using the JENA api)?

Nick
  • 1,743
  • 6
  • 23
  • 38

2 Answers2

0

You'll not that there exists a few assumptions in this answer.

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

In general, you cannot express the content of an entire dataset as RDF/XML. The reasoning is that the RDF Dataset is easiest to express in Quads rather than Triples. If you want to write out a whole dataset, including the named graphs, then you'll need to use another method:

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");
}
Rob Hall
  • 2,693
  • 16
  • 22
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();
        }
}