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 16:28:46 UTC

how to parameterize a date in a parametrized query?

Hi all,

Still having a bit of difficulty getting the sparql query to use a xs:date instead of xs:dateTime

In RDF we defined a productStatusDate  as follows:
<nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-28</nxp:productStatusDate>


I have following query and from Java  I want to filter out all products having a specific productStatusDate

#Query showcasing how to select typenumber and preflabel for all products
#having a specific productStatusDate
PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
SELECT ?typeNumber
WHERE
{
  ?x nxp:typeNumber ?typeNumber;
       nxp:productStatusDate ?productStatusDate .
}


I only noticed a setLiteral for Calendar but that does not seem to get the job done:

        ParameterizedSparqlString parQuery = new ParameterizedSparqlString(sQuery);
        Calendar calendar = Calendar.getInstance();
        calendar.set(2011, 10, 28, 0, 0, 0);    //2011-10-28
        parQuery.setLiteral("productStatusDate", calendar);
        Query query = parQuery.asQuery();
        System.out.println(query.toString());

I printed the query which looks like this:

PREFIX  nxp:  <http://purl.org/nxp/schema/v1/>

SELECT  ?typeNumber
WHERE
  { ?x nxp:typeNumber ?typeNumber .
    ?x nxp:productStatusDate "2011-11-27T23:00:00.663Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>
  }


How can I switch to using date ??

Thx in advance,
Robby

RE: how to parameterize a date in a parametrized query?

Posted by Robby Pelssers <Ro...@nxp.com>.
Interesting... I will bookmark this one for now and check it out next week.

Robby

-----Original Message-----
From: Martynas Jusevičius [mailto:martynas@graphity.org] 
Sent: Friday, August 17, 2012 11:11 AM
To: users@jena.apache.org
Subject: Re: how to parameterize a date in a parametrized query?

Robby,

SPIN API [SPIN] offers SPARQL serialization into RDF, so you can
construct (and parametrize) SPARQL queries using Jena API.
I've created a convenience builder class to make this task easier:
https://github.com/Graphity/graphity-ldp/blob/master/src/main/java/org/graphity/util/QueryBuilder.java

This is how you can build a Jena Query with it:

  QueryBuilder.fromDescribe().subQuery(QueryBuilder.fromResource(spin).limit(limit).offset(offset).orderBy(orderBy,
desc)).build()

Martynas
graphity.org

