Module: Stupidedi::Builder::Navigation

Included in:
StateMachine
Defined in:
lib/stupidedi/builder/navigation.rb

Querying the Current Position collapse

Accessing the Current Node collapse

Navigating the Tree collapse

Instance Method Details

#count(id, *elements) ⇒ Integer

Returns:

  • (Integer)


436
437
438
# File 'lib/stupidedi/builder/navigation.rb', line 436

def count(id, *elements)
  __count(false, id, elements)
end

#count!(id, *elements) ⇒ Integer

Returns:

  • (Integer)


441
442
443
# File 'lib/stupidedi/builder/navigation.rb', line 441

def count!(id, *elements)
  __count(true, id, elements)
end

#deterministic?Boolean

Is there exactly one valid parse tree in the current state?

Returns:

  • (Boolean)


19
20
21
# File 'lib/stupidedi/builder/navigation.rb', line 19

def deterministic?
  @active.length == 1
end

#distance(other) ⇒ Either<Integer>

Note:

This method uses AbstractCursor#between, which assumes the two cursors point to the same tree. If that is not the case, the results are undefined.

Returns the number of segments apart the current state is from the given StateMachine's state. Note the direction is not indicated by the return value, so a.distance(b) == b.distance(a) for all states a and b.

Examples:

m.distance(m)                          #=> Either.success(0)
m.next(10).flatmap{|n| n.distance(m) } #=> Either.success(10)

Returns:



71
72
73
74
75
76
77
# File 'lib/stupidedi/builder/navigation.rb', line 71

def distance(other)
  zipper.flatmap do |a|
    other.zipper.map do |b|
      a.between(b).count(&:segment?) - 1
    end
  end
end

#element(m, n = nil, o = nil) ⇒ Either<Zipper::AbstractCursor<Values::AbstractElementVal>>

Extracts the mth element from the current segment, if the current state is deterministic. Accepts optional arguments to extract a specific occurrence of a repeated element and/or a specific component from a composite element.



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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/stupidedi/builder/navigation.rb', line 122

def element(m, n = nil, o = nil)
  segment.flatmap do |s|
    unless m >= 1
      raise ArgumentError,
        "argument must be positive"
    end

    if s.node.invalid?
      # InvalidSegmentVal doesn't have child AbstractElementVals, its
      # children are SimpleElementTok, CompositeElementTok, etc, which
      # are not parsed values.
      return Either.failure("invalid segment")
    end

    designator = s.node.id.to_s
    definition = s.node.definition
    length     = definition.element_uses.length

    unless m <= length
      raise ArgumentError,
        "#{designator} segment has only #{length} elements"
    end

    designator  = designator + "%02d" % m
    value       = s.child(m - 1)

    if n.nil?
      return Either.success(value)
    elsif value.node.repeated?
      unless n >= 1
        raise ArgumentError,
          "argument must be positive"
      end

      limit = value.node.usage.repeat_count
      unless limit.include?(n)
        raise ArgumentError,
          "#{designator} can only occur #{limit.max} times"
      end

      unless value.node.children.defined_at?(n - 1)
        return Either.failure("#{designator} occurs only #{value.node.children.length} times")
      end

      value = value.child(n - 1)
      n, o  = o, nil

      return Either.success(value) if n.nil?
    end

    unless value.node.composite?
      raise ArgumentError,
        "#{designator} is a simple element"
    end

    unless o.nil?
      raise ArgumentError,
        "#{designator} is a non-repeatable composite element"
    end

    unless n >= 1
      raise ArgumentError,
        "argument must be positive"
    end

    length = definition.element_uses.at(m - 1).definition.component_uses.length
    unless n <= length
      raise ArgumentError,
        "#{designator} has only #{length} components"
    end

    if value.node.empty?
      Either.failure("#{designator} is empty")
    else
      Either.success(value.child(n - 1))
    end
  end
end

#elementn(m, n = nil, o = nil) ⇒ Either<Values::AbstractElementVal>

Extracts the mth element from the current segment, if the current state is deterministic. Accepts optional arguments to extract a specific occurrence of a repeated element and/or a specific component from a composite element.



207
208
209
# File 'lib/stupidedi/builder/navigation.rb', line 207

def elementn(m, n = nil, o = nil)
  element(m, n, o).map(&:node)
