Skip to content

Commit

Permalink
introduce Lanes and use lanes_cfg everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 23, 2024
1 parent 82486e0 commit 4ffc279
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 117 deletions.
4 changes: 3 additions & 1 deletion lib/trailblazer/workflow/collaboration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def ___FIXME_2BRM_initial_lane_positions(lanes)
end

def initial_lane_positions(lanes)
lanes.collect do |lane_id, activity|
lanes.to_h.collect do |_, cfg|
activity = cfg[:activity]

# start_catch_event_task = activity.to_h[:circuit].to_h[:start_task]
# FIXME: in the next pro version, the "start suspend" will be here instead of its catch event.
start_catch_event_id = Trailblazer::Activity::Introspect.Nodes(activity, task: activity.to_h[:circuit].to_h[:start_task]).id # DISCUSS: store IDs or the actual catch event in {:resumes}?
Expand Down
14 changes: 7 additions & 7 deletions lib/trailblazer/workflow/collaboration/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class Collaboration
#
# Return a hash, key is always a throw event (the actual task instance),
# value the [activity, catch_event] tuple.
def self.Messages(messages, id_to_lane)
def self.Messages(messages, lanes)
messages.collect do |throw_path, catch_path| # [["article moderation", "Event_0odjl3c"], ["<ui> author workflow", "Event_0co8ygx"]]
# throw
activity_id, task_id = throw_path
_, throw_task = find_task_for_ids(activity_id, task_id, id_to_lane)
activity_json_id, task_id = throw_path
_, throw_task = find_task_for_ids(activity_json_id, task_id, lanes)

# catch
activity_id, task_id = catch_path
catch_path = find_task_for_ids(activity_id, task_id, id_to_lane)
activity_json_id, task_id = catch_path
catch_path = find_task_for_ids(activity_json_id, task_id, lanes)

# thrower => [activity, catcher]
[throw_task, catch_path]
Expand All @@ -22,8 +22,8 @@ def self.Messages(messages, id_to_lane)
end

# Return [activity, task] for ID tuple.
def self.find_task_for_ids(activity_id, task_id, id_to_lane)
activity = id_to_lane.fetch(activity_id) # TODO: test exception.
def self.find_task_for_ids(activity_json_id, task_id, lanes)
activity = lanes.(json_id: activity_json_id)[:activity] # TODO: test exception.

