Class: SHACL::ValidationResult

Inherits:
Struct
  • Object
show all
Includes:
RDF::Enumerable
Defined in:
lib/shacl/validation_result.rb

Overview

A SHACL Validateion Result.

Also allows for a successful result, if the resultSeverity nil.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ValidationResult

Initializer calculates lexical values for URIs



27
28
29
30
31
32
33
34
35
36
# File 'lib/shacl/validation_result.rb', line 27

def initialize(*args)
  args = args.map do |v|
    if v.respond_to?(:qname) && !v.lexical && v.qname
      v = RDF::URI.new(v.to_s) if v.frozen?
      v.lexical = v.qname.join(':')
    end
    v
  end
  super(*args)
end

Instance Attribute Details

#componentObject

Returns the value of attribute component

Returns:

  • (Object)

    the current value of component



12
13
14
# File 'lib/shacl/validation_result.rb', line 12

def component
  @component
end

#detailsObject

Returns the value of attribute details

Returns:

  • (Object)

    the current value of details



12
13
14
# File 'lib/shacl/validation_result.rb', line 12

def details
  @details
end

#focusObject

Returns the value of attribute focus

Returns:

  • (Object)

    the current value of focus



12
13
14
# File 'lib/shacl/validation_result.rb', line 12

def focus
  @focus
end

#messageObject

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



12
13
14
# File 'lib/shacl/validation_result.rb', line 12

def message
  @message
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



12
13
14
# File 'lib/shacl/validation_result.rb', line 12

def path
  @path
end

#resultSeverityObject

Returns the value of attribute resultSeverity

Returns:

  • (Object)

    the current value of resultSeverity



12
13
14
# File 'lib/shacl/validation_result.rb', line 12

def resultSeverity
  @resultSeverity
end

#shapeObject

Returns the value of attribute shape

Returns:

  • (Object)

    the current value of shape



12
13
14
# File 'lib/shacl/validation_result.rb', line 12

def shape
  @shape
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



12
13
14
# File 'lib/shacl/validation_result.rb', line 12

def value
  @value
end

Class Method Details

.from_json(input, **options) ⇒ ValidationResult

Transform a JSON representation of a result, into a native representation

Parameters:

  • input (Hash)

Returns:

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/shacl/validation_result.rb', line 123

def from_json(input, **options)
  input = JSON.parse(input) if input.is_a?(String)
  input = JSON::LD::API.compact(input,
            "http://github.com/ruby-rdf/shacl/",
            expandContext: "http://github.com/ruby-rdf/shacl/")
  raise ArgumentError, "Expect report to be a hash" unless input.is_a?(Hash)
  result = self.new

  result.focus = Algebra::Operator.to_rdf(:focus, input['focusNode'], base: nil, vocab: false) if input['focusNode']
  result.path = Algebra::Operator.parse_path(input['resultPath'], **options) if input['resultPath']
  result.resultSeverity = Algebra::Operator.iri(input['resultSeverity'], **options) if input['resultSeverity']
  result.component = Algebra::Operator.iri(input['sourceConstraintComponent'], **options) if input['sourceConstraintComponent']
  result.shape = Algebra::Operator.iri(input['sourceShape'], **options) if input['sourceShape']
  result.value = Algebra::Operator.to_rdf(:value, input['value'], **options) if input['value']
  result.details = Algebra::Operator.to_rdf(:details, input['details'], **options) if input['details']
  result.message = Algebra::Operator.to_rdf(:message, input['message'], **options) if input['message']
  result
end

Instance Method Details

#==(other) ⇒ Boolean

To results are eql? if their overlapping properties are equal

Parameters:

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
# File 'lib/shacl/validation_result.rb', line 146

def ==(other)
  return false unless other.is_a?(ValidationResult)
  %i(focus path resultSeverity component shape value).all? do |prop|
    ours = self.send(prop)
    theirs = other.send(prop)
    theirs.nil? || (ours && ours.node? && theirs.node?) || ours && ours.eql?(theirs)
  end
end

#conform?Boolean Also known as: conforms?

A result conforms if it is not a violation

Returns:

  • (Boolean)


41
42
43
# File 'lib/shacl/validation_result.rb', line 41

def conform?
  resultSeverity.nil?
end

#each {|statement| ... }

This method returns an undefined value.

Yields statements for this result

Yields:

  • (statement)

    each statement

Yield Parameters:

  • statement (RDF::Statement)

Yield Returns:

  • (void)

    ignored



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/shacl/validation_result.rb', line 98

def each(&block)
  subject = RDF::Node.new
  block.call(RDF::Statement(subject, RDF.type, RDF::Vocab::SHACL.ValidationResult))

  block.call(RDF::Statement(subject, RDF::Vocab::SHACL.focusNode, focus)) if focus
  case path
  when RDF::URI
    block.call(RDF::Statement(subject, RDF::Vocab::SHACL.resultPath, path))
  when SPARQL::Algebra::Expression
    path.each_statement(&block)
    block.call(RDF::Statement(subject, RDF::Vocab::SHACL.resultPath, path.subject))
  end
  block.call(RDF::Statement(subject, RDF::Vocab::SHACL.resultSeverity, resultSeverity)) if resultSeverity
  block.call(RDF::Statement(subject, RDF::Vocab::SHACL.sourceConstraintComponent, component)) if component
  block.call(RDF::Statement(subject, RDF::Vocab::SHACL.sourceShape, shape)) if shape
  block.call(RDF::Statement(subject, RDF::Vocab::SHACL.value, value)) if value
  block.call(RDF::Statement(subject, RDF::Vocab::SHACL.detail, RDF::Literal(details))) if details
  block.call(RDF::Statement(subject, RDF::Vocab::SHACL.resultMessage, RDF::Literal(message))) if message
end

#inspectObject

Inspect as SXP



156
157
158
# File 'lib/shacl/validation_result.rb', line 156

def inspect
  SXP::Generator.string to_sxp_bin
end

#linter_messageHash{Symbol => Hash{Symbol => Array<String>}}

Create a hash of messages appropriate for linter-like output.

Returns:

  • (Hash{Symbol => Hash{Symbol => Array<String>}})


65
66
67
68
69
70
71
# File 'lib/shacl/validation_result.rb', line 65

def linter_message
  case
  when path then {path: {path.to_sxp => [to_s]}}
  when focus then {focus: {focus.to_sxp => [to_s]}}
  else {shape: {shape.to_sxp => [to_s]}}
  end
end

#to_sObject

Some humanized result for the report



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/shacl/validation_result.rb', line 75

def to_s
  "Result for: " +
  %i(value focus path shape resultSeverity component details message).map do |sym|
    v = self.send(sym)
    if v.respond_to?(:humanize)
      v.humanize
    elsif v.respond_to?(:lexical)
      v.lexical
    else
      v.to_sxp
    end
    (sym == :value ? v.to_sxp : "#{sym}: #{v.to_sxp}") if v
  end.compact.join("\n  ")
end

#to_sxp(**options) ⇒ String

Transform ValidationResult to SXP

Returns:

  • (String)


57
58
59
# File 'lib/shacl/validation_result.rb', line 57

def to_sxp(**options)
  self.to_sxp_bin.to_sxp(**options)
end

#to_sxp_binObject



46
47
48
49
50
51
# File 'lib/shacl/validation_result.rb', line 46

def to_sxp_bin
  %i(value focus path shape resultSeverity component details message).inject([:ValidationResult]) do |memo, sym|
    v = self.send(sym)
    v ? (memo + [[sym, *v]]) : memo
  end.to_sxp_bin
end