简体   繁体   English

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

[英]Extracting ontology namespaces/prefixes with OWL API

Within a .owl file, I'm declaring some prefixes like this:.owl文件中,我声明了一些前缀,如下所示:

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:并在 Java 项目中使用我的本体,如下所示:

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:现在我想用以下内容以编程方式构建一个Map<String, String>

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

How can I do this with OWL API (ie without parsing the .owl file by myself)?如何使用OWL API做到这一点(即不自己解析.owl文件)?

The prefixes found during parsing are held as part of the OWLDocumentFormat instance associated to the ontology:在解析期间找到的前缀作为与本体关联的OWLDocumentFormat实例的一部分保存:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM