用 OWL API 提取 ontology namespaces/prefixes

Extracting ontology namespaces/prefixes with OWL API

.owl 文件中,我声明了一些这样的前缀:

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

并在这样的 Java 项目中使用我的 ontology:

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

现在我想以编程方式构建一个 Map<String, String>,内容如下:

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

如何使用 OWL API 执行此操作(即不自己解析 .owl 文件)?

解析期间找到的前缀作为与 ontology 相关联的 OWLDocumentFormat 实例的一部分保存:

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