Examples from the lectures: Difference between revisions
From info216
Line 40: | Line 40: | ||
dcterm:title "Linked Data - The Story so Far" . | dcterm:title "Linked Data - The Story so Far" . | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This should not give a validation error. | ''This should not give a validation error.'' | ||
===Alternative shape graph=== | ===Alternative shape graph=== | ||
Keep the prefixes from the first examples. You can also write the property constraint as | Keep the prefixes from the first examples. You can also write the property constraint as an anyonymous node like this: | ||
<syntaxhighlight> | <syntaxhighlight lang="ttl"> | ||
kg:MainPaperShape | kg:MainPaperShape | ||
a sh:NodeShape ; | a sh:NodeShape ; | ||
Line 51: | Line 51: | ||
sh:path kg:year | sh:path kg:year | ||
] . | ] . | ||
</syntaxhighlight> | |||
''This is equivalent to the previous example (no validation error).'' | |||
===Cardinality constraints on properties=== | |||
Add a minimum constraint to the ''kg:year'' property: | |||
<syntaxhighlight lang="ttl"> | |||
kg:MainPaperShape | |||
a sh:NodeShape ; | |||
sh:targetClass kg:MainPaper ; | |||
sh:property [ | |||
sh:path kg:year ; | |||
sh:minCount 1 | |||
] . | |||
</syntaxhighlight> | |||
''Now you should get a validation error.'' | |||
* In the data graph, add two ''kg:year'' properties (e.g., 2008 and 2009) to ''kg_LOD_Paper'' to get rid of the error. | |||
* In the shapes graph, add a ''sh:maxCount 1'' constraint to get another validation error. | |||
* In the data graph, remove one ''kg:year'' property value from ''kg:LOD_Paper'' to get rid of the error (2009 is the right year). | |||
===Datatype constraint on literal property values=== | |||
Add the following property constraint to the previous example: | |||
<syntaxhighlight lang="ttl"> | |||
kg:MainPaperShape | |||
a sh:NodeShape ; | |||
sh:targetClass kg:MainPaper ; | |||
sh:property [ | |||
sh:path kg:year ; | |||
sh:minCount 1 ; | |||
sh:minCount 1 ; | |||
sh:datatype xsd:integer | |||
] . | |||
</syntaxhighlight> | |||
''This should not give a validation error.'' | |||
* In the shapes graph, change the datatype constraint to ''sh:datatype xsd:year''. Now you should get an error. | |||
* In the data graph, change the integer ''2009'' to the typed value ''"2009"^^xsd:year'' to get rid of the error. | |||
===Class and node kind constraints=== | |||
Add the following property constraints to the shape graph, either with a URI like this: | |||
<syntaxhighlight lang="ttl"> | |||
kg:MainPaperShape | |||
a sh:NodeShape ; | |||
sh:targetClass kg:MainPaper ; | |||
sh:property kg:MainPaperYearShape, MainPaperContributorShape | |||
... | |||
kg:MainPaperContributorShape | |||
sh:path dcterm:contributor ; | |||
sh:minCount 1 ; | |||
sh:class kg:MainAuthor ; | |||
sh:nodeKind sh:IRI . | |||
</syntaxhighlight> | |||
Or like this: | |||
<syntaxhighlight lang="ttl"> | |||
kg:MainPaperShape | |||
a sh:NodeShape ; | |||
sh:targetClass kg:MainPaper ; | |||
sh:property [ | |||
sh:path kg:year ; | |||
sh:minCount 1 ; | |||
sh:maxCount 1 ; | |||
sh:datatype xsd:year | |||
], [ | |||
sh:path dcterm:contributor ; | |||
sh:minCount 1 ; | |||
sh:class kg:MainAuthor ; | |||
sh:nodeKind sh:IRI | |||
] . | |||
</syntaxhighlight> | |||
''Either way should give you a validation error.'' | |||
* In the data graph, add ''dcterm:contributor "T. Berners-Lee"'' to get rid of the cardinality error. The error goes away, but you get two new ones instead. | |||
* In the data graph, add ''dcterm:contributor [ a kg:MainAuthor ; foaf:name "T. Berners-Lee" ]'' to get rid of the error. The class error goes away, but the IRI error remains. | |||
* In the data graph, create an IRI for Tim-Berners Lee to resolve the error: | |||
<syntaxhighlight lang="ttl"> | |||
... | |||
dcterm:contributor kg:TBL . | |||
kg:TBL | |||
a kg:MainAuthor; | |||
foaf:name "T. Berners-Lee" . | |||
</syntaxhighlight> | </syntaxhighlight> | ||
===Full example=== | ===Full example=== | ||
Final shape graph: | Final shape graph from the lecture: | ||
<syntaxhighlight lang="ttl"> | <syntaxhighlight lang="ttl"> | ||
@prefix sh: <http://www.w3.org/ns/shacl#> . | @prefix sh: <http://www.w3.org/ns/shacl#> . | ||
Line 85: | Line 170: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Final data graph: | Final data graph from the lecture: | ||
<syntaxhighlight lang="ttl"> | <syntaxhighlight lang="ttl"> | ||
@prefix sh: <http://www.w3.org/ns/shacl#> . | @prefix sh: <http://www.w3.org/ns/shacl#> . |
Revision as of 14:24, 2 March 2023
S07: SHACL
The examples are for use with the interactive SHACL Playground.
Minimal example
First shape graph:
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix dcterm: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix kg: <http://i2s.uib.no/kg4news/> .
@prefix th: <http://i2s.uib.no/kg4news/theme/> .
@prefix ss: <http://semanticscholar.org/> .
kg:MainPaperShape
a sh:NodeShape ;
sh:targetClass kg:MainPaper ;
sh:property kg:MainPaperYearShape .
kg:MainPaperYearShape
sh:path kg:year .
First data graph:
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix dcterm: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix kg: <http://i2s.uib.no/kg4news/> .
@prefix th: <http://i2s.uib.no/kg4news/theme/> .
@prefix ss: <http://semanticscholar.org/> .
kg:LOD_Paper
a kg:MainPaper ;
dcterm:title "Linked Data - The Story so Far" .
This should not give a validation error.
Alternative shape graph
Keep the prefixes from the first examples. You can also write the property constraint as an anyonymous node like this:
kg:MainPaperShape
a sh:NodeShape ;
sh:targetClass kg:MainPaper ;
sh:property [
sh:path kg:year
] .
This is equivalent to the previous example (no validation error).
Cardinality constraints on properties
Add a minimum constraint to the kg:year property:
kg:MainPaperShape
a sh:NodeShape ;
sh:targetClass kg:MainPaper ;
sh:property [
sh:path kg:year ;
sh:minCount 1
] .
Now you should get a validation error.
- In the data graph, add two kg:year properties (e.g., 2008 and 2009) to kg_LOD_Paper to get rid of the error.
- In the shapes graph, add a sh:maxCount 1 constraint to get another validation error.
- In the data graph, remove one kg:year property value from kg:LOD_Paper to get rid of the error (2009 is the right year).
Datatype constraint on literal property values
Add the following property constraint to the previous example:
kg:MainPaperShape
a sh:NodeShape ;
sh:targetClass kg:MainPaper ;
sh:property [
sh:path kg:year ;
sh:minCount 1 ;
sh:minCount 1 ;
sh:datatype xsd:integer
] .
This should not give a validation error.
- In the shapes graph, change the datatype constraint to sh:datatype xsd:year. Now you should get an error.
- In the data graph, change the integer 2009 to the typed value "2009"^^xsd:year to get rid of the error.
Class and node kind constraints
Add the following property constraints to the shape graph, either with a URI like this:
kg:MainPaperShape
a sh:NodeShape ;
sh:targetClass kg:MainPaper ;
sh:property kg:MainPaperYearShape, MainPaperContributorShape
...
kg:MainPaperContributorShape
sh:path dcterm:contributor ;
sh:minCount 1 ;
sh:class kg:MainAuthor ;
sh:nodeKind sh:IRI .
Or like this:
kg:MainPaperShape
a sh:NodeShape ;
sh:targetClass kg:MainPaper ;
sh:property [
sh:path kg:year ;
sh:minCount 1 ;
sh:maxCount 1 ;
sh:datatype xsd:year
], [
sh:path dcterm:contributor ;
sh:minCount 1 ;
sh:class kg:MainAuthor ;
sh:nodeKind sh:IRI
] .
Either way should give you a validation error.
- In the data graph, add dcterm:contributor "T. Berners-Lee" to get rid of the cardinality error. The error goes away, but you get two new ones instead.
- In the data graph, add dcterm:contributor [ a kg:MainAuthor ; foaf:name "T. Berners-Lee" ] to get rid of the error. The class error goes away, but the IRI error remains.
- In the data graph, create an IRI for Tim-Berners Lee to resolve the error:
...
dcterm:contributor kg:TBL .
kg:TBL
a kg:MainAuthor;
foaf:name "T. Berners-Lee" .
Full example
Final shape graph from the lecture:
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix dcterm: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix kg: <http://i2s.uib.no/kg4news/> .
@prefix th: <http://i2s.uib.no/kg4news/theme/> .
@prefix ss: <http://semanticscholar.org/> .
kg:MainPaperShape
a sh:NodeShape ;
sh:targetClass kg:MainPaper ;
sh:property kg:MainPaperYearShape, kg:MainPaperContributorShape .
kg:MainPaperYearShape
sh:path kg:year ;
sh:minCount 1 ;
sh:maxCount 1 ;
sh:datatype xsd:year .
kg:MainPaperContributorShape
sh:path dcterm:contributor ;
sh:minCount 1 ;
sh:class kg:MainAuthor ;
sh:nodeKind sh:IRI .
Final data graph from the lecture:
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix dcterm: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix kg: <http://i2s.uib.no/kg4news/> .
@prefix th: <http://i2s.uib.no/kg4news/theme/> .
@prefix ss: <http://semanticscholar.org/> .
kg:LOD_Paper
a kg:MainPaper ;
dcterm:title "Linked Data - The Story so Far" ;
kg:year "2006"^^xsd:year ;
dcterm:contributor kg:TBL, kg:CB .
kg:TBL
a kg:MainAuthor;
foaf:name "T. Berners-Lee" .
kg:CB
a kg:MainAuthor;
foaf:name "C. Bizer" .