From 763d7f3c2548a72e0156a2b1a2c6700d0b661684 Mon Sep 17 00:00:00 2001
From: YoucTagh
+ In this section, we'll walk you through a simple example that introduces the basics of SHACL Core. You'll see how to describe what your data should look like, and how SHACL checks if your data meets that description.
+
+ Imagine you have a set of people—Alice, Bob, Calvin—and you want to make sure:
+
+ That's what our shape (think "rulebook") will enforce on the data.
+
+ What's wrong here?
+ Relationship between SHACL and RDFS inferencing
+ Getting Started with SHACL Core
+
+ Simple Example:
+
+
+
+ 123-45-6789
).
+ ex:Company
in your data.
+ ex:ssn), the work affiliation (
ex:worksFor
), and the mandatory typing (rdf:type
).
+
+ Our Sample Data (Data Graph)
+
+
+ Here's the data we want to check:
+
+ {
+ "@graph": [
+ {
+ "@id": "ex:Alice",
+ "@type": "ex:Person",
+ "ex:ssn": "987-65-432A"
+ },
+ {
+ "@id": "ex:Bob",
+ "@type": "ex:Person",
+ "ex:ssn": [
+ "123-45-6789",
+ "124-35-6789"
+ ]
+ },
+ {
+ "@id": "ex:Calvin",
+ "@type": "ex:Person",
+ "ex:birthDate": {
+ "@type": "xsd:date",
+ "@value": "1971-07-07"
+ },
+ "ex:worksFor": {
+ "@id": "ex:UntypedCompany"
+ }
+ }
+ ]
+}
+
+
+ ex:UntypedCompany
, but that item isn't labeled as a ex:Company
; plus, Calvin has a ex:birthDate
, which we don't allow.
+
+ Here's how we say it clearly using SHACL's Turtle syntax, as well as JSON-LD and SHACL-C: +
+ +{ + "@id": "ex:PersonShape", + "@type": "sh:NodeShape", + "sh:closed": { + "@type": "xsd:boolean", + "@value": "true" + }, + "sh:ignoredProperties": { + "@list": [ + { + "@id": "rdf:type" + } + ] + }, + "sh:property": [ + { + "sh:datatype": { + "@id": "xsd:string" + }, + "sh:maxCount": { + "@type": "xsd:integer", + "@value": "1" + }, + "sh:path": { + "@id": "ex:ssn" + }, + "sh:pattern": "^\\d{3}-\\d{2}-\\d{4}$" + }, + { + "sh:class": { + "@id": "ex:Company" + }, + "sh:nodeKind": { + "@id": "sh:IRI" + }, + "sh:path": { + "@id": "ex:worksFor" + } + } + ], + "sh:targetClass": { + "@id": "ex:Person" + } +}+
shape ex:PersonShape -> ex:Person { + closed=true ignoredProperties=[rdf:type] . + ex:ssn xsd:string [0..1] pattern="^\\d{3}-\\d{2}-\\d{4}$" . + ex:worksFor IRI ex:Company . +}
+ Let's break that down: +
+sh:targetClass ex:Person
means "apply this rule to all people".
+ sh:property
block ensures:
+ sh:datatype xsd:string
),
+ sh:maxCount 1
),
+ sh:pattern "^\d{3}-\d{2}-\d{4}$"
).
+ ex:worksFor
property, its value must be an IRI and point to something that's a ex:Company
.
+ sh:closed true
means no properties beyond those listed are allowed (except ones explicitly ignored).
+ sh:ignoredProperties ( rdf:type )
lets rdf:type
slip through even though it's not in the allowed property list.
+ + When we run SHACL validation on our data graph using our shapes graph, the validator checks each Person against the rules we wrote. +
++ In plain English, here’s what it finds: +
+987-65-432A
has a letter where a digit should be).
+ sh:maxCount
says only one is allowed).
+ ex:UntypedCompany
) that isn’t declared as a ex:Company
.
+ ex:birthDate
that isn’t allowed by the shape.
+ + Here’s what a SHACL validation report for this example might look like (simplified for readability): +
+ +{ + "@type": "sh:ValidationReport", + "sh:conforms": { + "@type": "xsd:boolean", + "@value": "false" + }, + "sh:result": [ + { + "@type": "sh:ValidationResult", + "sh:focusNode": { + "@id": "ex:Alice" + }, + "sh:resultPath": { + "@id": "ex:ssn" + }, + "sh:resultSeverity": { + "@id": "sh:Violation" + }, + "sh:sourceConstraintComponent": { + "@id": "sh:PatternConstraintComponent" + }, + "sh:sourceShape": { + "@id": "_:b1" + }, + "sh:value": "987-65-432A", + "sh:resultMessage": "Value does not match pattern '^\d{3}-\d{2}-\d{4}$'" + }, + { + "@type": "sh:ValidationResult", + "sh:focusNode": { + "@id": "ex:Bob" + }, + "sh:resultPath": { + "@id": "ex:ssn" + }, + "sh:resultSeverity": { + "@id": "sh:Violation" + }, + "sh:sourceConstraintComponent": { + "@id": "sh:MaxCountConstraintComponent" + }, + "sh:sourceShape": { + "@id": "_:b1" + }, + "sh:resultMessage": "More than 1 values" + }, + { + "@type": "sh:ValidationResult", + "sh:focusNode": { + "@id": "ex:Calvin" + }, + "sh:resultPath": { + "@id": "ex:worksFor" + }, + "sh:resultSeverity": { + "@id": "sh:Violation" + }, + "sh:sourceConstraintComponent": { + "@id": "sh:ClassConstraintComponent" + }, + "sh:sourceShape": { + "@id": "_:b2" + }, + "sh:value": { + "@id": "ex:UntypedCompany" + }, + "sh:resultMessage": "Value does not have class ex:Company" + }, + { + "@type": "sh:ValidationResult", + "sh:focusNode": { + "@id": "ex:Calvin" + }, + "sh:resultPath": { + "@id": "ex:birthDate" + }, + "sh:resultSeverity": { + "@id": "sh:Violation" + }, + "sh:sourceConstraintComponent": { + "@id": "sh:ClosedConstraintComponent" + }, + "sh:sourceShape": { + "@id": "sh:PersonShape" + }, + "sh:value": { + "@type": "xsd:date", + "@value": "1971-07-07" + }, + "sh:resultMessage": "Predicate is not allowed (closed shape)" + } + ] +}+
How to read the report:
+sh:ValidationReport
is the overall report, with sh:conforms false
meaning at least one rule failed.
+ sh:ValidationResult
is one problem found:
+ sh:resultSeverity
— tells you how serious the problem is. In this example, all issues are sh:Violation
(the highest and default severity).
+ sh:focusNode
— the data node that failed.
+ sh:resultPath
— the property involved.
+ sh:value
— the actual value that triggered the failure.
+ sh:sourceConstraintComponent
— which kind of rule was broken (max count, pattern, class, etc.).
+ sh:sourceShape
— the shape that defined the rule.
+ sh:resultMessage
— a human-readable explanation.
+
- The following example data graph contains three SHACL instances of the class ex:Person
.
-
- We can use the shape declaration above to illustrate some of the key terminology used by SHACL.
- The target for the shape ex:PersonShape
is the set of all SHACL instances of the class ex:Person
.
- This is specified using the property sh:targetClass
.
- During the validation, these target nodes become focus nodes for the shape.
- The shape ex:PersonShape
is a node shape, which means that it applies to the focus nodes.
- It declares constraints on the focus nodes, for example using the parameters sh:closed
and sh:ignoredProperties
.
- The node shape also declares two other constraints with the property sh:property
,
- and each of these is backed by a property shape.
- These property shapes declare additional constraints using parameters such as sh:datatype
and sh:maxCount
.
-
- Some of the property shapes specify parameters from multiple constraint components in order to
- restrict multiple aspects of the property values.
- For example, in the property shape for ex:ssn
, parameters from three constraint components are used.
- The parameters of these constraint components are sh:datatype
, sh:pattern
and sh:maxCount
.
- For each focus node the property values of ex:ssn
will be validated against all three components.
-
- SHACL validation based on the provided data graph and shapes graph would produce the following validation report. - See the section Validation Report for details on the format. -
- -
- The validation results are enclosed in a validation report.
- The first validation result is produced because ex:Alice
has a value for ex:ssn
- that does not match the regular expression specified by the property sh:pattern
.
- The second validation result is produced because ex:Bob
has more than the permitted number of values
- for the property ex:ssn
as specified by the sh:maxCount
of 1.
- The third validation result is produced because ex:Calvin
has a value for ex:worksFor
- that does not have an rdf:type
triple that makes it a SHACL instance of ex:Company
.
- The forth validation result is produced because the shape ex:PersonShape
has the property sh:closed
set to true
- but ex:Calvin
uses the property ex:birthDate
which is neither one of the predicates from any of the
- property shapes of the shape, nor one of the properties listed using sh:ignoredProperties
.
-
- In this section, we'll walk you through a simple example that introduces the basics of SHACL Core. You'll see how to describe what your data should look like, and how SHACL checks if your data meets that description. + In this section, we will walk you through a simple example that introduces the basics of SHACL Core. + You will learn how to describe what your data should look like, and how a SHACL processor checks if your data meets that description.
- Imagine you have a set of people—Alice, Bob, Calvin—and you want to make sure: + Imagine you have a set of entities (Alice, Bob, Calvin) and you want explain to a computer or another human that
-123-45-6789
).
+ there is a class called ex:Person
that is the type of these entities
ex:Company
in your data.
+ every instance of ex:Person
has at most one Social Security Number (SSN), and that SSN needs to be a properly formatted text (like 123-45-6789
)
ex:ssn), the work affiliation (ex:worksFor
), and the mandatory typing (rdf:type
).
+ every instance of ex:Person
can work for one or more companies, but those companies must be typed as a ex:Company
in your data
+ ex:Person
, other than the SSN (
ex:ssn), the work affiliation (ex:worksFor
), and the mandatory typing (rdf:type
)
- That's what our shape (think "rulebook") will enforce on the data. -
+ Here is the data we want to describe and validate: +
++ What is wrong here? +
ex:UntypedCompany
,
+ but that entity is not labeled as a ex:Company
.
+ Furthermore, Calvin has a ex:birthDate
, which we do not allow in our data.
+ - Here's how we say it clearly using SHACL's Turtle syntax, as well as JSON-LD and SHACL-C: + Here is a self-contained example of how to represent our domain of interest. + In SHACL terminology, this is called a shapes graph, but you can also think + of this as a domain model or an ontology.
-+ Let us break that down: +
ex:UntypedCompany
) that isn’t declared as a ex:Company
.
+ sh:targetClass ex:Person
means "apply this constraint to all people".
ex:birthDate
that isn’t allowed by the shape.
+ The first sh:property
definition declares that:
+ sh:datatype xsd:string
),
+ sh:maxCount 1
),
+ sh:pattern "^\d{3}-\d{2}-\d{4}$"
).
+ sh:property
definition declares that:
+ ex:worksFor
property, its value must be an IRI and point to something that's a ex:Company
.
+ sh:closed true
means no properties beyond those listed are allowed (except ones explicitly ignored).
+ sh:ignoredProperties ( rdf:type )
lets rdf:type
slip through even though it's not in the allowed property list.
+ When we run SHACL validation on our data graph using our shapes graph, the validator checks each Person against the constraints that we wrote. +
++ In plain English, here is what it finds: +
+987-65-432A
has a letter where a digit should be).
+ sh:maxCount
says only one is allowed).
+ ex:UntypedCompany
) that is not declared as a ex:Company
.
+ ex:birthDate
that is not allowed by the shape.
+ - Here’s what a SHACL validation report for this example might look like (simplified for readability): + Here is what a SHACL validation report for this example might look like (simplified for readability):
-How to read the report:
+sh:ValidationReport
is the overall report, with sh:conforms false
meaning at least one rule failed.
+ sh:ValidationResult
is one problem found:
+ sh:resultSeverity
— tells you how serious the problem is. In this example, all issues are sh:Violation
(the highest and default severity).
+ sh:focusNode
— the data node that failed.
+ sh:resultPath
— the property involved.
+ sh:value
— the actual value that triggered the failure.
+ sh:sourceConstraintComponent
— which kind of constraint was broken (max count, pattern, class, etc.).
+ sh:sourceShape
— the shape that defined the constraint.
+ sh:resultMessage
— a human-readable explanation.
+
+ While SHACL is primarily designed to represent shapes, it also borrows terms and concepts such as
+ rdfs:Class
and rdfs:subClassOf
from the RDF Schema namespace.
+ Some people prefer to keep those concepts separate, as shown in the original example above
+ which had separate entities for ex:Person
and ex:PersonShape
.
+ However, it is also possible to couple them more closely together, and use sh:ShapeClass
+ to declare both a class and a shape at the same time.
+
+ Furthermore, sometimes you will see property shapes declared as blank nodes instead of IRIs. + This is a more compact notation, yet it means that the property shape cannot easily be referenced from + the outside, for example if some other graph wants to reuse a node shape but deactivate a property shape. +
++ The following Turtle example shows these two syntactic variations in action. +
+ ++ We can use the shape declarations above to introduce some of the formal terminology used by SHACL. + This may help you read the remainder of this specification. +
+
+ The target for the shape ex:PersonShape
is the set of all SHACL instances of the class ex:Person
.
+ This is specified using the property sh:targetClass
.
+ During the validation, these target nodes become focus nodes for the shape.
+ The shape ex:PersonShape
is a node shape, which means that it applies to the focus nodes.
+ It declares constraints on the focus nodes, for example using the parameters sh:closed
and sh:ignoredProperties
.
+ The node shape also declares two other constraints with the property sh:property
,
+ and each of these is backed by a property shape.
+ These property shapes declare additional constraints using parameters such as sh:datatype
and sh:maxCount
.
+
+ Some of the property shapes specify parameters from multiple constraint components in order to
+ restrict multiple aspects of the property values.
+ For example, in the property shape for ex:ssn
, parameters from three constraint components are used.
+ The parameters of these constraint components are sh:datatype
, sh:pattern
and sh:maxCount
.
+ For each focus node the property values of ex:ssn
will be validated against all three components.
+
- However, SHACL processors MAY operate on RDF graphs that include entailments [[!sparql12-entailment]] -
- either pre-computed before being submitted to a SHACL processor or performed on the fly as
+ However, SHACL processors MAY operate on RDF graphs that include entailments [[!sparql12-entailment]],
+ whether pre-computed before being submitted to a SHACL processor or performed on the fly as
part of SHACL processing (without modifying either data graph or shapes graph).
To support processing of entailments, SHACL includes the property
- sh:entailment
to indicate what inferencing is required
+ sh:entailment
to indicate the inferencing that is required
by a given shapes graph.
@@ -786,7 +786,7 @@
SHACL implementations MAY, but are not required to, support entailment regimes.
If a shapes graph contains any triple with the predicate sh:entailment
and object E
- and the SHACL processor does not support E
as an entailment regime for the given data graph
+ and the SHACL processor does not support E
as an entailment regime for the given data graph,
then the processor MUST signal a failure.
Otherwise, the SHACL processor MUST provide the entailments for all of the values of sh:entailment
in the shapes graph,
and any inferred triples MUST be returned by all queries against the data graph during the validation process.
@@ -798,7 +798,7 @@
In this section, we will walk you through a simple example that introduces the basics of SHACL Core. - You will learn how to describe what your data should look like, and how a SHACL processor checks if your data meets that description. + You will learn how to describe what your data should look like, and how a SHACL processor checks whether your data meets that description.
ex:Person
can work for one or more companies, but those companies must be typed as a ex:Company
in your data
ex:Person
, other than the SSN (
ex:ssn), the work affiliation (ex:worksFor
), and the mandatory typing (rdf:type
)
+ no other properties are allowed for an ex:Person
, other than the SSN (
ex:ssn), the work affiliation (ex:worksFor
), and the mandatory typing (rdf:type
)
ex:UntypedCompany
,
- but that entity is not labeled as a ex:Company
.
- Furthermore, Calvin has a ex:birthDate
, which we do not allow in our data.
+ but that entity is not labeled as an ex:Company
.
+ Furthermore, Calvin has an ex:birthDate
, which we do not allow in our data.
sh:closed true
means no properties beyond those listed are allowed (except ones explicitly ignored).
+ sh:closed true
means no properties beyond those listed are allowed (except any that are explicitly ignored).
sh:ignoredProperties ( rdf:type )
lets rdf:type
slip through even though it's not in the allowed property list.
@@ -1268,8 +1268,8 @@ Furthermore, sometimes you will see property shapes declared as blank nodes instead of IRIs. - This is a more compact notation, yet it means that the property shape cannot easily be referenced from - the outside, for example if some other graph wants to reuse a node shape but deactivate a property shape. + This is a more compact notation, but it means that the property shape cannot easily be referenced from + the outside; for example, if some other graph wants to reuse a node shape but deactivate a property shape.
The following Turtle example shows these two syntactic variations in action. @@ -1278,27 +1278,27 @@
How to read the report:
sh:ValidationReport
is the overall report, with sh:conforms false
meaning at least one rule failed.
+ sh:ValidationReport
is the overall report, with sh:conforms false
meaning that there was at least one violation.
sh:ValidationResult
is one problem found:
From 5a85d56b4945372a12a97723ca8d8354f0f06f8f Mon Sep 17 00:00:00 2001
From: Holger Knublauch In this section, we will walk you through a simple example that introduces the basics of SHACL Core. - You will learn how to describe what your data should look like, and how a SHACL processor checks whether your data meets that description. + You will learn how to describe how your data should look, and how a SHACL processor checks whether your data meets that description.
In this section, we will walk you through a simple example that introduces the basics of SHACL Core. - You will learn how to describe how your data should look, and how a SHACL processor checks whether your data meets that description. + You will learn to describe how your data should look, and how a SHACL processor checks whether your data meets that description.