Using OWL API, given an OWLClass, how can I get <rdfs:label> of it?
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());
}
}