Skip to content

Commit

Permalink
Discovery is almost finished, including the stub process.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 14, 2024
1 parent 6eb72ec commit a9ee1ed
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
70 changes: 70 additions & 0 deletions lib/trailblazer/workflow/discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def call(collaboration, start_position:, run_multiple_times: {}, initial_lane_po
# imply we start from a public resume and discover the path?
# we could save work on {run_multiple_times} with this.

collaboration, message_flow, start_position, initial_lane_positions = stub_tasks_for(collaboration, message_flow: message_flow, start_position: start_position, initial_lane_positions: initial_lane_positions)

# pp collaboration.to_h[:lanes][:ui].to_h
# raise

resumes_to_invoke = [
[
start_position,
Expand Down Expand Up @@ -114,6 +119,71 @@ def call(collaboration, start_position:, run_multiple_times: {}, initial_lane_po

return states, additional_state_data
end

def stub_tasks_for(collaboration, ignore_class: Trailblazer::Activity::End, message_flow:, start_position:, initial_lane_positions:)
stubbed_lanes = collaboration.to_h[:lanes].collect do |lane_id, activity|
circuit = activity.to_h[:circuit]
lane_map = circuit.to_h[:map].clone

# Filter out all termini and events.
tasks_to_stub = lane_map.reject { |task, links| task.is_a?(ignore_class) }

replaced_tasks = tasks_to_stub.collect do |task, links|
node = Activity::Introspect.Nodes(activity, task: task)
label = node.data[:label]

stub_task_name = "#{lane_id}:#{label}".to_sym
stub_task = Activity::Testing.def_steps(stub_task_name).method(stub_task_name)

# raise label.inspect
[task, stub_task]
end.to_h

# Now, "replace" (rather: "add") the original task with the stub.
replaced_tasks.each do |task, stub_task|
lane_map[stub_task] = lane_map[task]

# lane_map.delete(task) # FIXME: hating this. not really necessary
end

# Then, replace connections pointing to the original task.
new_circuit_map = lane_map.collect do |task, links|
new_links = links.collect do |signal, target|
(new_target = replaced_tasks[target]) ? [signal, new_target] : [signal, target]
end.to_h

[task, new_links]
end.to_h

new_circuit = Activity::Circuit.new(new_circuit_map, circuit.to_h[:end_events], start_task: circuit.to_h[:start_task])

lane = Activity.new(Activity::Schema.new(new_circuit, activity.to_h[:outputs], activity.to_h[:nodes], activity.to_h[:config])) # FIXME: breaking taskWrap here (which is no problem, actually).

# [lane_id, lane, replaced_tasks]
[lane_id, lane]
end.to_h

old_activity_2_new_activity = collaboration.to_h[:lanes].collect { |lane_id, activity| [activity, stubbed_lanes[lane_id]] }.to_h

puts "@@@@@@@@@ activity mapping @@@@@@@@@"
pp old_activity_2_new_activity

# stubbed_tasks = stubbed_lanes.flat_map { |id, lane, replaced_tasks| replaced_tasks.to_a }.to_h # DISCUSS: do we need this?

# message_flow = collaboration.to_h[:message_flow]
new_message_flow = message_flow.collect { |throw_evt, (activity, catch_evt)| [throw_evt, [old_activity_2_new_activity[activity], catch_evt]] }.to_h

new_start_position = Collaboration::Position.new(old_activity_2_new_activity.fetch(start_position.activity), start_position.task)
# raise start_position.inspect

new_initial_lane_positions = initial_lane_positions.collect do |position|
# TODO: make lane_positions {Position} instances, too.

raise position.inspect
end

return Collaboration::Schema.new(lanes: stubbed_lanes, message_flow: new_message_flow), new_message_flow, new_start_position, new_initial_lane_positions
end
end
end
end
1 change: 0 additions & 1 deletion test/collaboration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ def render_states(states, lanes:, additional_state_data:, task_map:)




# render_states(states, lanes: ___lanes___ = {lane_activity => "lifecycle", lane_activity_ui => "UI", approver_activity => "approver"}, additional_state_data: additional_state_data, task_map: task_map)
# raise "figure out how to build a generated state table"

Expand Down

0 comments on commit a9ee1ed

Please sign in to comment.