Вопрос

Using OWL API 3.4.9.

Given an OWLClass and on ontology, how can I get <rdfs:label> of that OWLClass in that ontology?

I hope to get the label in the type of String.

Это было полезно?

Решение

Inspired from the guide to the OWL-API, the following code should work (not tested):

//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());
      }
   }
}

Другие советы

The accepted answer is valid for OWLAPI version 3.x (3.4 and 3.5 versions) but not for OWL-API 4.x and newer.

To retrieve rdfs:label values asserted against OWL classes, try this instead:

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());
        }
    }
}

EDIT

As Ignazio has pointed out, EntitySearcher can also be used, for example:

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()); 
    }
}

Here is a method I wrote to extract labels from a class.

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top