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

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

(java - Extracting ontology namespaces/prefixes with OWL API)

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

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

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

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

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自己解析文件)?

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

在解析过程中找到的前缀作为OWLDocumentFormat与本体关联实例的一部分保存

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