gpt4 book ai didi

java - 保存和重用 SPARQL 查询的结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:40 25 4
gpt4 key购买 nike

我正在使用 Jena 通过 Eclipse 查询一个猫头鹰文件。我想做一些连续的查询,每个新查询都将使用前一个查询的结果。我想在不同的 ?SELECT 中执行它并保存每次查询的结果以便在新查询中再次使用它。我不想自己插入变量的值,而是自动从查询中插入。有什么想法是实现这一目标的合适方法吗?提前致谢

最佳答案

我认为这里有两个主要选项:

  1. 一个。如果您只对 ResultSet 中的一个 QuerySolution 感兴趣并且在本地运行这些查询,那么您可以使用 QuerySolution 并将其用作 QueryExecution 的 initialBindings 参数,以及QuerySolution 中的变量将用于第二个查询。这可能仅适用于本地查询(因为 ResultSet 中的某些值可能是空白节点,其值在远程上下文中没有意义),并且仅当您有一个感兴趣的 QuerySolution 时才有用。
  1. b。单个 QuerySolution 也可用于在 ParameterizedSparqlString 中设置一些值。在这种情况下,这些值在您创建实际的 Query 或 QueryExecution 对象之前被替换,如果需要,您也可以进行一些其他操作。如果某些结果是空白节点,您可能无法可靠地运行远程查询,但您能够将此方法用于远程端点,而您不能使用 initialBindings 方法。

  2. 更通用的解决方案使用 SPARQL 1.1's VALUES在 VALUES block 中提供 ResultSet 的内容。这更可移植,因为任何端点都应该能够处理 VALUES block ,而且它更通用,因为它可以处理一堆绑定(bind),而不仅仅是一个。

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.core.Var;
import com.hp.hpl.jena.sparql.engine.binding.Binding;


public class ReuseResults {

final static String turtleContent = "" +
"@prefix : <http://example.org/> .\n" +
"\n" +
":alice :hasName \"Alice\" .\n" +
":alice :hasAddress \"4222 Clinton Way\" .\n" +
":herman :hasName \"Herman\".\n" +
":herman :hasAddress \"1313 Mockingbird Lane\" .\n" +
":DrWho :hasAddress \"The TARDIS\"" +
"";

// Read the model from the turtle content
final static Model model = ModelFactory.createDefaultModel()
.read( new ByteArrayInputStream( turtleContent.getBytes()), null, "TURTLE" );

final static String findAlice = "" +
"prefix : <http://example.org/>\n" +
"select ?alice where {\n" +
" ?alice :hasName \"Alice\" .\n" +
"}\n" +
"";

final static String findAliceAddress = "" +
"prefix : <http://example.org/>\n" +
"select ?address where {\n" +
" ?alice :hasAddress ?address .\n" +
"}\n" +
"";

public static void useInitialBindingsFromQuerySolution() throws IOException {
System.out.println( "== useInitialBindingsFromQuerySolution ==" );
// execute the query that finds a binding for ?alice. There should be just one
// query solution in the result set.
final ResultSet aliceResults = QueryExecutionFactory.create( findAlice, model ).execSelect();
final QuerySolution solution = aliceResults.next();
// Use the single query solution from the result set as initial bindings for
// the second query (which uses the variable ?alice).
final ResultSet addressResults = QueryExecutionFactory.create( findAliceAddress, model, solution ).execSelect();
ResultSetFormatter.out( addressResults );
}

public static void useParameterizedSPARQLString() {
System.out.println( "== useParameterizedSPARQLString ==" );
// execute the query that finds a (single) binding for ?alice. Then create
// a query solution map containing those results.
final ResultSet aliceResults = QueryExecutionFactory.create( findAlice, model ).execSelect();
final QuerySolutionMap map = new QuerySolutionMap();
map.addAll( aliceResults.next() );
// Create a ParameterizedSparqlString from the findAliceAddress query string (if this
// approach were taken, findAliceAddress could actually *be* a Param.SparqlString, of
// course).
final ParameterizedSparqlString pss = new ParameterizedSparqlString( findAliceAddress );
System.out.println( pss.toString() );
pss.setParams( map );
System.out.println( pss.toString() );
// execute the query and show the results
ResultSetFormatter.out( QueryExecutionFactory.create( pss.toString(), model ).execSelect() );
}

final static String findNamed = "" +
"prefix : <http://example.org/>\n" +
"select ?person where {\n" +
" ?person :hasName [] .\n" +
"}\n" +
"";

final static String findPersonAddress = "" +
"prefix : <http://example.org/>\n" +
"select ?address where { " +
" ?person :hasAddress ?address .\n" +
"}\n" +
"";

public static void useValuesFromResultSet() {
System.out.println( "\n== useValuesFromResultSet ==" );
final ResultSet namedResults = QueryExecutionFactory.create( findNamed, model ).execSelect();
final QueryExecution qe = QueryExecutionFactory.create( findPersonAddress, model );
System.out.println( "=== Query Before Adding VALUES ===\n" + qe.getQuery() );
// Create a list of the variables from the result set
List<Var> variables = new ArrayList<>();
for ( final String varName : namedResults.getResultVars() ) {
variables.add( Var.alloc( varName ));
}
// Create a list of the bindings from the result set.
List<Binding> values = new ArrayList<>();
while ( namedResults.hasNext() ) {
values.add( namedResults.nextBinding() );
}
// add a values block to the query
qe.getQuery().setValuesDataBlock(variables, values);
System.out.println( "\n=== Query After Adding VALUES ===\n" + qe.getQuery() );
ResultSetFormatter.out( qe.execSelect() );
}

public static void main(String[] args) throws IOException {
useInitialBindingsFromQuerySolution(); // 1.a.
useParameterizedSPARQLString(); // 1.b.
useValuesFromResultSet(); // 2.
}
}

这是生成的输出。在第一种情况下,Alice 的地址被选中的事实表明 ?alice 的值从第一个 ResultSet 传送到 initialBindings。在第二种情况下,我在添加 VALUES 子句之前和之后打印了查询,以便清楚区别。请注意,即使 ResultSet 绑定(bind)了多个变量,这也会起作用。多个变量的语法为 VALUES (?v1 ... ?vN) { (value11 ... value1N) ... (valueM1 ... valueMN) }

== useInitialBindingsFromQuerySolution ==
----------------------
| address |
======================
| "4222 Clinton Way" |
----------------------

== useParameterizedSPARQLString ==
prefix : <http://example.org/>
select ?address where {
?alice :hasAddress ?address .
}

prefix : <http://example.org/>
select ?address where {
<http://example.org/alice> :hasAddress ?address .
}

----------------------
| address |
======================
| "4222 Clinton Way" |
----------------------

== useValuesFromResultSet ==
=== Query Before Adding VALUES ===
PREFIX : <http://example.org/>
SELECT ?address
WHERE
{ ?person :hasAddress ?address }

=== Query After Adding VALUES ===
PREFIX : <http://example.org/>
SELECT ?address
WHERE
{ ?person :hasAddress ?address }
VALUES ?person { :herman :alice }

---------------------------
| address |
===========================
| "1313 Mockingbird Lane" |
| "4222 Clinton Way" |
---------------------------

关于java - 保存和重用 SPARQL 查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17766541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com