Rad*_*adi 4 ontology sparql jena
我有一些rdf和rdfs文件,我想使用jena sparql实现来查询它,我的代码看起来像:
//model of my rdf file
Model model = ModelFactory.createMemModelMaker().createDefaultModel();
model.read(inputStream1, null);
//model of my ontology (word net) file
Model onto = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_RDFS_INF);
onto.read( inputStream2,null);
String queryString =
"PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "PREFIX wn:<http://www.webkb.org/theKB_terms.rdf/wn#> "
+ "SELECT ?person "
+ "WHERE {"
+ " ?person rdf:type wn:Person . "
+ " }";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, ????);
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results, query);
qe.close();
Run Code Online (Sandbox Code Playgroud)
我在rdf文件中有一个wordNet本体,我想在我的查询中使用这个本体自动进行推理(当我查询查询的人应该返回例如.男人,女人)那么如何将本体链接到我的查询?请帮我.
更新:现在我有两个模型:我应该运行我的查询?
QueryExecution qe = QueryExecutionFactory.create(query, ????);
Run Code Online (Sandbox Code Playgroud)
提前致谢.
关键是要认识到,在耶拿,它Model是中心抽象之一.推理模型只是一个Model,其中存在一些三元组,因为它们是由推理规则引起的,而不是从源文档中读入.因此,您只需要更改示例的第一行,即最初创建模型的位置.
虽然您可以直接创建推理模型,但通常最简单的方法是创建OntModel具有所需推理支持度的模型:
Model model = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_RDFS_INF );
Run Code Online (Sandbox Code Playgroud)
如果您需要不同的推理器或OWL支持,则可以选择不同的OntModelSpec常量.请注意,大型和/或复杂模型可能会导致查询速度变慢.
更新(编辑原始问题后)
要推理两个模型,你需要联合.你可以通过OntModel子模型的实力来做到这一点.我会改变你的例子如下(注意:我没有测试过这段代码,但它应该可以工作):
String rdfFile = "... your RDF file location ...";
Model source = FileManager.get().loadModel( rdfFile );
String ontFile = "... your ontology file location ...";
Model ont = FileManager.get().loadModel( ontFile );
Model m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_RDFS_INF, ont );
m.addSubModel( source );
String queryString =
"PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "PREFIX wn:<http://www.webkb.org/theKB_terms.rdf/wn#> "
+ "SELECT ?person "
+ "WHERE {"
+ " ?person rdf:type wn:Person . "
+ " }";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, m);
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results, query);
qe.close();
Run Code Online (Sandbox Code Playgroud)