Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Writing Jena ARQ functions



By default the list of functions allowed in the BIND or FILTER expressions is limited to the list specified in the SPARQL standard.

However Jena allows to specify our own functions.

Implement a function

Any function element used in a SPARQL query must implement the Jena Function interface. This function will be created by the factory by the FunctionFactory.create(String) method.

It is not necessary to implement directly the Function interface. It is rather better to extend one of the following abstract classes: The only abstract method to implement is the exec(...) method for each class:
  • The arguments of the method are in each case the list of arguments in the called function. Each argument is a NodeValue which represent the associated expression result in the SPARQL query
  • The result of the method is also a NodeValue which should be the result of the function expression
The NodeValue class has many static methods allowing to:
  • Convert a NodeValue to the associated Object underlying this node
  • Create a NodeValue from an Object

Example

The following function uses its two arguments to compute the oppposite length of a triangle using the hypothenuse and the angle:
      public class oppositeFunction extends FunctionBase2 {

        public oppositeFunction() {
          super();
        }

        public NodeValue exec(NodeValue hypothenusenv, NodeValue anglenv) {
          float hypothenuse = hypothenusenv.getFloat();
          float angle = anglenv.getFloat();
          float opposite = (float)(hypothenuse * Math.sin(angle));
          return NodeValue.makeFloat(opposite);
        }
      }

See also


Categories: builtin-applis

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence