以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 Web Services & Semantic Web Services 』  (http://bbs.xml.org.cn/list.asp?boardid=10)
----  我修改了程序,用jena+racer推理总是随机的  (http://bbs.xml.org.cn/dispbbs.asp?boardid=10&rootid=&id=74407)


--  作者:lixiaoming
--  发布时间:4/21/2009 9:58:00 PM

--  我修改了程序,用jena+racer推理总是随机的
我根据jpz6311whu大侠的指导,更改了程序如下:
public static void main( String[] args )  {
    // set up a configuration resource to connect to the reasoner
    // on port 2004 on the local system
   Model cModel = ModelFactory.createDefaultModel();
     Resource conf = cModel.createResource();
     conf.addProperty( ReasonerVocabulary.EXT_REASONER_URL,
                       cModel.createResource( "http://localhost:8080" ) );
  DIGReasonerFactory drf = (DIGReasonerFactory) ReasonerRegistry.theRegistry().getFactory(DIGReasonerFactory.URI);
  DIGReasoner r = (DIGReasoner) drf.create(conf);
  

//    System.out.println("sdfsdf");

    // now make a model
    OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
    spec.setReasoner( r );
    OntModel m = ModelFactory.createOntologyModel( spec, null );
        
  
    m.read("file:///c:/newAnimal.owl");
  
    for (Iterator i = m.listClasses(); i.hasNext();) {//输出他们的父类
        OntClass c = (OntClass) i.next();
        for (Iterator it = c.listSuperClasses(); it.hasNext();)

        {

                  OntClass sp = (OntClass) it.next();

                  String str = c.getModel().getGraph()

                                       .getPrefixMapping().shortForm(c.getURI()) // 获取URI

                                       + "'s superClass is " ;

                  String strSP = sp.getURI();

                  try{
                         str = str + ":" + strSP.substring(strSP.indexOf('#')+1);

                         System.out.println("     Class" +str);

                  }catch( Exception e ){}

        }

    }
得到的结果是随机的,就是说有时候推出来了,有时候就提示错误:
Class:Herbivore's superClass is :Animal
     Class:Leaf's superClass is :Plant
     Class:Lion's superClass is :Carnivore
     Class:Lion's superClass is :Animal
Exception in thread "main" java.lang.NullPointerException
 at com.hp.hpl.jena.shared.impl.PrefixMappingImpl.findMapping(PrefixMappingImpl.java:235)
 at com.hp.hpl.jena.shared.impl.PrefixMappingImpl.shortForm(PrefixMappingImpl.java:214)
 at com.hp.hpl.jena.graph.compose.PolyadicPrefixMappingImpl.shortForm(PolyadicPrefixMappingImpl.java:146)
 at DIGExample.main(DIGExample.java:53)
这是结果,最后总体是错误 java.lang.NullPointerException。不知道为什么,请提示一下


--  作者:jpz6311whu
--  发布时间:4/22/2009 11:28:00 AM

--  
不用shortForm函数,直接把URI打印出来试试
--  作者:lixiaoming
--  发布时间:4/22/2009 4:17:00 PM

--  
非常感谢您的指教,问题解决了,i
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms