使用OWL API 3.4.9。
鉴于OWLClass
和本体,如何在该本体中获得<rdfs:label>
的{{1}}?
我希望获得OWLClass
类型的标签。
答案 0 :(得分:6)
受guide to the 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());
}
}
}
答案 1 :(得分:4)
accepted answer对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(value instanceof OWLLiteral) {
System.out.println(c + " labelled " + ((OWLLiteral) value).getLiteral());
}
}
答案 2 :(得分:1)
这是我用来从类中提取标签的方法。
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;
}