以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 Semantic Web(语义Web)/描述逻辑/本体 』  (http://bbs.xml.org.cn/list.asp?boardid=2)
----  [原创]读《Semantic Web Programming》第五章时遇到的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=2&rootid=&id=86168)


--  作者:dorothyle
--  发布时间:8/11/2010 12:12:00 PM

--  [原创]读《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>
    我觉得这个提示的主要问题是代码中没有给出这个实际的文件流输入,请大家给予指点吧。


--  作者:dorothyle
--  发布时间:8/11/2010 12:24:00 PM

--  
请高手快快帮忙给点指导吧。
我目前还有一些问题想向大家请教,我想用eclipse设计一个网站页面,其中列出待查询的关键字,比如“杨贵妃”,我不知道这样的话我应该看哪方面的书呢?大家能否给些指导,谢谢大家了!
--  作者:laotao
--  发布时间:8/11/2010 1:28:00 PM

--  
运行这个java控制台程序需要提供4个参数(文件名啥的), 在eclipse里可以设置(run as -> run configurations -> Arguments)
--  作者:dorothyle
--  发布时间:8/11/2010 5:19:00 PM

--  
好吧,我再试试,有问题再问您吧
--  作者:roudera
--  发布时间:9/6/2010 11:46:00 AM

--  
大哥,可不可以给我发个这个文档,我找了好久都找不到,~~谢谢
lky.1002@yahoo.com.cn
--  作者:zhoucqucs
--  发布时间:11/18/2010 6:40:00 PM

--  
dorothyle同学,能否麻烦你把《Semantic Web Programming》的电子版发给俺研读一下!
zhoucqucs@163.com
十分感谢!!1
--  作者:jiangxiaolan
--  发布时间:11/4/2011 10:04:00 AM

--  
dorothyle,你好,我是刚刚接触semantic web,麻烦你能告诉我这个到底要怎么运行出结果,为什么我输入文件名之后后台总是告诉我的文件名是不合法的~~~~~~~
--  作者:zzh0792
--  发布时间:11/6/2011 12:07:00 AM

--  
dorothyle同学,能否麻烦你把《Semantic Web Programming》的电子版发给我发一份啊!zzh0792@163.com
十分感谢!!
--  作者:人联网
--  发布时间:12/5/2011 11:31:00 AM

--  
我也买了这本书,
--  作者:zhuzhenyuan
--  发布时间:12/29/2011 11:14:00 AM

--  
你好楼主,《Semantic Web Programming》这本书的电子版能不能给我也发一份啊,万分感谢!zzy.tsf2829@yahoo.com.cn
另,请问这方面有什么比较好的中文版的书看吗?
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
398.438ms