ruby-rdf / shacl

Implementation of Shapes Constraint Language (SHACL) for RDF.rb
The Unlicense
8 stars 3 forks source link

SHACL: Shapes Constraint Language (SHACL) for Ruby

This is a pure-Ruby library for working with the Shape Constraint Language to validate the shape of RDF graphs.

Gem Version Build Status Coverage Status Gitter chat

Features

Implementation Report

Install with gem install shacl

Description

The SHACL gem implements a SHACL Shape Expression engine.

Examples

require 'linkeddata'
require 'shacl'

graph = RDF::Graph.load("etc/doap.ttl")
shacl = SHACL.open("etc/doap.shacl")
report = shacl.execute(graph)
#=> ValidationReport(conform?, results*)

Command Line

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.

Documentation

https://ruby-rdf.github.io/shacl

Implementation Notes

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.

Matching Entailed Triples

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, ...]

Future work

This implementation is certainly not performant. Some things that can be be considered in future versions:

Change Log

See Release Notes on GitHub

Dependencies

Installation

The recommended installation method is via RubyGems. To install the latest official release of RDF.rb, do:

% [sudo] gem install shacl

Download

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

Resources

Mailing List

Author

Contributing

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.

License

This is free and unencumbered public domain software. For more information, see https://unlicense.org/ or the accompanying {file:LICENSE} file.