Skip to content

Commit

Permalink
move code to Present::StateTable.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 17, 2024
1 parent fefef27 commit 59523cd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 95 deletions.
2 changes: 2 additions & 0 deletions lib/trailblazer/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
71 changes: 0 additions & 71 deletions lib/trailblazer/workflow/discovery/present.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "▶"
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/trailblazer/workflow/discovery/present/event_table.rb
Original file line number Diff line number Diff line change
@@ -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
77 changes: 77 additions & 0 deletions lib/trailblazer/workflow/discovery/present/state_table.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 0 additions & 23 deletions test/collaboration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
%(+---------------------------------+----------------------------------------+
Expand Down

0 comments on commit 59523cd

Please sign in to comment.