Re: time arithmatic in SPARQL

Andy,

Thanks so much for trying to elucidate sparql for me. Your response was 
edifying in many ways: clarifying the use of date vs dateTime (which I 
knew), making slightly more clear how UNION works and showing how FILTER 
works with 'rows' (which I didn't know). Unfortunately, your response 
raised more questions for me than it answered. To help me try and clarify 
my problem, I created the following test cases in RDF:

<rdf:RDF  xmlns:ex="http://temp.example.org/terms/"
     xmlns:loc="http://simile.mit.edu/2005/05/ontologies/location#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

 	<ex:Event rdf:about="http://temp.example.org/terms/Event#case0">
 		<ex:date 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2007-12-31</ex:date>
 		<loc:place 
rdf:resource="http://temp.example.org/terms/Place#case0_place" />
 	</ex:Event>
 	<ex:Event rdf:about="http://temp.example.org/terms/Event#case1">
 		<ex:date 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-06</ex:date>
 		<loc:place 
rdf:resource="http://temp.example.org/terms/Place#case1_place" />
 	</ex:Event>
 	<ex:Event rdf:about="http://temp.example.org/terms/Event#case2">
 		<ex:starts 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-04</ex:starts>
 		<ex:finishes 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-05</ex:finishes>
 		<loc:place 
rdf:resource="http://temp.example.org/terms/Place#case2_place" />
 	</ex:Event>
 	<ex:Event rdf:about="http://temp.example.org/terms/Event#case3">
 		<ex:starts 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-07</ex:starts>
 		<ex:finishes 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-08</ex:finishes>
 		<loc:place 
rdf:resource="http://temp.example.org/terms/Place#case4_place" />
 	</ex:Event>
 	<ex:Event rdf:about="http://temp.example.org/terms/Event#case4">
 		<ex:starts 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-02</ex:starts>
 		<ex:finishes 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-03</ex:finishes>
 		<loc:place 
rdf:resource="http://temp.example.org/terms/Place#case3_place" />
 	</ex:Event>
 	<ex:Event rdf:about="http://temp.example.org/terms/Event#case5">
 		<ex:starts 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-09</ex:starts>
 		<ex:finishes 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-10</ex:finishes>
 		<loc:place 
rdf:resource="http://temp.example.org/terms/Place#case5_place" />
 	</ex:Event>
 	<ex:Event rdf:about="http://temp.example.org/terms/Event#case6">
 		<ex:starts 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-01</ex:starts>
 		<ex:finishes 
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-11</ex:finishes>
 		<loc:place 
rdf:resource="http://temp.example.org/terms/Place#case6_place" />
 	</ex:Event>

</rdf:RDF>

With this as the graph, I tried the following query and discovered that 
the >= and <= operators do not work as I expected they would

PREFIX ex: <http://temp.example.org/terms/>
PREFIX loc: <http://simile.mit.edu/2005/05/ontologies/location#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT *
WHERE {
 	{?event ex:date ?date .
 	FILTER (xsd:date(?date) >= xsd:date("2008-01-01") && 
xsd:date(?date) <= xsd:date("2008-01-11"))}
 	UNION {?event ex:starts ?start;
 			ex:finishes ?end.
 			FILTER (xsd:date(?start) >= xsd:date("2008-01-02") 
&& xsd:date(?end) <= xsd:date("2008-01-10"))}
}
ORDER BY ?event

This query returned cases 1, 2, and 3 where I expected it to return cases 
1-5.

If I do instead:

SELECT *
WHERE {
 	{?event ex:date ?date .
 	FILTER (xsd:date(?date) >= xsd:date("2008-01-01") && 
xsd:date(?date) <= xsd:date("2008-01-11"))}
 	UNION {?event ex:starts ?start;
 			ex:finishes ?end.
 			FILTER (xsd:date(?start) >= xsd:date("2008-01-01") 
&& xsd:date(?end) <= xsd:date("2008-01-11"))}
}

I get cases 1-5

Similarly I have to modify the first filter clause thus:
FILTER (xsd:date(?date) >= xsd:date("2007-12-30") && xsd:date(?date) <= 
xsd:date("2008-01-11"))
in order to include case 0 in the result.

Also, I haven't even touched on the question of how to retrieve case 6.
Idealy, I'm looking for a query that returns cases 1-6.

I thank you again for trying to enlighten the dense, and appeal to you 
again for further illumination.

Jon

Received on Thursday, 9 October 2008 22:58:01 UTC