问题
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