You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Nikolaos Beredimas <be...@gmail.com> on 2017/03/13 20:07:59 UTC

Overloading SPARQL operators for User defined non-XSD data types

In reference to the Rational datatype example in 'User defined non-XSD data
types'
at the bottom of the page at '
https://jena.apache.org/documentation/notes/typed-literals.html'


Is it possible, and if so how to do it,
to define/override suitable methods (which methods?) in the class
definition so that to overload SPARQL operators in jena's engine
for a user defined non-XSD datatype.

I am interested in both comparison (< > >= etc)
and arithmetic operators (+ - * /)

In reference to the given example the objective would be to do arithmetic
operations, for example the query :

SELECT ?result WHERE {
BIND ("3/5"^^urn:x-hp-dt:rational AS ?a) .
BIND ("4/7"^^urn:x-hp-dt:rational AS ?b) .
BIND (?a + ?b AS ?result) .
}

would return "41/35"^^urn:x-hp-dt:rational (or an equivalent representation
such as 82/70)

Also, for input data of the form:

<A> rdf:value "3/5"^^urn:x-hp-dt:rational .
<B> rdf:value "3/8"^^urn:x-hp-dt:rational .

the query

SELECT * WHERE {
?s rdf:value ?value .
FILTER (?value >= "1/2"^^urn:x-hp-dt:rational ) .
}

would return
?s    ?value
<A> "3/5"^^urn:x-hp-dt:rational


Finally, if the above is possible, how would one define operations and
results between different datatypes:
for example adding xsd:float with urn:x-hp-dt:rational
or comparing xsd:float with urn:x-hp-dt:rational

I am not actually interested in the specific Rational datatype use case,
but I figured since the example is there to use that as a reference.

Re: Overloading SPARQL operators for User defined non-XSD data types

Posted by Andy Seaborne <an...@apache.org>.
Nikolaos,

It's possible but it requires codebase changes. Having plug-in datatype 
for SPARQL was always a plan but it hasn't happened.  Adding oaal the 
functions and operators for XSD

 > https://jena.apache.org/documentation/notes/typed-literals.html

That is adding it to the type system of the API. That holds values and 
the application can manipulate the values in Java, so the application 
provides the manipulation code.


SPARQL requires operators.

The places that have to change are:

NodeValue and NodeValueOps

NodeValue is the carrier of terms for expressions and maps lexical forms 
to value representations.

NodeValue has the compare code, around NodeValue.compare.

NodeValueOps has the operations +, -, * and /.

The NodeValue code is quite old and clunky.  And some of what is needed 
for expression evaluation is just long and boring!

If your datatypes are a way of writing numbers down, you may be able to 
short-cut by mapping the value to one of the existing XSD types.

	Andy

On 13/03/17 20:07, Nikolaos Beredimas wrote:
> In reference to the Rational datatype example in 'User defined non-XSD data
> types'
> at the bottom of the page at '
> https://jena.apache.org/documentation/notes/typed-literals.html'
>
>
> Is it possible, and if so how to do it,
> to define/override suitable methods (which methods?) in the class
> definition so that to overload SPARQL operators in jena's engine
> for a user defined non-XSD datatype.
>
> I am interested in both comparison (< > >= etc)
> and arithmetic operators (+ - * /)
>
> In reference to the given example the objective would be to do arithmetic
> operations, for example the query :
>
> SELECT ?result WHERE {
> BIND ("3/5"^^urn:x-hp-dt:rational AS ?a) .
> BIND ("4/7"^^urn:x-hp-dt:rational AS ?b) .
> BIND (?a + ?b AS ?result) .
> }
>
> would return "41/35"^^urn:x-hp-dt:rational (or an equivalent representation
> such as 82/70)
>
> Also, for input data of the form:
>
> <A> rdf:value "3/5"^^urn:x-hp-dt:rational .
> <B> rdf:value "3/8"^^urn:x-hp-dt:rational .
>
> the query
>
> SELECT * WHERE {
> ?s rdf:value ?value .
> FILTER (?value >= "1/2"^^urn:x-hp-dt:rational ) .
> }
>
> would return
> ?s    ?value
> <A> "3/5"^^urn:x-hp-dt:rational
>
>
> Finally, if the above is possible, how would one define operations and
> results between different datatypes:
> for example adding xsd:float with urn:x-hp-dt:rational
> or comparing xsd:float with urn:x-hp-dt:rational
>
> I am not actually interested in the specific Rational datatype use case,
> but I figured since the example is there to use that as a reference.
>