Class: SHACL::Algebra::PropertyShape

Inherits:
Shape show all
Defined in:
vendor/bundler/ruby/3.2.0/bundler/gems/shacl-63ac93ccd0e9/lib/shacl/algebra/property_shape.rb

Constant Summary collapse

NAME =
:PropertyShape

Constants inherited from Shape

Shape::NODE_KIND_COMPARE

Constants inherited from Operator

Operator::BUILTIN_KEYS, Operator::PARAMETERS

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

Constants inherited from SPARQL::Algebra::Operator

SPARQL::Algebra::Operator::ARITY, SPARQL::Algebra::Operator::IsURI, SPARQL::Algebra::Operator::URI

Constants included from SPARQL::Algebra::Expression

SPARQL::Algebra::Expression::PATTERN_PARENTS

Instance Attribute Summary

Attributes inherited from Operator

#graph, #options, #shapes_graph

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods inherited from Shape

#builtin_class, #builtin_datatype, #builtin_disjoint, #builtin_equals, #builtin_hasValue, #builtin_in, #builtin_languageIn, #builtin_maxExclusive, #builtin_maxInclusive, #builtin_maxLength, #builtin_minExclusive, #builtin_minInclusive, #builtin_minLength, #builtin_nodeKind, #compare, #targetNodes

Methods inherited from Operator

add_component, apply_op, #comment, component_params, #deactivated?, from_expanded_value, from_json, #id, iri, #iri, #label, #not_satisfied, params, parse_path, #satisfy, to_rdf, #to_sxp_bin, #type

Methods included from JSON::LD::Utils

#add_value, #as_array, #as_resource, #blank_node?, #compare_values, #graph?, #has_value?, #index?, #list?, #node?, #node_or_ref?, #node_reference?, #property?, #simple_graph?, #value?

Methods included from RDF::Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, base_uri, #base_uri, base_uri=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #formulae, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, prefixes, #prefixes, prefixes=, #rewrite, #to_binary, #to_sparql, to_sparql, #to_sxp, #to_sxp_bin, #validate!, #variable?, #variables, #vars

Methods included from SPARQL::Algebra::Expression

cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator

Instance Method Details

#builtin_lessThan(property, node, path, value_nodes, **options) ⇒ Array<SHACL::ValidationResult>

Specifies the condition that each value node is smaller than all the objects of the triples that have the focus node as subject and the value of sh:lessThan as predicate.

Examples:

ex:LessThanExampleShape
	a sh:NodeShape ;
	sh:property [
		sh:path ex:startDate ;
		sh:lessThan ex:endDate ;
	] .

Parameters:

Returns:



92
93
94
95
96
97
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shacl-63ac93ccd0e9/lib/shacl/algebra/property_shape.rb', line 92

def builtin_lessThan(property, node, path, value_nodes, **options)
  property = property.first if property.is_a?(Array)
  terms = graph.query({subject: node, predicate: property}).objects
  compare(:<, terms, node, path, value_nodes,
          RDF::Vocab::SHACL.LessThanConstraintComponent, **options)
end

#builtin_lessThanOrEquals(property, node, path, value_nodes, **options) ⇒ Array<SHACL::ValidationResult>

Specifies the condition that each value node is smaller than or equal to all the objects of the triples that have the focus node as subject and the value of sh:lessThanOrEquals as predicate.

Parameters:

Returns:



106
107
108
109
110
111
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shacl-63ac93ccd0e9/lib/shacl/algebra/property_shape.rb', line 106

def builtin_lessThanOrEquals(property, node, path, value_nodes, **options)
  property = property.first if property.is_a?(Array)
  terms = graph.query({subject: node, predicate: property}).objects
  compare(:<=, terms, node, path, value_nodes,
          RDF::Vocab::SHACL.LessThanOrEqualsConstraintComponent, **options)
end

#builtin_maxCount(count, node, path, value_nodes, **options) ⇒ Array<SHACL::ValidationResult>

Specifies the maximum number of value nodes.

Parameters:

Returns:



124
125
126
127
128
129
130
131
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shacl-63ac93ccd0e9/lib/shacl/algebra/property_shape.rb', line 124

def builtin_maxCount(count, node, path, value_nodes, **options)
  count = count.first if count.is_a?(Array)
  satisfy(focus: node, path: path,
    message: "#{value_nodes.count} <= maxCount #{count}",
    resultSeverity: (options.fetch(:severity) unless value_nodes.count <= count.to_i),
    component: RDF::Vocab::SHACL.MaxCountConstraintComponent,
    **options)
end

#builtin_minCount(count, node, path, value_nodes, **options) ⇒ Array<SHACL::ValidationResult>

Specifies the minimum number of value nodes.

