-- 作者:ahchenxin
-- 发布时间:3/15/2012 6:54:00 PM
-- 使用SPARQL查询查不出结果,请教!
刚刚开始学习jena推理,最近在学习有关SPARQL有关的知识,编了一个很小的程序但是遇到了一点问题,在aa.rdf-xml.owl中只定义了两个类,一个是company还有它的子类aa,现在已知aa,想查询到它是谁的子类,但总是查不出结果,很奇怪。以下是源程序: import 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.ResultSet; import com.hp.hpl.jena.query.ResultSetFormatter; import com.hp.hpl.jena.rdf.model.InfModel; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.reasoner.Reasoner; import com.hp.hpl.jena.reasoner.ReasonerRegistry; public class Test6 { public String filePath = "E://owlfile//aa.rdf-xml.owl";// 本体文件 Model model;//定义本体模型 //构造函数,加载本体模型 public Test6() { model = ModelFactory.createDefaultModel(); loadModel(); } /** * 提交sparql的查询,输出返回结果 * @param queryStr */ public boolean runQuery(String queryStr) { Query query = QueryFactory.create(queryStr); Reasoner reasoner = ReasonerRegistry.getOWLReasoner(); InfModel inf = ModelFactory.createInfModel(reasoner, model); QueryExecution qe = QueryExecutionFactory.create(query, model); ResultSet results = qe.execSelect(); ResultSetFormatter.out(System.out,results,query); results = null; qe.close(); return true; } /** * 加载本地文件形式的本体模型,失败系统退出 * @return */ private void loadModel() { InputStreamReader in; try { FileInputStream file = new FileInputStream(filePath); in = new InputStreamReader(file, "UTF-8");//处理中文 model.read(in, null); 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) { String prefix = "PREFIX owl: <http://www.w3.org/2002/07/owl#>"+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + "PREFIX base: <http://www.owl-ontologies.com/Ontology1331799803.owl#> "; String k="base:aa"; String slect = "SELECT ?x "; String where = "WHERE { "+ k+" rdfs:subClassOf ?x .}"; Test5 myLearn=new Test5(); String queryStr=prefix+slect+where; myLearn.runQuery(queryStr); } } 其中aa.rdf-xml.owl文件的内容如下: <?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns="http://www.owl-ontologies.com/Ontology1331799803.owl#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xml:base="http://www.owl-ontologies.com/Ontology1331799803.owl" > <rdf:Description rdf:about=""> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/> </rdf:Description> <rdf:Description rdf:about="#aa"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/> <rdfs:subClassOf rdf:resource="#company"/> </rdf:Description> <rdf:Description rdf:about="#company"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/> </rdf:Description> </rdf:RDF> <!-- Created with Protege (with OWL Plugin 3.2, Build 355) http://protege.stanford.edu -->
|