Take name and count number of sibling classes in generated ontology using Java

人盡茶涼 提交于 2019-12-12 03:36:14

问题


I first generated the ontology. It was successful. Then, I want to take sibling classes name for each class and count number of sibling classes of each class in the generated ontology.As a example,

    Main super class- A
    Two sub classes of A - B , C
    Three sub classes of B- D, E 

I tried using following code. I used getSuperClass to get the super class, and then getSubClass to get the subclasses of that. I used arraylist for first take name of each sibling classes. So, in above example output should be like,

 [C] [B] [E] [D]

In above output, 1st one for B's sibling, 2nd one for C's sibling......I used Jena for generate ontology. ( I heared about SPARQL query, but I am very new to it.)

Following code only for get sibling class name. It gave nullpointer error. But seems like output is correct with full link. How to separate last part?

public ArrayList<String> countSiblingClasses(String ontoClass) {

    ontologyCreation();
    this. m.read("http://localhost/new/onto1.owl");
     ExtendedIterator<OntClass> classes = ((OntModel) m).listClasses(); 

     ArrayList<String> siblingsName = new ArrayList<String>();
     while (classes.hasNext()) {

    OntClass all= (OntClass) classes.next();
    String cls = all.getSuperClass().listSubClasses().toSet().toString();
    System.out.println("class names="+cls);

    siblingsName.add(cls);
   }
    return siblingsName; 
}

It gave out put,

Exception in thread "main"  class names=[http://localhost/new/E, http://localhost/new/D]
 class names=[http://localhost/new/E, http://localhost/new/D]
 class names=[http://localhost/new/C, http://localhost/new/B]
 class names=[http://localhost/new/C, http://localhost/new/B]
java.lang.NullPointerException
    at Final_Cal.OntologyCreation2.countSiblingClasses(OntologyCreation2.java:235)
    at Final_Cal.OntologyCreation2.main(OntologyCreation2.java:34)

回答1:


The javadoc for OntClass.getSuperClass() says:

A super-class of this class or null

A will not have a superclass.



来源:https://stackoverflow.com/questions/35678156/take-name-and-count-number-of-sibling-classes-in-generated-ontology-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!