return activity, Activity::Introspect.Nodes(activity, id: task_id).task
end
Expand Down
21 changes: 12 additions & 9 deletions lib/trailblazer/workflow/discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ def call(collaboration, start_task_position:, run_multiple_times: {}, initial_la

collaboration, message_flow, start_task_position, initial_lane_positions, activity_2_stub, original_task_2_stub_task = stub_tasks_for(collaboration, message_flow: message_flow, start_task_position: start_task_position, initial_lane_positions: initial_lane_positions)

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


resumes_to_invoke = [
[
Expand Down Expand Up @@ -127,7 +124,10 @@ def call(collaboration, start_task_position:, run_multiple_times: {}, initial_la
end

def stub_tasks_for(collaboration, ignore_class: Trailblazer::Activity::End, message_flow:, start_task_position:, initial_lane_positions:)
collected = collaboration.to_h[:lanes].collect do |lane_id, activity|
lanes = collaboration.to_h[:lanes].to_h.values # FIXME: don't use {collaboration} here!

collected = lanes.collect do |lane_cfg|
activity = lane_cfg[:activity]
circuit = activity.to_h[:circuit]
lane_map = circuit.to_h[:map].clone

Expand All @@ -138,7 +138,7 @@ def stub_tasks_for(collaboration, ignore_class: Trailblazer::Activity::End, mess
node = Activity::Introspect.Nodes(activity, task: task)
label = node.data[:label] || node.id # TODO: test this case, when there's no {:label}.

stub_task_name = "#{lane_id}:#{label}".to_sym
stub_task_name = "#{lane_cfg[:label]}:#{label}".to_sym
stub_task = Activity::Testing.def_tasks(stub_task_name).method(stub_task_name)

# raise label.inspect
Expand Down Expand Up @@ -177,15 +177,15 @@ def stub_tasks_for(collaboration, ignore_class: Trailblazer::Activity::End, mess

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

[[lane_id, lane], replaced_tasks]
[[lane_cfg[:label], lane], replaced_tasks]
end

# DISCUSS: interestingly, we don't need this map as all stubbed tasks are user tasks, and positions always reference suspends or catch events, which arent' stubbed.
original_task_2_stub_task = collected.inject({}) { |memo, (_, replaced_tasks)| memo.merge(replaced_tasks) }

stubbed_lanes = collected.collect { |lane, _| lane }.to_h
stubbed_lanes = collected.collect { |lane_label_2_activity, _| lane_label_2_activity }.to_h

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

new_message_flow = message_flow.collect { |throw_evt, (activity, catch_evt)| [throw_evt, [activity_2_stub[activity], catch_evt]] }.to_h

Expand All @@ -203,17 +203,20 @@ def stub_tasks_for(collaboration, ignore_class: Trailblazer::Activity::End, mess

# Get the original lane activity and tasks for a {Positions} set from the stubbed ones.
def unstub_positions(activity_2_stub, original_task_2_stub_task, positions, lanes: {})
lane_activities = lanes.to_h.collect { |_, lane_cfg| lane_cfg[:activity] }

real_positions = positions.to_a.collect do |position|
Collaboration::Position.new(
activity_2_stub.invert.fetch(position.activity),
position.task # since the task will always be a suspend, a resume or terminus, we can safely use the stubbed one, which is identical to the original.
)
end.sort { |a, b| lanes.values.index(a.activity) <=> lanes.values.index(b.activity) }
end.sort { |a, b| lane_activities.index(a.activity) <=> lane_activities.index(b.activity) }

Collaboration::Positions.new(real_positions)
end

def unstub_configuration(activity_2_stub, configuration, lanes:)
puts "@@@@@ #{lanes.inspect}"
real_lane_positions = unstub_positions(activity_2_stub, nil, configuration.lane_positions, lanes: lanes)

real_last_lane = activity_2_stub[configuration.last_lane]
Expand Down
29 changes: 24 additions & 5 deletions lib/trailblazer/workflow/introspect.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
module Trailblazer
module Workflow
module Introspect
class Lanes
def initialize(lanes_cfg)
ary = lanes_cfg.collect { |json_id, cfg| cfg.merge(json_id: json_id) }

@ary = ary
end

def call(**options)
key, value = options.keys[0], options.values[0]

@ary.find { |options| options[key] == value } or raise
end

def to_h
@ary.collect { |cfg| [cfg[:json_id], cfg] }.to_h
end
end

# Rendering-specific code using {Discovery:states}.
# https://stackoverflow.com/questions/22885702/html-for-the-pause-symbol-in-audio-and-video-control
module Present
Expand Down Expand Up @@ -56,13 +74,14 @@ def readable_name_for_suspend_or_terminus(activity, event, **options)
end
end

def lane_options_for(activity, task, lanes_cfg:)
lanes_cfg.values.find { |options| options[:activity] == activity } or raise
end

#if lane_icons.key?(lane_name) # TODO: handle default!
def lane_label_for(activity, task, lanes_cfg:, show_icon: true)
lane_options_for(activity, task, lanes_cfg: lanes_cfg)[:icon]
lanes_cfg.(activity: activity)[:icon]
end

def lane_options_for_position(position, lanes_cfg:, **)
activity, _ = position.to_a
lanes_cfg.(activity: activity)
end

def id_for_position(lane_position)
Expand Down
6 changes: 3 additions & 3 deletions lib/trailblazer/workflow/introspect/event_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def call(iteration_set, render_ids: true, hide_lanes: [], lanes_cfg:, **)
end


lane_labels = lanes_cfg.collect { |id, cfg| cfg[:label] }
lane_labels = lanes_cfg.to_h.collect { |_, cfg| cfg[:label] }

lane_labels = lane_labels - hide_lanes # TODO: extract, new feature.

Expand All @@ -47,7 +47,7 @@ def call(iteration_set, render_ids: true, hide_lanes: [], lanes_cfg:, **)
# @private
def render_lane_position_columns(start_positions, **options)
lane_positions = start_positions.to_a.flat_map do |lane_position|
lane_label = Present.lane_options_for(*lane_position.to_a, **options)[:label]
lane_label = Present.lane_options_for_position(lane_position, **options)[:label]

readable_lane_position = Present.readable_name_for_suspend_or_terminus(*lane_position.to_a, **options)

Expand Down Expand Up @@ -77,7 +77,7 @@ def render_lane_position_id_columns(start_positions, **options)


def lane_label_for(lane_position, **options)
Present.lane_options_for(*lane_position.to_a, **options)[:label]
Present.lane_options_for_position(lane_position, **options)[:label]
end
end
end # Introspect
Expand Down
4 changes: 2 additions & 2 deletions lib/trailblazer/workflow/introspect/iteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def serialize_suspend_positions(start_positions, **options)
end

def id_tuple_for(activity, task, lanes_cfg:)
activity_id = lanes_cfg.values.find { |cfg| cfg[:activity] == activity }[:label]
activity_id = lanes_cfg.(activity: activity)[:label]
task_id = Trailblazer::Activity::Introspect.Nodes(activity, task: task).id

return activity_id, task_id
Expand Down Expand Up @@ -111,7 +111,7 @@ module Deserialize

def call(structure, lanes_cfg:)
iterations = structure.collect do |attributes|
label_2_activity = lanes_cfg.values.collect { |cfg| [cfg[:label], cfg[:activity]] }.to_h
label_2_activity = lanes_cfg.to_h.values.collect { |cfg| [cfg[:label], cfg[:activity]] }.to_h

Iteration.new(
attributes["id"],
Expand Down
8 changes: 4 additions & 4 deletions lib/trailblazer/workflow/test/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def assert_positions(asserted_positions, expected_positions, lanes_cfg:, test_pl
end

# Compile error message when the expected lane position doesn't match the actual one.
def self.error_message_for(position, expected_position, lanes_cfg:) # TODO: test me.
def self.error_message_for(position, expected_position, **options) # TODO: test me.
# TODO: make the labels use UTF8 icons etc, as in the CLI rendering code.
expected_label = Introspect::Present.readable_name_for_suspend_or_terminus(*position.to_a, lanes_cfg: lanes_cfg)
actual_label = Introspect::Present.readable_name_for_suspend_or_terminus(*expected_position.to_a, lanes_cfg: lanes_cfg)
expected_label = Introspect::Present.readable_name_for_suspend_or_terminus(*position.to_a, **options)
actual_label = Introspect::Present.readable_name_for_suspend_or_terminus(*expected_position.to_a, **options)

lane_label = Introspect::Present.lane_options_for(*position.to_a, lanes_cfg: lanes_cfg)[:label]
lane_label = Introspect::Present.lane_options_for_position(position, **options)[:label]

"Lane #{lane_label}:\n expected #{expected_label}\n actual #{actual_label}"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/trailblazer/workflow/test/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def render_combined_column_labels(positions_rows, **options)
end

def compute_combined_column_widths(position_rows, lanes_cfg:, **)
chars_to_filter = Introspect::Present::ICONS.values + lanes_cfg.collect { |_, cfg| cfg[:icon] } # TODO: do this way up in the code path.
chars_to_filter = Introspect::Present::ICONS.values + lanes_cfg.to_h.collect { |_, cfg| cfg[:icon] } # TODO: do this way up in the code path.

# Find out the longest entry per lane.
columns = lanes_cfg.collect { |_, cfg| [cfg[:activity], []] }.to_h # {<lifecycle> => [], ...}
columns = lanes_cfg.to_h.collect { |_, cfg| [cfg[:activity], []] }.to_h # {<lifecycle> => [], ...}

position_rows.each do |event_labels|
event_labels.each do |(activity, suspend_label)|
Expand Down
2 changes: 1 addition & 1 deletion test/advance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AdvanceTest < Minitest::Spec

it "what" do
# TODO: this is something that shouldn't be done every time.
states, lanes_sorted, lanes_cfg, schema, message_flow = states()
states, schema, lanes_cfg, message_flow = states()

iteration_set = Trailblazer::Workflow::Introspect::Iteration::Set.from_discovered_states(states, lanes_cfg: lanes_cfg)

Expand Down
Loading

0 comments on commit 4ffc279

Please sign in to comment.