On 19/12/13 10:10, Dibyanshu Jaiswal wrote:
 From the reply what I can figure out is: I need to perform 2 steps.
1. To register the function class with a given URI using -
FunctionRegistry.get().put(String uri, Class<?> funcClass)

2. To make a new Class for that function which extends FunctionBase1 or say
implements  class Function. Where I need to write my function definition in
MyFunction.exec(NodeValue v){}

After which I can use my defined function like any other inbuilt ARQ
function.
Am I right?

Yes.

        Andy



On Thu, Dec 19, 2013 at 3:07 PM, Andy Seaborne <[email protected]> wrote:

On 19/12/13 05:30, Dibyanshu Jaiswal wrote:

Hi all!

I want to define my own functions which i would like to use via a SPARQL
query. For E.g.

   SELECT (<http://example/square>(3) AS ?ThreeSquared) { }

1. SELECT (<myFunctionURI>(parameter) AS ?x){}
OR
2. SELECT ?names WHERE { ?names rdf:type NS:ClassA.
FILTER(NS:myFunction(?names)}

   Is that possible??

The corresponding effort made by me is as follows:
The following package :
com.hp.hpl.jena.sparql.function.user<http://jena.
apache.org/documentation/javadoc/arq/com/hp/hpl/jena/
sparql/function/user/package-summary.html>and
its class
UserDefinedFunctionFactory<http://jena.apache.org/
documentation/javadoc/arq/com/hp/hpl/jena/sparql/function/user/
UserDefinedFunctionFactory.html>gives

a brief description of defining our own functions which supports as
shown in Example 1 above. But I need something like Example 2 shown above.
Having browsing Jena-core and ARQ Javadocs, I can find classes like
FunctionFactory, FunctionRegistry, FunctionBase etc, by which I see some
hope of what I want. Can I achieve this? Or such function are limited to
the ARQ engine and cannot be expanded?

To Breif: I want to define a fuction, register it with a URI and hence use
it via SPARQL Query Engine as NS:myFunction() in FILTER and etc constructs
of a SPARQL query.



Yes - you can do that.  ARQ, internally, uses the public mechanism to add
all the additional functions that are not keywords in the langauge.

UserDefinedFunctions are, in fact, macros for expressions.

Completely new functionality is added by implemenign

Take FN_Abs (which is fn:abs or <http://www.w3.org/2005/xpath-
functions#abs -- the form of the URI does not matter as the compiler
expands them all during parsing.

The same functions act in filters and SELECT expressions.  Your examples 1
and 2 are using the same mechanism.

See
com.hp.hpl.jena.sparql.function.StandardFunctions

The core function is:

FunctionRegistry.get().put(String uri, Class<?> funcClass)

or specifically

registry.put(
     "http://www.w3.org/2005/xpath-functions#abs";,
      FN_Abs.class)


and the implementation is

public class FN_Abs extends FunctionBase1
{
     public FN_Abs() { super() ; }

     @Override
     public NodeValue exec(NodeValue v)
     { return XSDFuncOp.abs(v) ; }
}

Your code must implement interface Function.

There are helper classes for the cases of one, two, three and four
arguments.  Functions, in general, can be variable numbers of arguments.

FunctionRegistries can be per query execution - I've shown using the
global one which is usually all you need.

         Andy








Reply via email to