On Fri, Aug 17, 2012 at 12:03 PM, Robby Pelssers <Ro...@nxp.com> wrote:
> Hi Andy,
>
> Thx for the elaboration.  Just wondering how others are using the ARQ api.  What I want to accomplish in the end is to write a dynamic Apache Cocoon generator that takes a SPARQL query as input and builds a Map from either (all request parameters / sitemap parameters) and injects that map as a QuerySolution (binding parameters from the query). If I get this working I can do some pretty slick stuff ;-)
>
> Robby
>
>
>
> -----Original Message-----
> From: Andy Seaborne [mailto:andy.seaborne.apache@gmail.com] On Behalf Of Andy Seaborne
> Sent: Thursday, August 16, 2012 5:01 PM
> To: users@jena.apache.org
> Subject: Re: how to parameterize a date in a parametrized query?
>
> The problem is Java's idea of "date" or "Calendar" is not an xsd:date -
> it's an xsd:dateTime.
>
> Hence when converted to Java, you get a dateTime.
>
> In practice, the java<->xsd mapping is a touch fraught for
> dates/calendars. It's a bit fragile for even numbers - whether to use
> xsd:int or xsd:integer when that may matter (in TDB it looses the
> distinction and is more value-based).
>
> For example, in your data there is no timezone.
>
> So it is safer to take control and make an RDF literal with carefully
> controlled XSD date.  Your solution is the right one.
>
> ARQ's helper code is in:
>
> com.hp.hpl.jena.sparql.util.Utils
>
> You'll see it does it's own SimpleDateFormat because the Jena datatype
> comes from Xerces and is value-based, which likes to make it all Z.  If
> the lexical form matters,
>
> See also
>
> javax.xml.datatype.XMLGregorianCalendar
>
> while there is no helper code for that (it's quite new), it is at least
> XSD-ish.
>
>         Andy
>
> On 16/08/12 15:42, Robby Pelssers wrote:
>> I noticed that this will get the job done but does that mean there is no shortcut to just inject a java.util.Date and handle it properly?
>>
>> parQuery.setLiteral("productStatusDate", "2012-01-03", XSDDatatype.XSDdate);
>>
>> Robby
>>
>> -----Original Message-----
>> From: Robby Pelssers [mailto:Robby.Pelssers@nxp.com]
>> Sent: Thursday, August 16, 2012 4:29 PM
>> To: users@jena.apache.org
>> Subject: how to parameterize a date in a parametrized query?
>>
>> Hi all,
>>
>> Still having a bit of difficulty getting the sparql query to use a xs:date instead of xs:dateTime
>>
>> In RDF we defined a productStatusDate  as follows:
>> <nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-28</nxp:productStatusDate>
>>
>>
>> I have following query and from Java  I want to filter out all products having a specific productStatusDate
>>
>> #Query showcasing how to select typenumber and preflabel for all products
>> #having a specific productStatusDate
>> PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
>> SELECT ?typeNumber
>> WHERE
>> {
>>    ?x nxp:typeNumber ?typeNumber;
>>         nxp:productStatusDate ?productStatusDate .
>> }
>>
>>
>> I only noticed a setLiteral for Calendar but that does not seem to get the job done:
>>
>>          ParameterizedSparqlString parQuery = new ParameterizedSparqlString(sQuery);
>>          Calendar calendar = Calendar.getInstance();
>>          calendar.set(2011, 10, 28, 0, 0, 0);    //2011-10-28
>>          parQuery.setLiteral("productStatusDate", calendar);
>>          Query query = parQuery.asQuery();
>>          System.out.println(query.toString());
>>
>> I printed the query which looks like this:
>>
>> PREFIX  nxp:  <http://purl.org/nxp/schema/v1/>
>>
>> SELECT  ?typeNumber
>> WHERE
>>    { ?x nxp:typeNumber ?typeNumber .
>>      ?x nxp:productStatusDate "2011-11-27T23:00:00.663Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>
>>    }
>>
>>
>> How can I switch to using date ??
>>
>> Thx in advance,
>> Robby
>>
>

Re: how to parameterize a date in a parametrized query?

Posted by Martynas Jusevičius <ma...@graphity.org>.
Robby,

SPIN API [SPIN] offers SPARQL serialization into RDF, so you can
construct (and parametrize) SPARQL queries using Jena API.
I've created a convenience builder class to make this task easier:
https://github.com/Graphity/graphity-ldp/blob/master/src/main/java/org/graphity/util/QueryBuilder.java

This is how you can build a Jena Query with it:

  QueryBuilder.fromDescribe().subQuery(QueryBuilder.fromResource(spin).limit(limit).offset(offset).orderBy(orderBy,
desc)).build()

Martynas
graphity.org

On Fri, Aug 17, 2012 at 12:03 PM, Robby Pelssers <Ro...@nxp.com> wrote:
> Hi Andy,
>
> Thx for the elaboration.  Just wondering how others are using the ARQ api.  What I want to accomplish in the end is to write a dynamic Apache Cocoon generator that takes a SPARQL query as input and builds a Map from either (all request parameters / sitemap parameters) and injects that map as a QuerySolution (binding parameters from the query). If I get this working I can do some pretty slick stuff ;-)
>
> Robby
>
>
>
> -----Original Message-----
> From: Andy Seaborne [mailto:andy.seaborne.apache@gmail.com] On Behalf Of Andy Seaborne
> Sent: Thursday, August 16, 2012 5:01 PM
> To: users@jena.apache.org
> Subject: Re: how to parameterize a date in a parametrized query?
>
> The problem is Java's idea of "date" or "Calendar" is not an xsd:date -
> it's an xsd:dateTime.
>
> Hence when converted to Java, you get a dateTime.
>
> In practice, the java<->xsd mapping is a touch fraught for
> dates/calendars. It's a bit fragile for even numbers - whether to use
> xsd:int or xsd:integer when that may matter (in TDB it looses the
> distinction and is more value-based).
>
> For example, in your data there is no timezone.
>
> So it is safer to take control and make an RDF literal with carefully
> controlled XSD date.  Your solution is the right one.
>
> ARQ's helper code is in:
>
> com.hp.hpl.jena.sparql.util.Utils
>
> You'll see it does it's own SimpleDateFormat because the Jena datatype
> comes from Xerces and is value-based, which likes to make it all Z.  If
> the lexical form matters,
>
> See also
>
> javax.xml.datatype.XMLGregorianCalendar
>
> while there is no helper code for that (it's quite new), it is at least
> XSD-ish.
>
>         Andy
>
> On 16/08/12 15:42, Robby Pelssers wrote:
>> I noticed that this will get the job done but does that mean there is no shortcut to just inject a java.util.Date and handle it properly?
>>
>> parQuery.setLiteral("productStatusDate", "2012-01-03", XSDDatatype.XSDdate);
>>
>> Robby
>>
>> -----Original Message-----
>> From: Robby Pelssers [mailto:Robby.Pelssers@nxp.com]
>> Sent: Thursday, August 16, 2012 4:29 PM
>> To: users@jena.apache.org
>> Subject: how to parameterize a date in a parametrized query?
>>
>> Hi all,
>>
>> Still having a bit of difficulty getting the sparql query to use a xs:date instead of xs:dateTime
>>
>> In RDF we defined a productStatusDate  as follows:
>> <nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-28</nxp:productStatusDate>
>>
>>
>> I have following query and from Java  I want to filter out all products having a specific productStatusDate
>>
>> #Query showcasing how to select typenumber and preflabel for all products
>> #having a specific productStatusDate
>> PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
>> SELECT ?typeNumber
>> WHERE
>> {
>>    ?x nxp:typeNumber ?typeNumber;
>>         nxp:productStatusDate ?productStatusDate .
>> }
>>
>>
>> I only noticed a setLiteral for Calendar but that does not seem to get the job done:
>>
>>          ParameterizedSparqlString parQuery = new ParameterizedSparqlString(sQuery);
>>          Calendar calendar = Calendar.getInstance();
>>          calendar.set(2011, 10, 28, 0, 0, 0);    //2011-10-28
>>          parQuery.setLiteral("productStatusDate", calendar);
>>          Query query = parQuery.asQuery();
>>          System.out.println(query.toString());
>>
>> I printed the query which looks like this:
>>
>> PREFIX  nxp:  <http://purl.org/nxp/schema/v1/>
>>
>> SELECT  ?typeNumber
>> WHERE
>>    { ?x nxp:typeNumber ?typeNumber .
>>      ?x nxp:productStatusDate "2011-11-27T23:00:00.663Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>
>>    }
>>
>>
>> How can I switch to using date ??
>>
>> Thx in advance,
>> Robby
>>
>

RE: how to parameterize a date in a parametrized query?

Posted by Robby Pelssers <Ro...@nxp.com>.
Hi Andy,

Thx for the elaboration.  Just wondering how others are using the ARQ api.  What I want to accomplish in the end is to write a dynamic Apache Cocoon generator that takes a SPARQL query as input and builds a Map from either (all request parameters / sitemap parameters) and injects that map as a QuerySolution (binding parameters from the query). If I get this working I can do some pretty slick stuff ;-)

