diff --git a/lib/trailblazer/workflow.rb b/lib/trailblazer/workflow.rb index 756bdc0..93dce56 100644 --- a/lib/trailblazer/workflow.rb +++ b/lib/trailblazer/workflow.rb @@ -19,3 +19,5 @@ module Workflow require "trailblazer/workflow/discovery" require "trailblazer/workflow/discovery/present" +require "trailblazer/workflow/discovery/present/state_table" +require "trailblazer/workflow/discovery/present/event_table" diff --git a/lib/trailblazer/workflow/discovery/present.rb b/lib/trailblazer/workflow/discovery/present.rb index 9ccfb3a..eceb497 100644 --- a/lib/trailblazer/workflow/discovery/present.rb +++ b/lib/trailblazer/workflow/discovery/present.rb @@ -12,7 +12,6 @@ def label_for_next_task(activity, catch_event) Trailblazer::Activity::Introspect.Nodes(activity, task: task_after_catch).data[:label] || task_after_catch end - def readable_name_for_catch_event(activity, catch_event, lanes_cfg: {}) envelope_icon = "(✉)➔" # TODO: implement {envelope_icon} flag. envelope_icon = "▶" @@ -37,77 +36,7 @@ def lane_options_for(activity, task, lanes_cfg:) lanes_cfg.values.find { |options| options[:activity] == activity } or raise end - # Each row represents a configuration of suspends aka "state". - # The state knows its possible resume events. - # does the state know which state fields belong to it? - # - # TODO: move that to separate module {StateTable.call}. - def render_cli_state_table(discovered_states, lanes_cfg:) - # raise discovery_state_table.inspect - start_position_to_catch = {} - - # Key by lane_positions, which represent a state. - # State (lane_positions) => [events (start position)] - states = {} - - # Collect the invoked start positions per Positions configuration. - # This implies the possible "catch events" per configuration. - discovered_states.each do |row| - positions_before, start_position = row[:positions_before] - - # raise positions_before.inspect - # puts positions_before.to_a.collect { |p| - # # puts "@@@@@ #{p.inspect}" - # next if p.task.to_h["resumes"].nil? - # resumes_from_suspend(*p).collect { |catch_event| readable_name_for_catch_event(p.activity, catch_event, lanes_cfg: lanes_cfg) } - - # }.inspect - - events = states[positions_before] - events = [] if events.nil? - - events << start_position - - states[positions_before] = events - end - - # render - cli_rows = states.flat_map do |configuration, catch_events| - suggested_state_name = suggested_state_name_for(catch_events) - suggested_state_name = "⛊ #{suggested_state_name}".inspect - - triggerable_events = catch_events - .collect { |event_position| readable_name_for_catch_event(*event_position.to_a, lanes_cfg: lanes_cfg).inspect } - .uniq - .join(", ") - - - Hash[ - "state name", - suggested_state_name, - - "triggerable events", - triggerable_events - ] - end - - Hirb::Helpers::Table.render(cli_rows, fields: [ - "state name", - "triggerable events", - # *lane_ids, - ], - max_width: 186, - ) # 186 for laptop 13" - end - - # TODO: move to StateTable - def suggested_state_name_for(catch_events) - catch_events - .collect { |event_position| label_for_next_task(*event_position.to_a) } - .uniq - .join("/") - end end end end diff --git a/lib/trailblazer/workflow/discovery/present/event_table.rb b/lib/trailblazer/workflow/discovery/present/event_table.rb new file mode 100644 index 0000000..27b8d3c --- /dev/null +++ b/lib/trailblazer/workflow/discovery/present/event_table.rb @@ -0,0 +1,13 @@ +module Trailblazer + module Workflow + module Discovery + # Rendering-specific code using {Discovery:states}. + module Present + module EventTable + module_function + + end + end + end + end +end diff --git a/lib/trailblazer/workflow/discovery/present/state_table.rb b/lib/trailblazer/workflow/discovery/present/state_table.rb new file mode 100644 index 0000000..2ab6fa4 --- /dev/null +++ b/lib/trailblazer/workflow/discovery/present/state_table.rb @@ -0,0 +1,77 @@ +module Trailblazer + module Workflow + module Discovery + # Rendering-specific code using {Discovery:states}. + module Present + module StateTable + module_function + + # Each row represents a configuration of suspends aka "state". + # The state knows its possible resume events. + # does the state know which state fields belong to it? + # + # TODO: move that to separate module {StateTable.call}. + def call(discovered_states, lanes_cfg:) + # raise discovery_state_table.inspect + start_position_to_catch = {} + + # Key by lane_positions, which represent a state. + # State (lane_positions) => [events (start position)] + states = {} + + # Collect the invoked start positions per Positions configuration. + # This implies the possible "catch events" per configuration. + discovered_states.each do |row| + positions_before, start_position = row[:positions_before] + + events = states[positions_before] + events = [] if events.nil? + + events << start_position + + states[positions_before] = events + end + + # render + cli_rows = states.flat_map do |configuration, catch_events| + suggested_state_name = suggested_state_name_for(catch_events) + + suggested_state_name = "⛊ #{suggested_state_name}".inspect + + triggerable_events = catch_events + .collect { |event_position| Present.readable_name_for_catch_event(*event_position.to_a, lanes_cfg: lanes_cfg).inspect } + .uniq + .join(", ") + + + Hash[ + "state name", + suggested_state_name, + + "triggerable events", + triggerable_events + ] + end + + Hirb::Helpers::Table.render(cli_rows, fields: [ + "state name", + "triggerable events", + # *lane_ids, + ], + max_width: 186, + ) # 186 for laptop 13" + end + + # @private + # TODO: move to StateTable + def suggested_state_name_for(catch_events) + catch_events + .collect { |event_position| Present.label_for_next_task(*event_position.to_a) } + .uniq + .join("/") + end + end + end + end + end +end diff --git a/test/collaboration_test.rb b/test/collaboration_test.rb index 358d5f8..996c80d 100644 --- a/test/collaboration_test.rb +++ b/test/collaboration_test.rb @@ -99,29 +99,6 @@ def render_states(states, lanes:, additional_state_data:, task_map:) approver_activity = lanes[:approver] - - # DISCUSS: technically, this is an event table, not a state table. - # state_table = Trailblazer::Workflow::State::Discovery.generate_state_table(states, lanes: lanes_cfg) - - cli_state_table = Trailblazer::Workflow::State::Discovery.render_cli_state_table(states, lanes: lanes_cfg) - puts cli_state_table - assert_equal cli_state_table, -%(+---------------------------------+--------------------------------------------+ -| state name | triggerable events | -+---------------------------------+--------------------------------------------+ -| "> Create form" | "UI: ▶Create form" | -| "> Create" | "UI: ▶Create" | -| "> Update form/Notify approver" | "UI: ▶Update form", "UI: ▶Notify approver" | -| "> Update" | "UI: ▶Update" | -| "> Delete? form/Publish" | "UI: ▶Delete? form", "UI: ▶Publish" | -| "> Revise form" | "UI: ▶Revise form" | -| "> Delete/Cancel" | "UI: ▶Delete", "UI: ▶Cancel" | -| "> Archive" | "UI: ▶Archive" | -| "> Revise" | "UI: ▶Revise" | -+---------------------------------+--------------------------------------------+ -9 rows in set) - - # currently, from this table we can read the discovery process, what states were discovered and what start lane positions those states imply. # we still have redundant states here, as the discovery algorithm was instructed to invoke several events multiple times. cli_state_table = Trailblazer::Workflow::State::Discovery.render_cli_event_table(state_table) diff --git a/test/discovery_test.rb b/test/discovery_test.rb index 3381972..13a94e6 100644 --- a/test/discovery_test.rb +++ b/test/discovery_test.rb @@ -235,7 +235,7 @@ def assert_position_after(actual_configuration, expected_ids, lanes:) # DISCUSS: technically, this is an event table, not a state table. # state_table = Trailblazer::Workflow::State::Discovery.generate_state_table(states, lanes: lanes_cfg) - cli_state_table = Trailblazer::Workflow::Discovery::Present.render_cli_state_table(states, lanes_cfg: lanes_cfg) + cli_state_table = Trailblazer::Workflow::Discovery::Present::StateTable.(states, lanes_cfg: lanes_cfg) puts cli_state_table assert_equal cli_state_table, %(+---------------------------------+----------------------------------------+