新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   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)/描述逻辑/本体 』 → [原创]读《Semantic Web Programming》第五章时遇到的问题 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 27417 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: [原创]读《Semantic Web Programming》第五章时遇到的问题 举报  打印  推荐  IE收藏夹 
       本主题类别: Ontology Language | RDF/RDFS | Description Logics    
     dorothyle 美女呀,离线,快来找我吧!
      
      
      等级:大二(研究汇编)
      文章:44
      积分:285
      门派:XML.ORG.CN
      注册:2009/9/15

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给dorothyle发送一个短消息 把dorothyle加入好友 查看dorothyle的个人资料 搜索dorothyle在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看dorothyle的博客楼主
    发贴心情 [原创]读《Semantic Web Programming》第五章时遇到的问题

    《Semantic Web Programming》第五章是Modeling Knowledge in the Real World,文章中指出OWL仅仅是一种本体语言,它不是一个应用程序,而是一个工具用来详细解释资源的语义,定义知识模型的一个工具。
        第五章中有一段代码,是针对一个本体实现不同级别的推理,代码如下:
    package net.semwebprogramming.chapter5.PelletReasoning;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Iterator;

    import org.mindswap.pellet.jena.PelletReasonerFactory;

    import com.hp.hpl.jena.ontology.Individual;
    import com.hp.hpl.jena.ontology.OntModel;
    import com.hp.hpl.jena.ontology.OntModelSpec;
    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.model.Statement;
    import com.hp.hpl.jena.rdf.model.StmtIterator;
    import com.hp.hpl.jena.reasoner.Reasoner;
    import com.hp.hpl.jena.reasoner.ValidityReport;
    import com.hp.hpl.jena.util.iterator.ExtendedIterator;

    public class InferenceExample
    {
       /**
        * This program takes 4 parameters an input file name
        * an output file name an input file format a reasoning
        * level {RDFS, OWL-DL}
        */
       public static void main(String[] args)
       {
        //validate the program arguments
          if(args.length != 4)
          {   
             System.err.println("Usage: java InferenceExample "
                + "<input file> <input format> <output file> "
                + "<none|rdfs|owl>");
             return;
          }

          String inputFileName = args[0];
          String inputFileFormat = args[1];
          String outputFileName = args[2];
          String reasoningLevel = args[3];

          //create an input stream for the input file
          FileInputStream inputStream = null;
          PrintWriter writer = null;
          try
          {
             inputStream = new FileInputStream(inputFileName);
          } catch (FileNotFoundException e) {
             System.err.println("'" + inputFileName
                + "' is an invalid input file.");
             return;
          }
          
          //create an output print writer for the results
          try
          {
             writer = new PrintWriter(outputFileName);
          } catch (FileNotFoundException e) {
             System.err.println("'" + outputFileName
                + "' is an invalid output file.");
             return;
          }
          
          //create the appropriate jena model
          OntModel model = null;
          if("none".equals(reasoningLevel.toLowerCase()))
          {
             /*
              * "none" is jena model with OWL_DL
              * ontologies loaded and no inference enabled
              */
             model = ModelFactory.createOntologyModel(
                OntModelSpec.OWL_DL_MEM);
          }
          else if("rdfs".equals(reasoningLevel.toLowerCase()))
          {
             /*
              * "rdfs" is jena model with OWL_DL
              * ontologies loaded and RDFS inference enabled
              */
             model = ModelFactory.createOntologyModel(
                OntModelSpec.OWL_DL_MEM_RDFS_INF);
          }
          else if("owl".equals(reasoningLevel.toLowerCase()))
          {
             /*
              * "owl" is jena model with OWL_DL ontologies
              * wrapped around a pellet-based inference model
              */
             Reasoner reasoner =
                PelletReasonerFactory.theInstance().create();
             Model infModel = ModelFactory.createInfModel(
                reasoner, ModelFactory.createDefaultModel());
             model = ModelFactory.createOntologyModel(
                OntModelSpec.OWL_DL_MEM, infModel);
          }
          else
          {
             //invalid inference setting
             System.err.println("Invalid inference setting, "
                + "choose one of <none|rdfs|owl>.");
             return;
          }
          
          //load the facts into the model
          model.read(inputStream, null, inputFileFormat);
          
          //validate the file
          ValidityReport validityReport = model.validate();
          if(validityReport != null && !validityReport.isValid())
          {
             Iterator i = validityReport.getReports();
             while(i.hasNext())
             {
                System.err.println(
                   ((ValidityReport.Report)i.next()).getDescription());
             }
             return;
          }
          
          //Iterate over the individuals, print statements about them
          ExtendedIterator iIndividuals = model.listIndividuals();
          while(iIndividuals.hasNext())
          {
             Individual i = (Individual)iIndividuals.next();
             printIndividual(i, writer);
          }
          iIndividuals.close();
          
          writer.close();
          model.close();
       }
       
       /**
        * Print information about the individual
        * @param i The individual to output
        * @param writer The writer to which to output
        */
       public static void printIndividual(
          Individual i, PrintWriter writer)
       {
          //print the local name of the individual (to keep it terse)
          writer.println("Individual: " + i.getLocalName());
          
          //print the statements about this individual
          StmtIterator iProperties = i.listProperties();
          while(iProperties.hasNext())
          {
             Statement s = (Statement)iProperties.next();
             writer.println("  " + s.getPredicate().getLocalName()
                + " : " + s.getObject().toString());
          }
          iProperties.close();
          writer.println();
       }
    }

        以上代码用到的本体如下:

    此主题相关图片如下:
    按此在新窗口浏览图片

        针对该本体进行三个不同级别的推理分别得到如下的结果:
       第一个结果:Performing No Inference

    此主题相关图片如下:
    按此在新窗口浏览图片

        第二个推理结果:Performing RDFS Inference

    此主题相关图片如下:
    按此在新窗口浏览图片

        第三个推理结果:Performing OWL Inference

    此主题相关图片如下:
    按此在新窗口浏览图片

        看完以上这段内容以后,我明白了OWL定义了不同的推理机制,每一种机制采用的推理方法都是不同的,每种方法都有自己的语义规定,比如OWL  EL,QL,RL等,不同的语言得到的推理结果也是不同的。
        我现在的问题就是,书中的网站没有给出这段本体的代码,关于本体我自己可以用Protege实现,但是现在的问题是我不知道怎样把关于本体的代码代入到以上实现推理的这段代码当中,我想在eclipse当中实现以上三个推理的结果,可是我弄不明白inputStream, inputFileFormat这些地方都相应地应该写什么?我这是遇到实际编程的问题了,所以向大家请教。我在eclipse里运行这段代码,得到的揭示是如下的红字:
    Usage: java InferenceExample <input file> <input format> <output file> <none|rdfs|owl>
        我觉得这个提示的主要问题是代码中没有给出这个实际的文件流输入,请大家给予指点吧。


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/8/11 12:12:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/10 8:31:55

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

     *树形目录 (最近20个回帖) 顶端 
    主题:  [原创]读《Semantic Web Programming》第五章时遇到的问..(6588字) - dorothyle,2010年8月11日
        回复:  你好楼主,《Semantic Web Programming》这本书的电子版能不能给我也发一份啊,..(154字) - zhuzhenyuan,2011年12月29日
        回复:  我也买了这本书,(16字) - 人联网,2011年12月5日
        回复:  dorothyle同学,能否麻烦你把《Semantic Web Programming》的电子版发..(108字) - zzh0792,2011年11月6日
        回复:  dorothyle,你好,我是刚刚接触semantic web,麻烦你能告诉我这个到底要怎么运行出..(144字) - jiangxiaolan,2011年11月4日
        回复:  dorothyle同学,能否麻烦你把《Semantic Web Programming》的电子版发..(113字) - zhoucqucs,2010年11月18日
        回复:  大哥,可不可以给我发个这个文档,我找了好久都找不到,~~谢谢lky.1002@yahoo.com..(81字) - roudera,2010年9月6日
        回复:  好吧,我再试试,有问题再问您吧(30字) - dorothyle,2010年8月11日
        回复:  运行这个java控制台程序需要提供4个参数(文件名啥的), 在eclipse里可以设置(run a..(113字) - laotao,2010年8月11日
        回复:  请高手快快帮忙给点指导吧。我目前还有一些问题想向大家请教,我想用eclipse设计一个网站页面,..(199字) - dorothyle,2010年8月11日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    2,232.422ms