Virtuoso and the Shapes Constraint Language (SHACL)

What is Shapes Constraint Language (SHACL)?

SHACL is an Ontology that defines a collection of terms for achieving the following tasks, declaratively:

  1. Validating the structure (or shape) of an RDF-based Entity Relationship Graph
  2. Using SPARQL as a Rule Language via CONSTRUCT queries – just like SPIN

Why is it important?

It enables testing and enforcement of structural integrity in a Knowledge Graph. It achieves this by enabling declarative definition and validation of subject and object structure (shape) associated with an entity relationship type.

How do I use it?

In regards to Virtuoso, it is delivered via a built-in function.

Function Definition

CREATE FUNCTION
DB.DBA.SHACL_COMPILE_SG ( in sg_list any array, in proc_name_pattern varchar := null)
RETURNS varchar

Function Description

sg_list - an array of IRIs denoting graphs comprising SHACL Shape Definitions
proc_name_pattern - an optional string that is placed inside names of created stored
procedures.

This function compiles the SHACL Declarations into Stored
Procedures and returns a string that is the name of top-level Validator
Function.

The top-level Validator Function is defined as follows.

CREATE PROCEDURE DB.DBA.SOME_NAME_HERE (
in specific_targets any array,
in dg_list any array,
in onto_list any array,
in vrg_iri any array)

Description

specific_targets – is NULL if everything in data graphs should be
validated, otherwise it should be a list of subjects to validate.

dg_list – a list of IRIs (or IRI_IDs) of data graphs to be validated.
If more than one graph is specified then a slow non-compiler version will
be used. If exactly one graph is specified then a much faster compiled
SHACL Validator is used.

onto_list – a list of IRIs (or IRI_IDs) of ontology graphs. These
graphs provide data about subclasses etc., but triples of them are not
validated; SHACL rules ofthat graphs are also ignored.

vrg_iri – an IRI for resulting validation report graph. It would be as follows with regards to Shape Validation.

Validation Example

SHACL graph personexample.ttl is compiled into stored procedure DB.DBA.SHACL__SG715348612__VALIDATE ().

DB.DBA.SHACL__SG715348612__VALIDATE () can then be invoked as part of a triggered data entry routine.

Validation Results
Expected result of validation
@prefix rdf:	<http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh:	<http://www.w3.org/ns/shacl#> .
@prefix xsd:	<http://www.w3.org/2001/XMLSchema#> .
@prefix virtrdf:	<http://www.openlinksw.com/schemas/virtrdf#> .
@prefix ns4:	<http://datashapes.org/sh/tests/core/complex/personexample.test#> .

_:vb1887799
	rdf:type	sh:ValidationReport ;
	sh:conforms	"false"^^xsd:boolean ;
	sh:result	[	rdf:type	sh:ValidationResult ;
				sh:focusNode	ns4:Alice ;
				sh:resultPath	ns4:ssn ;
				sh:resultSeverity	sh:Violation ;
				sh:sourceConstraintComponent	sh:PatternConstraintComponent ;
				sh:sourceShape	_:vb1887793 ;
				sh:value	"987-65-432A" ;
				virtrdf:bnode-row	"68" ] ,
		[	rdf:type	sh:ValidationResult ;
				sh:focusNode	ns4:Bob ;
				sh:resultPath	ns4:ssn ;
				sh:resultSeverity	sh:Violation ;
				sh:sourceConstraintComponent	sh:MaxCountConstraintComponent ;
				sh:sourceShape	_:vb1887793 ;
				virtrdf:bnode-row	"77" ] ,
		[	rdf:type	sh:ValidationResult ;
				sh:focusNode	ns4:Calvin ;
				sh:resultPath	ns4:birthDate ;
				sh:resultSeverity	sh:Violation ;
				sh:sourceConstraintComponent	sh:ClosedConstraintComponent ;
				sh:sourceShape	ns4:PersonShape ;
				sh:value	"1999-09-09"^^xsd:date ;
				virtrdf:bnode-row	"85" ] ,
		[	rdf:type	sh:ValidationResult ;
				sh:focusNode	ns4:Calvin ;
				sh:resultPath	ns4:worksFor ;
				sh:resultSeverity	sh:Violation ;
				sh:sourceConstraintComponent	sh:ClassConstraintComponent ;
				sh:sourceShape	[ ] ;
				sh:value	ns4:UntypedCompany ;
				virtrdf:bnode-row	"94" ] ;
	virtrdf:bnode-row	"65" .

