This is a pure-Ruby library for working with the Shape Constraint Language to validate the shape of RDF graphs.
Install with gem install shacl
The SHACL gem implements a SHACL Shape Expression engine.
require 'linkeddata'
require 'shacl'
graph = RDF::Graph.load("etc/doap.ttl")
shacl = SHACL.open("etc/doap.shacl")
report = shacl.execute(graph)
#=> ValidationReport(conform?, results*)
TODO: When the linkeddata
gem is installed, RDF.rb includes a rdf
executable which acts as a wrapper to perform a number of different
operations on RDF files, including SHACL. The commands specific to SHACL is
*shacl
: Validate repository given shape
Using this command requires shacl
, which references a URI or file path to the SHACL shapes graph. Other options are shape
and focus
.
Example usage:
rdf shacl https://ruby-rdf.github.io/shacl/etc/doap.ttl \
--shape https://ruby-rdf.github.io/shacl/etc/doap-shape.ttl
The result will add the SHACL validation report to the output graph, optionally replacing the graph with the results, expressing the results as an s-expression, or adding the results as output messages.
https://ruby-rdf.github.io/shacl
Similar to the ShEx gem and to the general strategy for querying graphs in the SPARQL gem, the strategy is to parse SHACL shapes into executable operators, which are called recursively to create result sets corresponding to matched nodes and properties.
The shape graph is parsed into JSON-LD, and then converted into S-Expressions, which match the execution path. These S-Expressions can be parsed to recreate the executable shape constraints.
Evaluating the shapes against a graph results in a {SHACL::ValidationReport} indicating conformance, along with a set of individual {SHACL::ValidationResult} instances.
The resulting validation report can be compared with other validation reports, used as native Ruby objects, serialized to s-expressions, or used as an RDF::Enumerable to retrieve the RDF representation of the report, as defined in SHACL Spec.
SHACL-SPARQL variable bindings pass a solution to the query composed of the necessary bindings rather than rewrite the query. Supports SHACL-based Constraints.
Many tests check for entailed triples, such as entailed super-classes of explicit rdf:type
values. If this is required for a given application, the RDF::Reasoner gem can be used to create such entailed triples.
require 'shacl'
require 'rdf/reasoner'
RDF::Reasoner.apply(:rdfs)
graph = RDF::Graph.load("etc/doap.ttl")
graph.entail!
shacl = SHACL.open("etc/doap.shacl")
results = shacl.execute(graph)
#=> [ValidationResult, ...]
This implementation is certainly not performant. Some things that can be be considered in future versions:
targetNode
and targetClass
and other targets to allow a more efficient query to find relevant resources in the data graph and not simply iterrate through each top-level shape.The recommended installation method is via RubyGems. To install the latest official release of RDF.rb, do:
% [sudo] gem install shacl
To get a local working copy of the development repository, do:
% git clone git://github.com/ruby-rdf/shacl.git
Alternatively, download the latest development version as a tarball as follows:
% wget https://github.com/ruby-rdf/shacl/tarball/master
This repository uses Git Flow to mange development and release activity. All submissions must be on a feature branch based on the develop branch to ease staging and integration.
git diff --check
to make sure of this..gemspec
or VERSION
files. If you need to change them,
do so on your private branch only.CREDITS
file and the
corresponding list in the the README
. Alphabetical order applies.AUTHORS
file. If your contributions are significant
enough, be assured we will eventually add you in there.This is free and unencumbered public domain software. For more information, see https://unlicense.org/ or the accompanying {file:LICENSE} file.