0

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)
Emalka
  • 381
  • 2
  • 4
  • 16
  • 1
    Is there a reason why you are using a HashMap? Is there a reason why a tree wouldn't work here? – Debosmit Ray Feb 28 '16 at 09:36
  • Thank u for your reply. What is you mentioned by tree? ontology is tree type highrarchy. I used HashMap for future procedure of my project. Ok if I avoid HashMap part and just want to take name of sibling classes and each number of sibling classes. Then can u give me some idea? I used " String cls = all.getSuperClass().listSubClasses().toSet().toString().;" . this give out put like "[http://localhost/first/B, http://localhost/first/C . How to separate only last part as [B , C ]? – Emalka Feb 28 '16 at 09:52
  • 1
    Java debugging: which line is the NPE on? look at that line and detemine where the NPE is from. Create the line up if necessary. Print `all` to determine which iteration of the loop you can in. – AndyS Feb 28 '16 at 15:49
  • 1
    [localhost/first/B, localhost/first/C] splitting each string based on "/" and accessing the last index of the returned array will yield B or C. Can you post a sample that will allow us to recreate the NPE? Please refer to this link on [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and edit your post. – Debosmit Ray Feb 28 '16 at 22:03
  • @DebosmitRay Thank you very much for reply. I could found NPE. It is because of Superclass became null. – Emalka Feb 29 '16 at 05:59

1 Answers1

1

The javadoc for OntClass.getSuperClass() says:

A super-class of this class or null

A will not have a superclass.

AndyS
  • 16,345
  • 17
  • 21
  • Thank you for your reply. Yes ,it is a reason for Null pointer exception. I used if( all.hasSuperClass()) for avoid it. – Emalka Feb 29 '16 at 03:01