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 September 12, 2016

The Shapes Constraint Language (SHACL) is an in-progress W3C standard enabling the specification of rich data constraints as part of semantic data models, both for RDFS/OWL ontologies and native SHACL shapes. Think of SHACL as doing for RDF data what XML Schemas do for XML data. Although SHACL is not yet standardized, the syntax of the language is sufficiently stable that the W3C is encouraging software vendors to start working on SHACL support in their tools. TopQuadrant, as a driving member of the SHACL working group, has included SHACL support for advanced users in its TopBraid Composer editing tool since release 5.1. As of release 5.2, TopQuadrant's Web products EVN and EDG also include SHACL support.

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

TopBraid EVN and EDG include SHACL editing support as part of the Ontology Editor. Any newly created Ontology is enabled to include SHACL constraints. In this initial support, SHACL Shapes are tied to classes and limited to property constraints. Future versions will gradually extend this to cover all of the advanced features of SHACL with convenient Web interfaces in the future. 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.

So let's get going with a new Ontology:

SHACL-Web Create Ontology

 

To start editing, click on Edit Production Copy, 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 constraint to limit its permissible values. Property constraints are in general attached to a Shape and SHACL allows classes to be also shapes. In TopBraid, click Add Property Constraint… above the hierarchy, with the SSN property selected.

SHACL Web Create PropertyConstraint

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 of the constraint:

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 constraint, 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 constraint 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 constraint, select the class or property in the hierarchy and click on the grayish box after property constraints 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).

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

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 Export tab. Those reports are a convenient place to examine and fix multiple errors at once, e.g. after a new constraint has been added.

The SHACL editing capabilities can of course be applied to 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. The resulting Ontologies can be imported into other TopBraid EVN or EDG vocabularies, or you can export them to a Turtle, XML or JSON-LD file.

Archives

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