Class: Wolflow::StructuredLoop

Inherits:
ExclusiveChoice show all
Defined in:
lib/wolflow/structured_loop.rb

Instance Attribute Summary

Attributes inherited from ExclusiveChoice

#else_tasks

Attributes inherited from MultiChoice

#condition_next_tasks

Attributes inherited from TaskSpec

#connects_to, #id, #name, #prev_tasks, #workflow_spec

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ExclusiveChoice

#connect_else, #initialize, #inspect, #next_tasks, #precedes?, #to_hash_tree, #workflow_spec=

Methods inherited from MultiChoice

#build_next_tasks, #initialize, #inspect, #next_tasks, #precedes?, #to_hash_tree, #workflow_spec=

Methods inherited from TaskSpec

#child_of?, #each_ancestor, #each_parent, #each_successor, inherited, #initialize

Constructor Details

This class inherits a constructor from Wolflow::ExclusiveChoice

Class Method Details

.from_hash(hash) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wolflow/structured_loop.rb', line 81

def from_hash(hash)
  case hash
  in {
    reset_task: [{ type: String, **}, TaskSpec],
    else_tasks: [*, TaskSpec, *] => else_tasks,
    **args
   }
    super
  in {
    reset_task: [{ type: String, **}, String] => reset_task,
    else_tasks: [*, String, *] => else_tasks,
    **args
    }
    spec = super(**args)
    spec.connects_to = {
      reset_task: reset_task,
      else_tasks: else_tasks
    }
    spec
  else # rubocop:disable Lint/DuplicateBranch
    super
  end
end

Instance Method Details

#connect(condition, *task_specs) ⇒ Object

Raises:



5
6
7
8
9
10
11
12
13
14
# File 'lib/wolflow/structured_loop.rb', line 5

def connect(condition, *task_specs)
  raise TaskSpecError, "can only link with one parent" unless task_specs.size == 1

  # @type var task_spec: TaskSpec
  task_spec = task_specs.first

  raise TaskSpecError, "can only link with parent" unless child_of?(task_spec)

  super
end

#connects_with(tasks) ⇒ Object

private-ish



49
50
51
52
53
54
55
56
57
58
# File 'lib/wolflow/structured_loop.rb', line 49

def connects_with(tasks)
  return unless @connects_to

  cond, id = @connects_to.fetch(:reset_task)
  cond = Operators.from_hash(cond)
  reset_task = tasks[id]
  reset_task.connects_with(tasks)
  connect(cond, reset_task)
  connect_else(*@connects_to.fetch(:else_tasks).map(&tasks.method(:[])))
end

#each_child(task, &blk) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/wolflow/structured_loop.rb', line 40

def each_child(task, &blk)
  super(task) do |child|
    next unless task.task_spec.else_tasks.include?(child.task_spec)

    blk.call(child)
  end
end

#on_complete(task) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wolflow/structured_loop.rb', line 16

def on_complete(task)
  @condition_next_tasks.each do |cond, next_tasks|
    next unless cond.call(task)

    parent_task = task.children.find { |child_task| next_tasks.include?(child_task.task_spec) }

    raise Error, "parent task not found for #{task}" unless parent_task

    reset_task(parent_task)

    return # rubocop:disable Lint/NonLocalExitFromIterator
  end
  task.mark_as_complete!

  predict(task, @else_tasks)
end

#to_hashObject



33
34
35
36
37
38
# File 'lib/wolflow/structured_loop.rb', line 33

def to_hash
  hs = super
  hs[:reset_task] = hs.delete(:condition_next_tasks).first
  hs[:else_tasks] = hs.delete(:else_tasks)
  hs
end