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

Java 使用OwlApi从带有标签的owl文件中提取子类,java,owl,Java,Owl,我正在研究从owl文件中提取类和子类。我在dlquery教程示例的指导下使用OwlApi。除了处理带有保留字符的实体外,它工作得很好。有人建议我使用标签注释而不是从IRIs提取实体,特别是使用AnnotationValueShortFormProvider而不是SimpleShortFormProvider。下面是我检索所有子类的代码。例如,让我们使用“美国”作为实体。 其中shortFormProvider是AnnotationValueShortFormProvider的实例 我的问题是

我正在研究从owl文件中提取类和子类。我在dlquery教程示例的指导下使用OwlApi。除了处理带有保留字符的实体外,它工作得很好。有人建议我使用标签注释而不是从IRIs提取实体,特别是使用AnnotationValueShortFormProvider而不是SimpleShortFormProvider。下面是我检索所有子类的代码。例如,让我们使用“美国”作为实体。

其中shortFormProvider是AnnotationValueShortFormProvider的实例


我的问题是,由于解析字符串将提取前缀/标记“United”,因此如何在不解析字符串“United”的情况下实例化classExpression?或者,我们是否可以使用另一个示例代码块来使用标签注释而不是IRIs来检索子类?

如果您有类似“United States”的标签,Java字符串应该类似于“United States”。 单引号用于多字文字值

如果您有标签值,也可以直接在本体中查找,而无需使用曼彻斯特语法解析器。在您可以找到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();
        }
    }
}
}
for(OWLClass-OWLClass:o.getClassesInSignature()){
//获取使用label属性的类上的注释
for(owlnotation注释:owlClass.getAnnotations(o,dataFactoryf.getRDFSLabel()){
if(注释.getValue()实例OWLLiteral){
OWLLiteral val=(OWLLiteral)annotation.getValue();
if(val.getLiteral().equals(inputLabel)){
//此时,owlClass变量就是您要查找的owlClass
NodeSet subclass=this.reasoner.getsubclass(owlClass,direct);
返回子类;
}
}
}
}

是的,该代码帮助我提取标签,我已经在做了。但是我现在如何提取该标签的所有子类(或子类中的标签),就像上面的函数一样?所有文档示例都指向使用IRIs获取子类而不是标签。一旦找到上面的owlClass变量,就可以像在示例中使用classExpression一样使用它。我将更新答案。更新后的代码将尝试从类IRI而不是从标签中提取子类。例如,如果使用类为IRI“US(America)”的owl文件运行代码,它将失败,并显示消息:
线程“main”org.semanticweb.owlapi.expression.ParserException中的异常:在第1行第1列遇到untitled-ontology-24。应为以下之一:类名对象属性名数据属性名不反转({
。我想我真正的问题是,如何提取支持保留IRI字符(如括号)的子类?我认为答案是使用“标签”。您可以添加一个您试图解析的示例字符串吗?在我的示例中,类IRI从未用于匹配输入标签,只有rdfs:label值用于匹配类。请注意,类标签不是IRI的一部分-你说的标签是指本地名称还是类IRI的片段?代码可以工作并标记为已接受!我没有用代码更改更新“getSuperClasses”方法,因此出现了错误。非常感谢!在幕后,看起来它使用的是类的IRI,而不是实验室的IRIel或IRI前缀(如果它有保留字符)来获取子类/超类。这没关系。但出于好奇,为什么大多数示例都使用曼彻斯特语法解析器,而不是像上面那样使用owl类来获取子类?
this.parser = new DLQueryParser(rootOntology, shortFormProvider);
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();
        }
    }
}
}