Result of validation
@prefix rdf:	<http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix virtrdf:	<http://www.openlinksw.com/schemas/virtrdf#> .
@prefix sh:	<http://www.w3.org/ns/shacl#> .
@prefix xsd:	<http://www.w3.org/2001/XMLSchema#> .
@prefix ns4:	<http://datashapes.org/sh/tests/core/complex/personexample.test#> .

virtrdf:ValidationReport
	rdf:type	sh:ValidationReport ;
	sh:conforms	"false"^^xsd:boolean ;
	sh:result	[	rdf:type	sh:ValidationResult ;
				sh:detail	[	rdf:type	sh:AbstractResult ;
						virtrdf:sh-detail-value	"^\\d{3}-\\d{2}-\\d{4}$" ;
						virtrdf:sh-detail-name	"pattern" ] ;
				sh:focusNode	ns4:Alice ;
				sh:resultMessage	"{value} does not match REGEX {pattern}" ;
				sh:resultPath	ns4:ssn ;
				sh:resultSeverity	sh:Violation ;
				sh:sourceConstraintComponent	sh:PatternConstraintComponent ;
				sh:sourceShape	ns4:PersonShape ;
				sh:value	"987-65-432A" ] ,
		[	rdf:type	sh:ValidationResult ;
				sh:detail	[	rdf:type	sh:AbstractResult ;
						virtrdf:sh-detail-value	2 ;
						virtrdf:sh-detail-name	"count" ] ,
				[	rdf:type	sh:AbstractResult ;
						virtrdf:sh-detail-value	1 ;
						virtrdf:sh-detail-name	"maxCount" ] ;
				sh:focusNode	ns4:Bob ;
				sh:resultMessage	"The number of value nodes is greater than {maxCount}" ;
				sh:resultPath	ns4:ssn ;
				sh:resultSeverity	sh:Violation ;
				sh:sourceConstraintComponent	sh:MaxCountConstraintComponent ;
				sh:sourceShape	ns4:PersonShape ] ,
		[	rdf:type	sh:ValidationResult ;
				sh:detail	[	rdf:type	sh:AbstractResult ;
						virtrdf:sh-detail-value	ns4:Company ;
						virtrdf:sh-detail-name	"class" ] ;
				sh:focusNode	ns4:Calvin ;
				sh:resultMessage	"{value} is not of the {class} or its subclass" ;
				sh:resultPath	ns4:worksFor ;
				sh:resultSeverity	sh:Violation ;
				sh:sourceConstraintComponent	sh:ClassConstraintComponent ;
				sh:sourceShape	ns4:PersonShape ;
				sh:value	ns4:UntypedCompany ] ,
		[	rdf:type	sh:ValidationResult ;
				sh:focusNode	ns4:Calvin ;
				sh:resultMessage	"{path} is not an expected predicate" ;
				sh:resultPath	ns4:birthDate ;
				sh:resultSeverity	sh:Violation ;
				sh:sourceConstraintComponent	sh:ClosedConstraintComponent ;
				sh:sourceShape	ns4:PersonShape ;
				sh:value	"1999-09-09"^^xsd:date ] .

Related:

Hi Mr. Kingsley!

I am currently performing a benchmark on RDF stores.

I can see your post is from 2020.

Is this still the best SHACL validation approach for Virtuoso? Or has SHACL been integrated further into Virtuoso?

Thank you!

Hi,

What do you mean by further integration of SHACL into Virtuoso, specifically?

Hi!

What I mean is, does Virtuoso provide native support for SHACL in any specific section or part of Virtuoso?

I imagine these functions are executed in the ISQL interface (?)

Thank you!