java本体,使用Java获取生成的本体中同级类的名称和数量

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 countSiblingClasses(String ontoClass) {

ontologyCreation();

this. m.read("http://localhost/new/onto1.owl");

ExtendedIterator classes = ((OntModel) m).listClasses();

ArrayList siblingsName = new ArrayList();

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)

解决方案

The javadoc for OntClass.getSuperClass() says:

A super-class of this class or null

A will not have a superclass.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值