You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Phil Ashworth <pa...@gmail.com> on 2013/10/21 23:07:35 UTC

Collecting Resultsets in a model and retrieving a resultset from it

Hi There
This may be a crazy example but I'm wondering if anyone could suggest where
I'm going wrong (both in code and/or approach.)

Basically I want to use a list of QuerySolutonmaps to create a number of
resultsets from a ParameterizedSparqlString.
I want to combine the resultsets together. For this I've used a model (
perhaps incorrectly).
And in the end create a single result set that will be consumed by
something else.
The code below is where I've got to.
It works up to the last line where it throws the error shown.
If anyone could point out my error or offer another way to acomplish the
same thing it would greatly appreciated.

Many Thanks
Phil

    // select query previously defined in variable selectquery
    //the model to run the query across previously defined as model
    //get the passed querysolutionmap list
    final java.util.List<com.hp.hpl.jena.query.QuerySolutionMap>
inputQryList = (java.util.ArrayList)globalMap.get(inputBindings); // don't
worry aboout globalmap
    //create an iterator for the solution maps
    java.util.Iterator<com.hp.hpl.jena.query.QuerySolutionMap>
inputQryListIter = inputQryList.iterator();
    //create a model to compile all the results sets in.
    final com.hp.hpl.jena.rdf.model.Model resultModel =
com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel();
    //loop over all the QuerySolution maps
    while (inputQryListIter.hasNext())
    {
        final com.hp.hpl.jena.query.QuerySolutionMap soln_map =
inputQryListIter.next();

        //set parameterizsed sparql string
        final com.hp.hpl.jena.query.ParameterizedSparqlString pss = new
com.hp.hpl.jena.query.ParameterizedSparqlString(selectquery,soln_map);
        final com.hp.hpl.jena.query.QueryExecution qexec =
com.hp.hpl.jena.query.QueryExecutionFactory.create(pss.toString(), model) ;
        final com.hp.hpl.jena.query.ResultSet tempresults  =
qexec.execSelect() ; // get the resultset.
        // bring all the resultsets together in a new model.... may be a
better way to do this?
        final com.hp.hpl.jena.rdf.model.Model tempModel =
com.hp.hpl.jena.query.ResultSetFormatter.toModel(tempresults);
        resultModel.add(tempModel);
        qexec_<%=cid %>.close();
    }
    resultModel.write(System.out) //This show that all the code above has
worked and I see resultset syntax in the model for the select queries with
correctly bound varaibles
    // now I want to create a resultset from the model
    final com.hp.hpl.jena.sparql.resultset.SPARQLResult finalresults =  new
com.hp.hpl.jena.sparql.resultset.SPARQLResult(resultModel);
    //Everything works fine up to here.
    final com.hp.hpl.jena.query.ResultSet results  =
finalresults.getResultSet();

    The last line throws the error.
    com.hp.hpl.jena.sparql.resultset.ResultSetException: Not an ResultSet
result
    at com.hp.hpl.jena.sparql.resultset.SPARQLResult.getResultSet

Re: Collecting Resultsets in a model and retrieving a resultset from it

Posted by Phil Ashworth <pa...@gmail.com>.
Thanks
Much appreciated.
Phil


On Mon, Oct 21, 2013 at 10:33 PM, Andy Seaborne <an...@apache.org> wrote:

> On 21/10/13 22:07, Phil Ashworth wrote:
>
>> I want to combine the resultsets together. For this I've used a model (
>> perhaps incorrectly).
>>
>
> I'm afraid so.
>
> To get a list of results, use ResultSetFormatter toList and concatenate
> the lists.  If you really want a ResultSet, get as a list of Bindings
> (you'll need to write the loop), and use ResultSetStream/
>
>       final com.hp.hpl.jena.sparql.**resultset.SPARQLResult finalresults
>> =  new
>> com.hp.hpl.jena.sparql.**resultset.SPARQLResult(**resultModel);
>>      //Everything works fine up to here.
>>      final com.hp.hpl.jena.query.**ResultSet results  =
>> finalresults.getResultSet();
>>
>>      The last line throws the error.
>>      com.hp.hpl.jena.sparql.**resultset.ResultSetException: Not an
>> ResultSet
>> result
>>      at com.hp.hpl.jena.sparql.**resultset.SPARQLResult.**getResultSet
>>
>
> SPARQLResult is a usually internal class for carrying around any kind of
> SPARQL results.  It's a very simple types holder.
>
> A model is the outcome of a CONSTRUCT or DESCRIBE.  The fact it is a
> ResultSet encoded in a model is opaque to the SPARQLResult class.  A model
> is from CONSTRUCT or DESCRIBE.
>
> You have put a model into a SPARQLResult so you can't get a result set out.
>
>         Andy
>

Re: Collecting Resultsets in a model and retrieving a resultset from it

Posted by Andy Seaborne <an...@apache.org>.
On 21/10/13 22:07, Phil Ashworth wrote:
> I want to combine the resultsets together. For this I've used a model (
> perhaps incorrectly).

I'm afraid so.

To get a list of results, use ResultSetFormatter toList and concatenate 
the lists.  If you really want a ResultSet, get as a list of Bindings 
(you'll need to write the loop), and use ResultSetStream/

>      final com.hp.hpl.jena.sparql.resultset.SPARQLResult finalresults =  new
> com.hp.hpl.jena.sparql.resultset.SPARQLResult(resultModel);
>      //Everything works fine up to here.
>      final com.hp.hpl.jena.query.ResultSet results  =
> finalresults.getResultSet();
>
>      The last line throws the error.
>      com.hp.hpl.jena.sparql.resultset.ResultSetException: Not an ResultSet
> result
>      at com.hp.hpl.jena.sparql.resultset.SPARQLResult.getResultSet

SPARQLResult is a usually internal class for carrying around any kind of 
SPARQL results.  It's a very simple types holder.

A model is the outcome of a CONSTRUCT or DESCRIBE.  The fact it is a 
ResultSet encoded in a model is opaque to the SPARQLResult class.  A 
model is from CONSTRUCT or DESCRIBE.

You have put a model into a SPARQLResult so you can't get a result set out.

	Andy