-- 作者:jiangqiangln
-- 发布时间:10/18/2009 6:12:00 PM
-- [求助]大家看下jsp+javabean+owl程序错在哪里
javabean: mport java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; /** * @author Vision * */ public class view { /** * @param args */ public String filePath = "src\\ontology\\test.rdf-xml.owl";// 本体文件 Model model;//定义本体模型 ResultSet results; /** * 构造函数,加载本体模型 */ public view() { model = ModelFactory.createDefaultModel(); loadModel(); } /** * 提交sparql的查询,输出返回结果 * @param queryStr * @return */ public boolean runQuery(String queryStr) { Query query = QueryFactory.create(queryStr); QueryExecution qe = QueryExecutionFactory.create(query, model); ResultSet results = qe.execSelect(); return false; } public ResultSet getRs() { return results; } /** * 加载本地文件形式的本体模型,失败系统退出 * * @return */ private void loadModel() { InputStreamReader in; try { FileInputStream file = new FileInputStream(filePath); in = new InputStreamReader(file, "UTF-8");//处理中文 model.read(in, null); /* * 基于规则的推理代码段,有必要的加 String rule = "[owl:inverseOf: (?pa * owl:inverseOf ?pb) (?a ?pa ?b)->(?b ?pb ?a)]"; Reasoner reasoner = * new GenericRuleReasoner(Rule.parseRules(rule)); model = * ModelFactory.createInfModel(reasoner, model); */ in.close(); } catch (FileNotFoundException e) { System.out.println("无法打开本体文件,程序将终止"); System.exit(0); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } public static void main(String[] args) { view myLearn=new view(); String queryStr="prefix test: <http://vision.pku.edu.cn/test.owl#>"+ "select * "+ "from <http://vision.pku.edu.cn/test.owl>"+ "where "+ "{"+ "?book test:hasName ?hasName."+ "}"; myLearn.runQuery(queryStr ); } } jsp程序: <% String queryStr="prefix test: <http://vision.pku.edu.cn/test.owl#>"+ "select * "+ "from <http://vision.pku.edu.cn/test.owl>"+ "where "+ "{"+ "?book test:hasName ?hasName."+ "}"; bean1.runQuery(queryStr); ResultSet results = bean1.getRs(); while (results.hasNext()) { QuerySolution soln = results.nextSolution(); System.out.print(soln.get("book").toString()); System.out.print("\t"); System.out.print(soln.get("hasName").toString()); System.out.print("\t"); } %>
|