This document introduces a SHACL-based RDF vocabulary that can be used to represent, share and reuse parameterizable SPARQL queries. Two types of so-called Templates are introduced, for CONSTRUCT and SELECT queries.

This document uses the prefix dash which represents the DASH Data Shapes namespace http://datashapes.org/dash# which is accessible via its URL http://datashapes.org/dash.

Goals

This document introduces a general-purpose vocabulary that can easily be supported by APIs and tools from various vendors. Example implementations are the TopBraid SHACL API (see the class org.topbraid.shacl.templates.Templates) and TopQuadrant's SWP Technology (see ui:call from version 6.3 onwards).

This specification can be regarded as the next generation of SPIN Templates. Like most features from the DASH namespace, the specifications here may serve as input to future iterations of the official SHACL standards.

SPARQL CONSTRUCT Templates

The class dash:SPARQLConstructTemplate can be instantiated to represent one or more SPARQL CONSTRUCT queries [[sparql11-query]] that may be parameterized and thus executed with different input values. Each SPARQL CONSTRUCT Template must have at least one value for sh:construct and may use sh:prefixes to link to prefix declarations, see Prefix Declarations of SPARQL Queries in [[shacl]].

The following example declares a template that takes a string literal as parameter ex:label and defines a CONSTRUCT query that produces three RDF statements, declaring an OWL Class as a subclass of owl:Thing.

# baseURI: http://example.org
# imports: http://datashapes.org/dash
# prefix: ex

@prefix dash: <http://datashapes.org/dash#> .
@prefix ex: <http://example.org#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.org>
	a owl:Ontology ;
	owl:imports <http://datashapes.org/dash> ;
	sh:declare [
		a sh:PrefixDeclaration ;
		sh:namespace "http://example.org#"^^xsd:anyURI ;
		sh:prefix "ex" ;
	] .

ex:CreateRootClass
	a dash:SPARQLConstructTemplate ;
	rdfs:label "Create Root Class" ;
	rdfs:comment "Creates a new owl:Class as subclass of owl:Thing, with a given rdfs:label." ;
	sh:parameter [
		a sh:Parameter ;
		sh:path ex:label ;
		sh:datatype xsd:string ;
		sh:description "The label of the new class. Also used to construct the class URI." ;
	] ;
	sh:prefixes <http://example.org> ;
	sh:construct """
		CONSTRUCT {
			?class a owl:Class .
			?class rdfs:subClassOf owl:Thing .
			?class rdfs:label $label .
		}
		WHERE {
			BIND (IRI(CONCAT("http://example.org#", ENCODE_FOR_URI($label))) AS ?class) .
		}""" .

When the template above is executed with the parameter value "My Class" then it will produce the following triples:

ex:My%20Class
	a owl:Class ;
	rdfs:subClassOf owl:Thing ;
	rdfs:label "My Class" .

At execution time, the parameter values become pre-bound variables in the SPARQL query. The SHACL-SPARQL spec defines the rules converting local names to SPARQL variable names. For example, ex:label declares the pre-bound variable $label.

By convention, variables that can be pre-bound SHOULD be written in the $ variable notation, while all other variables are encoded using ?. For brevity, pre-bound variables are highlighted in bold in the examples.

SPARQL CONSTRUCT Templates may declare multiple queries using sh:construct, and an execution engine will return the union of all CONSTRUCTed result graphs.

SPARQL SELECT Templates

The class dash:SPARQLSelectTemplate can be instantiated to represent a SPARQL SELECT query [[sparql11-query]] that may be parameterized and thus executed with different input values. Each SPARQL SELECT Template must have exactly one value for sh:select and may use sh:prefixes to link to prefix declarations, see Prefix Declarations of SPARQL Queries in [[shacl]].

The following example declares a template that takes a string literal as parameter ex:label and defines a CONSTRUCT query that produces three RDF statements, declaring an OWL Class as a subclass of owl:Thing.

# baseURI: http://example.org
# imports: http://datashapes.org/dash
# prefix: ex

@prefix dash: <http://datashapes.org/dash#> .
@prefix ex: <http://example.org#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.org>
	a owl:Ontology ;
	owl:imports <http://datashapes.org/dash> ;
	sh:declare [
		a sh:PrefixDeclaration ;
		sh:namespace "http://example.org#"^^xsd:anyURI ;
		sh:prefix "ex" ;
	] .

ex:GetSubClasses
	a dash:SPARQLSelectTemplate ;
	rdfs:label "Get Subclasses" ;
	rdfs:comment "Gets all subclasses of a given class including their rdfs:labels." ;
	sh:parameter [
		a sh:Parameter ;
		sh:path ex:class ;
		sh:class rdfs:Class ;
		sh:description "The class to get the subclasses of." ;
	] ;
	sh:prefixes <http://example.org> ;
	sh:select """
		SELECT ?subClass ?label
		WHERE {
			?subClass rdfs:subClassOf+ $class .
			OPTIONAL { ?subClass rdfs:label ?label } . 
		}""" .

At execution time, the parameter values become pre-bound variables in the SPARQL query. For example, ex:class declares the pre-bound variable $class.

By convention, variables that can be pre-bound SHOULD be written in the $ variable notation, while all other variables are encoded using ?.

Note that SELECT templates may at the same time be instances of dash:SPARQLMultiFunction.