You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Guilherme Maranhão <gu...@gmail.com> on 2014/10/29 01:15:45 UTC

Executing SPARQL BIND clause with Jena

Hi,

I need to use a BIND clause in my SPARQL query to be executed with Jena API.

The problem is that it seems Jena is ignoring the BIND clauses. Does it
support it?
I have seen that there is a possibility to construct the Jena Query object
with a QuerySolutionMap to put my bindind clauses. The problem with this
solution is that I won't be able to create this object in my code. I need
to have the SPARQL String ready to be executed with the BINDs clause.

The SPARQL query is this:

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX actor: <http://linkserver.icmc.usp.br/ckonto/actor#>
ask
where
{
 ?y actor:hasBirthday ?some_date .
 bind(xsd:dateTime(now()) as ?current_date) .
bind((year(?current_date)-year(?some_date)) as ?age) .
filter (?age >= "4"^^<http://www.w3.org/2001/XMLSchema#int>) .
}

The Query is being executed as follows:
Query query = QueryFactory.create(sparql);
QueryExecution qe = QueryExecutionFactory.create(query, model);
qe.execAsk();
...

So, the point is that it returns FALSE. However, if I execute this query on
Protege, it returns TRUE.

What am I missing?
Does Jena support BIND in the SPARQL query?
Is there a way to query for age without BIND clause?

Thanks a lot,

Guilherme

Re: Executing SPARQL BIND clause with Jena

Posted by Andy Seaborne <an...@apache.org>.
On 29/10/14 10:23, Guilherme Maranhão wrote:
> Thanks Andy,
>
> ?some_date is the instance's birthday that is already informed on the model
> and it is a xsd:date. Do you think it will be a problem when comparing it
> with year(?current_date), that is xsd:dateTime?

Aside:
That's not what you code does.
year() applies to xsd:date.  year returns an integer. After year applied 
twice, the arithmetic is on integers.

>
> I'll try changing the ?age datatype.
>
> Thanks a lot,

A small sample of data to produce a complete, minimal example would be 
helpful.

Also, version information would be useful.

	Andy

>
> Guilherme
>
> On Wed, Oct 29, 2014 at 7:15 AM, Andy Seaborne <an...@apache.org> wrote:
>
>> On 29/10/14 00:15, Guilherme Maranhão wrote:
>>
>>> Hi,
>>>
>>> I need to use a BIND clause in my SPARQL query to be executed with Jena
>>> API.
>>>
>>> The problem is that it seems Jena is ignoring the BIND clauses. Does it
>>> support it?
>>> I have seen that there is a possibility to construct the Jena Query object
>>> with a QuerySolutionMap to put my bindind clauses. The problem with this
>>> solution is that I won't be able to create this object in my code. I need
>>> to have the SPARQL String ready to be executed with the BINDs clause.
>>>
>>> The SPARQL query is this:
>>>
>>> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
>>> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>>> PREFIX actor: <http://linkserver.icmc.usp.br/ckonto/actor#>
>>> ask
>>> where
>>> {
>>>    ?y actor:hasBirthday ?some_date .
>>>    bind(xsd:dateTime(now()) as ?current_date) .
>>>
>>
>> now() is an xsd:dateTime, no need to cast.
>>
>>
>>   bind((year(?current_date)-year(?some_date)) as ?age) .
>>>
>>
>> 1/ If ?some_date is illformed in someway, year(?some_date) is an error and
>> then ?age is unbound.
>>
>> For example, if ?some_date is a string.
>>
>> 2/ That does not calculate someone's age in years!
>>
>> Suppose A on 2014-12-31 and B 2015-01-01
>> then year(B) - year(A) is 1 but A and B are one day apart.
>>
>>
>>   filter (?age >= "4"^^<http://www.w3.org/2001/XMLSchema#int>) .
>>> }
>>>
>>
>> Try
>> A/ Make sure ?age is an xsd:dateTime
>> then
>> B/
>> FILTER(now()-?age > "P4Y"^^xsd:duration)
>>
>>>
>>> The Query is being executed as follows:
>>> Query query = QueryFactory.create(sparql);
>>> QueryExecution qe = QueryExecutionFactory.create(query, model);
>>> qe.execAsk();
>>> ...
>>>
>>> So, the point is that it returns FALSE. However, if I execute this query
>>> on
>>> Protege, it returns TRUE.
>>>
>>> What am I missing?
>>>
>>
>> Inference?
>>
>>   Does Jena support BIND in the SPARQL query?
>>>
>>
>> Yes
>>
>>   Is there a way to query for age without BIND clause?
>>>
>>
>> Yes (unrelated point though)
>>
>>
>>> Thanks a lot,
>>>
>>> Guilherme
>>>
>>>
>>
>


Re: Executing SPARQL BIND clause with Jena

Posted by Guilherme Maranhão <gu...@gmail.com>.
Thanks Andy,

?some_date is the instance's birthday that is already informed on the model
and it is a xsd:date. Do you think it will be a problem when comparing it
with year(?current_date), that is xsd:dateTime?

I'll try changing the ?age datatype.

Thanks a lot,

Guilherme

On Wed, Oct 29, 2014 at 7:15 AM, Andy Seaborne <an...@apache.org> wrote:

> On 29/10/14 00:15, Guilherme Maranhão wrote:
>
>> Hi,
>>
>> I need to use a BIND clause in my SPARQL query to be executed with Jena
>> API.
>>
>> The problem is that it seems Jena is ignoring the BIND clauses. Does it
>> support it?
>> I have seen that there is a possibility to construct the Jena Query object
>> with a QuerySolutionMap to put my bindind clauses. The problem with this
>> solution is that I won't be able to create this object in my code. I need
>> to have the SPARQL String ready to be executed with the BINDs clause.
>>
>> The SPARQL query is this:
>>
>> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
>> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>> PREFIX actor: <http://linkserver.icmc.usp.br/ckonto/actor#>
>> ask
>> where
>> {
>>   ?y actor:hasBirthday ?some_date .
>>   bind(xsd:dateTime(now()) as ?current_date) .
>>
>
> now() is an xsd:dateTime, no need to cast.
>
>
>  bind((year(?current_date)-year(?some_date)) as ?age) .
>>
>
> 1/ If ?some_date is illformed in someway, year(?some_date) is an error and
> then ?age is unbound.
>
> For example, if ?some_date is a string.
>
> 2/ That does not calculate someone's age in years!
>
> Suppose A on 2014-12-31 and B 2015-01-01
> then year(B) - year(A) is 1 but A and B are one day apart.
>
>
>  filter (?age >= "4"^^<http://www.w3.org/2001/XMLSchema#int>) .
>> }
>>
>
> Try
> A/ Make sure ?age is an xsd:dateTime
> then
> B/
> FILTER(now()-?age > "P4Y"^^xsd:duration)
>
>>
>> The Query is being executed as follows:
>> Query query = QueryFactory.create(sparql);
>> QueryExecution qe = QueryExecutionFactory.create(query, model);
>> qe.execAsk();
>> ...
>>
>> So, the point is that it returns FALSE. However, if I execute this query
>> on
>> Protege, it returns TRUE.
>>
>> What am I missing?
>>
>
> Inference?
>
>  Does Jena support BIND in the SPARQL query?
>>
>
> Yes
>
>  Is there a way to query for age without BIND clause?
>>
>
> Yes (unrelated point though)
>
>
>> Thanks a lot,
>>
>> Guilherme
>>
>>
>

Re: Executing SPARQL BIND clause with Jena

Posted by Andy Seaborne <an...@apache.org>.
On 29/10/14 00:15, Guilherme Maranhão wrote:
> Hi,
>
> I need to use a BIND clause in my SPARQL query to be executed with Jena API.
>
> The problem is that it seems Jena is ignoring the BIND clauses. Does it
> support it?
> I have seen that there is a possibility to construct the Jena Query object
> with a QuerySolutionMap to put my bindind clauses. The problem with this
> solution is that I won't be able to create this object in my code. I need
> to have the SPARQL String ready to be executed with the BINDs clause.
>
> The SPARQL query is this:
>
> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
> PREFIX actor: <http://linkserver.icmc.usp.br/ckonto/actor#>
> ask
> where
> {
>   ?y actor:hasBirthday ?some_date .
>   bind(xsd:dateTime(now()) as ?current_date) .

now() is an xsd:dateTime, no need to cast.


> bind((year(?current_date)-year(?some_date)) as ?age) .

1/ If ?some_date is illformed in someway, year(?some_date) is an error 
and then ?age is unbound.

For example, if ?some_date is a string.

2/ That does not calculate someone's age in years!

Suppose A on 2014-12-31 and B 2015-01-01
then year(B) - year(A) is 1 but A and B are one day apart.


> filter (?age >= "4"^^<http://www.w3.org/2001/XMLSchema#int>) .
> }

Try
A/ Make sure ?age is an xsd:dateTime
then
B/
FILTER(now()-?age > "P4Y"^^xsd:duration)
>
> The Query is being executed as follows:
> Query query = QueryFactory.create(sparql);
> QueryExecution qe = QueryExecutionFactory.create(query, model);
> qe.execAsk();
> ...
>
> So, the point is that it returns FALSE. However, if I execute this query on
> Protege, it returns TRUE.
>
> What am I missing?

Inference?

> Does Jena support BIND in the SPARQL query?

Yes

> Is there a way to query for age without BIND clause?

Yes (unrelated point though)

>
> Thanks a lot,
>
> Guilherme
>