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/08/22 18:19:47 UTC

Binding Variables for sparqlService

Hi Guys
I noticed the documentation states*
"
QuerySolutionMap initialBinding = new
QuerySolutionMap();initialBinding.add("name", personResource);qe =
QueryExecutionFactory.create(query, dataset, initialBinding);*

* *

*This is often much simpler than the string equivalent since you don't have
to escape quotes in literals. (Beware that this doesn't work for
sparqlService, which is a great shame. It would be nice to spend some time
remedying that.) "*

I'm actually in the situation that I would like to bind variables for a
sparqlService.

Can you suggest the best way for me to approach this?

Thanks In advance

Phil

Re: Binding Variables for sparqlService

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

Can I follow up with a couple more questions?

1. I presume ParamaterizedSparqlString  is like a query and can be used in
QueryExecutionFactory.create() as the query?





On Thu, Aug 22, 2013 at 5:27 PM, Rob Vesse <rv...@yarcdata.com> wrote:

> See ParamaterizedSparqlString -
> http://jena.apache.org/documentation/query/parameterized-sparql-strings.htm
> l
>
> Rob
>
>
>
> On 8/22/13 9:19 AM, "Phil Ashworth" <pa...@gmail.com> wrote:
>
> >Hi Guys
> >I noticed the documentation states*
> >"
> >QuerySolutionMap initialBinding = new
> >QuerySolutionMap();initialBinding.add("name", personResource);qe =
> >QueryExecutionFactory.create(query, dataset, initialBinding);*
> >
> >* *
> >
> >*This is often much simpler than the string equivalent since you don't
> >have
> >to escape quotes in literals. (Beware that this doesn't work for
> >sparqlService, which is a great shame. It would be nice to spend some time
> >remedying that.) "*
> >
> >I'm actually in the situation that I would like to bind variables for a
> >sparqlService.
> >
> >Can you suggest the best way for me to approach this?
> >
> >Thanks In advance
> >
> >Phil
>
>

Re: Binding Variables for sparqlService

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Thu, Aug 22, 2013 at 1:36 PM, Phil Ashworth <pa...@gmail.com> wrote:
> Thanks Joshua Very helpful
> I saw the the code from 3 weeks ago.
> I thought that was neat.
> But I'm presuming that won't work for parameterisedsparqlstring. And I need
> to use this for a sparql service clause.
>
> So I guess I willI have to just iterate over the bindings I want to happen,
> rather than put them in a values block.

ParameterizedSparqlString has `append` methods, so while you can't
simply add a values block, you can use the same kind of iteration over
the values that you need and construct a values block with something
like:

pss.append( "VALUES (" );
// ... figure out what vars to append
pss.append( "?v1 ?v2 ..." );
pss.append( ") {" );
for ( ... ) {
  pss.append( "(" );
  pss.appendNode( <value for v1> );
  pss.append( " " );
  pss.appendNode( <value for v2> );
  // ...
  pss.append( ")" );
}
pss.append( "}" );

If you want more than one set of specified bindings for one or more
variables, I don't see that you can get around using a VALUES block,
regardless of how you construct it.

//JT
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: Binding Variables for sparqlService

Posted by Phil Ashworth <pa...@gmail.com>.
Thanks Joshua Very helpful
I saw the the code from 3 weeks ago.
I thought that was neat.
But I'm presuming that won't work for parameterisedsparqlstring. And I need
to use this for a sparql service clause.

So I guess I willI have to just iterate over the bindings I want to happen,
rather than put them in a values block.
Thanks Rob and Joshua



On Thu, Aug 22, 2013 at 5:42 PM, Joshua TAYLOR <jo...@gmail.com>wrote:

> > On 8/22/13 9:19 AM, "Phil Ashworth" <pa...@gmail.com> wrote:
> >
> >>Hi Guys
> >>I noticed the documentation states*
> >>"
> >>QuerySolutionMap initialBinding = new
> >>QuerySolutionMap();initialBinding.add("name", personResource);qe =
> >>QueryExecutionFactory.create(query, dataset, initialBinding);*
> >>
> >>* *
> >>
> >>*This is often much simpler than the string equivalent since you don't
> >>have
> >>to escape quotes in literals. (Beware that this doesn't work for
> >>sparqlService, which is a great shame. It would be nice to spend some
> time
> >>remedying that.) "*
> >>
> >>I'm actually in the situation that I would like to bind variables for a
> >>sparqlService.
>
> On Thu, Aug 22, 2013 at 12:27 PM, Rob Vesse <rv...@yarcdata.com> wrote:
> > See ParamaterizedSparqlString -
> >
> http://jena.apache.org/documentation/query/parameterized-sparql-strings.htm
> > l
>
> In addition to the documentation, there are some code samples floating
> around the web, too.
>
> This stack overflow answer has code demonstrating the use of a
> parameterized SPARQL string:
>
> http://stackoverflow.com/questions/16737653/get-latitude-and-longitude-of-a-place-dbpedia
>
> Additionally, if you need more than one set of bindings, there's an
> not-too-old (about 3 weeks) about programmatically constructing a
> VALUES block for query here (which doesn't use a
> ParameterizedSparqlString):
>
> http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3CCA+Q4Jnn9vce94x2mMEPUHnwjsJ4kxEkSrTuwF8N43jSC37CE8g@mail.gmail.com%3E
>
> //JT
>
> --
> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>

