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
.
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());
}
}
}
val.getLiteral()
returns the pt
version of the string –
Cerf 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());
}
}
EntitySearcher
approach. I don't understand the details, but it appears to give different results under some circumstances. –
Simarouba 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;
}
© 2022 - 2024 — McMap. All rights reserved.
cls.getAnnotations(o, df.getRDFSLabel())
method is no longer available in 4.0.2, as of today. – Crespi