User-Defined SPIN Functions and Templates

SPIN provides powerful mechanisms to define new SPARQL functions as well as SPARQL query templates. Technical details on how to create them are available in various places, including the SPIN specification online and the Understanding SPIN Functions blog. The following paragraphs explain how to use them in TopBraid.

If you have functions or templates that are of general interest, and that you want to have available everywhere, you should store them in files having .spin. in their name, e.g. .spin.ttl. When TopBraid starts up, the system will pre-load all those files and add those functions to the global function registries.

Apart from that mechanism, all SPIN functions and templates from the current file (or its imports) will be available. It is therefore typically sufficient to import files that define those functions and templates.

SPIN functions and templates are also available to executing SPARQLMotion scripts. However, there is one important limitation: when a SM script executes, the functions and templates from the currently executing module may not be visible. The workarounds are to use global .spin. files, or let the script itself import the function and template declarations.

For more information, see the blog Understanding SPIN Functions.

Defining SPIN Functions and Magic Properties

It is possible to edit SPIN functions "manually". For example, use the class tree to create a subclass of spin:Functions, then edit its spin:body and then add the arguments of the function. In order to add an argument, find spin:constraint on the form of your function, and select Create from SPIN template... to create an instance of spl:Argument. A more convenient mechanism is typically to use Create spl:Argument... instead.

If you already have a SPARQL snippet that you want to turn into a function or magic property, you can use the powerful Extract SPIN Function... or Extract SPIN Magic Property... refactorings. In order to use them, highlight a valid, self-contained snippet in a SPARQL editor and right click to open the context menu.