Using OWL API, given an OWLClass, how can I get <rdfs:label> of it?
Asked Answered
F

3

7

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.

Ferule answered 26/12, 2013 at 6:47 Comment(5)
I would recommend to use 3.4.8 for the time being, because 3.4.9 has been built with an error, which means the distribution jar does not contain all needed classes. 3.4.10 will fix the issue, and should be released in January 2014Mauri
To be more precise, this is the issue tracking the problem with 3.4.9Mauri
@Ignazio, can you please double-check my answer below? It seems the cls.getAnnotations(o, df.getRDFSLabel()) method is no longer available in 4.0.2, as of today.Crespi
The method is available in version 3.x; it has been changed to a static method in EntitySearcher in version 4.xMauri
Thank you, I will amend my answer with both approaches.Crespi
T
7

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());
      }
   }
}
Tele answered 26/12, 2013 at 12:37 Comment(2)
How do you ensure that val.getLiteral() returns the pt version of the stringCerf
You would probably need more code to check for that, here's just some pseudo-cdoeTele
C
6

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()); 
    }
}
Crespi answered 19/2, 2015 at 3:39 Comment(3)
The getAnnotations() method is available on OWLEntity in version 3.x; it has been changed to a static method in EntitySearcher in version 4.x.Mauri
The code you show is one way to get the same result; other ways are going through EntitySearcher and using an OWLAnnotationValueVisitorEx to extract the literal value. EntitySearcher also provides a method that accepts an annotation property, so that only the label assertions would be retrieved.Mauri
Care should be taken with the EntitySearcher approach. I don't understand the details, but it appears to give different results under some circumstances.Simarouba
A
1

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;
}
Almoner answered 12/9, 2016 at 3:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.