On 19/12/13 12:29, Dibyanshu Jaiswal wrote:
Thanks..Its Working!!
for the query
SELECT (NS:MYFunc(3.5) AS ?ThreeSquared) { }

To my observation say suppose I use the MYFunc in a FILTER construct then
it is invoked
several times for each record obtained by the SPO triple pattern specified
in the WHERE clause.

Yes - a FILTER is applied to every row in the results it applies to.

I want to use it in a query like as follows:
SELECT ?Names WHERE {?Name rdf:type NS:SomeClass. FILTER(NS:MYFunc(?Names))}

So in This case for each instance of NS:SomeClass, MYFunc will be invoked.

Another thing that would like to confirm is:
1. What will be the return type of MYFunc

Your choice.

The Java type must be NodeValue but a NodeValue can be a string, IRI, bNode , number, date, etc etc - or even your own literal datatype.

There are rules for whether a value is "true" or "false" on a FILTER -

http://www.w3.org/TR/sparql11-query/#ebv

For e.g. If I want to fetch some information regarding to the Instance Name
passed to MYFunc and the Information i want to return is some desciption of
that Instance in say String format.

I am not that much familiar with SPARQL syntax but as mentioned above, i
want MYFunc to return some Information which is returned in the SPARQL
query Result.

Either in the SEELCt clause as you have currently or

BIND(NS:MYFunc(?Names) AS ?MyVarName)

which is an assignment and can go in a pattern.  See the spec for details.

        Andy




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

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