使用 OWL API 提取本体命名空间/前缀

use*_*843 1 java namespaces ontology prefix owl-api

在一个.owl文件中,我声明了一些前缀,如下所示:

Prefix(:=<http://default.ont/my_ont/>)
Prefix(ex:=<http://example.org/ex#>)
Prefix(ex2:=<http://example2.org/ex#>)
...
Run Code Online (Sandbox Code Playgroud)

在 Java 项目中使用我的本体,如下所示:

OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File(resourceFullPath(ontologyFilename)));
Run Code Online (Sandbox Code Playgroud)

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

{
  ""    -> "http://default.ont/my_ont/",
  "ex"  -> "http://example.org/ex#",
  "ex2" -> "http://example2.org/ex#"
}
Run Code Online (Sandbox Code Playgroud)

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

Ign*_*zio 5

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

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