Skip to content

Commit

Permalink
state table generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 21, 2024
1 parent 932a1a1 commit f5bbe08
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 33 deletions.
5 changes: 2 additions & 3 deletions lib/trailblazer/workflow/advance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call(ctx, event_label:, iteration_set:, message_flow:, **)
message_flow: message_flow,
)

signal = return_signal_for(configuration, iteration_set, **position_options)
signal = return_signal_for(configuration, iteration_set, **position_options,)

return signal, [ctx, flow_options]
end
Expand All @@ -41,8 +41,7 @@ def return_signal_for(configuration, iteration_set, start_task_position:, **)
# Find all recorded iterations that started with our {start_task_position}.
iterations = iteration_set.to_a.find_all { |iteration| iteration.start_task_position == start_task_position }

# raise positions_to_iteration_table.inspect

# raise state_table[start_task_position].inspect

travelled_iteration = iterations.find { |iteration| configuration.lane_positions == iteration.suspend_positions } or raise "no matching travelled path found"

Expand Down
2 changes: 1 addition & 1 deletion lib/trailblazer/workflow/introspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def lane_label_for(activity, task, lanes_cfg:, show_icon: true)
lane_options_for(activity, task, lanes_cfg: lanes_cfg)[:icon]
end

def id_for_task(lane_position)
def id_for_position(lane_position)
Trailblazer::Activity::Introspect.Nodes(lane_position.activity, task: lane_position.task).id
end

Expand Down
4 changes: 2 additions & 2 deletions lib/trailblazer/workflow/introspect/event_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def call(iteration_set, render_ids: true, hide_lanes: [], lanes_cfg:, **)
# FIXME: use developer for coloring.
# def bg_gray(str); "\e[47m#{str}\e[0m" end
if render_ids
triggered_event_id_column = {"triggered event" => Present.id_for_task(start_task_position)}
triggered_event_id_column = {"triggered event" => Present.id_for_position(start_task_position)}

rows << render_lane_position_id_columns(start_positions, lanes_cfg: lanes_cfg)
.merge(triggered_event_id_column)
Expand Down Expand Up @@ -62,7 +62,7 @@ def render_lane_position_id_columns(start_positions, **options)
lane_position_ids = start_positions.to_a.flat_map do |lane_position|
# Present.readable_id_label(*lane_position.to_a, **options)
lane_label = lane_label_for(lane_position, **options) # FIXME: redundant in {#render_lane_position_columns}
id = Present.id_for_task(lane_position)
id = Present.id_for_position(lane_position)

[
lane_label, # column name
Expand Down
3 changes: 2 additions & 1 deletion lib/trailblazer/workflow/introspect/iteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Introspect
# It is usually generated from a discovery process.
# A {Set} of {Iteration}s comprises of all possible events and outcomes. This is the interface
# for higher-level tools such as Advance, state tables, test plans or outcome deciders to work with.
# While this set can be auto-discovered, it technically could be hand-crafted, but, why?
#
# DISCUSS: maybe the naming/namespace will change.
# maybe Introspect::Iteration?
Expand All @@ -14,7 +15,7 @@ def self.from_discovered_states(discovered_states, **options)
iterations = discovered_states.collect do |row|
triggered_catch_event_position = row[:positions_before][1]

id = Introspect::Present.id_for_task(triggered_catch_event_position)
id = Introspect::Present.id_for_position(triggered_catch_event_position)
event_label = Test::Plan::CommentHeader.start_position_label(row[:positions_before][1], row, **options)

Iteration.new(
Expand Down
69 changes: 48 additions & 21 deletions lib/trailblazer/workflow/introspect/state_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,7 @@ module StateTable
def call(iteration_set, lanes_cfg:)
states = aggregate_by_state(iteration_set)

# render
cli_rows = states.flat_map do |positions, 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
cli_rows = render_data(states, lanes_cfg: lanes_cfg)

columns = ["state name", "triggerable events"]
Present::Table.render(columns, cli_rows)
Expand Down Expand Up @@ -58,13 +39,59 @@ def aggregate_by_state(iteration_set)
states
end

def render_data(states, lanes_cfg:)
cli_rows = states.flat_map do |positions, 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,

:positions,
positions,

:catch_events,
catch_events
]
end
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("/")
.join("♦")
end

# Generate code stubs for a state guard class.
module Generate
module_function

def call(iteration_set, **options)
states = StateTable.aggregate_by_state(iteration_set)
cli_rows = StateTable.render_data(states, **options)

cli_rows.collect do |row|
{
suggested_state_name: row["state name"],
id: row[:catch_events].collect { |position| Present.id_for_position(position) }.sort

}
end.join("\n")
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/advance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AdvanceTest < Minitest::Spec

iteration_set = Trailblazer::Workflow::Introspect::Iteration::Set.from_discovered_states(states, lanes_cfg: lanes_cfg)
# "states"
positions_to_iteration_table = Trailblazer::Workflow::Introspect::StateTable.aggregate_by_state(iteration_set)
state_table = Trailblazer::Workflow::Introspect::StateTable.aggregate_by_state(iteration_set)

ctx = {params: [], seq: []}

Expand All @@ -26,7 +26,7 @@ class AdvanceTest < Minitest::Spec
iteration_set: iteration_set, # this is basically the "dictionary" for lookups of positions.
state_guards: {}, # TODO: design/implement this.

positions_to_iteration_table: positions_to_iteration_table,
state_table: state_table,
)

assert_equal signal.inspect, %(Trailblazer::Activity::Right)
Expand Down
6 changes: 3 additions & 3 deletions test/discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ def assert_position_after(actual_configuration, expected_ids, lanes:)
+---------------------------------+----------------------------------------+
| "⏸︎ Create form" | "☝ ⏵︎Create form" |
| "⏸︎ Create" | "☝ ⏵︎Create" |
| "⏸︎ Update form/Notify approver" | "☝ ⏵︎Update form", "☝ ⏵︎Notify approver" |
| "⏸︎ Update formNotify approver" | "☝ ⏵︎Update form", "☝ ⏵︎Notify approver" |
| "⏸︎ Update" | "☝ ⏵︎Update" |
| "⏸︎ Delete? form/Publish" | "☝ ⏵︎Delete? form", "☝ ⏵︎Publish" |
| "⏸︎ Delete? formPublish" | "☝ ⏵︎Delete? form", "☝ ⏵︎Publish" |
| "⏸︎ Revise form" | "☝ ⏵︎Revise form" |
| "⏸︎ Delete/Cancel" | "☝ ⏵︎Delete", "☝ ⏵︎Cancel" |
| "⏸︎ DeleteCancel" | "☝ ⏵︎Delete", "☝ ⏵︎Cancel" |
| "⏸︎ Archive" | "☝ ⏵︎Archive" |
| "⏸︎ Revise" | "☝ ⏵︎Revise" |
+---------------------------------+----------------------------------------+)
Expand Down

0 comments on commit f5bbe08

Please sign in to comment.