我在DBPedia工作 . 我想运行一些sparql . 所以我从这里下载文件:http://wiki.dbpedia.org/services-resources/ontology .
如您所见,DBpedia将其三元组分成不同的文件 . rdf:type properties 的文件和 others properties 的另一个文件 .
我将每个文件导入MySQL(坚持模型); Condsider我按顺序命名模型“A”和“B”!代码如下:
为 DBpedia Ontology RDF type statements
private Model AModel;
private Model BModel;
DBConnection connection = new DBConnection(DB_URL, DB_USER, DB_PASSWORD, DB_TYPE);
AModel = maker.createModel("A",true);
model.begin();
InputStream in =this.getClass().getClassLoader().getResourceAsStream("some address /instance_types_en.nt);
model.read(in,null);// Commit the database transactionmodel.commit();
------------
再次为 DBpedia Ontology other A-Box properties
DBConnection connection = new DBConnection(DB_URL, DB_USER, DB_PASSWORD, DB_TYPE);
BModel = maker.createModel("B",true);
model.begin();
InputStream in =this.getClass().getClassLoader().getResourceAsStream("some address /mappingbased_properties_en.nt);
model.read(in,null);// Commit the database transactionmodel.commit();
所以在这里,我们有两个模型,一个是 for DBpedia Ontology RDF type statements 和 another for DBpedia Ontology other A-Box properties .
问题是我们有一个需要同时进行信息交换的sparql . (rdf:type properties and non rdf:type prpperties);如:
Who was the wife of President Lincoln?
它的等价sparql是:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX onto: <http://dbpedia.org/ontology/>
SELECT ?uri ?string
WHERE
{
?person rdf:type onto:President .
?person foaf:surname "Lincoln"@en .
?person onto:spouse ?uri.
OPTIONAL {?uri rdfs:label ?string .}
FILTER (lang(?string) = "en")
}
所以我应该在模型中运行这个sparql . 但是jena让我只在一个模型中执行sparql:
所以我必须运行它:
QueryExecution qe1 = QueryExecutionFactory.create(query, AModel));
要么
QueryExecution qe1 = QueryExecutionFactory.create(query, BModel));
**
?人:配偶?uri . 在AModel和?person rdf:输入:Presiden在BModel中!
所以这个查询没有返回记录!
那么解决方案是什么?我的程序生成正确的sparql但我没有相应的数据集来返回响应!因为我有不同的型号,所以我不能在这里使用Union运营商!
1 回答
有几种方法:
将所有数据读入同一模型
使用联合模型(ModelFactory.createUnion或MultiUnion) .
将模型分别读入RDF数据集(RDB无法实现) .
创建一个数据集,其中包含两个模型 .
如果您使用的是具有DBConnection的Jena版本,则其中一些可能无法正常工作,因为代码太旧了 .