2014-08-27 108 views
0

我对返回单个结果的Jena模型有SPARQL查询。我如何访问该结果,因为我无法迭代,因为只有一个元素?我尝试了2个选项,但都失败了。我使用ResultSetFormatter将结果转换为JSONObject,但我发现这些键不是我的变量。此外,我试图使用toList()方法将其转换为QuerySolution列表,但它返回空列表。任何帮助?如何从Jena SPAQL结果集中获取第一个元素

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()); 

resultList是空的。有一个结果......

回答

3

ResultSets已默认情况下,生产上的需求。你只能遍历它们一次,然后结果被消耗。你做

ResultSet result = qe.execSelect(); 
String text = ResultSetFormatter.asText(result); 

后,你可能无法从做

ResultSetFormatter.outputAsJSON(b, result); 

ResultSetFormatter.toList(result); 

反而得到任何结果,您应该复制与ResultSet中,例如,

ResultSet results = ResultSetFactory.copyResults(qe.execSelect()); 
+1

如果这是重复的,我不会感到惊讶一些现有的问题和答案,但经过一番搜索,我没有找到一个。 – 2014-08-27 19:02:05

相关问题