简体   繁体   English

使用OwlApi从带有标签的owl文件中提取子类

[英]Using OwlApi to extract subclasses from owl file with labels

I am looking at extracting classes and subclasses from an owl file. 我正在寻找从猫头鹰文件中提取类和子类。 I am using the OwlApi with some guidance from dlquery tutorial example. 我在dlquery教程示例的一些指导下使用OwlApi。 It works well except for handling entities with reserved characters. 除处理带有保留字符的实体外,它效果很好。 I have been advised to use label annotation instead of extracting entities from IRIs, and specifically use the AnnotationValueShortFormProvider instead of SimpleShortFormProvider. 建议我使用标签注释,而不是从IRI中提取实体,特别是使用AnnotationValueShortFormProvider而不是SimpleShortFormProvider。 Here is my piece of code to retrieve all subclasses. 这是我的代码,用于检索所有子类。 Let's use 'United States' for example as the entity. 让我们以“美国”为例。

 private Set<OWLClass> getSubClasses(String cls, boolean direct) { if (cls.trim().length() == 0) { return Collections.emptySet(); } OWLClassExpression classExpression = this.parser.parseClassExpression(cls); NodeSet<OWLClass> subClasses = this.reasoner.getSubClasses(classExpression, direct); return subClasses.getFlattened(); } 

My parser is set as such: 我的解析器设置如下:

 this.parser = new DLQueryParser(rootOntology, shortFormProvider); 

where shortFormProvider is an instance of AnnotationValueShortFormProvider 其中shortFormProvider是AnnotationValueShortFormProvider的实例

My question is, how do I instantiate classExpression without parsing the String 'United States', since parsing the string will extract the prefix/token 'United'? 我的问题是,我如何在不解析字符串'United States'的情况下实例化classExpression,因为解析字符串将提取前缀/令牌'United'? Or is there another sample code block we could use to retrieve subclasses from using label annotations instead of an IRIs? 还是有另一个示例代码块可用于通过使用标签注释而不是IRI来检索子类?

If you have a label like 'United States', the Java string should look like "'United States'". 如果您有类似“美国”的标签,则Java字符串应看起来像“美国”。 Single quotes are used for multi word literal values. 单引号用于多字文字值。

If you have the label value you can also look up directly in the ontology without having to use a Manchester syntax parser. 如果您具有标签值,则也可以直接在本体中查找,而不必使用曼彻斯特语法解析器。 In the same documentation page where you can find the DL query example there are also examples on how to tho this. 在同一文档页面中,您可以找到DL查询示例,还有有关如何进行此查询的示例。

for(OWLClass owlClass: o.getClassesInSignature()){
// Get the annotations on the class that use the label property
for (OWLAnnotation annotation : owlClass.getAnnotations(o, dataFactoryf.getRDFSLabel())) {
    if (annotation.getValue() instanceof OWLLiteral) {
        OWLLiteral val = (OWLLiteral) annotation.getValue();
        if (val.getLiteral().equals(inputLabel)) {
            // at this point, the owlClass variable is the OWLClass you were looking for
            NodeSet<OWLClass> subClasses = this.reasoner.getSubClasses(owlClass, direct);
            return subClasses.getFlattened();
        }
    }
}
}

https://github.com/owlcs/owlapi/wiki/Documentation https://github.com/owlcs/owlapi/wiki/文档

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

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