Hi Andy
Andy Seaborne wrote:
> On 15/12/11 17:25, Paolo Castagna wrote:
>> Hi Andy,
>> thanks for the quick reply.
>>
>> Andy Seaborne wrote:
>>> ARQ has it's own evaluation engine. Currently, it needs code changes to
>>> extend it. It covers most of XSD already.
>>
>> I had a look into NodeValue.java [1], would it be possible to do
>> something
>> along these lines?
>
> See NodeValue.compareAlways (which has the SPARQL rules for ORDER BY
> which are more than just "<")
Indeed! :-)
>
>>
>> Index: src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java
>> ===================================================================
>> --- src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java
>> (revision 1214849)
>> +++ src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java
>> (working copy)
>> @@ -1036,6 +1036,14 @@
>> boolean b = ((Boolean)lit.getValue()).booleanValue() ;
>> return new NodeValueBoolean(b, node) ;
>> }
>> +
>> +
>> + Object clazz = lit.getDatatype().getJavaClass();
>> + if ( clazz.getClass().isInstance(Double.class) )
>> + {
>> + double d = ((Number)lit.getValue()).doubleValue() ;
>> + return new NodeValueDouble(d, node) ;
>> + }
>
> That makes it a double, a number, no units.
> (aside from the fact doubles are NOT numbers in XSD - use xsd:decimal -
> their value space is "m × 2^e" i.e. (m,e) pairs, for fixed length m and
> e extended with NaN, Inf, -Inf and a lot of machinery for comparison and
> addition etc for rounding and mapping ).
>
> Saying a temperature has a value which is a number is wrong. Different
> value spaces.
>
> Can you compare the height of a mountain with a temperature? Ones in
> meters, the other in Kelvin.
>
>>
>> // If wired into the TypeMapper via
>> RomanNumeralDatatype.enableAsFirstClassDatatype
>> // if ( RomanNumeralDatatype.get().isValidLiteral(lit) )
>
> Note that RomanNumeralDatatype are another way to write integers. Same
> value space.
>
> Creating an ordering function that returns a number (no units) means
> comparison is defined.
I got an example of what I was trying to achieve (although I am not sure
the implementation is the best way to do it... in particular I think the
parse() method is still 'wrong' because it returns a Double).
Code here:
https://github.com/castagna/jena-examples/blob/master/src/main/java/org/apache/jena/examples/ExampleDT_01.java
https://github.com/castagna/jena-examples/blob/master/src/main/java/org/apache/jena/examples/temperature.java
Output:
---- Data ----
<x4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#value>
"25"^^<http://jena.apache.org/datatypes/temperature/kelvin> .
<x2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#value>
"15"^^<http://jena.apache.org/datatypes/temperature/celsius> .
<x5> <http://www.w3.org/1999/02/22-rdf-syntax-ns#value>
"25"^^<http://jena.apache.org/datatypes/temperature/rankine> .
<x3> <http://www.w3.org/1999/02/22-rdf-syntax-ns#value>
"25"^^<http://jena.apache.org/datatypes/temperature/fahrenheit> .
<x1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#value>
"25"^^<http://jena.apache.org/datatypes/temperature/celsius> .
---- Query ----
PREFIX java: <java:org.apache.jena.examples.>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT * WHERE {
?s rdf:value ?temperature .
}
ORDER BY java:temperature( ?temperature )
---- Results ----
--------------------------------------------------------------------------
| s | temperature |
==========================================================================
| <x5> | "25"^^<http://jena.apache.org/datatypes/temperature/rankine> |
| <x4> | "25"^^<http://jena.apache.org/datatypes/temperature/kelvin> |
| <x3> | "25"^^<http://jena.apache.org/datatypes/temperature/fahrenheit> |
| <x2> | "15"^^<http://jena.apache.org/datatypes/temperature/celsius> |
| <x1> | "25"^^<http://jena.apache.org/datatypes/temperature/celsius> |
--------------------------------------------------------------------------
Thanks.
Paolo
>
>> [1]
>> https://svn.apache.org/repos/asf/incubator/jena/Jena2/ARQ/trunk/src/main/java/com/hp/hpl/jena/sparql/expr/NodeValue.java
>>