Skip to content

Commit

Permalink
start introducing Positions and Position.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 9, 2024
1 parent 38346e8 commit bf25c28
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 212 deletions.
50 changes: 46 additions & 4 deletions lib/trailblazer/workflow/collaboration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,50 @@ def to_h
task: task
}
end

def to_a
[
activity,
task
]
end
end

# DISCUSS: is lane_positions same as Configuration?
class Positions
def initialize(positions)
@positions = positions

@activity_to_task = positions.collect { |position| [position.activity, position.task] }.to_h
freeze
end

def [](activity) # TODO: do that mapping in constructor.
@activity_to_task[activity]
end

def replace(activity, task)
replaced_position = @positions.find { |position| position.activity == activity }

new_positions = @positions - [replaced_position] + [Position.new(activity, task)]

Positions.new(new_positions)
end

def to_a
@positions
end

# Iterate [activity, task] directly, without the {Position} instance.
def collect(&block)
@positions
.collect { |position| position.to_a }
.collect(&block)
end
end



# DISCUSS: rename to State or something better matching?
# Keeps the collaboration lane positions after running, it's a "state"
class Configuration < Struct.new(:lane_positions, :signal, :ctx, :flow_options, :last_lane, keyword_init: true) # DISCUSS: should we keep ctx/flow_options?
Expand Down Expand Up @@ -119,13 +161,13 @@ def advance(collaboration, (ctx, flow), circuit_options, lane_positions:, start_
# {:activity} and {:task} are the targeted position.
def validate_targeted_position(lane_positions, activity:, task:)
# the *actual* receiver position, where we're currently.
actual_receiver_position = lane_positions[activity] # receiver should always be in a suspend task/event gateway.
actual_receiver_task = lane_positions[activity] # receiver should always be in a suspend task/event gateway.

# puts "######### #{} | #{task}
# >>>>>>>>> #{actual_receiver_position}"
# >>>>>>>>> #{actual_receiver_task}"
# puts

reachable_catch_events = actual_receiver_position.to_h["resumes"]
reachable_catch_events = actual_receiver_task.to_h["resumes"]
.collect { |catch_id| Trailblazer::Activity::Introspect.Nodes(activity, id: catch_id).task }

# if possible_catch_event_ids =
Expand All @@ -138,7 +180,7 @@ def validate_targeted_position(lane_positions, activity:, task:)
# @private
# @param signal Workflow::Event::Suspend
def advance_position(lane_positions, activity, suspend_event)
lane_positions.merge(activity => suspend_event)
lane_positions.replace(activity, suspend_event)
end


Expand Down
5 changes: 2 additions & 3 deletions lib/trailblazer/workflow/state/discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def self.generate_state_table(discovery_states, lanes:)
# catch_event =

# Go through each lane.
lane_position_tuples = lane_positions.flat_map do |lane_position|
lane_position_tuples = lane_positions.to_a.collect do |lane_position|
next if lane_position.nil? # FIXME: why do we have that?


Expand Down Expand Up @@ -246,11 +246,10 @@ def self.generate_from(states)
rows = states.collect do |state|

lane_positions, start_position = state # DISCUSS: introduce a Discovery::State object?

# triggered_catch_event_id = Trailblazer::Activity::Introspect.Nodes(start_position.activity, task: start_position.task).id

# Go through each lane.
row = lane_positions.flat_map do |activity, suspend|
row = lane_positions.collect do |activity, suspend|
next if suspend.to_h["resumes"].nil?

resumes = resumes_from_suspend(activity, suspend)
Expand Down
8 changes: 6 additions & 2 deletions lib/trailblazer/workflow/state/discovery/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ def self.format_start_positions_for(rows, column_name:, formatted_col_name:, lan

columns_2_length = columns.collect { |lane_label, lengths| [lane_label, lengths.sort.last] }.to_h

# TODO: always same col order!!!
rows = rows.collect do |row|
columns = row[column_name].collect do |lane_label, catch_label|
position_columns = Hash[row[column_name]]

sorted_columns = lane_icons.keys.collect { |name, _| [name, position_columns[name]] }


columns = sorted_columns.collect do |lane_label, catch_label|
col_length = columns_2_length[lane_label]
lane_label = lane_icons[lane_label]

Expand Down
2 changes: 2 additions & 0 deletions lib/trailblazer/workflow/test/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def self.for(test_structure, input:)
"Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *#{row[:tuple].inspect})"
end.join(", ")}]
lane_positions = Trailblazer::Workflow::Collaboration::Positions.new(lane_positions)
configuration, (ctx, flow) = Trailblazer::Workflow::Collaboration::Synchronous.advance(
schema,
[{seq: []}, {throw: []}],
Expand Down
37 changes: 22 additions & 15 deletions test/collaboration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def decider(ctx, decision: true, **)
extended_initial_lane_positions = initial_lane_positions.merge(
approver_activity => approver_start_suspend
)
extended_initial_lane_positions = Trailblazer::Workflow::Collaboration::Positions.new(extended_initial_lane_positions.collect { |activity, task| Trailblazer::Workflow::Collaboration::Position.new(activity, task) })


# State discovery:
# The idea is that we collect suspend events and follow up on all their resumes (catch) events.
Expand Down Expand Up @@ -382,6 +384,8 @@ def render_states(states, lanes:, additional_state_data:, task_map:)
], max_width: 186) # 186 for laptop 13"
end



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"

