使用OWL API 3.4.9.
给定一个OWLClass与本体,我怎么能得到<rdfs:label>的是OWLClass在本体论?
我希望得到的标签类型String.
受到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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
该接受的答案是有效的OWLAPI 3.x版本(3.4和3.5版本),而不是OWLAPI 4.x和更新。
要检索rdfs:label针对OWL类声明的值,请尝试以下操作:
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
正如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());
}
}
Run Code Online (Sandbox Code Playgroud)