Re: Binding Variables for sparqlService

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Thu, Aug 22, 2013 at 3:38 PM, Phil Ashworth <pa...@gmail.com> wrote:
> Hi Guys Sorry for asking one last dumb question
>
> Is ParameterizedSparqlString the preferred way to do variable bindings in
> all scenarios?
>
> I ask because I'm not sure if there are performance gains to be had using
> the query.setValuesDataBlock or QueryExecutionFactory.create(query, dataset,
> initialBinding) methods when the query is not going against a sparql
> service.

I can't speak as to which will have better performance characteristics
(and of course, that depends on what you mean by performance, too;
speed?  memory usage?  something else?), and this is probably a
question best answered by some tests and profiling.  I wanted to point
out though, since you seem to be considering
ParameterizedSparqlStrings for local and remote queries, and
programmatically constructed Queries and initialBindings for remote
queries only, that both ParameterizedSparqlStrings and the
programmatically constructed queries should work in both local and
remote situations.  All three can be used locally, but the
initialBinding approach cannot be used for remote queries.

//JT
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: Binding Variables for sparqlService

Posted by Phil Ashworth <pa...@gmail.com>.
Hi Guys Sorry for asking one last dumb question

Is ParameterizedSparqlString the preferred way to do variable bindings in
all scenarios?

I ask because I'm not sure if there are performance gains to be had using
the query.setValuesDataBlock or QueryExecutionFactory.create(query, dataset,
initialBinding) methods when the query is not going against a sparql
service.

Thanks once again.
Phil



On Thu, Aug 22, 2013 at 5:42 PM, Joshua TAYLOR <jo...@gmail.com>wrote:

> > On 8/22/13 9:19 AM, "Phil Ashworth" <pa...@gmail.com> wrote:
> >
> >>Hi Guys
> >>I noticed the documentation states*
> >>"
> >>QuerySolutionMap initialBinding = new
> >>QuerySolutionMap();initialBinding.add("name", personResource);qe =
> >>QueryExecutionFactory.create(query, dataset, initialBinding);*
> >>
> >>* *
> >>
> >>*This is often much simpler than the string equivalent since you don't
> >>have
> >>to escape quotes in literals. (Beware that this doesn't work for
> >>sparqlService, which is a great shame. It would be nice to spend some
> time
> >>remedying that.) "*
> >>
> >>I'm actually in the situation that I would like to bind variables for a
> >>sparqlService.
>
> On Thu, Aug 22, 2013 at 12:27 PM, Rob Vesse <rv...@yarcdata.com> wrote:
> > See ParamaterizedSparqlString -
> >
> http://jena.apache.org/documentation/query/parameterized-sparql-strings.htm
> > l
>
> In addition to the documentation, there are some code samples floating
> around the web, too.
>
> This stack overflow answer has code demonstrating the use of a
> parameterized SPARQL string:
>
> http://stackoverflow.com/questions/16737653/get-latitude-and-longitude-of-a-place-dbpedia
>
> Additionally, if you need more than one set of bindings, there's an
> not-too-old (about 3 weeks) about programmatically constructing a
> VALUES block for query here (which doesn't use a
> ParameterizedSparqlString):
>
> http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3CCA+Q4Jnn9vce94x2mMEPUHnwjsJ4kxEkSrTuwF8N43jSC37CE8g@mail.gmail.com%3E
>
> //JT
>
> --
> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>

Re: Binding Variables for sparqlService

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Mon, Sep 9, 2013 at 12:33 PM, Phil Ashworth <pa...@gmail.com> wrote:
>
> HI Joshua
> Sorry I think I caused some confusion.
> The code was the example from the web site you pointed me to.
> That code used a second model to capture all the information from all of the constructs.
> Basically I want to do the same but for a select statement.
> Selects come back as result set of query solutions so I was wondering if there an easy way of combining all the querysolutions from all of the result sets from multiple selects and then have an resultsets to iterate over them.

