我为websemantic学习jena。我尝试使用getAllSubClass函数来读取本体(.owl)。
level1-> level2子类level1-> level3子类level2-> level4 子类level3-> level5子类level4
这是我的代码:
public void getAllSubClass(String class)
{
String temp = uri+""+class;
OntClass classOwl = ontology.getOntClass(temp);
do
{
System.out.println(classOwl.getLocalName());
temp = uri+""+classOwl.getLocalName();
classOwl = ontology.getOntClass(temp);
classOwl = classOwl.getSubClass();
}while(!classOwl.getLocalName().equals((Object)"Nothing"));
}
如果class level1 / level2 / level3输出总是在level3中停止 如果class level4输出level4-level5
我的代码如何获取输出level1 ... level5?
答案 0 :(得分:0)
我已经解决了这个问题,我创建了一个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;
}
}
如果没有实例化,只需调用示例JenaHelper.getConceptsNames("...","...")
。祝你好运。