Expand Down Expand Up @@ -547,21 +551,21 @@ def render_states(states, lanes:, additional_state_data:, task_map:)
%(+----------------------+--------------------------------------------------------------------------------+--------------------------------------------------------------------------------+
| triggered catch | start_configuration_formatted | expected_lane_positions_formatted |
+----------------------+--------------------------------------------------------------------------------+--------------------------------------------------------------------------------+
| ☝ ▶Create form | ▶Create ☝ ▶Create form ☑ ▶#<Trail... | ▶Create ☝ ▶Create ☑ ▶#<Trail... |
| ☝ ▶Create | ▶Create ☝ ▶Create ☑ ▶#<Trail... | ▶Update ▶Notify approver ☝ ▶Update form ▶Notify approver ☑ ▶#<Trail... |
| ☝ ▶Create ⛞ | ▶Create ☝ ▶Create ☑ ▶#<Trail... | ▶Create ☝ ▶Create ☑ ▶#<Trail... |
| ☝ ▶Update form | ▶Update ▶Notify approver ☝ ▶Update form ▶Notify approver ☑ ▶#<Trail... | ▶Update ▶Notify approver ☝ ▶Update ☑ ▶#<Trail... |
| ☝ ▶Notify approver | ▶Update ▶Notify approver ☝ ▶Update form ▶Notify approver ☑ ▶#<Trail... | ⛾ ▶Publish ▶Delete ▶Update ☝ ▶Update form ▶Delete? form ▶Publish ☑ ◉End.fai... |
| ☝ ▶Update | ▶Update ▶Notify approver ☝ ▶Update ☑ ▶#<Trail... | ⛾ ▶Notify approver ▶Update ☝ ▶Update form ▶Notify approver ☑ ▶#<Trail... |
| ☝ ▶Notify approver ⛞ | ▶Update ▶Notify approver ☝ ▶Update form ▶Notify approver ☑ ▶#<Trail... | ▶Revise ☝ ▶Revise form ☑ ◉End.suc... |
| ☝ ▶Delete? form | ⛾ ▶Publish ▶Delete ▶Update ☝ ▶Update form ▶Delete? form ▶Publish | ⛾ ▶Publish ▶Delete ▶Update ☝ ▶Delete ▶Cancel ☑ ◉End.fai... |
| ☝ ▶Publish | ⛾ ▶Publish ▶Delete ▶Update ☝ ▶Update form ▶Delete? form ▶Publish | ▶Archive ☝ ▶Archive ☑ ◉End.fai... |
| ☝ ▶Update ⛞ | ▶Update ▶Notify approver ☝ ▶Update ☑ ▶#<Trail... | ▶Update ▶Notify approver ☝ ▶Update ☑ ▶#<Trail... |
| ☝ ▶Revise form | ▶Revise ☝ ▶Revise form | ⛾ ▶Revise ☝ ▶Revise ☑ ◉End.suc... |
| ☝ ▶Delete | ⛾ ▶Publish ▶Delete ▶Update ☝ ▶Delete ▶Cancel | ◉End.success ☝ ◉End.success ☑ ◉End.fai... |
| ☝ ▶Cancel | ⛾ ▶Publish ▶Delete ▶Update ☝ ▶Delete ▶Cancel | ⛾ ▶Publish ▶Delete ▶Update ☝ ▶Update form ▶Delete? form ▶Publish ☑ ◉End.fai... |
| ☝ ▶Archive | ▶Archive ☝ ▶Archive | ⛾ ◉End.success ☝ ◉End.success ☑ ◉End.fai... |
| ☝ ▶Revise | ▶Revise ☝ ▶Revise | ⛾ ▶Revise ▶Notify approver ☝ ▶Update form ▶Notify approver ☑ ◉End.suc... |
| ☝ ▶Create form | ▶Create form ⛾ ▶Create ☑ ▶#<Trail... | ▶Create ⛾ ▶Create ☑ ▶#<Trail... |
| ☝ ▶Create | ▶Create ⛾ ▶Create ☑ ▶#<Trail... | ▶Update form ▶Notify approver ⛾ ▶Update ▶Notify approver ☑ ▶#<Trail... |
| ☝ ▶Create ⛞ | ▶Create ⛾ ▶Create ☑ ▶#<Trail... | ▶Create ⛾ ▶Create ☑ ▶#<Trail... |
| ☝ ▶Update form | ▶Update form ▶Notify approver ⛾ ▶Update ▶Notify approver ☑ ▶#<Trail... | ▶Update ⛾ ▶Update ▶Notify approver ☑ ▶#<Trail... |
| ☝ ▶Notify approver | ▶Update form ▶Notify approver ⛾ ▶Update ▶Notify approver ☑ ▶#<Trail... | ☝ ▶Update form ▶Delete? form ▶Publish ⛾ ▶Publish ▶Delete ▶Update ☑ ◉End.fai... |
| ☝ ▶Update | ▶Update ⛾ ▶Update ▶Notify approver ☑ ▶#<Trail... | ☝ ▶Update form ▶Notify approver ⛾ ▶Notify approver ▶Update ☑ ▶#<Trail... |
| ☝ ▶Notify approver ⛞ | ▶Update form ▶Notify approver ⛾ ▶Update ▶Notify approver ☑ ▶#<Trail... | ▶Revise form ⛾ ▶Revise ☑ ◉End.suc... |
| ☝ ▶Delete? form | ☝ ▶Update form ▶Delete? form ▶Publish ⛾ ▶Publish ▶Delete ▶Update ☑ ... | ☝ ▶Delete ▶Cancel ⛾ ▶Publish ▶Delete ▶Update ☑ ◉End.fai... |
| ☝ ▶Publish | ☝ ▶Update form ▶Delete? form ▶Publish ⛾ ▶Publish ▶Delete ▶Update ☑ ... | ▶Archive ⛾ ▶Archive ☑ ◉End.fai... |
| ☝ ▶Update ⛞ | ▶Update ⛾ ▶Update ▶Notify approver ☑ ▶#<Trail... | ▶Update ⛾ ▶Update ▶Notify approver ☑ ▶#<Trail... |
| ☝ ▶Revise form | ▶Revise form ⛾ ▶Revise ... | ☝ ▶Revise ⛾ ▶Revise ☑ ◉End.suc... |
| ☝ ▶Delete | ☝ ▶Delete ▶Cancel ⛾ ▶Publish ▶Delete ▶Update ☑ ... | ◉End.success ⛾ ◉End.success ☑ ◉End.fai... |
| ☝ ▶Cancel | ☝ ▶Delete ▶Cancel ⛾ ▶Publish ▶Delete ▶Update ☑ ... | ☝ ▶Update form ▶Delete? form ▶Publish ⛾ ▶Publish ▶Delete ▶Update ☑ ◉End.fai... |
| ☝ ▶Archive | ▶Archive ⛾ ▶Archive ... | ☝ ◉End.success ⛾ ◉End.success ☑ ◉End.fai... |
| ☝ ▶Revise | ▶Revise ⛾ ▶Revise ... | ☝ ▶Update form ▶Notify approver ⛾ ▶Revise ▶Notify approver ☑ ◉End.suc... |
+----------------------+--------------------------------------------------------------------------------+--------------------------------------------------------------------------------+
15 rows in set)

Expand Down Expand Up @@ -598,6 +602,9 @@ def render_states(states, lanes:, additional_state_data:, task_map:)
#DISCUSS: here, we could also ask the State layer for the start configuration, on a different level.
lane_positions = [Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *["lifecycle", "suspend-Gateway_01p7uj7"]), Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *["UI", "suspend-Gateway_1xs96ik"])]

lane_positions = Trailblazer::Workflow::Collaboration::Positions.new(lane_positions)


configuration, (ctx, flow) = Trailblazer::Workflow::Collaboration::Synchronous.advance(
schema,
[{seq: []}, {throw: []}],
Expand Down
Loading

0 comments on commit bf25c28

Please sign in to comment.