mohalim
mohalim

Reputation: 3

Save ontology file OWL API

I am saving an owl file as shown in OWL API example file.

File file = File.createTempFile("sample", "saving");            
OWLOntologyFormat format = manager.getOntologyFormat(ontology);     
OWLXMLOntologyFormat owlxmlFormat = new OWLXMLOntologyFormat();
if (format.isPrefixOWLOntologyFormat()) {
   owlxmlFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());
}
manager.saveOntology(ontology, owlxmlFormat, IRI.create(file.toURI()));

I also tried following code.

File file = new File("sample.owl");         
OWLOntologyFormat format = manager.getOntologyFormat(ontology);     
OWLXMLOntologyFormat owlxmlFormat = new OWLXMLOntologyFormat();
if (format.isPrefixOWLOntologyFormat()) {
   owlxmlFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());
}
manager.saveOntology(ontology, owlxmlFormat, IRI.create(file.toURI()));

Both methods failed to save the file. Please help.

Edit:

Following codes for creating the ontology and the manager

manager = OWLManager.createOWLOntologyManager();
reasonerFactory = (OWLReasonerFactory) PelletReasonerFactory.getInstance();
dataFactory = manager.getOWLDataFactory();
pm = new DefaultPrefixManager(BASE_URL);
File file = new File(filename);
OWLOntology ontology = null;
try {
    ontology = manager.loadOntologyFromOntologyDocument(file);
} catch (OWLOntologyCreationException e) {
    System.out.println("Fail to load file. " + e);
}

For your information, the following code able to save (modified) ontology to the original file.

manager.saveOntology(ontology);

Thank you.

Upvotes: 0

Views: 4222

Answers (2)

loopasam
loopasam

Reputation: 3136

The following code should read an existing ontology file (test.owl) and save it in a different format (test-format.owl). Make sure the original file exists and contains axioms (the getAxiomCount() method.

//Create the manager
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
//File with an existing ontology - make sure it's there!
File file = new File("/home/test.owl");
//Load the ontology from the file
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);
//Check if the ontology contains any axioms     
System.out.println("Number of axioms: " + ontology.getAxiomCount());

//Create a file for the new format
File fileformated = new File("/home/test-format.owl");
//Save the ontology in a different format
OWLOntologyFormat format = manager.getOntologyFormat(ontology);
OWLXMLOntologyFormat owlxmlFormat = new OWLXMLOntologyFormat();
if (format.isPrefixOWLOntologyFormat()) { 
  owlxmlFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat()); 
}
manager.saveOntology(ontology, owlxmlFormat, IRI.create(fileformated.toURI()));

Upvotes: 1

Ignazio
Ignazio

Reputation: 10659

Notice that the file you're creating is a temp file - it is not supposed to exist after the code has exited.

I suppose you want a permanent record; in which case, create a file the regular way:

File file = new File("path/to/file");

Edit: I noticed you tried this too. Do you have write access to the folder the code is running in? Did you get any errors reported?

Upvotes: 2

Related Questions