Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

QueryResults



The QueryResults class allows to decode the XML results of a SPARQL request.

Overview

The result of a SPARQL request can be either: The QueryResultsUtilities class allows to parse the XML and create an associated data structure which can be processed by your program.

queryresults
The various static methods QueryResults instance create a QueryResults instance.

QueryResults class

The QueryResults class has the following methods:
Each result is a QueryResults.Result element.

Results map

The QueryResults.getResults() return the map of results. The QueryResults.getFirstResult() method return the firt element in the map according to the Map order.

The key of the result Map will be the SELECT variable used to order the result (by the orderedBy argument)). If this variable was not part of the SELECT variables, then it will be the first encountered variable in the columns.

For example:
   String resultsAsXML = <the XML results returned by the SPARQL engine>);
   QueryResults results = QueryResultsUtilities.getOrderedResults(resultsAsXML, true, "name");
   Map<String, QueryResults.Result> map = results.getResults(); // the map will be ordered by the values of the "?name" variable

When you get the results map, you will get an orderd map, but the map will be ordered by the values of the QueryResults.orderedBy() element (the one you will have passed on in the QueryResultsUtilities. If you want eh results to be ordered exactly as what the SPARQL engine returned, you should get the results list.

Results list

The QueryResults.getOrderedResultsList() and QueryResults.getOrderedResult(int index) methods return the results in the order it had been returned by the SPARQL engine.

For example:
   String resultsAsXML = <the XML results returned by the SPARQL engine>);
   QueryResults results = QueryResultsUtilities.getOrderedResults(resultsAsXML, true, "name");
   List<QueryResults.Result> list = results.getResultsList(); // the map will be ordered in the order it had been returned by the SPARQL engine

QueryResults.Result class

The QueryResults.Result represent one result (returned by the results map or the results list). It contains the Map of attributes for one result.

This class has the following methods:
Each value is a QueryResults.Value element.

QueryResults.Value class

The QueryResults.Value class represent one value in a result.

This class has the following methods:
Results which are owl-time will be an instance of the OwlTime class.

Examples

Basic example

Suppose the following query:
   SELECT ?number ?type
   WHERE { 
      ?alarm rdf:type inav:Alarm .
      ?alarm inav:AlarmNumber ?number .
      ?alarm inav:hasAlarmType ?type .
   }    
The following code will print the ?number and ?type of each result:
   public void subscribe(ServiceInstance service) {
      QueryResults results = (QueryResults) service.getData("response").getValue();
      Iterator<QueryResults.Result> it = results.getOrderedResultsList().iterator();
      while (it.hasNext()) {
       QueryResults.Result result = it.next();
        int number = result.getValue("number").getValueAsInt();
        String type = result.getValue("type").getValueAsString();
        System.out.println("Alarm " + number + ": " + type);
      }
   }

The results will be ordered in the order it had been returned by the SPARQL engine.

Second example

Suppose the following SPARQL XML result:
   <sparql xmlns="http://www.w3.org/2005/sparql-results#">
      <head>
         <variable name="name"/>
         <variable name="age"/>
      </head>
      <results>
         <result> 
            <binding name="name">
               <literal xml:lang="en">Bob</literal>
            </binding>
            <binding name="age">
               <literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal>
            </binding>
         </result>
         <result> 
            <binding name="name">
               <literal xml:lang="en">Bobby</literal>
            </binding>
            <binding name="age">
               <literal datatype="http://www.w3.org/2001/XMLSchema#integer">40</literal>
            </binding>
         </result>      
      </results>
   </sparql>
If we call:
   QueryResults results = QueryResultsUtilities.getOrderedResults(<XML String>, true, "name");

The results will be ordered by the values of the "?name" variable.


Wr will have two results:
  • The first result is the element with the name "Bob"
  • The second result is the element with the name "Bobby"

See also


Categories: Queryresults

Copyright 2025 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence