Class: Glossarist::Validation::ShaclValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/glossarist/validation/shacl_validator.rb

Overview

Validates Turtle output against SHACL shapes.

Shapes resolution order:

1. Explicit :shapes_path option
2. Vendored shapes at data/concept-model/shapes/glossarist.shacl.ttl
 (shipped with the gem, relative to the gem root)

Constant Summary collapse

VENDORED_SHAPES_PATH =
File.expand_path(
  "../../../data/concept-model/shapes/glossarist.shacl.ttl",
  __dir__
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shapes_path: nil) ⇒ ShaclValidator

Returns a new instance of ShaclValidator.



24
25
26
# File 'lib/glossarist/validation/shacl_validator.rb', line 24

def initialize(shapes_path: nil)
  @shapes_path = shapes_path || self.class.default_shapes_path
end

Instance Attribute Details

#shapes_pathObject (readonly)

Returns the value of attribute shapes_path.



22
23
24
# File 'lib/glossarist/validation/shacl_validator.rb', line 22

def shapes_path
  @shapes_path
end

Class Method Details

.default_shapes_pathObject

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
# File 'lib/glossarist/validation/shacl_validator.rb', line 47

def default_shapes_path
  return VENDORED_SHAPES_PATH if File.exist?(VENDORED_SHAPES_PATH)

  raise ArgumentError,
        "No SHACL shapes path provided and the vendored shapes " \
        "file (#{VENDORED_SHAPES_PATH}) is missing. Pass " \
        "shapes_path: '/path/to/glossarist.shacl.ttl'."
end

Instance Method Details

#validate_files(paths) ⇒ Object



28
29
30
# File 'lib/glossarist/validation/shacl_validator.rb', line 28

def validate_files(paths)
  validate_graphs(Array(paths).map { |p| load_graph(p) })
end

#validate_graphs(graphs) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/glossarist/validation/shacl_validator.rb', line 32

def validate_graphs(graphs)
  shapes = SHACL::Shapes.from_graph(load_graph(@shapes_path))
  failures = []

  graphs.each do |graph|
    report = shapes.execute(graph)
    next if report.conform?

    failures << Report.new(results: report.results)
  end

  AggregateReport.new(failures: failures)
end