From a9ee1ed4643c462b25294e6510913cc47e8c67ef Mon Sep 17 00:00:00 2001 From: Nick Sutterer Date: Wed, 14 Feb 2024 17:45:47 +0100 Subject: [PATCH] Discovery is almost finished, including the stub process. --- lib/trailblazer/workflow/discovery.rb | 70 +++++++++++++++++++++++++++ test/collaboration_test.rb | 1 - 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/lib/trailblazer/workflow/discovery.rb b/lib/trailblazer/workflow/discovery.rb index b0659b7..56c5e5a 100644 --- a/lib/trailblazer/workflow/discovery.rb +++ b/lib/trailblazer/workflow/discovery.rb @@ -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, @@ -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 diff --git a/test/collaboration_test.rb b/test/collaboration_test.rb index 5f8bc7d..5db4f3d 100644 --- a/test/collaboration_test.rb +++ b/test/collaboration_test.rb @@ -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"