新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Semantic Web(语义Web,语义网或语义万维网, Web 3.0)及相关理论,如:Ontology(本体,本体论), OWL(Web Ontology Langauge,Web本体语言), Description Logic(DL, 描述逻辑),RDFa,Ontology Engineering等。
    [返回] 计算机科学论坛W3CHINA.ORG讨论区 - Web新技术讨论『 Semantic Web(语义Web)/描述逻辑/本体 』 → [求助]用sparql查询数据库中的本体,不知道错在哪里 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 16749 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [求助]用sparql查询数据库中的本体,不知道错在哪里 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     aximou 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:10
      积分:122
      门派:W3CHINA.ORG
      注册:2006/9/24

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给aximou发送一个短消息 把aximou加入好友 查看aximou的个人资料 搜索aximou在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看aximou的博客楼主
    发贴心情 [求助]用sparql查询数据库中的本体,不知道错在哪里

    以下是存储在数据库中的本体:
    <?xml version="1.0"?>
    <rdf:RDF
        xmlns="http://www.owl-ontologies.com/Ontology1215496170.owl#"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
      xml:base="http://www.owl-ontologies.com/Ontology1215496170.owl">
      <owl:Ontology rdf:about=""/>
      <owl:Class rdf:ID="Company"/>
      <owl:Class rdf:ID="Trade"/>
      <owl:Class rdf:ID="People"/>
      <owl:ObjectProperty rdf:ID="belongTo">
        <rdfs:range rdf:resource="#Trade"/>
        <rdfs:domain>
          <owl:Class>
            <owl:unionOf rdf:parseType="Collection">
              <owl:Class rdf:about="#Company"/>
              <owl:Class rdf:about="#People"/>
            </owl:unionOf>
          </owl:Class>
        </rdfs:domain>
      </owl:ObjectProperty>
      <owl:ObjectProperty rdf:ID="workIn">
        <rdfs:range rdf:resource="#Company"/>
        <rdfs:domain rdf:resource="#People"/>
      </owl:ObjectProperty>
      <Company rdf:ID="CCB">
        <belongTo>
          <Trade rdf:ID="finace"/>
        </belongTo>
      </Company>
      <People rdf:ID="Tom">
        <workIn rdf:resource="#CCB"/>
      </People>
    </rdf:RDF>


    以下是程序文件:
    import java.util.*;

    import com.hp.hpl.jena.reasoner.*;

    import com.hp.hpl.jena.query.*;

    import com.hp.hpl.jena.reasoner.rulesys.*;

    import com.hp.hpl.jena.ontology.*;

    import com.hp.hpl.jena.db.*;

    import com.hp.hpl.jena.rdf.model.*;

    import com.hp.hpl.jena.util.*;

    public class test{
     
     public static void main(String args[]){
       String strURL="jdbc:mysql://localhost/test";
      String strUser="root";
      String strPW="";
      String strDB="MySQL";
      String driver="com.mysql.jdbc.Driver";
      try{
      Class.forName(driver);
      }catch(ClassNotFoundException e){};
       
      // 创建一个数据库连接
      IDBConnection conn = new DBConnection (strURL, strUser, strPW, strDB );
      // 使用数据库连接参数创建一个模型制造器
      ModelMaker maker = ModelFactory.createModelRDBMaker(conn);             
         // 创建一个默认模型,命名为test
         Model base = maker.createModel("test");
      OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
      spec.setImportModelMaker(maker);
      OntModel newmodel = ModelFactory.createOntologyModel(spec,base);

      String rules="[rule:(?x http://www.test.com/medical#workIn ?y)(?y http://www.test.com/medical#belongTo ?z)->(?x http://www.test.com/medical#belongTo ?z)]";
      Reasoner reasoner=new GenericRuleReasoner(Rule.parseRules(rules));
      InfModel inf = ModelFactory.createInfModel(reasoner, newmodel);
    //查询Tom属于哪个行业
      String queryString="PREFIX med:<http://www.test.com/medical#> "+
             "select ?person ?trade "+
             "where {?person med:belongTo ?trade}";
      
      Query query = QueryFactory.create(queryString);
       
         QueryExecution qe = QueryExecutionFactory.create(query, inf);
         ResultSet results = qe.execSelect();
         /*打印结果*/
         ResultSetFormatter.out(System.out, results, query);
         qe.close();
      
     }
    }

    查询的结果显示为:
    ------------------
    | person | trade |
    ==================
    ----------

    不知道是sparql语句写的不对,所以出来的结果为空?还是程序中创建模型时创建的时空模型?不过后一种可能我想不会,因为在创建这个模型后,我可以将它的类和属性都读出来……不过不大确信
    跪求高人解答!


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/11 16:06:00
     
     jpz6311whu 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      等级:研三(收到微软亚洲研究院的Offer了)(版主)
      文章:1718
      积分:10610
      门派:W3CHINA.ORG
      注册:2005/4/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jpz6311whu发送一个短消息 把jpz6311whu加入好友 查看jpz6311whu的个人资料 搜索jpz6311whu在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jpz6311whu的博客2
    发贴心情 
    Jena中的SPARQL不能检索推理之后的本体库中推理出来的内容,只能检索推理之前的原始本体库
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/15 12:46:00
     
     aximou 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:10
      积分:122
      门派:W3CHINA.ORG
      注册:2006/9/24

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给aximou发送一个短消息 把aximou加入好友 查看aximou的个人资料 搜索aximou在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看aximou的博客3
    发贴心情 
    谢谢jpz6311whu!!!
    不过还是不明白,为什么只能检索推理之前的原始本体?如果是这样,那么需要查询推理之后的本体,该如何解决呢?
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/18 17:11:00
     
     jpz6311whu 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      等级:研三(收到微软亚洲研究院的Offer了)(版主)
      文章:1718
      积分:10610
      门派:W3CHINA.ORG
      注册:2005/4/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jpz6311whu发送一个短消息 把jpz6311whu加入好友 查看jpz6311whu的个人资料 搜索jpz6311whu在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jpz6311whu的博客4
    发贴心情 
    这个是因为Jena推理和存储机制的特点,它将原始本体和推理的新本体分开了,后者放在另外的posit里面,所以无法用SPARQL查询到

    Extended list statements
    The default API supports accessing all entailed information at the level of individual triples. This is surprisingly flexible but there are queries which cannot be easily supported this way. The first such is when the query needs to make reference to an expression which is not already present in the data. For example, in description logic systems it is often possible to ask if there are any instances of some class expression. Whereas using the triple-based approach we can only ask if there are any instances of some class already defined (though it could be defined by a bNode rather than be explicitly named).

    To overcome this limitation the InfModel API supports a notion of "posit", that is a set of assertions which can be used to temporarily declare new information such as the definition of some class expression. These temporary assertions can then be referenced by the other arguments to the listStatements command. With the current reasoners this is an expensive operation, involving the temporary creation of an entire new model with the additional posits added and all inference has to start again from scratch. Thus it is worth considering preloading your data with expressions you might need to query over. However, for some external reasoners, especially description logic reasoners, we anticipate restricted uses of this form of listStatement will be important.

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/19 18:50:00
     
     aximou 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:10
      积分:122
      门派:W3CHINA.ORG
      注册:2006/9/24

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给aximou发送一个短消息 把aximou加入好友 查看aximou的个人资料 搜索aximou在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看aximou的博客5
    发贴心情 
    十分感谢!
    看来这种查询只能使用InfModel类中的listStatements()方法了?
    仔细看了远觉得没有使用sparql查询来得方便,比较沮丧的说……
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/22 15:32:00
     
     jxlhljh 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究C++)
      文章:27
      积分:218
      门派:XML.ORG.CN
      注册:2008/5/23

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jxlhljh发送一个短消息 把jxlhljh加入好友 查看jxlhljh的个人资料 搜索jxlhljh在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jxlhljh的博客6
    发贴心情 
    可以推理查询的,我拿你了做了下实验。存数据库麻烦我就没做数据库的实验你可以做一下看,下面是我的程序,我把 你的owl文件存名为1.owl,放在D盘根目录下,

    1.owl文件如下:
    <?xml version="1.0"?>
    <rdf:RDF
        xmlns="http://www.owl-ontologies.com/Ontology1215496170.owl#"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
      xml:base="http://www.owl-ontologies.com/Ontology1215496170.owl">
      <owl:Ontology rdf:about=""/>
      <owl:Class rdf:ID="Company"/>
      <owl:Class rdf:ID="Trade"/>
      <owl:Class rdf:ID="People"/>
      <owl:ObjectProperty rdf:ID="belongTo">
        <rdfs:range rdf:resource="#Trade"/>
        <rdfs:domain>
          <owl:Class>
            <owl:unionOf rdf:parseType="Collection">
              <owl:Class rdf:about="#Company"/>
              <owl:Class rdf:about="#People"/>
            </owl:unionOf>
          </owl:Class>
        </rdfs:domain>
      </owl:ObjectProperty>
      <owl:ObjectProperty rdf:ID="workIn">
        <rdfs:range rdf:resource="#Company"/>
        <rdfs:domain rdf:resource="#People"/>
      </owl:ObjectProperty>
      <Company rdf:ID="CCB">
        <belongTo>
          <Trade rdf:ID="finace"/>
        </belongTo>
      </Company>
      <People rdf:ID="Tom">
        <workIn rdf:resource="#CCB"/>
      </People>
    </rdf:RDF>

    我在java中的代码如下:

    package test;

    import java.io.File;
    import java.io.FileInputStream;

    import com.hp.hpl.jena.reasoner.*;

    import com.hp.hpl.jena.query.*;

    import com.hp.hpl.jena.reasoner.rulesys.*;

    import com.hp.hpl.jena.ontology.*;

    import com.hp.hpl.jena.rdf.model.*;

    public class Test{

    public static void main(String args[]){
      
      try
      {
      OntModel m = ModelFactory.createOntologyModel();
     File file = new File("d:\\1.owl");
     FileInputStream in = new FileInputStream(file);
     m.read(in,null);
     
     String rules="[rule:(?y http://www.owl-ontologies.com/Ontology1215496170.owl#belongTo ?z)"+
       "(?x http://www.owl-ontologies.com/Ontology1215496170.owl#workIn ?y)->"+
       "(?x http://www.owl-ontologies.com/Ontology1215496170.owl#belongTo ?z)]";
     Reasoner reasoner=new GenericRuleReasoner(Rule.parseRules(rules));
     InfModel inf = ModelFactory.createInfModel(reasoner, m);
     
     String queryString="PREFIX med:<http://www.owl-ontologies.com/Ontology1215496170.owl#> "+
        "select ?person ?trade "+
        "where {?person med:belongTo ?trade}";
     
     
     
     Query query = QueryFactory.create(queryString);
     QueryExecution qe = QueryExecutionFactory.create(query, inf);
        ResultSet results = qe.execSelect();
        ResultSetFormatter.out(System.out, results, query);
     
      }catch(Exception e){e.printStackTrace();}
    }
    }

    实验结果为:
    ------------------------
    | person  | trade      |
    ========================
    | med:Tom | med:finace |
    | med:CCB | med:finace |
    ------------------------

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/25 11:30:00
     
     jxlhljh 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究C++)
      文章:27
      积分:218
      门派:XML.ORG.CN
      注册:2008/5/23

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jxlhljh发送一个短消息 把jxlhljh加入好友 查看jxlhljh的个人资料 搜索jxlhljh在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jxlhljh的博客7
    发贴心情 
    你的错误在于,你写规则文件写错了。规则文件里面的http://www.test.com/medical#
    应该改为xmlns里面的http://www.owl-ontologies.com/Ontology1215496170.owl#
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/25 11:32:00
     
     aximou 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:10
      积分:122
      门派:W3CHINA.ORG
      注册:2006/9/24

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给aximou发送一个短消息 把aximou加入好友 查看aximou的个人资料 搜索aximou在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看aximou的博客8
    发贴心情 
    确实是规则文件中的名字空间写错了.修改之后通过数据库查询也通过!谢谢jxlhljh!!也谢谢jpz6311whu!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/7/25 16:01:00
     
     zibo 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:18
      积分:155
      门派:XML.ORG.CN
      注册:2007/10/13

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zibo发送一个短消息 把zibo加入好友 查看zibo的个人资料 搜索zibo在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zibo的博客9
    发贴心情 
    我按照上面修改规则得不到结果,不知道为什么?但是不从数据库中获取可以。
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/8/4 13:06:00
     
     hrballen 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(高数修炼中)
      文章:38
      积分:198
      门派:XML.ORG.CN
      注册:2008/7/13

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给hrballen发送一个短消息 把hrballen加入好友 查看hrballen的个人资料 搜索hrballen在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看hrballen的博客10
    发贴心情 
    你的结果不太对吧,你输出的是belongTo对象属性的定义域和值域所对应的实例,而并非真正满足你所定义的规则吧,因为多出了ccb finance这个结果,这是我的看法,请指教
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/8/5 10:36:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/9/15 21:53:51

    本主题贴数14,分页: [1] [2]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    132.813ms