簡體   English   中英

如何從Jena SPAQL結果集中獲取第一個元素

[英]How to get the first element from Jena SPAQL Resultset

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

resultList為空,但有一個結果...

默認情況下,結果集是按需生成的。 您只能對它們進行一次迭代,然后使用結果。 做完之后

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

你可能做不到任何結果

ResultSetFormatter.outputAsJSON(b, result);

要么

ResultSetFormatter.toList(result);

相反,您應該使用以下命令復制ResultSet:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM