Class UserDefinedFunctionFactory
- All Implemented Interfaces:
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 macros/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 its 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.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a functionvoid
Adds a functionvoid
clear()
Clears all function definitionsCreates a function for the given URIGets the definition of the function (if registered)static UserDefinedFunctionFactory
Gets the static instance of the factoryboolean
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 registeredvoid
Removes a function definitionvoid
setPreserveDependencies
(boolean allow) Sets whether user functions may explicitly depend on each other, seegetPreserveDependencies()
for explanation of this behavior
-
Method Details
-
getFactory
Gets the static instance of the factory- Returns:
- Function Factory
-
getPreserveDependencies
public boolean getPreserveDependencies()Gets whether user defined functions may preserve dependencies on each other (default false)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 in which case this option may be disabled with the corresponding
setPreserveDependencies(boolean)
setter- Returns:
- Whether explicit dependencies are allowed
-
setPreserveDependencies
public void setPreserveDependencies(boolean allow) Sets whether user functions may explicitly depend on each other, seegetPreserveDependencies()
for explanation of this behavior- Parameters:
allow
- Whether to preserve dependencies
-
create
Creates a function for the given URI- Specified by:
create
in interfaceFunctionFactory
- Parameters:
uri
- URI- Returns:
- Function
- Throws:
SSE_ExprBuildException
- Thrown if the given URI is not a known function
-
add
Adds a function- Parameters:
uri
- URIe
- Expressionargs
- Arguments
-
add
Adds a functionThis 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
- Parameters:
uri
- URIexpr
- Expression String (in SPARQL syntax)args
- Arguments- Throws:
QueryParseException
- Thrown if the expression string is not valid syntax
-
remove
Removes a function definition- Parameters:
uri
- URI- Throws:
NoSuchElementException
- Thrown if a function with the given URI does not exist
-
get
Gets the definition of the function (if registered)- Parameters:
uri
- URI- Returns:
- Function Definition if registered, null otherwise
-
isRegistered
Gets whether a function with the given URI has been registered- Parameters:
uri
- URI- Returns:
- True if registered, false otherwise
-
clear
public void clear()Clears all function definitions
-