Search code examples
javaowl-api

How to Store OWL Ontology in Functional Syntax


I am trying to create and store an ontology file in functional format using OWL API:

OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.createOntology();
OWLDataFactory factory = manager.getOWLDataFactory();

PrefixManager pm = new FunctionalSyntaxDocumentFormat();
pm.setDefaultPrefix(" :");

OWLClass item = factory.getOWLClass(IRI.create("item"), pm);
manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(item));

manager.saveOntology(ontology, new FunctionalSyntaxDocumentFormat(), new FileOutputStream("FileName"))

The result in the saved file for this axiom is this:

Declaration(Class(< :item>))

How do I get rid of the < > brackets around entities? It happens to all entities that I create, and it is preventing my file from being parsed correctly.


Solution

  • Two issues: there should not be a space in the default prefix, and the prefix manager you are setting the prefix on must be the same used in the call to saveOntology(). You can just pass the first functional document format to the last method in your code.

    Edit: After trying to run the code, I think there's a bit of a bug in the OWL API. It is necessary to set the format on the manager for the prefixes to be picked up properly. That should not be necessary. However, there's a workaround.

        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.createOntology();
        OWLDataFactory factory = manager.getOWLDataFactory();
        FunctionalSyntaxDocumentFormat pm = new FunctionalSyntaxDocumentFormat();
        pm.setPrefix(":", "http://test.owl/test#");
        manager.setOntologyFormat(ontology, pm);
        OWLClass item = factory.getOWLClass("item", pm);
        manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(item));
        manager.saveOntology(ontology, System.out);