Skip to content

Commit

Permalink
Finish 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Jan 4, 2021
2 parents bd82bdd + ca34d78 commit 5987186
Show file tree
Hide file tree
Showing 51 changed files with 13,626 additions and 2 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_token: z50UjdFEZqzhCBcjNNk676SvFZP3dbWUF
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This workflow runs continuous CI across different versions of ruby on all branches and pull requests to develop.

name: CI

on:
push:
branches: [ '**' ]
pull_request:
branches: [ develop ]
workflow_dispatch:

jobs:
tests:
name: Ruby ${{ matrix.ruby }}
if: "contains(github.event.commits[0].message, '[ci skip]') == false"
runs-on: ubuntu-latest
env:
CI: true
strategy:
fail-fast: false
matrix:
ruby:
- 2.4
- 2.5
- 2.6
- 2.7
#- 3.0 # until net-http-persistent is updaated
#- ruby-head # until net-http-persistent is updaated
- jruby
steps:
- name: Clone repository
uses: actions/checkout@v2
- name: Clone Test Suite repository
uses: actions/checkout@v2
with:
repository: w3c/data-shapes
path: spec/w3c-data-shapes
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Install dependencies
run: bundle install --jobs 4 --retry 3
- name: Run tests
run: bundle exec rspec spec

15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.DS_Store
.tmp
.yardoc
pkg
tmp
/*.gem
/.rbx/
/doc/
Gemfile.lock
.bundle/
*.sw?
benchmark/
/.byebug_history
/coverage/
/doc/
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: ruby
script: "bundle exec rspec spec"
env:
- CI=true
rvm:
- 2.5
- 2.6
- 2.7
- ruby-head
- jruby
cache: bundler
sudo: false
matrix:
allow_failures:
- rvm: jruby
- rvm: ruby-head
dist: trusty
11 changes: 11 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--title "SHACL: Shapes Constraint Language (SHACL) for Ruby"
--output-dir doc/yard
--protected
--no-private
--hide-void-return
--markup markdown
--readme README.md
-
VERSION
LICENSE
etc/earl.html
29 changes: 29 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
source 'https://rubygems.org'

gemspec

gem 'rdf',
git: 'https://github.com/ruby-rdf/rdf.git',
branch: 'develop'

group :development, :test do
gem 'json-ld', git: 'https://github.com/ruby-rdf/json-ld.git', branch: 'develop'
gem 'rdf-isomorphic', git: 'https://github.com/ruby-rdf/rdf-isomorphic.git', branch: 'develop'
gem 'rdf-ordered-repo', git: 'https://github.com/ruby-rdf/rdf-ordered-repo.git', branch: 'develop'
gem 'rdf-reasoner', git: 'https://github.com/ruby-rdf/rdf-reasoner.git', branch: 'develop'
gem 'rdf-spec', git: 'https://github.com/ruby-rdf/rdf-spec.git', branch: 'develop'
gem 'rdf-turtle', git: 'https://github.com/ruby-rdf/rdf-turtle.git', branch: 'develop'
gem 'rdf-xsd', git: 'https://github.com/ruby-rdf/rdf-xsd.git', branch: 'develop'
gem 'sparql', git: 'https://github.com/ruby-rdf/sparql.git', branch: 'develop'
gem 'sxp', git: 'https://github.com/dryruby/sxp.rb.git', branch: 'develop'

gem 'rake'
gem 'simplecov', '~> 0.16', platform: :mri
gem 'coveralls', '~> 0.8', '>= 0.8.23', platform: :mri

gem 'earl-report', platform: :mri
end

group :debug do
gem 'byebug', platform: :mri
end
161 changes: 159 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,159 @@
# shacl
Implementation of Shapes Constraint Language (SHACL) for RDF.rb
# SHACL: Shapes Constraint Language (SHACL) for Ruby

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

[![Gem Version](https://badge.fury.io/rb/shacl.png)](https://badge.fury.io/rb/shacl)
[![Build Status](https://github.com/ruby-rdf/shacl/workflows/CI/badge.svg?branch=develop)](https://github.com/ruby-rdf/shacl/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/ruby-rdf/shacl/badge.svg)](https://coveralls.io/github/ruby-rdf/shacl)
[![Gitter chat](https://badges.gitter.im/ruby-rdf/rdf.png)](https://gitter.im/ruby-rdf/rdf)

## Features

* 100% pure Ruby with minimal dependencies and no bloat.
* Fully compatible with [SHACL][SHACL Spec] specifications.
* 100% free and unencumbered [public domain](https://unlicense.org/) software.

[Implementation Report](https://ruby-rdf.github.io/shacl/etc/earl.html)

Install with `gem install shacl`

## Description

The SHACL gem implements a [SHACL][SHACL Spec] 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://rubydoc.info/github/ruby-rdf/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][].

### 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:

* Support for SHACL-SPARQL
* Index shapes on `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.
* Cache target nodes as JSON-LD to reduce the need to separately query for each constraint.
* Reasoner should support limited RDFS/OWL entailment from the data graph, not just pre-defined vocabularies.

## Dependencies

* [Ruby](https://ruby-lang.org/) (>= 2.4)
* [RDF.rb](https://rubygems.org/gems/rdf) (~> 3.1, => 3.1.8)
* [SPARQL](https://rubygems.org/gems/sparql) (~> 3.1, => 3.1.4)
* [json-ld](https://rubygems.org/gems/sparql) (~> 3.1, => 3.1.7)
* [sxp](https://rubygems.org/gems/sxp) (~> 1.1)

## Installation

The recommended installation method is via [RubyGems](https://rubygems.org/).
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

* <https://rubydoc.info/github/ruby-rdf/shacl>
* <https://github.com/ruby-rdf/shacl>
* <https://rubygems.org/gems/shacl>

## Mailing List

* <https://lists.w3.org/Archives/Public/public-rdf-ruby/>

## Author

* [Gregg Kellogg](https://github.com/gkellogg) - <https://greggkellogg.net/>

## Contributing

This repository uses [Git Flow](https://github.com/nvie/gitflow) to mange development and release activity. All submissions _must_ be on a feature branch based on the _develop_ branch to ease staging and integration.

* Do your best to adhere to the existing coding conventions and idioms.
* Don't use hard tabs, and don't leave trailing whitespace on any line.
Before committing, run `git diff --check` to make sure of this.
* Do document every method you add using [YARD][] annotations. Read the
[tutorial][YARD-GS] or just look at the existing code for examples.
* Don't touch the `.gemspec` or `VERSION` files. If you need to change them,
do so on your private branch only.
* Do feel free to add yourself to the `CREDITS` file and the
corresponding list in the the `README`. Alphabetical order applies.
* Don't touch the `AUTHORS` file. If your contributions are significant
enough, be assured we will eventually add you in there.
* Do note that in order for us to merge any non-trivial changes (as a rule
of thumb, additions larger than about 15 lines of code), we need an
explicit [public domain dedication][PDD] on record from you,
which you will be asked to agree to on the first commit to a repo within the organization.
Note that the agreement applies to all repos in the [Ruby RDF](https://github.com/ruby-rdf/) organization.

## License

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

[RDF]: https://www.w3.org/RDF/
[YARD]: https://yardoc.org/
[YARD-GS]: https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
[PDD]: https://unlicense.org/#unlicensing-contributions
[S-Expressions]: https://en.wikipedia.org/wiki/S-expression
[RDF.rb]: https://ruby-rdf.github.com/rdf
[RDF::Reasoner]: https://ruby-rdf.github.com/rdf-reasoner
[SPARQL gem]: https://ruby-rdf.github.com/sparql
[SXP gem]: https://ruby-rdf.github.com/sxp
[SHACL Spec]: https://www.w3.org/TR/shacl/
[ShEx gem]: https://ruby-rdf.github.com/shex
67 changes: 67 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env ruby
require 'rdf/vocab'
require 'rdf/turtle'

namespace :gem do
desc "Build the shacl-#{File.read('VERSION').chomp}.gem file"
task :build do
sh "gem build shacl.gemspec && mv shacl-#{File.read('VERSION').chomp}.gem pkg/"
end

desc "Release the shacl-#{File.read('VERSION').chomp}.gem file"
task :release do
sh "gem push pkg/shacl-#{File.read('VERSION').chomp}.gem"
end
end

desc "Generate SHACL vocabulary class"
task :vocab do
puts "Generate lib/shacl/vocab.rb"
cmd = "bundle exec rdf"
cmd += " serialize --uri 'http://www.w3.org/ns/shacl#' --output-format vocabulary --ordered"
cmd += " --module-name RDF::Vocab"
cmd += " --class-name SHACL"
cmd += " --strict"
cmd += " -o lib/shacl/vocab.rb_t"
cmd += " https://www.w3.org/ns/shacl.ttl"
puts " #{cmd}"
begin
%x{#{cmd} && sed 's/\r//g' lib/rdf/vocab/shacl.rb_t > lib/rdf/vocab/shacl.rb}
rescue
puts "Failed to load shacl vocabulary: #{$!.message}"
ensure
%x{rm -f lib/rdf/vocab/shacl.rb_t}
end
end

desc "Build cached context for interpreting IRIs from JSON representation"
task context: "lib/shacl/context.rb"

file "lib/shacl/context.rb" => "lib/shacl/shapes.rb" do
File.open("lib/shacl/context.rb", "w") do |f|
require 'json/ld'
require 'shacl'
c = JSON::LD::Context.parse(SHACL::Shapes::SHAPES_FRAME['@context'])
c.context_base = "http://github.com/ruby-rdf/shacl/"
f.write c.to_rb
end
end

file 'etc/manifests.ttl' do
%x{script/tc --write-manifests -o etc/manifests.ttl}
end

desc "Generate EARL report"
task :earl => 'etc/earl.html'

file 'etc/earl.ttl' => 'etc/doap.ttl' do
%x{script/tc --earl -o etc/earl.ttl}
end

file 'etc/earl.jsonld' => %w(etc/earl.ttl etc/manifests.ttl etc/template.haml) do
%x{(cd etc; earl-report --format json -o earl.jsonld earl.ttl)}
end

file 'etc/earl.html' => 'etc/earl.jsonld' do
%x{(cd etc; earl-report --json --format html --template template.haml -o earl.html earl.jsonld)}
end
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
5 changes: 5 additions & 0 deletions etc/.earl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
:format: :json
:manifest: manifests.ttl
:bibRef: ! '[[shacl]]'
:name: Shapes Constraint Language (SHACL)
6 changes: 6 additions & 0 deletions etc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# EARL results for SHACL.

../script/tc --write-manifests -o manifests.ttl
../script/tc --earl -o earl.ttl
earl-report --format json -o earl.jsonld earl.ttl
earl-report --json --format html --template template.haml -o earl.html earl.jsonld

0 comments on commit 5987186

Please sign in to comment.