How about ResultSetMem [1]?  It's got a constructor that looks like it
does what you want:

public ResultSetMem(ResultSet... sets)

Create an in-memory result set from an array of ResulSets. It is
assumed that all the ResultSets from the array have the same
variables.
Parameters:sets - the ResultSet objects to concatenate.

[1] http://jena.apache.org/documentation/javadoc/arq/com/hp/hpl/jena/sparql/resultset/ResultSetMem.html


> On Mon, Sep 9, 2013 at 2:08 PM, Joshua TAYLOR <jo...@gmail.com> wrote:
>>
>> On Fri, Sep 6, 2013 at 5:11 PM, Phil Ashworth <pa...@gmail.com> wrote:
>> > Sorry to revisit this thread
>> > In one of the links you kindly provided there is a great example for using
>> > PSS in a construct ( see below)
>> > I really like the example. It's neat that you combine the models together in
>> > a new model before writing it out.
>> >
>> > I have a similar problem but I want to run a series of select queries
>> > combing the results of the resultsets before doing something with all the
>> > results.
>> >
>> > I've tried a couple ways of doing this but I keep hitting hurdles.
>> > The obvious way is to add each resultset querysolution into a
>> > list<querysolution> and then use an iterator over the list.
>> > But then I can't cast the list Iterator as a resultset which I would like to
>> > do to try and reuse variables in the code whether the it is running multiple
>> > selects or just one.
>> >
>> > Is using a list<querysolution>.iterator() the best way to achieve the
>> > outcome?
>> >
>> > public class DBPediaQuery {
>> >   public static void main( String[] args ) {
>> >     final String dbpedia = "http://dbpedia.org/sparql";
>> >     final ParameterizedSparqlString queryString
>> >       = new ParameterizedSparqlString(
>> >             "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>"+
>> >             "PREFIX dbo: <http://dbpedia.org/ontology/>" +
>> >             "CONSTRUCT WHERE {" +
>> >             "  ?s a dbo:Place ." +
>> >             "  ?s geo:lat ?lat ." +
>> >             "  ?s geo:long ?long ." +
>> >             "}" );
>> >     Model allResults = ModelFactory.createDefaultModel();
>> >     for ( String mountain : new String[] { "Mount_Monadnock",
>> > "Mount_Lafayette" } ) {
>> >       queryString.setIri( "?s", "http://dbpedia.org/resource/" + mountain );
>> >       QueryExecution exec = QueryExecutionFactory.sparqlService( dbpedia,
>> > queryString.toString() );
>> >       Model results = exec.execConstruct();
>> >       allResults.add( results );
>> >     }
>> >     allResults.setNsPrefix( "geo",
>> > "http://www.w3.org/2003/01/geo/wgs84_pos#" );
>> >     allResults.setNsPrefix( "dbo", "http://dbpedia.org/ontology/" );
>> >     allResults.setNsPrefix( "dbr", "http://dbpedia.org/resource/" );
>> >     allResults.write( System.out, "N3" );
>> >   }
>> > }
>>
>> Sorry for the delay in response.  I'm not quite clear from the code
>> that you posted what exactly you're trying to do.  ResultSet is just
>> an interface, and it wouldn't be all that hard to implement a
>> ResultSet that wraps a iterator over QuerySolutions.  However, if the
>> problem is that you're using QEF.sparqlService:
>>
>> >       QueryExecution exec = QueryExecutionFactory.sparqlService( dbpedia, queryString.toString() );
>>
>> perhaps you could use the 'service' keyword in the query, e.g.,
>>
>> PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
>> PREFIX dbo: <http://dbpedia.org/ontology/>
>> CONSTRUCT WHERE {
>>   SERVICE <http://dbpedia.org/sparql> {
>>     ?s a dbo:Place .
>>     ?s geo:lat ?lat .
>>     ?s geo:long ?long .
>>   }
>> }
>>
>> and execute the query as you would if you weren't using
>> QEF.sparqlService.  I'm not sure whether that works or not, but if it
>> does, and your problem arises from using QEF.sparqlService, it seems
>> like a relatively quick workaround.
>>
>> --
>> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>
>



-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: Binding Variables for sparqlService

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Fri, Sep 6, 2013 at 5:11 PM, Phil Ashworth <pa...@gmail.com> wrote:
> Sorry to revisit this thread
> In one of the links you kindly provided there is a great example for using
> PSS in a construct ( see below)
> I really like the example. It's neat that you combine the models together in
> a new model before writing it out.
>
> I have a similar problem but I want to run a series of select queries
> combing the results of the resultsets before doing something with all the
> results.
>
> I've tried a couple ways of doing this but I keep hitting hurdles.
> The obvious way is to add each resultset querysolution into a
> list<querysolution> and then use an iterator over the list.
> But then I can't cast the list Iterator as a resultset which I would like to
> do to try and reuse variables in the code whether the it is running multiple
> selects or just one.
>
> Is using a list<querysolution>.iterator() the best way to achieve the
> outcome?
>
> public class DBPediaQuery {
>   public static void main( String[] args ) {
>     final String dbpedia = "http://dbpedia.org/sparql";
>     final ParameterizedSparqlString queryString
>       = new ParameterizedSparqlString(
>             "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>"+
>             "PREFIX dbo: <http://dbpedia.org/ontology/>" +
>             "CONSTRUCT WHERE {" +
>             "  ?s a dbo:Place ." +
>             "  ?s geo:lat ?lat ." +
>             "  ?s geo:long ?long ." +
>             "}" );
>     Model allResults = ModelFactory.createDefaultModel();
>     for ( String mountain : new String[] { "Mount_Monadnock",
> "Mount_Lafayette" } ) {
>       queryString.setIri( "?s", "http://dbpedia.org/resource/" + mountain );
>       QueryExecution exec = QueryExecutionFactory.sparqlService( dbpedia,
> queryString.toString() );
>       Model results = exec.execConstruct();
>       allResults.add( results );
>     }
>     allResults.setNsPrefix( "geo",
> "http://www.w3.org/2003/01/geo/wgs84_pos#" );
>     allResults.setNsPrefix( "dbo", "http://dbpedia.org/ontology/" );
>     allResults.setNsPrefix( "dbr", "http://dbpedia.org/resource/" );
>     allResults.write( System.out, "N3" );
>   }
> }

Sorry for the delay in response.  I'm not quite clear from the code
that you posted what exactly you're trying to do.  ResultSet is just
an interface, and it wouldn't be all that hard to implement a
ResultSet that wraps a iterator over QuerySolutions.  However, if the
problem is that you're using QEF.sparqlService:

>       QueryExecution exec = QueryExecutionFactory.sparqlService( dbpedia, queryString.toString() );

perhaps you could use the 'service' keyword in the query, e.g.,

PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
CONSTRUCT WHERE {
  SERVICE <http://dbpedia.org/sparql> {
    ?s a dbo:Place .
    ?s geo:lat ?lat .
    ?s geo:long ?long .
  }
}

and execute the query as you would if you weren't using
QEF.sparqlService.  I'm not sure whether that works or not, but if it
does, and your problem arises from using QEF.sparqlService, it seems
like a relatively quick workaround.

--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: Binding Variables for sparqlService

Posted by Phil Ashworth <pa...@gmail.com>.
Hi Joshua
Sorry to revisit this thread
In one of the links you kindly provided there is a great example for using
PSS in a construct ( see below)
I really like the example. It's neat that you combine the models together
in a new model before writing it out.

I have a similar problem but I want to run a series of select queries
combing the results of the resultsets before doing something with all the
results.

I've tried a couple ways of doing this but I keep hitting hurdles.
The obvious way is to add each resultset querysolution into a
list<querysolution> and then use an iterator over the list.
But then I can't cast the list Iterator as a resultset which I would like
to do to try and reuse variables in the code whether the it is running
multiple selects or just one.

Is using a list<querysolution>.iterator() the best way to achieve the
outcome?
Cheers
Phil


public class DBPediaQuery {
  public static void main( String[] args ) {
    final String dbpedia = "http://dbpedia.org/sparql";
    final ParameterizedSparqlString queryString
      = new ParameterizedSparqlString(
            "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>"+
            "PREFIX dbo: <http://dbpedia.org/ontology/>" +
            "CONSTRUCT WHERE {" +
            "  ?s a dbo:Place ." +
            "  ?s geo:lat ?lat ." +
            "  ?s geo:long ?long ." +
            "}" );
    Model allResults = ModelFactory.createDefaultModel();
    for ( String mountain : new String[] { "Mount_Monadnock",
"Mount_Lafayette" } ) {
      queryString.setIri( "?s", "http://dbpedia.org/resource/" + mountain );
      QueryExecution exec = QueryExecutionFactory.sparqlService(
dbpedia, queryString.toString() );
      Model results = exec.execConstruct();
      allResults.add( results );
    }
    allResults.setNsPrefix( "geo", "http://www.w3.org/2003/01/geo/wgs84_pos#" );
    allResults.setNsPrefix( "dbo", "http://dbpedia.org/ontology/" );
    allResults.setNsPrefix( "dbr", "http://dbpedia.org/resource/" );
    allResults.write( System.out, "N3" );
  }}




