Skip to content

Commit

Permalink
Advance can figure out return signal by checking :outcome.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 21, 2024
1 parent f61889f commit 5f9b82d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
27 changes: 24 additions & 3 deletions lib/trailblazer/workflow/advance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ module Workflow
module Advance
module_function

def call(schema, ctx, event_label:, iteration_set:, **)
def call(ctx, event_label:, iteration_set:, message_flow:, positions_to_iteration_table:, **)
planned_iteration = iteration_set.to_a.find { |iteration| iteration.event_label == event_label }

# TODO: run the state guard here.

# TODO: those positions could also be passed in manually, without using an Iteration::Set.
position_options = position_options_from_iteration(planned_iteration) # :start_task_position and :start_positions


configuration, (ctx, flow) = Trailblazer::Workflow::Collaboration::Synchronous.advance(
schema,
configuration, (ctx, flow_options) = Trailblazer::Workflow::Collaboration::Synchronous.advance(
[ctx, {throw: []}],
{}, # circuit_options

**position_options,
message_flow: message_flow,
)

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

return signal, [ctx, flow_options]
end

# Computes {:start_task_position} and {:start_positions}.
Expand All @@ -30,6 +33,24 @@ def position_options_from_iteration(iteration)
lane_positions: iteration.start_positions # current position/"state"
}
end

def return_signal_for(configuration, positions_to_iteration_table, iteration_set, start_task_position:, **)
collaboration_signal = configuration.signal

# TODO: "prepare" this in a "state table"?
# 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 }


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

possible_signals = {
success: Trailblazer::Activity::Right,
failure: Trailblazer::Activity::Left,
}

possible_signals.fetch(travelled_iteration.outcome)
end
end
end
end
16 changes: 9 additions & 7 deletions test/advance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ class AdvanceTest < Minitest::Spec
include DiscoveredStates

it "what" do
schema, lanes, message_flow, initial_lane_positions, lanes_cfg = build_schema()

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

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

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

# TODO: this should be suitable to be dropped into an endpoint.

signal, (ctx, flow_options) = Trailblazer::Workflow::Advance.(
schema,
ctx,
**schema.to_h,
message_flow: message_flow,
event_label: "☝ ⏵︎Update",
lanes_cfg: lanes_cfg, # TODO: make this part of {schema}.

iteration_set: iteration_set, # this is basically the "dictionary" for lookups of positions.
state_guards: {} # TODO: design/implement this.
state_guards: {}, # TODO: design/implement this.

positions_to_iteration_table: positions_to_iteration_table,
)

assert_equal signal.inspect, %(Trailblazer::Activity::Right)
end
end

0 comments on commit 5f9b82d

Please sign in to comment.