我对 Jena 模型进行 SPARQL 查询,返回单个结果。由于只有一个元素而无法迭代,如何访问该结果?我尝试了 2 个选项,但都失败了。我使用 ResultSetFormatter 将结果转换为 JSONObject,但我发现键不是我的变量。此外,我尝试使用 toList() 方法将其转换为 QuerySolution 列表,但它返回空列表。有什么帮助吗?
public void insertMedcationContext(JSONObject medcontext) {
connection.getDataset().begin(ReadWrite.WRITE);
Model model = connection.getDataset().getDefaultModel();
String medActivityQuery = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX fn: <http://www.w3.org/2005/xpath-functions#>\n"
+ "PREFIX medication:<http://www.cs.kaist.ac.kr/medication/ontology#>\n"
+ "PREFIX resource:<http://www.cs.kaist.ac.kr/medication/resource#>\n"
+ "PREFIX time:<http://www.w3.org/2006/time#>\n"
+ "SELECT ?activity ((?deschour - ?timestamp) AS ?gap) (fn:abs(?gap)AS ?gapabsolute)\n"
+ "WHERE\n"
+ "{?activity rdf:type medication:MedicationActivity .\n"
+ "?activity medication:belongsTo ?schedule .\n"
+ "?activity medication:expectedTime ?time .\n"
+ "?time time:hasTimeDescription ?desc .\n"
+ "?desc time:year ?descyear .\n"
+ "?desc time:month ?descmonth .\n"
+ "?desc time:day ?descdate .\n"
+ "?desc time:hour ?deschour .\n"
+ "}\n"
+ "ORDER BY (?gapabsolute)\n"
+ "LIMIT 1";
try {
Resource schedule = model.createResource(
nameSpace + medcontext.getString("schedule"),
MEDICATION.Schedule);
Resource scheduleResource = model.getResource(schedule.getURI());
ParameterizedSparqlString parameterizedQuery = new ParameterizedSparqlString(
medActivityQuery);
parameterizedQuery.setParam("schedule", scheduleResource);
JSONObject timestamp = new JSONObject();
timestamp = medcontext.getJSONObject("exacttime");
parameterizedQuery.setLiteral("descyear",Integer.toString(timestamp.getInt("year")),XSDDatatype.XSDgYear);
parameterizedQuery.setLiteral("descmonth",Integer.toString(timestamp.getInt("month")),XSDDatatype.XSDgMonth);
parameterizedQuery.setLiteral("descdate",Integer.toString(timestamp.getInt("date")),XSDDatatype.XSDgDay);
parameterizedQuery.setLiteral("timestamp",Integer.toString(timestamp.getInt("hour")),XSDDatatype.XSDnonNegativeInteger);
Query query = QueryFactory.create(parameterizedQuery.toString());
QueryExecution qe = QueryExecutionFactory.create(query, model);
try {
ResultSet result = qe.execSelect();
String text = ResultSetFormatter.asText(result);
System.out.println(text);
ByteArrayOutputStream b = new ByteArrayOutputStream();
ResultSetFormatter.outputAsJSON(b, result);
JSONObject jsonResult = new JSONObject (b.toString());
System.out.print(jsonResult);
List <QuerySolution> resultList = ResultSetFormatter.toList(result);
// Get the right medication activity from the model for which context is incoming
Resource rightActivity = null;
QuerySolution row = resultList.get(0);
rightActivity = row.getResource("activity");
System.out.print(rightActivity.toString());
结果列表为空,但只有一个结果...
最佳答案
默认情况下,结果集是按需生成的。您只能迭代它们一次,然后结果就会被消耗。完成后
ResultSet result = qe.execSelect();
String text = ResultSetFormatter.asText(result);
你可能无法从这样做得到任何结果
ResultSetFormatter.outputAsJSON(b, result);
或
ResultSetFormatter.toList(result);
相反,您应该使用例如
复制结果集ResultSet results = ResultSetFactory.copyResults( qe.execSelect() );
关于java - 如何从 Jena SPARQL 结果集中获取第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25533924/