Examples:

ex:MinCountExampleShape
	a sh:PropertyShape ;
	sh:targetNode ex:Alice, ex:Bob ;
	sh:path ex:name ;
	sh:minCount 1 .

Parameters:

Returns:



147
148
149
150
151
152
153
154
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shacl-63ac93ccd0e9/lib/shacl/algebra/property_shape.rb', line 147

def builtin_minCount(count, node, path, value_nodes, **options)
  count = count.first if count.is_a?(Array)
  satisfy(focus: node, path: path,
    message: "#{value_nodes.count} >= minCount #{count}",
    resultSeverity: (options.fetch(:severity) unless value_nodes.count >= count.to_i),
    component: RDF::Vocab::SHACL.MinCountConstraintComponent,
    **options)
end

#builtin_uniqueLang(uniq, node, path, value_nodes, **options) ⇒ Array<SHACL::ValidationResult>

The property sh:uniqueLang can be set to true to specify that no pair of value nodes may use the same language tag.

Parameters:

Returns:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shacl-63ac93ccd0e9/lib/shacl/algebra/property_shape.rb', line 163

def builtin_uniqueLang(uniq, node, path, value_nodes, **options)
  uniq = uniq.first if uniq.is_a?(Array)
  if !value_nodes.all?(&:literal?)
    not_satisfied(focus: node, path: path,
      message: "not all values are literals",
      resultSeverity: options.fetch(:severity),
      component: RDF::Vocab::SHACL.UniqueLangConstraintComponent,
      **options)
  elsif value_nodes.map(&:language).compact.length != value_nodes.map(&:language).compact.uniq.length
    not_satisfied(focus: node, path: path,
      message: "not all values have unique language tags",
      resultSeverity: options.fetch(:severity),
      component: RDF::Vocab::SHACL.UniqueLangConstraintComponent,
      **options)
  else
    satisfy(focus: node, path: path,
      message: "all literals have unique language tags",
      component: RDF::Vocab::SHACL.UniqueLangConstraintComponent,
      **options)
  end
end

#conforms(node, depth: 0, **options) ⇒ Array<SHACL::ValidationResult>

Validates the specified property within graph, a list of ValidationResult.

A property conforms the nodes found by evaluating it's path all conform.

Parameters:

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shacl-63ac93ccd0e9/lib/shacl/algebra/property_shape.rb', line 16

def conforms(node, depth: 0, **options)
  return [] if deactivated?
  options = id ? options.merge(shape: id) : options
  options[:severity] = @options[:severity] if @options[:severity]
  options[:severity] ||= RDF::Vocab::SHACL.Violation

  path = @options[:path]
  log_debug(NAME, depth: depth) {SXP::Generator.string({id: id, node: node, path: path}.to_sxp_bin)}
  log_error(NAME, "no path", depth: depth) unless path

  # Turn the `path` attribute into a SPARQL Property Path and evaluate to find related nodes.
  value_nodes = if path.is_a?(RDF::URI)
    graph.query({subject: node, predicate: path}).objects
  elsif path.evaluatable?
    path.execute(graph,
      subject: node,
      object: RDF::Query::Variable.new(:object)).map do
        |soln| soln[:object]
    end.compact.uniq
  else
    log_error(NAME, "Can't handle path", depth: depth) {path.to_sxp}
    []
  end

  # Evaluate against builtins
  builtin_results = @options.map do |k, v|
    self.send("builtin_#{k}".to_sym, v, node, path, value_nodes, depth: depth + 1, **options) if self.respond_to?("builtin_#{k}".to_sym)
  end.flatten.compact

  # Evaluate against operands
  op_results = operands.map do |op|
    if op.is_a?(QualifiedValueConstraintComponent) || op.is_a?(SPARQLConstraintComponent)
      # All value nodes are passed
      op.conforms(node, path: path, value_nodes: value_nodes, depth: depth + 1, **options)
    else
      value_nodes.map do |n|
       res = op.conforms(n, path: path, depth: depth + 1, **options)
       if op.is_a?(NodeShape) && !res.all?(&:conform?)
         # Special case for embedded NodeShape
         not_satisfied(focus: node, path: path,
           value: n,
           message: "node does not conform to #{op.id}",
           resultSeverity: options.fetch(:severity),
           component: RDF::Vocab::SHACL.NodeConstraintComponent,
           **options)
       else
         res
       end
     end
    end
  end.flatten.compact

  builtin_results + op_results
end

#pathRDF::URI, SPARQL::Algebra::Expression

The path defined on this property shape



73
74
75
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shacl-63ac93ccd0e9/lib/shacl/algebra/property_shape.rb', line 73

def path
  @options[:path]
end