SPARQLMotion Example: UserDefinedFunction

This SPARQLMotion script demonstrates how to extend SPARQLMotion and SPARQL with new SPARQL functions. The resulting SPARQL function can be called in FILTER or LET clauses of SPARQL queries (both in TopBraid or in other SPARQLMotion scripts). The function can also be used like any other module in visual SPARQLMotion scripts.

Download Function Definition in N3 format (2008-06-24)

Download an example file that uses the function in N3 format (2008-06-24)

Note: Requires TopBraid Composer 2.6.0 or above

The example function's sms file adds a function udf:myFunction to the SPARQL functions registry. The new function can be used in SPARQL LET or FILTER clauses in any SPARQL query, as shown in the following screenshot. The function extracts the string between "(" and ")" characters and returns it.

SPARQL Query

To try out this example, download the two files above into your TopBraid Composer workspace and restart the tool. At start-up, TopBraid scans all files ending with .sms.* and checks whether they define any new SPARQL Functions (or Web Services). In this case, the sms file contains a new subclass of smf:StringFunctionModule (and thus sm:FunctionModule) called udf:myFunction.

SPARQL Function Class

According to the SPARQLMotion class definition patterns, the function class defines its input and output parameters using cardinality restrictions. Move your mouse over the icon next to the restriction and press the small + icon to open up the restrictions using nested forms. In addition to the actual cardinality (either cardinality 1 or maxCardinality 1 for optional values), the restriction should have an rdfs:comment that describes the what the argument/result does. The sm:outputVariable restriction must have a sm:defaultValue such as "result". Input parameters must have a value for sm:argumentIndex starting at 0 (first argument).

The function class udf:myFunction points to the beginning of a SPARQLMotion script that does the actual work. For illustration purposes, the script has been made a bit more complicating to demonstrate that you can run any arbitrary chain of processing steps. The first module automatically takes the function's input arguments (here, ?text). The end module of the script must bind the result variable (here, ?result).

Function Graph