end

#empty?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/stupidedi/builder/navigation.rb', line 23

def empty?
  value = @active.head.node.zipper

  until value.root?
    value = value.up
  end

  value.node.children.empty?
end

#find(id, *elements) ⇒ Either<StateMachine>

Returns a StateMachine positioned on the next matching segment, excluding Values::InvalidSegmentVals, that satisfies the given element constraints. The search space is limited to certain related elements described in [Navigating.md]

Examples:

machine.find(:ST, nil, nil, "005010X222")

Returns:



418
419
420
# File 'lib/stupidedi/builder/navigation.rb', line 418

def find(id, *elements)
  __find(false, id, elements)
end

#find!(id, *elements) ⇒ Either<StateMachine>

Returns a StateMachine positioned on the next matching segment, including Values::InvalidSegmentVals, that satisfies the given element constraints. The search space is limited to certain related elements described in [Navigating.md]

Examples:

machine.find!(:ST, nil, nil, "005010X222")

Returns:



431
432
433
# File 'lib/stupidedi/builder/navigation.rb', line 431

def find!(id, *elements)
  __find(true, id, elements)
end

#firstEither<StateMachine>

Returns a new StateMachine positioned on the first segment in the parse tree, if there are any segments in the parse tree.

Returns:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/stupidedi/builder/navigation.rb', line 218

def first
  active = roots.map do |zipper|
    state = zipper
    value = zipper.node.zipper

    until value.node.segment? or value.leaf?
      value = value.down
      state = state.down
    end

    unless value.node.segment?
      return Either.failure("no segments")
    end

    # Synchronize the two parallel state and value nodes
    unless value.eql?(state.node.zipper)
      state = state.replace(state.node.copy(:zipper => value))
    end

    state
  end

  Either.success(StateMachine.new(@config, active))
end

#first?Boolean

Is this the first segment?

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'lib/stupidedi/builder/navigation.rb', line 34

def first?
  value = @active.head.node.zipper

  until value.root?
    return false unless value.first?
    value = value.up
  end

  return true
end

#iterate(id, *elements) {|| ... } ⇒ Either<Array<Object>>

Convenience method to iterate repeated occurrences of a segment by iteratively calling find. Beware this doesn't check that the segment is allowed to repeat, so calling iterate(:XYZ) may yield an XYZ segment and then throw an exception when searching for the second, if XYZ is not repeatable.

Yield Parameters:

Yield Returns:

  • (Object)

Returns:



454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/stupidedi/builder/navigation.rb', line 454

def iterate(id, *elements)
  a = []
  m = find(id, *elements)
  return m unless m.defined?

  while m.defined?
    m = m.flatmap do |n|
      a << yield(n)
      n.find(id, *elements)
    end
  end

  Either.success(a)
end

#lastEither<StateMachine>

Returns a new StateMachine positioned on the last segment in the parse tree, if there are any segments in the parse tree.

Returns:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/stupidedi/builder/navigation.rb', line 247

def last
  active = roots.map do |zipper|
    state = zipper
    value = zipper.node.zipper

    until value.node.segment? or value.leaf?
      value = value.down.last
      state = state.down.last
    end

    unless value.node.segment?
      return Either.failure("no segments")
    end

    # Synchronize the two parallel state and value nodes
    unless value.eql?(state.node.zipper)
      state = state.replace(state.node.copy(:zipper => value))
    end

    state
  end

  Either.success(StateMachine.new(@config, active))
end

#last?Boolean

Is this the last segment?

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
# File 'lib/stupidedi/builder/navigation.rb', line 46

def last?
  value = @active.head.node.zipper

  until value.root?
    return false unless value.last?
    value = value.up
  end

  return true
end

#next(count = 1) ⇒ Either<StateMachine>

Returns a new StateMachine positioned on the next segment, if there is a next segment. Optionally, a count argument may be provided that indicates how many segments to advance.

Returns:



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/stupidedi/builder/navigation.rb', line 324

