Download TopBraid ComposerPurchase TopBraid ComposerSupportSitemap

SPIN Support in TopBraid

SPIN is a standards-based way to define rules and constraints for Semantic Web data. Using SPIN one can express business rules in SPARQL and execute them directly on any RDF data and models. SPIN is an open specification that has been submitted to the World Wide Web Consortium (W3C) and can be viewed on the W3C site.

SPIN combines concepts from object oriented languages, query languages, and rule-based systems to describe object behavior on the web of data. One of the basic ideas of SPIN is to link class definitions with SPARQL queries to capture constraints and rules that formalize the expected behavior of those classes.

There are many applications of SPIN, including the following

  • 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, 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

For a quick overview of SPIN, take a look at SPIN in Five Slides presentation.

SPIN is supported in all TopBraid products:

  • TopBraid Composer offers convenient SPIN/SPARQL editors with syntax highlighting and auto-completion
  • Wizards are provided for building reusable SPARQL queries as SPIN templates. SPIN templates are SPARQL queries parameterized with pre-bound variables
  • TopBraid includes SPIN templates for OWL 2 RL profile and for checking constraints defined in the SKOS standard for building taxonomies. Other data quality constraints libraries are becoming available from SPIN users.
  • Built-in inference engine TopSPIN is used to execute SPIN rules. It can be used together with other types of inferencing. Incremental inferencing mode is available
  • SPIN-based constraint checking is available in TopBraid Composer and TopBraid Ensemble
  • SPIN functions can be defined and executed as web services with TopBraid Live
  • Java programmers can create and process SPIN models with the open source TopBraid SPIN API

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.

Constraint checking

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.

Constraint violation

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.

SPIN rules

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 to 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.

Inferred age

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.

SPARL function

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

Calculate values and assign to a given variable

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

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.

Template

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.

Template wizard

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.

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.

Unit conversion

More Information

Please visit http://spinrdf.org for details on the SPIN specification.

The SPIN W3C Member Submission provides details for implementors of SPIN-based libraries.

A working example SPIN file (the Kennedy ontology with SPIN rules and constraints) can be found at http://topbraid.org/examples/kennedysSPIN

spacer spacerSPARQL Inferencing Notation

 

 

SPARQL Rules -
(SPIN) Documentation

SPARQL Rules (SPIN) Tutorial (PDF)

SPIN Home page (including SPIN in 5 pages)

SPIN W3C Member Submission