public class UserDefinedFunctionFactory extends Object implements FunctionFactory
User defined functions provide a simple mechanism for a user to inject custom functions into SPARQL processing without the need to write any code. These functions essentially act as aliases for another SPARQL expression and serve as a means to aid users in simplifying their SPARQL queries.
For example we can define a square function like so:
List<Var> args = new ArrayList<Var>(Var.alloc("x")); UserDefinedFunctionFactory.getFactory().add("http://example/square", "?x * ?x", args);
We can then use this in queries like so:
SELECT (<http://example/square>(3) AS ?ThreeSquared) { }
Internally the call to the square function is translated into it's equivalent SPARQL expression and executed in that form.
User defined functions may rely on each other but this has some risks, therefore the default behaviour is to not preserve these dependencies
but rather to expand the function definitions to give the resulting expression associated with a function. Please see getPreserveDependencies()
for more information on this.
Modifier and Type | Method and Description |
---|---|
void |
add(String uri,
Expr e,
List<Var> args)
Adds a function
|
void |
add(String uri,
String expr,
List<Var> args)
Adds a function
|
void |
clear()
Clears all function definitions
|
Function |
create(String uri)
Creates a function for the given URI
|
UserDefinedFunctionDefinition |
get(String uri)
Gets the definition of the function (if registered)
|
static UserDefinedFunctionFactory |
getFactory()
Gets the static instance of the factory
|
boolean |
getPreserveDependencies()
Gets whether user defined functions may preserve dependencies on each other (default false)
|
boolean |
isRegistered(String uri)
Gets whether a function with the given URI has been registered
|
void |
remove(String uri)
Removes a function definition
|
void |
setPreserveDependencies(boolean allow)
Sets whether user functions may explicitly depend on each other, see
getPreserveDependencies() for explanation of this behavior |
public static UserDefinedFunctionFactory getFactory()
public boolean getPreserveDependencies()
When this is disabled (as it is by default) function definitions are fully expanded at registration time. So if you add a function that references an existing user defined function it will be expanded to include the resulting expression rather than left with a reference to another function. This protects the user from depending on other functions whose definitions are later removed or changed.
However it may sometimes be desirable to have dependencies preserved which case this option may be
disabled with the corresponding setPreserveDependencies(boolean)
setter
public void setPreserveDependencies(boolean allow)
getPreserveDependencies()
for explanation of this behaviorallow
- Whether to preserve dependenciespublic Function create(String uri)
create
in interface FunctionFactory
uri
- URIExprBuildException
- Thrown if the given URI is not a known functionpublic void add(String uri, Expr e, List<Var> args)
uri
- URIe
- Expressionargs
- Argumentspublic void add(String uri, String expr, List<Var> args) throws ParseException
This method will build the expression to use based on the expression string given, strings must match the SPARQL expression syntax e.g.
(?x * ?y) + 5
uri
- URIexpr
- Expression String (in SPARQL syntax)args
- ArgumentsParseException
- Thrown if the expression string is not valid syntaxpublic void remove(String uri)
uri
- URINoSuchElementException
- Thrown if a function with the given URI does not existpublic UserDefinedFunctionDefinition get(String uri)
uri
- URIpublic boolean isRegistered(String uri)
uri
- URIpublic void clear()
Licenced under the Apache License, Version 2.0