On Thu, Aug 22, 2013 at 5:42 PM, Joshua TAYLOR <jo...@gmail.com>wrote:

> > On 8/22/13 9:19 AM, "Phil Ashworth" <pa...@gmail.com> wrote:
> >
> >>Hi Guys
> >>I noticed the documentation states*
> >>"
> >>QuerySolutionMap initialBinding = new
> >>QuerySolutionMap();initialBinding.add("name", personResource);qe =
> >>QueryExecutionFactory.create(query, dataset, initialBinding);*
> >>
> >>* *
> >>
> >>*This is often much simpler than the string equivalent since you don't
> >>have
> >>to escape quotes in literals. (Beware that this doesn't work for
> >>sparqlService, which is a great shame. It would be nice to spend some
> time
> >>remedying that.) "*
> >>
> >>I'm actually in the situation that I would like to bind variables for a
> >>sparqlService.
>
> On Thu, Aug 22, 2013 at 12:27 PM, Rob Vesse <rv...@yarcdata.com> wrote:
> > See ParamaterizedSparqlString -
> >
> http://jena.apache.org/documentation/query/parameterized-sparql-strings.htm
> > l
>
> In addition to the documentation, there are some code samples floating
> around the web, too.
>
> This stack overflow answer has code demonstrating the use of a
> parameterized SPARQL string:
>
> http://stackoverflow.com/questions/16737653/get-latitude-and-longitude-of-a-place-dbpedia
>
> Additionally, if you need more than one set of bindings, there's an
> not-too-old (about 3 weeks) about programmatically constructing a
> VALUES block for query here (which doesn't use a
> ParameterizedSparqlString):
>
> http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3CCA+Q4Jnn9vce94x2mMEPUHnwjsJ4kxEkSrTuwF8N43jSC37CE8g@mail.gmail.com%3E
>
> //JT
>
> --
> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>

Re: Binding Variables for sparqlService

Posted by Joshua TAYLOR <jo...@gmail.com>.
> On 8/22/13 9:19 AM, "Phil Ashworth" <pa...@gmail.com> wrote:
>
>>Hi Guys
>>I noticed the documentation states*
>>"
>>QuerySolutionMap initialBinding = new
>>QuerySolutionMap();initialBinding.add("name", personResource);qe =
>>QueryExecutionFactory.create(query, dataset, initialBinding);*
>>
>>* *
>>
>>*This is often much simpler than the string equivalent since you don't
>>have
>>to escape quotes in literals. (Beware that this doesn't work for
>>sparqlService, which is a great shame. It would be nice to spend some time
>>remedying that.) "*
>>
>>I'm actually in the situation that I would like to bind variables for a
>>sparqlService.

On Thu, Aug 22, 2013 at 12:27 PM, Rob Vesse <rv...@yarcdata.com> wrote:
> See ParamaterizedSparqlString -
> http://jena.apache.org/documentation/query/parameterized-sparql-strings.htm
> l

In addition to the documentation, there are some code samples floating
around the web, too.

This stack overflow answer has code demonstrating the use of a
parameterized SPARQL string:
http://stackoverflow.com/questions/16737653/get-latitude-and-longitude-of-a-place-dbpedia

Additionally, if you need more than one set of bindings, there's an
not-too-old (about 3 weeks) about programmatically constructing a
VALUES block for query here (which doesn't use a
ParameterizedSparqlString):
http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3CCA+Q4Jnn9vce94x2mMEPUHnwjsJ4kxEkSrTuwF8N43jSC37CE8g@mail.gmail.com%3E

//JT

-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: Binding Variables for sparqlService

Posted by Rob Vesse <rv...@yarcdata.com>.
See ParamaterizedSparqlString -
http://jena.apache.org/documentation/query/parameterized-sparql-strings.htm
l

Rob



On 8/22/13 9:19 AM, "Phil Ashworth" <pa...@gmail.com> wrote:

>Hi Guys
>I noticed the documentation states*
>"
>QuerySolutionMap initialBinding = new
>QuerySolutionMap();initialBinding.add("name", personResource);qe =
>QueryExecutionFactory.create(query, dataset, initialBinding);*
>
>* *
>
>*This is often much simpler than the string equivalent since you don't
>have
>to escape quotes in literals. (Beware that this doesn't work for
>sparqlService, which is a great shame. It would be nice to spend some time
>remedying that.) "*
>
>I'm actually in the situation that I would like to bind variables for a
>sparqlService.
>
>Can you suggest the best way for me to approach this?
>
>Thanks In advance
>
>Phil