我已经解决了这个问题,我创建了一个 JenaHelper 类来解析使用 Protege 创建的 Ontologoy (.owl) 文件。我希望这将帮助您,我开发了用于检索所有概念、概念关系、所有值和特定概念的值的方法。
public class JenaHelper {
public JenaHelper() {
}
public static Set<String> getConceptsNames(Model model, String uri) {
Set<String> conceptsName = new HashSet<String>();
StmtIterator sIter = model.listStatements();
for (; sIter.hasNext();) {
Statement stmt = sIter.nextStatement();
String subject = stmt.getSubject().toString();
String predicat = stmt.getPredicate().toString();
String object = stmt.getObject().toString();
if (subject.contains(uri) || predicat.contains(uri) || object.contains(uri)) {
String s = subject.substring(subject.indexOf("#") + 1);
String p = predicat.substring(predicat.indexOf("#") + 1);
String o = object.substring(object.indexOf("#") + 1);
if ("subClassOf".equals(p) && !s.toLowerCase().equals("thing") && !o.toLowerCase().equals("thing")
&& !s.contains("/") && !o.contains("/")
&& !s.contains(":") && !o.contains(":")
&& !s.contains("-") && !o.contains("-")) {
conceptsName.add(s);
conceptsName.add(o);
}
}
}
sIter.close();
return conceptsName;
}
public static Set<String> getValues(Model model, String uri) {
Set<String> values = new HashSet<String>();
StmtIterator sIter = model.listStatements();
for (; sIter.hasNext();) {
Statement stmt = sIter.nextStatement();
String subject = stmt.getSubject().toString();
String predicat = stmt.getPredicate().toString();
if (subject.contains(uri) || predicat.contains(uri)) {
String s = subject.substring(subject.indexOf("#") + 1);
String p = predicat.substring(predicat.indexOf("#") + 1);
if ("type".equals(p)) {
values.add(s);
}
}
}
sIter.close();
return values;
}
public static Set<String> getValuesOfConcept(Model model, String className, String uri){
Set<String> valuesConcept = new HashSet<String>();
StmtIterator sIter = model.listStatements();
for (; sIter.hasNext();) {
Statement stmt = sIter.nextStatement();
String subject = stmt.getSubject().toString();
String predicat = stmt.getPredicate().toString();
String object = stmt.getObject().toString();
if (subject.contains(uri) || predicat.contains(uri) || object.contains(uri)) {
String s = subject.substring(subject.indexOf("#") + 1);
String p = predicat.substring(predicat.indexOf("#") + 1);
String o = object.substring(object.indexOf("#") + 1);
if (className.equals(o)) {
if ("type".equals(p) ) {
valuesConcept.add(s);
}
}
}
}
sIter.close();
return valuesConcept;
}
public static String getRelation(Model model, String conceptSource, String conceptDestination, String uri){
String relationName = "Fail";
StmtIterator sIter = model.listStatements();
while(sIter.hasNext()) {
Statement stmt = sIter.nextStatement();
String subject = stmt.getSubject().toString();
String predicat = stmt.getPredicate().toString();
String object = stmt.getObject().toString();
if (subject.contains(uri) || predicat.contains(uri) || object.contains(uri)) {
String s = subject.substring(subject.indexOf("#") + 1);
String p = predicat.substring(predicat.indexOf("#") + 1);
String o = object.substring(object.indexOf("#") + 1);
if (o.equals(conceptSource) && s.equals(conceptDestination)) {
relationName = p;
break;
}
}
}
sIter.close();
return relationName;
}
}
没有实例化只需调用 example JenaHelper.getConceptsNames("...","...")
。祝你好运。