Using SHACL Data Constraints in the TopBraid Web Products EVN and EDG » TopQuadrant, Inc

+1 919.300.7945     Blog     Downloads     Purchase     Contact

Using SHACL Data Constraints in the TopBraid Web Products EVN and EDG

Posted by Holger Knublauch on April 17, 2017

(original posted on September 6, 2016)

What is SHACL?

The RDF Shapes Constraint Language (SHACL) is a new W3C standard for specifying rich data constraints as part of semantic data models. It can be used with RDFS/OWL ontologies or, alternatively, an entire model can be described only in terms of SHACL shapes. Think of SHACL as doing for RDF data what XML Schemas do for XML data. 

Sitting on top of the RDF linked data model, SHACL provides a very flexible architecture for representing domain models, knowledge bases and other data assets. While RDF Schema alone can only be used to represent very basic facts about classes attributes and relationships, and is targeted towards inferencing, SHACL adds a rich declarative language for representing constraints that may be used to validate data, to drive user interfaces, to render complex data structures or to define the contracts of web services.

How can I start using SHACL?

SHACL is currently a W3C Candidate Recommendation. The syntax of the language is stable and ready to use. It is not expected to change. Several implementers are currently going through the testing stage with the final release of SHACL as a W3C Technical Recommendation projected to happen as early as June 2017.

TopQuadrant, as a driving member of the SHACL working group, has included SHACL support for advanced users in its TopBraid Composer IDE since release 5.1. As of release 5.2, TopQuadrant's Web products TopBraid EVN and TopBraid EDG also include SHACL support.

If you are already a user of these products, you can follow along with our short tutorial. If you are not a user yet, not a problem! You can download a trial version of TopBraid Composer Maestro Edition which includes a localhost demo of EVN and EDG. Or you can request a server evaluation account.

TopBraid EVN and EDG include SHACL editing support as part of the Ontology Editor. Any newly created Ontology is automatically enabled to include SHACL constraints. In this initial support, SHACL Shapes are tied to classes and limited to defining property constraints. Future versions will gradually extend this to cover all of the advanced features of SHACL using convenient Web interfaces.

Note SHACL is a fully extensible language. If you need to, for example, write SPARQL-based constraints that are not yet supported by the Web UI, simply use TopBraid Composer or any other tool with full SHACL support, and import the resulting SHACL file into the EVN or EDG Ontologies.

Step by Step Tutorial

So let's get going with a new Ontology:

SHACL-Web Create Ontology

 

To start editing, click on Edit Production Copy. Collapse the search panel on the right as we will not need it. Then, select the root class Thing and click Create class…

SHACL Web Create Class

Creating properties is done using the corresponding buttons above the class hierarchy. For now, let's create an attribute / datatype property for the social security number (SSN):

SHACL Web Create Attribute

At this stage, you have the option to make global statements about the property using the conventional RDFS properties for domain and range. While SHACL does not require these, for now we recommend to use RDFS statements where they make sense, so that your ontologies remain compatible to tools that only understand RDFS or OWL. In the case of the SSN property, we set the range to string and press Save Changes.

Now that the property has been declared and attached to the example class, we can define a SHACL property shape to limit its permissible values. Property shapes are typically attached to a SHACL node shape which aggregates all the “rules” we will define across the various properties. SHACL allows classes to be also node shapes. SHACL has a concept of targets to specify what resources a SHACL shape applies to. When a class is used as a node shape, it means that the shape applies to all class members. This is what we will do in our example as we define the permissible values for resources that are members of class Person.

In TopBraid, click Add Property Shape… above the hierarchy, with the SSN property selected.

SHACL Web Create Property Shape

The check box not extensible outside of this vocabulary is used to select whether the constraint itself should get a URI or not. Either value is fine in our example scenario, but if you plan to build a generic SHACL model that others may want to extend and modify, leave the checkbox unchecked. This takes us to the following screen, where we can fill in the various parameters to constrain the values of the social security number:

SHACL Web PropertyConstraint 1

There is already a long list of options here, and more are likely to be supported in future versions since SHACL is extensible with all kinds of new constraint types. The screenshot above shows the upper portion of a property shape, where we can specify how the property should be displayed in the context of the surrounding class – properties may have different display names depending on where they are used. For now we only set the maximum cardinality (max count) to 1 to instruct tools that this property may not take more than one value.

SHACL Web PropertyConstraint 2

In the lower part of the property shape form, we have a variety of SHACL features available to limit the value type, value range and various string characteristics. In our example, we can use a regular expression (^\d{3}-?\d{2}-?\d{4}$) to state that social security numbers consist of 3-dash-2-dash-4 digits, and (to be less geeky) limit min length and max length of the property to 11 characters. Don't forget Save Changes.

If you ever need to go back to editing this particular property shape, select the class or property in the tree on the left and click on the grayish box displayed after property shapes in the middle of the view:

SHACL Web Class with SSN

For the sake of making this example a tiny bit more interesting, let's create a relationship / object property to represent the children.

SHACL Web Create Relationship

I'll leave this as an exercise, but assume we want to state that the child property is non-recursive, because no person may have him or herself as child or grandchild. (The non-recursive constraint type is from the DASH extension namespace that is shipping with TopBraid). Hint: look for the constraint to use in the Relationships section of the form.

Now that we have some properties and “interesting” constraints, let's create an example Instance of Person and experience data validation:

SHACL Web Instance

In the example Person above, we have made two mistakes (can you spot them?). Attempting to Save Changes performs the SHACL validation for us:

SHACL Web Violations

If a submitted form contains errors such as above, TopBraid will display details about which property and value is involved. For some constraint violations, TopBraid even attempts to find a fix and offers Suggestions. For example, in the case of the social security number that got a bit too long, TopBraid's SHACL engine suggests to either delete the offending value, or make it shorter so that the max length constraint gets fulfilled again. See the DASH Suggestions Vocabulary for details on how this mechanism works internally, and how to extend it. Press Apply to accept the suggestion before confirming the edit with Save Changes.

To get an overview of all constraint violations in the whole vocabulary, use Constraint Violations Report on the Reports tab. Such reports offer a convenient place to examine and fix multiple errors at once, e.g. when a new constraint has been added and pre-existing data may not conform to it.

SHACL and your existing ontologies

SHACL can of course be used to enrich your existing ontologies. The following screenshots illustrate some constraints that have been defined for the schema.org data model…

SHACL Web schema.org example 1

… and the corresponding constraint violations report for some example instances:

SHACL Web schema.org example 2

 

In order to apply SHACL on top of an existing data model, simply create an Ontology that includes (owl:imports) the classes, properties and instances that you want to constrain.

In the current version, the TopBraid platform uses Turtle snippets to display and edit complex shape expressions. The screenshot below shows a constraint stating that the values of schema:address at the class schema:Person need to be either strings or instances of schema:Address.

SHACL Web Complex Constraint

This short walk-through illustrates some of the ways that you can use SHACL in TopBraid EDG.

The resulting Ontologies can be included into other TopBraid EVN or EDG asset collections, or you can export them to a Turtle, XML or JSON-LD file. In either case, you can use these shapes to run data validation not only at edit time as demonstrated here, but also in a batch mode by utilizing SHACL validation RESTFull API provided in TopBraid.

Archives

About TopQuadrant | Glossary | Privacy Statement | Legal | Terms of Use
© Copyright 2001-2017 TopQuadrant, Inc. All Rights Reserved.