def next(count = 1)
  unless count > 0
    raise ArgumentError,
      "count must be positive"
  end

  active = @active.map do |zipper|
    state = zipper
    value = zipper.node.zipper

    count.times do
      while not value.root? and value.last?
        value = value.up
        state = state.up
      end

      if value.root?
        return Either.failure("cannot move to next after last segment")
      end

      value = value.next
      state = state.next

      until value.node.segment?
        value = value.down
        state = state.down
      end
    end

    # Synchronize the two parallel state and value nodes
    unless value.eql?(state.node.zipper)
      state = state.replace(state.node.copy(:zipper => value))
    end

    state
  end

  Either.success(StateMachine.new(@config, active))
end

#parentEither<StateMachine>

Returns a new StateMachine positioned on the first segment of the parent structure. For example, when the current segment belongs to a loop but it's not the first segment in that loop, this method will rewind to the first segment in the loop. If the current position is the first segment of a loop, this method will rewind to the first segment in the loop's parent structure.

Returns:



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/stupidedi/builder/navigation.rb', line 280

def parent
  active = []

  @active.each do |zipper|
    state = zipper
    value = zipper.node.zipper

    while value.first? and not value.root?
      value = value.up
      state = state.up
    end

    if value.root?
      break
    end

    value = value.first
    state = state.first

    until value.node.segment?
      value = value.down
      state = state.down
    end

    # Synchronize the two parallel state and value nodes
    unless value.eql?(state.node.zipper)
      state = state.replace(state.node.copy(:zipper => value))
    end

    active << state
  end

  if active.empty?
    Either.failure("no parent segment")
  else
    Either.success(StateMachine.new(@config, active))
  end
end

#prev(count = 1) ⇒ Either<StateMachine>

Returns a new StateMachine positioned on the previous segment, if there is a previous segment. Optionally, a count argument may be provided that indicates how many segments to rewind.

Returns:



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/stupidedi/builder/navigation.rb', line 369

def prev(count = 1)
  unless count > 0
    raise ArgumentError,
      "count must be positive"
  end

  active = @active.map do |zipper|
    state = zipper
    value = zipper.node.zipper

    count.times do
      while not value.root? and value.first?
        value = value.up
        state = state.up
      end

      if value.root?
        return Either.failure("cannot move to prev before first segment")
      end

      state = state.prev
      value = value.prev

      until value.node.segment?
        value = value.down.last
        state = state.down.last
      end
    end

    # Synchronize the two parallel state and value nodes
    unless value.eql?(state.node.zipper)
      state = state.replace(state.node.copy(:zipper => value))
    end

    state
  end

  Either.success(StateMachine.new(@config, active))
end

#segmentEither<Zipper::AbstractCursor<Values::SegmentVal>>

Extracts the segment from the current state, if the current state is deterministic and positioned on a segment.



98
99
100
101
102
103
104
105
106
# File 'lib/stupidedi/builder/navigation.rb', line 98

def segment
  zipper.flatmap do |z|
    if z.node.segment?
      Either.success(z)
    else
      Either.failure("not a segment")
    end
  end
end

#segmentnEither<Values::SegmentVal>

Extracts the segment from the current state, if the current state is deterministic and positioned on a segment.

Returns:



112
113
114
# File 'lib/stupidedi/builder/navigation.rb', line 112

def segmentn
  segment.map(&:node)
end

#sequence(pattern, *patterns) ⇒ Either<StateMachine>

Sequence multiple traversals together, by iteratively calling find. Each argument must be either a single segment ID or a list whose first element is a segment ID and remaining elements are segment constraints.

Examples:

machine.sequence(:GS, :ST, :HL)
machine.sequence(:GS, [:ST, "837"], [:HL, nil, "20"])

Returns:



479
480
481
482
483
# File 'lib/stupidedi/builder/navigation.rb', line 479

def sequence(pattern, *patterns)
  patterns.inject(find(*pattern)) do |m, p|
    m = m.flatmap{|n| n.find(*p) }
  end
end

#successorsArray<InstructionTable>

Returns:



14
15
16
# File 'lib/stupidedi/builder/navigation.rb', line 14

def successors
  @active.map{|a| a.node.instructions }
end

#zipperEither<Zipper::AbstractCursor<Values::AbstractVal>>

Returns the current position within the parse tree, if the current state is deterministic.



86
87
88
89
90
91
92
# File 'lib/stupidedi/builder/navigation.rb', line 86

def zipper
  if deterministic?
    Either.success(@active.head.node.zipper)
  else
    Either.failure("non-deterministic state")
  end
end