以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 Semantic Web(语义Web)/描述逻辑/本体 』  (http://bbs.xml.org.cn/list.asp?boardid=2)
----  急,SPARQL查询出错  (http://bbs.xml.org.cn/dispbbs.asp?boardid=2&rootid=&id=38169)


--  作者:ly2122003
--  发布时间:9/21/2006 5:35:00 PM

--  急,SPARQL查询出错
代码如下:
    Model model;
        Model tempModel;
        InputStreamReader in;
        try {
         FileInputStream file = new FileInputStream("tuxingxue.owl");
         in = new InputStreamReader(file, "UTF-8");//可以读入包括中文的本体模型
         tempModel = ModelFactory.createDefaultModel();
         tempModel.read(in, null);//读入模型
         Reasoner owlReasoner = ReasonerRegistry.getOWLReasoner();
            Reasoner reasoner = owlReasoner.bindSchema(tempModel);  
            model = ModelFactory.createInfModel(reasoner, tempModel);

        } catch (FileNotFoundException e) {
         System.out.print("无法打开本体文件,程序将终止");
         System.exit(0);
        } catch (IOException e) {
         e.printStackTrace();
}

           String queryString =       
            "SELECT ?x ?mingshen " +
               "WHERE " +
               " {  ?x  <http://www.owl-ontologies.com/unnamed.owl#mingshen>?mingshen. "+
               "       }";

           Query q=QueryFactory.create(queryString);
           QueryExecution qe = QueryExecutionFactory.create(q, tempModel);
           com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
           ResultSetFormatter.out(System.out, results, q);

        qe.close();
        
    }
红色处出错,提示The local variable tempModel may not have been initialized

还有
"SELECT ?x ?mingshen " +
               "WHERE " +
               " {  ?x  <http://www.owl-ontologies.com/unnamed.owl#mingshen>?mingshen. "+
               "       }";
中,mingshen如果换为中文"名称",该怎么写.

请各位高手一定要帮忙啊,多谢大家了.


--  作者:goodhero
--  发布时间:9/21/2006 9:36:00 PM

--  
tmpModel的实例化在try{}中,如果发生错误则tmpModel可能会没有被初始化,所以给出该提示,请把查询程序也放入try{}中,试一试。
--  作者:ly2122003
--  发布时间:9/22/2006 8:55:00 AM

--  
谢谢goodhero,已经能查询了,但结果是乱码
---------------------------------------------------------------------------------------------------------------------
| x                                                     | mingshen                                                  |
=====================================================================================================================
| <http://www.owl-ontologies.com/unnamed.owl#huaxiansf> | "mingshen鐢荤嚎绠楁硶"^^<http://www.w3.org/2001/XMLSchema#string> |
---------------------------------------------------------------------------------------------------------------------

还有mingshen如果换为中文"名称",该怎么写.
谢谢


--  作者:goodhero
--  发布时间:9/22/2006 9:04:00 AM

--  
能把owl文件发给我吗,我试一试
--  作者:goodhero
--  发布时间:9/22/2006 9:04:00 AM

--  
能把owl文件发给我吗,我试一试,sedbseminar@163.com
--  作者:ly2122003
--  发布时间:9/22/2006 9:37:00 AM

--  
已发送,请查收,谢谢!
--  作者:goodhero
--  发布时间:9/22/2006 11:14:00 AM

--  
把ResultSetFormatter.out(System.out, results, q);注释掉
换为:
while(results.hasNext()){
  RDFNode rd=results.nextSolution().get("mingshen");
System.out.println(rd.toString());
}
可以正常显示,我觉得主要是ResultSetFormatter输出的字符编码问题
第2个问题用该方法也能正常显示
--  作者:icebuddy
--  发布时间:9/22/2006 4:56:00 PM

--  
QueryFactory 这个方法我这里怎么没有啊,我用的是jena2.4的
--  作者:goodhero
--  发布时间:9/23/2006 12:19:00 AM

--  
com.hp.hpl.jena.query.QueryFactory,如果遇到找不到的类,就查一下jena的api文档吧,上面可以找到类所在的包
--  作者:jiexincao
--  发布时间:9/24/2006 5:26:00 PM

--  
关于乱码的问题可以搜索以前的帖子

--  作者:ly2122003
--  发布时间:9/24/2006 9:41:00 PM

--  
非常感谢各位,我再试试看。
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
6,558.594ms