|
SPIN is a collection of RDF vocabularies enabling the use of SPARQL to define constraints and inference rules on Semantic Web models. SPIN also provides meta-modeling capabilities that allow users to define their own SPARQL functions and query templates without having to write Java code. Finally, SPIN includes a ready to use library of common functions.
SPIN has been designed by TopQuadrant to solve real-world customer needs such as:
- Calculate the value of a property based on other properties - for example, area of a geometric figure as a product of its height and width, age of a person as a difference between today's date and person's birthday, a display name as a concatenation of the first and last names
- Check constraints and perform data validation - for example, a need to automatically raise inconsistency flags when currently available information does not fit the specified integrity constraints
- Isolate a set of rules to be executed under certain conditions - for example, to support incremental reasoning, to initialize certain values when a resource is first created, or to drive interactive applications
SPIN vocabularies are published by TopQuadrant as an open standard for anyone to use. TopBraid Suite provides a number of SPIN-based capabilities including:
- Convenient SPIN/SPARQL editors with features such as syntax highlighting and auto-completion
- A built-in inference engine TopSPIN that can be used to execute SPIN rules, also in conjunction with other types of inferencing
- An option to do incremental inferencing to automatically derive property values from edited values
- A useful constraint checking mode that validates input at edit time
- Comprehensive support for defining and using user-defined SPIN functions and templates
- Mechanisms to execute SPIN and SPARQLMotion functions as web services
Constraint Checking
The following screenshot provides an example of a SPIN constraint definition. Constraints are conditions or unit tests that all instances of a class (and its subclasses) must fulfill at any time. Constraint violations can be useful to guide input and to verify a model's state.

The constraint above is an ASK query that checks whether the values of the property age are within a reasonable range. If a Person has an age less than 0 or greater than 120, a constraint violation will be issued and TopBraid Composer will display warnings at suitable places on the form, as shown below. Note that SPIN uses the system variable ?this to refer to the current instance of the class to check.

Inference Rules
The following TopBraid Composer screenshot shows an example class Person with three SPIN rules to infer the values of the age, grandFather and grandMother properties.

As you can see above, SPIN rules are basically SPARQL CONSTRUCT queries that are attached to a given class. The system variable ?this is used to refer the current instance of the class to query and infer instance values. The first rule in the example also uses a user-defined SPARQL function getCurrentYear() which is declared in the SPIN function framework using other SPARQL functions as part of the ontology. The other two example rules (with the yellow background) are instances of a user-defined SPIN template. In the following screenshot, the (blue) value for the age property has been automatically calculated by the TopSPIN inference engine.

TopBraid's SPIN engine has an option to execute SPIN inferences incrementally. For example, if the user changes the value of birthYear, the value for age will be updated automatically.
User-Defined SPARQL Functions
The powerful SPIN meta-modeling capabilities can be used to extend the expressivity of SPARQL, or even to build domain-specific modeling languages. TopBraid makes it easy to define your own SPIN/SPARQL functions. The following screenshot shows a function that gets the father of a given person (?arg1). When the function is called, TopBraid's SPARQL engine will call the SPARQL query specified as spin:body of the function.

Once the function has been defined, it can be used in any SPARQL query, for example in LET statements to calculate new values and assign to a given variable.

User-defined SPIN functions can also be used in FILTER clauses to validate values.

These usage examples illustrate that SPIN functions are an efficient means to encapsulate reusable SPARQL queries so that they can be used in multiple contexts. Furthermore, SPIN functions (and templates) are self-describing URI resources which can be shared on the Semantic Web.
SPIN Query Templates
In addition to user-defined functions, SPIN can also be used to encapsulate arbitrary other SPARQL queries so that they can be reused. A template is like a boxed SPARQL query that has arguments to customize it for a specific use case. The example below defines a template that wraps a CONSTRUCT query to derive one property value (e.g., grandFather) from the parents of the parents of the given resource ?this.

The template can now be inserted wherever a CONSTRUCT query is expected, for example as a spin:rule. The Kennedy example further up on this page includes two usages of this template: one to compute grand fathers from the male grand parents, and one to compute the grand mothers from the female grand parents. In order to instantiate such a template, TopBraid provides a convenient wizard as shown in the next screen.

This template mechanism is extremely powerful as it allows you to define your own modeling vocabulary. Instead of having to type in SPARQL rules or constraints, you can prepare common modeling patterns and encapsulate them as SPIN templates. The templates have formal semantics by virtue of the nested SPARQL queries, and are thus executable and can be shared together with your domain model.
In principle it is even possible to define a modeling language similar to OWL or RDF Schema in terms of SPIN templates. SPIN provides a library of reusable templates and functions for tasks such as checking cardinality constraints, value ranges and setting default values. One of those templates, called spl:Attribute combines all this into a single object similar to object-oriented modeling. The following example class Tree has two attributes heightCM and heightFeet, defined as constraint using the Attribute template.

The screenshot also shows the usage of another template as a spin:rule: a template rule is used to convert units, in this case from centimeters to feet. Backed by a comprehensive NASA units ontology developed at TopQuadrant, this template "knows" how to convert one unit to another. The SPIN template encapsulates all this mathematical knowledge and makes it reusable to infer the value of one property (in one unit) from the value of another unit (in another unit). The domain experts only needs to select start unit and end unit, as well as start and end properties, and the SPIN template will do the rest. So when someone instantiates the class Tree then the value in feet will be automatically inferred whenever the value in centimeters is edited, as shown below.

More Information
Please visit http://spinrdf.org for details on the SPIN specification.
A working example SPIN file (the Kennedy ontology with SPIN rules and constraints) can be found at http://topbraid.org/examples/kennedysSPIN |