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