Class: ShEx::Algebra::EachOf

Inherits:
Operator show all
Includes:
TripleExpression
Defined in:
lib/shex/algebra/each_of.rb

Constant Summary collapse

NAME =
:eachOf

Constants inherited from Operator

Operator::ARITY

Instance Attribute Summary

Attributes inherited from Operator

#id, #logger, #operands, #options, #schema

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TripleExpression

#maximum, #minimum, #triple_constraints, #triple_expression?

Methods inherited from Operator

#base_uri, #closed?, #each_descendant, #eql?, #expression, #expressions, #focus, #focus=, #initialize, #inspect, #iri, iri, #json_type, #matched, #matched=, #message, #message=, #not_matched, #not_satisfied, #operand, #parent, #parent=, #satisfied, #satisfied=, #satisfy, #semact?, #semantic_actions, #serialize_value, #status, #structure_error, #to_h, #to_json, #to_sxp, #to_sxp_bin, #triple_expression?, #unmatched, #unmatched=, #unsatisfied, #unsatisfied=, value, #value

Constructor Details

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

Class Method Details

.from_shexj(operator, options = {}) ⇒ Operator

Creates an operator instance from a parsed ShExJ representation

Returns:

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/shex/algebra/each_of.rb', line 11

def self.from_shexj(operator, options = {})
  raise ArgumentError unless operator.is_a?(Hash) && operator['type'] == 'EachOf'
  raise ArgumentError, "missing expressions in #{operator.inspect}" unless operator.has_key?('expressions')
  super
end

Instance Method Details

#matches(arcs_in, arcs_out, depth: 0) ⇒ TripleExpression

expr is an EachOf and there is some partition of T into T1, T2,… such that for every expression expr1, expr2,… in shapeExprs, matches(Tn, exprn, m)…

Parameters:

  • arcs_in (Array<RDF::Statement>)
  • arcs_out (Array<RDF::Statement>)

Returns:

Raises:

  • (ShEx::NotMatched)

    with ‘expression` accessor to access `matched` and `unmatched` statements along with `satisfied` and `unsatisfied` operations.



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
70
71
72
73
# File 'lib/shex/algebra/each_of.rb', line 23

def matches(arcs_in, arcs_out, depth: 0)
  status "", depth: depth
  results, satisfied, unsatisfied = [], [], []
  num_iters, max = 0, maximum

  # enter semantic acts
  semantic_actions.each {|op| op.enter(arcs_in: arcs_in, arcs_out: arcs_out, depth: depth + 1)}

  while num_iters < max
    begin
      matched_this_iter = []
      expressions.select {|o| o.is_a?(TripleExpression) || o.is_a?(RDF::Resource)}.all? do |op|
        begin
          op = schema.find(op) if op.is_a?(RDF::Resource)
          matched_op = op.matches(arcs_in - matched_this_iter, arcs_out - matched_this_iter, depth: depth + 1)
          satisfied << matched_op
          matched_this_iter += matched_op.matched
        rescue ShEx::NotMatched => e
          status "not matched: #{e.message}", depth: depth
          unsatisfied << e.expression
          raise
        end
      end
      results += matched_this_iter
      arcs_in -= matched_this_iter
      arcs_out -= matched_this_iter
      num_iters += 1
      status "matched #{results.length} statements after #{num_iters} iterations", depth: depth
    rescue ShEx::NotMatched => e
      status "no match after #{num_iters} iterations (ignored)", depth: depth
      break
    end
  end

  # Max violations handled in Shape
  if num_iters < minimum
    raise ShEx::NotMatched, "Minimum Cardinality Violation: #{results.length} < #{minimum}"
  end

  # Last, evaluate semantic acts
  semantic_actions.each {|op| op.satisfies?(nil, matched: results, depth: depth + 1)}

  satisfy matched: results, satisfied: satisfied, depth: depth
rescue ShEx::NotMatched, ShEx::NotSatisfied => e
  not_matched e.message,
              matched:   results,   unmatched:   ((arcs_in + arcs_out).uniq - results),
              satisfied: satisfied, unsatisfied: unsatisfied,
              depth:     depth
ensure
  semantic_actions.each {|op| op.exit(matched: matched, depth: depth + 1)}
end

#validate!Operator

expressions must be TripleExpressions

Returns:

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/shex/algebra/each_of.rb', line 80

def validate!
  expressions.each do |op|
    case op
    when TripleExpression
    when RDF::Resource
      ref = schema.find(op)
      ref.is_a?(TripleExpression) ||
      structure_error("#{json_type} must reference a TripleExpression: #{ref}")
    else
      structure_error("#{json_type} must reference a TripleExpression: #{ref}")
    end
  end
  super
end