javanamespacesontologyprefixowl-api

Extracting ontology namespaces/prefixes with OWL API


Within a .owl file, I'm declaring some prefixes like this:

Prefix(:=<http://default.ont/my_ont/>)
Prefix(ex:=<http://example.org/ex#>)
Prefix(ex2:=<http://example2.org/ex#>)
...

And using my ontology in a Java project like this:

OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File(resourceFullPath(ontologyFilename)));

Now I want to build a Map<String, String> in a programmatic way with the following content:

{
  ""    -> "http://default.ont/my_ont/",
  "ex"  -> "http://example.org/ex#",
  "ex2" -> "http://example2.org/ex#"
}

How can I do this with OWL API (i.e. without parsing the .owl file by myself)?


Solution

  • The prefixes found during parsing are held as part of the OWLDocumentFormat instance associated to the ontology:

    OWLDocumentFormat format = manager.getOntologyFormat(ontology);
    if (format.isPrefixOWLDocumentFormat()) {
        // this is the map you need
        Map<String, String> map = format.asPrefixOWLDocumentFormat().getPrefixName2PrefixMap();
    }