Warm tip: This article is reproduced from serverfault.com, please click

Extracting ontology namespaces/prefixes with OWL API

发布于 2020-12-28 16:37:12

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)?

Questioner
user402843
Viewed
1
Ignazio 2020-12-29 04:10:14

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();
}