You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Robby Pelssers <Ro...@nxp.com> on 2012/08/16 11:19:17 UTC

how to use ARQ api to parametrize following query

Hi all,

I have following query which executes just fine.  But I'm interested to find out what is best practice to parametrize this query so that the "BUK" part  can be injected.


*         How would I need to alter this query?

*         How would the below code snippet need to be altered?

       String query = "....some query..."
       Query query = QueryFactory.create(sQuery);
       QueryExecution qexec = QueryExecutionFactory.create(query, model);
       ResultSet results = qexec.execSelect();


#Query showcasing how to select typenumber and preflabel for all products
#for which typenumber starts with BUK
PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
PREFIX skos:  <http://www.w3.org/2004/02/skos/core#>
SELECT ?typeNumber ?prefLabel
WHERE
{
  ?x nxp:typeNumber ?typeNumber;
     skos:prefLabel ?prefLabel .
     FILTER(STRSTARTS(STR(?typeNumber), "BUK"))
}

Thx in advance,
Robby

Re: how to use ARQ api to parametrize following query

Posted by Andy Seaborne <an...@apache.org>.
Does

     com.hp.hpl.jena.query.ParameterizedSparqlString

meet your needs?

The other way is to use an initial binding for ?searchstring in 
QueryExecutionFactory.create.

	Andy

(An example in "src-examples" would be nice ....)

On 16/08/12 10:55, Robby Pelssers wrote:
> I alread tested a bit around and this seems to work where I create a plain literal.  I will now test if that also works for non string parameters and so on.
>
> SPARQL:
> ***********************************************************
> #Query showcasing how to select typenumber and preflabel for all products
> #for which typenumber starts with BUK
> PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
> PREFIX skos:  <http://www.w3.org/2004/02/skos/core#>
> SELECT ?typeNumber ?prefLabel
> WHERE
> {
>    ?x nxp:typeNumber ?typeNumber;
>       skos:prefLabel ?prefLabel .
>       FILTER(STRSTARTS(STR(?typeNumber), ?searchstring))
> }
> ***********************************************************
> There are quite a few classes of our own just in case anyone wonders... but the idea is clear I guess.
>          //return all typenumbers and their corresponding preflabels
>          QuerySolutionMap parameters = new QuerySolutionMap();
>          Literal searchString = ResourceFactory.createPlainLiteral("PMN");
>          parameters.add("searchstring", searchString);
>          String sQuery = IOUtils.toString(readClassPathResource("sparql/query4.sparql"), "UTF-8");
>          ParameterizedSparqlString parQuery = new ParameterizedSparqlString(sQuery, parameters);
>          Query query = parQuery.asQuery();
>          Model model = readModelFromInputStream("data/basictypes-sample.nt", "basictypes", ModelLanguage.NTRIPLE);
>          QueryExecution qexec = QueryExecutionFactory.create(query, model);
>          ResultSet results = qexec.execSelect();
>          ResultSetFormatter.out(System.out, results, query);
>          qexec.close();
>
> ***********************************************************
>
> Robby
> -----Original Message-----
> From: Robby Pelssers [mailto:Robby.Pelssers@nxp.com]
> Sent: Thursday, August 16, 2012 11:19 AM
> To: users@jena.apache.org
> Subject: how to use ARQ api to parametrize following query
>
> Hi all,
>
> I have following query which executes just fine.  But I'm interested to find out what is best practice to parametrize this query so that the "BUK" part  can be injected.
>
>
> *         How would I need to alter this query?
>
> *         How would the below code snippet need to be altered?
>
>         String query = "....some query..."
>         Query query = QueryFactory.create(sQuery);
>         QueryExecution qexec = QueryExecutionFactory.create(query, model);
>         ResultSet results = qexec.execSelect();
>
>
> #Query showcasing how to select typenumber and preflabel for all products
> #for which typenumber starts with BUK
> PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
> PREFIX skos:  <http://www.w3.org/2004/02/skos/core#>
> SELECT ?typeNumber ?prefLabel
> WHERE
> {
>    ?x nxp:typeNumber ?typeNumber;
>       skos:prefLabel ?prefLabel .
>       FILTER(STRSTARTS(STR(?typeNumber), "BUK"))
> }
>
> Thx in advance,
> Robby
>


RE: how to use ARQ api to parametrize following query

Posted by Robby Pelssers <Ro...@nxp.com>.
I alread tested a bit around and this seems to work where I create a plain literal.  I will now test if that also works for non string parameters and so on.

SPARQL:
***********************************************************
#Query showcasing how to select typenumber and preflabel for all products
#for which typenumber starts with BUK
PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
PREFIX skos:  <http://www.w3.org/2004/02/skos/core#>
SELECT ?typeNumber ?prefLabel
WHERE
{
  ?x nxp:typeNumber ?typeNumber;
     skos:prefLabel ?prefLabel .
     FILTER(STRSTARTS(STR(?typeNumber), ?searchstring))
}
***********************************************************
There are quite a few classes of our own just in case anyone wonders... but the idea is clear I guess.
        //return all typenumbers and their corresponding preflabels
        QuerySolutionMap parameters = new QuerySolutionMap();
        Literal searchString = ResourceFactory.createPlainLiteral("PMN");
        parameters.add("searchstring", searchString);
        String sQuery = IOUtils.toString(readClassPathResource("sparql/query4.sparql"), "UTF-8");
        ParameterizedSparqlString parQuery = new ParameterizedSparqlString(sQuery, parameters);
        Query query = parQuery.asQuery();
        Model model = readModelFromInputStream("data/basictypes-sample.nt", "basictypes", ModelLanguage.NTRIPLE);
        QueryExecution qexec = QueryExecutionFactory.create(query, model);
        ResultSet results = qexec.execSelect();
        ResultSetFormatter.out(System.out, results, query);
        qexec.close();

***********************************************************

Robby
-----Original Message-----
From: Robby Pelssers [mailto:Robby.Pelssers@nxp.com] 
Sent: Thursday, August 16, 2012 11:19 AM
To: users@jena.apache.org
Subject: how to use ARQ api to parametrize following query

Hi all,

I have following query which executes just fine.  But I'm interested to find out what is best practice to parametrize this query so that the "BUK" part  can be injected.


*         How would I need to alter this query?

*         How would the below code snippet need to be altered?

       String query = "....some query..."
       Query query = QueryFactory.create(sQuery);
       QueryExecution qexec = QueryExecutionFactory.create(query, model);
       ResultSet results = qexec.execSelect();


#Query showcasing how to select typenumber and preflabel for all products
#for which typenumber starts with BUK
PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
PREFIX skos:  <http://www.w3.org/2004/02/skos/core#>
SELECT ?typeNumber ?prefLabel
WHERE
{
  ?x nxp:typeNumber ?typeNumber;
     skos:prefLabel ?prefLabel .
     FILTER(STRSTARTS(STR(?typeNumber), "BUK"))
}

Thx in advance,
Robby