使用OWL API,给定一个OWLClass,我怎样才能得到它的<rdfs:label>?



使用 OWL API 3.4.9.

给定一个关于本体的OWLClass,我怎样才能<rdfs:label>到该本体中的OWLClass

我希望获得String类型的标签.

受OWL-API指南的启发,以下代码应该可以工作(未经过测试):

//Initialise
OWLOntologyManager m = create();
OWLOntology o = m.loadOntologyFromOntologyDocument(pizza_iri);
OWLDataFactory df = OWLManager.getOWLDataFactory();
//Get your class of interest
OWLClass cls = df.getOWLClass(IRI.create(pizza_iri + "#foo"));
// Get the annotations on the class that use the label property (rdfs:label)
for (OWLAnnotation annotation : cls.getAnnotations(o, df.getRDFSLabel())) {
  if (annotation.getValue() instanceof OWLLiteral) {
    OWLLiteral val = (OWLLiteral) annotation.getValue();
    // look for portuguese labels - can be skipped
      if (val.hasLang("pt")) {
        //Get your String here
        System.out.println(cls + " labelled " + val.getLiteral());
      }
   }
}

接受的答案对 OWLAPI 版本 3.x(3.4 和 3.5 版本)有效,但对 OWL-API 4.x 及更高版本无效。

要检索针对 OWL 类断言rdfs:label值,请尝试以下操作:

OWLClass c = ...; 
OWLOntology o = ...;
IRI cIRI = c.getIRI();
for(OWLAnnotationAssertionAxiom a : ont.getAnnotationAssertionAxioms(cIRI)) {
    if(a.getProperty().isLabel()) {
        if(a.getValue() instanceof OWLLiteral) {
            OWLLiteral val = (OWLLiteral) a.getValue();
            System.out.println(c + " labelled " + val.getLiteral());
        }
    }
}

编辑

正如Ignazio所指出的,也可以使用EntitySearcher,例如:

OWLClass c = ...; 
OWLOntology o = ...;
for(OWLAnnotation a : EntitySearcher.getAnnotations(c, o, factory.getRDFSLabel())) {
    OWLAnnotationValue val = a.getValue();
    if(val instanceof OWLLiteral) {
        System.out.println(c + " labelled " + ((OWLLiteral) val).getLiteral()); 
    }
}

这是我编写的从类中提取标签的方法。

private List<String> classLabels(OWLClass class){
    List<String> labels;
    labels = ontologiaOWL.annotationAssertionAxioms(class.getIRI())
            //get only the annotations with rdf Label property
            .filter(axiom -> axiom.getProperty().getIRI().getIRIString().equals(OWLRDFVocabulary.RDFS_LABEL.getIRI().getIRIString()))
            .map(axiom -> axiom.getAnnotation())
            .filter(annotation-> annotation.getValue() instanceof OWLLiteral)
            .map(annotation -> (OWLLiteral) annotation.getValue())
            .map(literal -> literal.getLiteral())
            .collect(Collectors.toList());
     return labels;
}

相关内容

最新更新