Class: Stupidedi::Builder::ConstraintTable::ValueBased

Inherits:
Stupidedi::Builder::ConstraintTable show all
Defined in:
lib/stupidedi/builder/constraint_table.rb

Overview

Chooses the subset of Instruction values based on the distinguishing values allowed by each Schema::SegmentUse. For instance, there are often several loops that begin with ‘NM1`, which are distinguished by the qualifier in element `NM101`.

Instance Method Summary collapse

Methods inherited from Stupidedi::Builder::ConstraintTable

build

Constructor Details

#initialize(instructions) ⇒ ValueBased

Returns a new instance of ValueBased.



100
101
102
# File 'lib/stupidedi/builder/constraint_table.rb', line 100

def initialize(instructions)
  @instructions = instructions
end

Instance Method Details

#matches(segment_tok, strict) ⇒ Array<Instruction>

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/stupidedi/builder/constraint_table.rb', line 105

def matches(segment_tok, strict)
  invalid = true  # Were all possibly distinguishing elements invalid?
  present = false # Were any possibly distinguishing elements present?

  @__basis ||= basis(shallowest(@instructions))
  @__basis.head.each do |(n, m), map|
    value = deconstruct(segment_tok.element_toks, n, m)

    case value
    when nil, :not_used, :default
      # ignore
    else
      singleton = map.at(value)
      present   = true

      unless singleton.nil?
        return singleton
      else
        if strict
          designator = "#{segment_tok.id}#{'%02d' % (n + 1)}"
          designator << "-%02d" % m unless m.nil?

          raise ArgumentError,
            "#{value.inspect} is not allowed in #{designator}"
        end
      end
    end
  end

  # If we reach this line, none of the present elements could, on its
  # own, narrow the search space to a single Instruction. We now test
  # the combination of elements to iteratively narrow the search space
  space = @instructions

  # @todo: These filters should be ordered by probable effectiveness,
  # so we narrow the search space by the largest amount in the fewest
  # number of steps.
  @__basis.last.each do |(n, m), map|
    value = deconstruct(segment_tok.element_toks, n, m)

    unless value.nil?
      subset  = map.at(value)
      present = true

      unless subset.blank?
        invalid = false
        space  &= subset

        if space.length <= 1
          return space
        end
      else
        if strict
          designator = "#{segment_tok.id}#{'%02d' % n}"
          designator << "-%02d" % m unless m.nil?

          raise ArgumentError,
            "#{value.inspect} is not allowed in #{designator}"
        end
      end
    end
  end

  if invalid and present
    []
  else
    space
  end
end