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

    >> Web服务(Web Services,WS), 语义Web服务(Semantic Web Services, SWS)讨论区: WSDL, SOAP, UDDI, DAML-S, OWL-S, SWSF, SWSL, WSMO, WSML,BPEL, BPEL4WS, WSFL, WS-*,REST, PSL, Pi-calculus(Pi演算), Petri-net,WSRF,
    [返回] 中文XML论坛 - 专业的XML技术讨论区W3CHINA.ORG讨论区 - Web新技术讨论『 Web Services & Semantic Web Services 』 → rule试验遇到问题 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7271 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: rule试验遇到问题 举报  打印  推荐  IE收藏夹 
       本主题类别: Ontology Engineering | RDF/RDFS    
     lixiaoming 帅哥哟,离线,有人找我吗?
      
      
      等级:大二期末(数据结构考了98分!)
      文章:78
      积分:409
      门派:XML.ORG.CN
      注册:2008/12/8

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给lixiaoming发送一个短消息 把lixiaoming加入好友 查看lixiaoming的个人资料 搜索lixiaoming在『 Web Services & Semantic Web Services 』的所有贴子 引用回复这个贴子 回复这个贴子 查看lixiaoming的博客楼主
    发贴心情 rule试验遇到问题

    [size=3]这是个利用rule进行的查询推理。(前提已经把本体存入到数据库中)
    public class tuilichaxun {

     public static void searchOnto1(OntModel model){
         /*设置规则*/
         String rule = "[rule1:(?x http://www.owl-ontologies.com/Expert.owl#research ?y) " +
             "(?y http://www.owl-ontologies.com/Expert.owl#associate ?z) " +
             "->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)]";
       
         /*查询语句*/
         String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
         "SELECT ?expert ?subject " +
         "WHERE {?expert Expert:familiar_with ?subject} ";
       
         /*创建推理机*/
            
        //使用默认jena推理机
        Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule));
       
         InfModel inf = ModelFactory.createInfModel(reasoner2, model);
        Query query = QueryFactory.create(queryString);
         
         QueryExecution qe = QueryExecutionFactory.create(query, inf);
             
         ResultSet results = qe.execSelect();
       
         /*打印结果*/
         ResultSetFormatter.out(System.out, results, query);
        
         qe.close();
     }
     /**
      * @param args
      */
     public static void main(String[] args) {
       String DB_URL = "jdbc:mysql://localhost/expert1";
          String DB_USER = "root";
          String DB_PASSWD = "root";
          String DB = "MySQL";
          String DB_DRIVER = "com.mysql.jdbc.Driver";
      
      try {
             Class.forName("com.mysql.jdbc.Driver");
         } catch (ClassNotFoundException e) {
              e.printStackTrace();
     }
     
          
          IDBConnection con = s.connectDB(DB_URL,DB_USER, DB_PASSWD, DB);
          System.out.println(con);
        
     OntModel model = s.getModelFromDB(con, "expert");


          tuilichaxun.searchOnto1(model);
      }


     }
    运行结果:
    -----------------------------------------------------------
    | expert            | subject                             |
    ===================================
    | Expert:ChenJianer | Expert:Computer_Software_and_Theory |
    -----------------------------------------------------------
    在这里我制订了一条rule"[rule1:(?x http://www.owl-ontologies.com/Expert.owl#research ?y) " +
             "(?y http://www.owl-ontologies.com/Expert.owl#associate ?z) " +
             "->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)]";

    x p1 y ,y p2 z, --->x p3 z设置好这条规则之后,本体中建立了一个实例:

    <Expert rdf:ID="ChenJianer">
        <country_is>
            <Country rdf:ID="America"/>
        </country_is>
        <graduate_from>
            <College rdf:ID="Central_South_University"/>
        </graduate_from>
        <gender rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</gender>
        <people_is>
            <People rdf:ID="Chinese"/>
        </people_is>
        <research>
            <Profession rdf:ID="Bioinformatics">
            
            <be_researched rdf:resource="#ChenJianer"/>
            <associat>
               <Computer_Science_and_Technology rdf:ID="Computer_Software_and_Theory">
                  
               <be_associated rdf:resource="#Bioinformatics"/>
                  <be_associated>
       <Profession rdf:ID="Computer_Optimization_Algorithm">
                  <associat rdf:resource="#Computer_Software_and_Theory"/>
                  <be_researched rdf:resource="#ChenJianer"/>
                      </Profession>
                  </be_associated>
                  <be_familiar_with rdf:resource="#ChenJianer"/>
                </Computer_Science_and_Technology>
             </associat>
           
             </Profession>
          </research>
    <research rdf:resource="#Computer_Optimization_Algorithm"/>
    <E_mail xml:lang="en">shalen2008@sina.com</E_mail>
    <Technical_level xml:lang="en">1</Technical_level>
    <Graduation_time rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-03-04</Graduation_time>
    <familiar_with rdf:resource="#Computer_Software_and_Theor"/>这里已经有了familar_with了,经过sparql查询之后就输出它的值: Computer_Software_and_Theor
      
    <workplace xml:lang="en">Washington University</workplace>
    <mother_language_is rdf:resource="#english"/>
    <name xml:lang="en">ChenJianer</name>
    <work_tel xml:lang="en">882555121</work_tel>
    <home_address xml:lang="en">NewYork001</home_address>
    <ID_card xml:lang="en">4216633322656</ID_card>
    <is_Academician rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</is_Academician>
    <birthday rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-03-23</birthday>
    </Expert>

    在上面标红的位置,如果不使用rule,
    用sparql:PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
         "SELECT ?expert ?subject " +
         "WHERE {?expert Expert:familiar_with ?subject} ";
    一样可以得到结果
    现在很明显:expert:ChenJianer research Bioinformatics,
    Bioinformatics associate Computer_Software_and_Theory(注意代码中标红的地方)
    那么根据规则:"[rule1:(?x http://www.owl-ontologies.com/Expert.owl#research ?y) " +
             "(?y http://www.owl-ontologies.com/Expert.owl#associate ?z) " +
             "->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)]";

    expert:ChenJianer  familiar_with  Computer_Software_and_Theory
    但是为什么我把这条规则去掉以后,不使用rule,和jena的推理引擎,直接使用sparql查询本体,可以得到一样的结果,我自己认为是
    <familiar_with rdf:resource="#Computer_Software_and_Theory"/>(在代码中红的地方)这条代码的问题,请问怎么让这条rule,对输出的结果产生影响啊,就是说使得带有rule,和不带有rule得到的结果不同。请指教


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/4/24 20:14:00
     
     Humphrey 帅哥哟,离线,有人找我吗?狮子座1981-7-23
      
      
      威望:1
      等级:研二(搞定了DL,再搞定F-Logic!)
      文章:937
      积分:5743
      门派:W3CHINA.ORG
      注册:2008/3/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Humphrey发送一个短消息 把Humphrey加入好友 查看Humphrey的个人资料 搜索Humphrey在『 Web Services & Semantic Web Services 』的所有贴子 引用回复这个贴子 回复这个贴子 查看Humphrey的博客2
    发贴心情 
    事实上,查询也可以看作一种简单的推理。在逻辑关系只有一重的情况下,查询操作完全可以获得和推理相同的效果;即使是二重获更高层次的逻辑关系也可以通过制定缜密的查询策略并通过查询获得,但是这样的查询要远远比推理复杂,可能几句简单的推理程序能够得到的结果就需要几十条甚至上百条查询语句才能查得结果(也有例外的情况,可能查询操作根本无法完成,只能应用推理解决)。虽然这样的说法并不严谨,但是也能够在一定程度上说明二者之间的关系。因此您不必为它们能够获得相同的结果感到吃惊,这完全有可能。

    ----------------------------------------------
    鸿丰

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

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jpz6311whu发送一个短消息 把jpz6311whu加入好友 查看jpz6311whu的个人资料 搜索jpz6311whu在『 Web Services & Semantic Web Services 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jpz6311whu的博客3
    发贴心情 
    楼主不用数据库存储方式试试
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/4/25 12:06:00
     
     Humphrey 帅哥哟,离线,有人找我吗?狮子座1981-7-23
      
      
      威望:1
      等级:研二(搞定了DL,再搞定F-Logic!)
      文章:937
      积分:5743
      门派:W3CHINA.ORG
      注册:2008/3/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Humphrey发送一个短消息 把Humphrey加入好友 查看Humphrey的个人资料 搜索Humphrey在『 Web Services & Semantic Web Services 』的所有贴子 引用回复这个贴子 回复这个贴子 查看Humphrey的博客4
    发贴心情 
    楼主不必惊慌,只要您取得的结果是正确的,就有理由相信它。
    甚至您可能把您这项工作完全采用关系数据库来实现,用结构化查询语言来检索,其实也是可以的。在本体库或数据库简单的前提下,这些方法都是可以通用的。除非您能够找到一种含有足够复杂的描述逻辑的领域并对这个领域建立本体和实现推理,才有可能看出推理和查询的差距。我想版主同志也是这个意思。

    ----------------------------------------------
    鸿丰

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/4/25 14:25:00
     
     lixiaoming 帅哥哟,离线,有人找我吗?
      
      
      等级:大二期末(数据结构考了98分!)
      文章:78
      积分:409
      门派:XML.ORG.CN
      注册:2008/12/8

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给lixiaoming发送一个短消息 把lixiaoming加入好友 查看lixiaoming的个人资料 搜索lixiaoming在『 Web Services & Semantic Web Services 』的所有贴子 引用回复这个贴子 回复这个贴子 查看lixiaoming的博客5
    发贴心情 
    感谢您们的指教,我现在特别想看知道,没有rule之后,这个查询会不会影响,如果如Humphrey您所说,那么在做实例的时候,如果考虑周全的话,就完全不用添加推理了,比如说我上面做的实例,expert:ChenJianer research Bioinformatics,
    Bioinformatics associate Computer_Software_and_Theory
    那么expert:ChenJianer  familiar_with  Computer_Software_and_Theory
    这是推理出来的结果,现在的问题是,假如我在实例中没有
    <familiar_with rdf:resource="#Computer_Software_and_Theor"/>这句的话,通过sparql,是直接查不到:expert:ChenJianer  familiar_with  Computer_Software_and_Theory
    我也按照jpz6311whu版主的意见,直接读取本体文件。结果是使用rule和不使用rule,查询还是得到同一个结果,因为这是实例中有这条语句:
    <familiar_with rdf:resource="#Computer_Software_and_Theor"/>(就是上面红色加深的部分)
    我把这条语句去掉之后,则使用rule和不使用rule都输不出结果了,所以这个rule还是没有起到作用,希望再指教指教,这里是我修改后的代码

    public class expertrule {

     public static void searchOnto1( OntModel ontModel){
         /*设置规则*/
      String rule = "[rule1:(?x http://www.owl-ontologies.com/Expert.owl#research ?y) " +
            "(?y http://www.owl-ontologies.com/Expert.owl#associate ?z) " +
            "->(?x http://www.owl-ontologies.com/Expert.owl#familiar_with ?z)]";
      
       String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
          "SELECT ?expert ?subject " +
          "WHERE {?expert Expert:familiar_with ?subject} ";
      
      
        Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule));
        //使用规则
          InfModel inf = ModelFactory.createInfModel(reasoner2,ontModel);
          Query query = QueryFactory.create(queryString);
          
          QueryExecution qe = QueryExecutionFactory.create(query, inf);
          ResultSet results = qe.execSelect();
        
          /*打印结果*/
          ResultSetFormatter.out(System.out, results, query);
         
          qe.close();
     }
     /**
      * @param args
      */
     public static void main(String[] args) {
      
    OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
        ontModel.read("file:///d:/Expert.owl");
      
          expertrule.searchOnto1(ontModel);
      }


     }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/4/26 9:56:00
     
     Humphrey 帅哥哟,离线,有人找我吗?狮子座1981-7-23
      
      
      威望:1
      等级:研二(搞定了DL,再搞定F-Logic!)
      文章:937
      积分:5743
      门派:W3CHINA.ORG
      注册:2008/3/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Humphrey发送一个短消息 把Humphrey加入好友 查看Humphrey的个人资料 搜索Humphrey在『 Web Services & Semantic Web Services 』的所有贴子 引用回复这个贴子 回复这个贴子 查看Humphrey的博客6
    发贴心情 
    该语句对本体指向的语句完整性有影响吧?如果没有推理或查询结果应该和没有指定有效的对象有关。

    ----------------------------------------------
    鸿丰

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/4/27 11:26:00
     
     GoogleAdSense狮子座1981-7-23
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Web Services & Semantic Web Services 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/11 20:35:34

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

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