Robby



-----Original Message-----
From: Andy Seaborne [mailto:andy.seaborne.apache@gmail.com] On Behalf Of Andy Seaborne
Sent: Thursday, August 16, 2012 5:01 PM
To: users@jena.apache.org
Subject: Re: how to parameterize a date in a parametrized query?

The problem is Java's idea of "date" or "Calendar" is not an xsd:date - 
it's an xsd:dateTime.

Hence when converted to Java, you get a dateTime.

In practice, the java<->xsd mapping is a touch fraught for 
dates/calendars. It's a bit fragile for even numbers - whether to use 
xsd:int or xsd:integer when that may matter (in TDB it looses the 
distinction and is more value-based).

For example, in your data there is no timezone.

So it is safer to take control and make an RDF literal with carefully 
controlled XSD date.  Your solution is the right one.

ARQ's helper code is in:

com.hp.hpl.jena.sparql.util.Utils

You'll see it does it's own SimpleDateFormat because the Jena datatype 
comes from Xerces and is value-based, which likes to make it all Z.  If 
the lexical form matters,

See also

javax.xml.datatype.XMLGregorianCalendar

while there is no helper code for that (it's quite new), it is at least 
XSD-ish.

	Andy

On 16/08/12 15:42, Robby Pelssers wrote:
> I noticed that this will get the job done but does that mean there is no shortcut to just inject a java.util.Date and handle it properly?
>
> parQuery.setLiteral("productStatusDate", "2012-01-03", XSDDatatype.XSDdate);
>
> Robby
>
> -----Original Message-----
> From: Robby Pelssers [mailto:Robby.Pelssers@nxp.com]
> Sent: Thursday, August 16, 2012 4:29 PM
> To: users@jena.apache.org
> Subject: how to parameterize a date in a parametrized query?
>
> Hi all,
>
> Still having a bit of difficulty getting the sparql query to use a xs:date instead of xs:dateTime
>
> In RDF we defined a productStatusDate  as follows:
> <nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-28</nxp:productStatusDate>
>
>
> I have following query and from Java  I want to filter out all products having a specific productStatusDate
>
> #Query showcasing how to select typenumber and preflabel for all products
> #having a specific productStatusDate
> PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
> SELECT ?typeNumber
> WHERE
> {
>    ?x nxp:typeNumber ?typeNumber;
>         nxp:productStatusDate ?productStatusDate .
> }
>
>
> I only noticed a setLiteral for Calendar but that does not seem to get the job done:
>
>          ParameterizedSparqlString parQuery = new ParameterizedSparqlString(sQuery);
>          Calendar calendar = Calendar.getInstance();
>          calendar.set(2011, 10, 28, 0, 0, 0);    //2011-10-28
>          parQuery.setLiteral("productStatusDate", calendar);
>          Query query = parQuery.asQuery();
>          System.out.println(query.toString());
>
> I printed the query which looks like this:
>
> PREFIX  nxp:  <http://purl.org/nxp/schema/v1/>
>
> SELECT  ?typeNumber
> WHERE
>    { ?x nxp:typeNumber ?typeNumber .
>      ?x nxp:productStatusDate "2011-11-27T23:00:00.663Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>
>    }
>
>
> How can I switch to using date ??
>
> Thx in advance,
> Robby
>


Re: how to parameterize a date in a parametrized query?

Posted by Andy Seaborne <an...@apache.org>.
The problem is Java's idea of "date" or "Calendar" is not an xsd:date - 
it's an xsd:dateTime.

Hence when converted to Java, you get a dateTime.

In practice, the java<->xsd mapping is a touch fraught for 
dates/calendars. It's a bit fragile for even numbers - whether to use 
xsd:int or xsd:integer when that may matter (in TDB it looses the 
distinction and is more value-based).

For example, in your data there is no timezone.

So it is safer to take control and make an RDF literal with carefully 
controlled XSD date.  Your solution is the right one.

ARQ's helper code is in:

com.hp.hpl.jena.sparql.util.Utils

You'll see it does it's own SimpleDateFormat because the Jena datatype 
comes from Xerces and is value-based, which likes to make it all Z.  If 
the lexical form matters,

See also

javax.xml.datatype.XMLGregorianCalendar

while there is no helper code for that (it's quite new), it is at least 
XSD-ish.

	Andy

On 16/08/12 15:42, Robby Pelssers wrote:
> I noticed that this will get the job done but does that mean there is no shortcut to just inject a java.util.Date and handle it properly?
>
> parQuery.setLiteral("productStatusDate", "2012-01-03", XSDDatatype.XSDdate);
>
> Robby
>
> -----Original Message-----
> From: Robby Pelssers [mailto:Robby.Pelssers@nxp.com]
> Sent: Thursday, August 16, 2012 4:29 PM
> To: users@jena.apache.org
> Subject: how to parameterize a date in a parametrized query?
>
> Hi all,
>
> Still having a bit of difficulty getting the sparql query to use a xs:date instead of xs:dateTime
>
> In RDF we defined a productStatusDate  as follows:
> <nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-28</nxp:productStatusDate>
>
>
> I have following query and from Java  I want to filter out all products having a specific productStatusDate
>
> #Query showcasing how to select typenumber and preflabel for all products
> #having a specific productStatusDate
> PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
> SELECT ?typeNumber
> WHERE
> {
>    ?x nxp:typeNumber ?typeNumber;
>         nxp:productStatusDate ?productStatusDate .
> }
>
>
> I only noticed a setLiteral for Calendar but that does not seem to get the job done:
>
>          ParameterizedSparqlString parQuery = new ParameterizedSparqlString(sQuery);
>          Calendar calendar = Calendar.getInstance();
>          calendar.set(2011, 10, 28, 0, 0, 0);    //2011-10-28
>          parQuery.setLiteral("productStatusDate", calendar);
>          Query query = parQuery.asQuery();
>          System.out.println(query.toString());
>
> I printed the query which looks like this:
>
> PREFIX  nxp:  <http://purl.org/nxp/schema/v1/>
>
> SELECT  ?typeNumber
> WHERE
>    { ?x nxp:typeNumber ?typeNumber .
>      ?x nxp:productStatusDate "2011-11-27T23:00:00.663Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>
>    }
>
>
> How can I switch to using date ??
>
> Thx in advance,
> Robby
>


RE: how to parameterize a date in a parametrized query?

Posted by Robby Pelssers <Ro...@nxp.com>.
I noticed that this will get the job done but does that mean there is no shortcut to just inject a java.util.Date and handle it properly?

parQuery.setLiteral("productStatusDate", "2012-01-03", XSDDatatype.XSDdate);

Robby

-----Original Message-----
From: Robby Pelssers [mailto:Robby.Pelssers@nxp.com] 
Sent: Thursday, August 16, 2012 4:29 PM
To: users@jena.apache.org
Subject: how to parameterize a date in a parametrized query?

Hi all,

Still having a bit of difficulty getting the sparql query to use a xs:date instead of xs:dateTime

In RDF we defined a productStatusDate  as follows:
<nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-28</nxp:productStatusDate>


I have following query and from Java  I want to filter out all products having a specific productStatusDate

#Query showcasing how to select typenumber and preflabel for all products
#having a specific productStatusDate
PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
SELECT ?typeNumber
WHERE
{
  ?x nxp:typeNumber ?typeNumber;
       nxp:productStatusDate ?productStatusDate .
}


I only noticed a setLiteral for Calendar but that does not seem to get the job done:

        ParameterizedSparqlString parQuery = new ParameterizedSparqlString(sQuery);
        Calendar calendar = Calendar.getInstance();
        calendar.set(2011, 10, 28, 0, 0, 0);    //2011-10-28
        parQuery.setLiteral("productStatusDate", calendar);
        Query query = parQuery.asQuery();
        System.out.println(query.toString());

I printed the query which looks like this:

PREFIX  nxp:  <http://purl.org/nxp/schema/v1/>

SELECT  ?typeNumber
WHERE
  { ?x nxp:typeNumber ?typeNumber .
    ?x nxp:productStatusDate "2011-11-27T23:00:00.663Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>
  }


How can I switch to using date ??

Thx in advance,
Robby