Select Jena 中的 FROM 子句未返回任何结果
Select FROM clause in Jena returning no results
我们在使用 Jena 数据集中的 sparql FROM 子句跨多个图可靠地发出 sparql 查询时遇到问题。
这是问题的一个例子:
final String subject = "http://example.com/ont/breakfast#espresso";
final String graph1 = "http://example.com/ont/breakfast/graph#espresso_definition";
final String graph2 = "http://example.com/ont/breakfast/graph#espresso_decoration";
// Add some triples to graphs within the dataset
Dataset dataset = DatasetFactory.create();
Model modelG1 = dataset.getNamedModel(graph1);
Resource espressoTypeG1 = modelG1.createResource(subject)
.addProperty(RDF.type, OWL.Class);
Resource espressoLabelG1 = modelG1.createResource(subject)
.addProperty(RDFS.label, "Espresso");
Model modelG2 = dataset.getNamedModel(graph2);
Resource espressoLabelG2 = modelG2.createResource(subject)
.addProperty(RDFS.label, "Black Gold");
// The query to execute - returns no results
String queryString = "select * FROM <" + graph1 + "> FROM <" + graph2 + "> " +
"{ <" + subject + "> ?p ?o }";
// This, however, works:
// String queryString = "select * { graph ?g { <" + subject + "> ?p ?o } }";
// Run the query
Query query = QueryFactory.create(queryString);
try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
System.out.println(result);
}
}
values 子句和 graph 关键字的组合帮助我们解决了大多数需要处理多个图的场景在同一个查询中。在某些查询中,这会变得非常笨拙或效率低下。
我们如何才能在单个数据集中跨多个模型正确发出查询?
请注意,查询在编译时是未知的,因此我们不能依赖于在 Java 代码中手动创建 models 的并集。此外,通常使用从文件加载、sparql 更新和调用 dataset.asDatasetGraph().add(...).
的组合来添加数据
FROM
和 FROM NAMED
的处理取决于所使用的 Dataset
实现是否支持它,默认的内存中实现默认不支持它。
要强制使用数据集,您可以使用 DynamicDatasets
and DatasetDescription
助手 类 来解析查询指定的数据集,例如
Dataset resolvedDataset =
DynamicDatasets.dynamicDataset(DatasetDescription.create(query), dataset, false);
try (QueryExecution qe = QueryExecutionFactory.create(query, resolvedDataset)) {
// Normal result processing logic goes here...
}
我们在使用 Jena 数据集中的 sparql FROM 子句跨多个图可靠地发出 sparql 查询时遇到问题。
这是问题的一个例子:
final String subject = "http://example.com/ont/breakfast#espresso";
final String graph1 = "http://example.com/ont/breakfast/graph#espresso_definition";
final String graph2 = "http://example.com/ont/breakfast/graph#espresso_decoration";
// Add some triples to graphs within the dataset
Dataset dataset = DatasetFactory.create();
Model modelG1 = dataset.getNamedModel(graph1);
Resource espressoTypeG1 = modelG1.createResource(subject)
.addProperty(RDF.type, OWL.Class);
Resource espressoLabelG1 = modelG1.createResource(subject)
.addProperty(RDFS.label, "Espresso");
Model modelG2 = dataset.getNamedModel(graph2);
Resource espressoLabelG2 = modelG2.createResource(subject)
.addProperty(RDFS.label, "Black Gold");
// The query to execute - returns no results
String queryString = "select * FROM <" + graph1 + "> FROM <" + graph2 + "> " +
"{ <" + subject + "> ?p ?o }";
// This, however, works:
// String queryString = "select * { graph ?g { <" + subject + "> ?p ?o } }";
// Run the query
Query query = QueryFactory.create(queryString);
try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
System.out.println(result);
}
}
values 子句和 graph 关键字的组合帮助我们解决了大多数需要处理多个图的场景在同一个查询中。在某些查询中,这会变得非常笨拙或效率低下。
我们如何才能在单个数据集中跨多个模型正确发出查询?
请注意,查询在编译时是未知的,因此我们不能依赖于在 Java 代码中手动创建 models 的并集。此外,通常使用从文件加载、sparql 更新和调用 dataset.asDatasetGraph().add(...).
的组合来添加数据FROM
和 FROM NAMED
的处理取决于所使用的 Dataset
实现是否支持它,默认的内存中实现默认不支持它。
要强制使用数据集,您可以使用 DynamicDatasets
and DatasetDescription
助手 类 来解析查询指定的数据集,例如
Dataset resolvedDataset =
DynamicDatasets.dynamicDataset(DatasetDescription.create(query), dataset, false);
try (QueryExecution qe = QueryExecutionFactory.create(query, resolvedDataset)) {
// Normal result processing logic goes here...
}