Search code examples
owlprotegeowl-api

How to set the Ontology ID of an anonymous Ontology using the OWL API


I have a file containing an ontology without an ontology id (the ontology tag <Ontology/> is empty). The used serialization format is RDF/XML. My goal is to serialize the file, set an ontology id and write the file back using the OWLAPI. Unfortunatly I don't know how to do this. I tried the following:

ontology = ontologyManager.loadOntologyFromOntologyDocument(new File("filename"));
ontologyManager.setOntologyDocumentIRI(ontology, IRI.create("http://www.mydesiredIri.com/abc"));
ontologyManager.saveOntology(ontology,new FileOutputStream(new File("outputfile")));

By running the code, the Ontology-ID is not added to the ontology. Instead of <Ontology rdf:about="http://www.mydesiredIri.com/abc"/> the tag is still emtpy. What I am doing wrong?

Thank you!

Kind regards


Solution

  • OWLOntologyManager.setOntologyDocumentIRI() is for setting the document IRI of the ontology, not the ontology IRI itself. The difference between the two is that the document IRI is a resolvable URL or a file path (i.e., int can be used to parse the ontology), while the ontology IRI is the symbolic name of the ontology (it does not need to be resolvable and it can even be missing - which is the case for anonymous ontologies).

    To set the ontology IRI, use:

    //versionIRI can be null
    OWLOntologyID newOntologyID = new OWLOntologyID(ontologyIRI, versionIRI);
    // Create the change that will set our version IRI
    SetOntologyID setOntologyID = new SetOntologyID(ontology, newOntologyID);
    // Apply the change
    manager.applyChange(setOntologyID);
    

    After this, save the ontology as usual.