Skip to content

Commit

Permalink
we almost can discover a real collaboration by stubbing its tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 15, 2024
1 parent a9ee1ed commit 674cc52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
30 changes: 18 additions & 12 deletions lib/trailblazer/workflow/discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,16 @@ def stub_tasks_for(collaboration, ignore_class: Trailblazer::Activity::End, mess

replaced_tasks = tasks_to_stub.collect do |task, links|
node = Activity::Introspect.Nodes(activity, task: task)
label = node.data[:label]
label = node.data[:label] || node.id # TODO: test this case, when there's no {:label}.

stub_task_name = "#{lane_id}:#{label}".to_sym
stub_task = Activity::Testing.def_steps(stub_task_name).method(stub_task_name)
stub_task = Activity::Testing.def_tasks(stub_task_name).method(stub_task_name)

# raise label.inspect
[task, stub_task]
end.to_h

# TODO: really remove original tasks from the circuit, as this causes confusion.
# Now, "replace" (rather: "add") the original task with the stub.
replaced_tasks.each do |task, stub_task|
lane_map[stub_task] = lane_map[task]
Expand All @@ -155,33 +156,38 @@ def stub_tasks_for(collaboration, ignore_class: Trailblazer::Activity::End, mess
[task, new_links]
end.to_h

# Add nodes for introspection
new_nodes = replaced_tasks.collect do |task, stub_task|
node = Activity::Introspect.Nodes(activity, task: task)
new_node_attributes = Activity::Schema::Nodes::Attributes.new(node.id, stub_task, node.data, node.outputs)

[stub_task, new_node_attributes]
end.to_h

existing_nodes = activity.to_h[:nodes]
new_nodes = existing_nodes.merge(new_nodes) # DISCUSS: Nodes#merge is not official.

new_circuit = Activity::Circuit.new(new_circuit_map, circuit.to_h[:end_events], start_task: circuit.to_h[:start_task])

lane = Activity.new(Activity::Schema.new(new_circuit, activity.to_h[:outputs], activity.to_h[:nodes], activity.to_h[:config])) # FIXME: breaking taskWrap here (which is no problem, actually).
lane = Activity.new(Activity::Schema.new(new_circuit, activity.to_h[:outputs], new_nodes, activity.to_h[:config])) # FIXME: breaking taskWrap here (which is no problem, actually).

# [lane_id, lane, replaced_tasks]
[lane_id, lane]
end.to_h

old_activity_2_new_activity = collaboration.to_h[:lanes].collect { |lane_id, activity| [activity, stubbed_lanes[lane_id]] }.to_h

puts "@@@@@@@@@ activity mapping @@@@@@@@@"
pp old_activity_2_new_activity

# stubbed_tasks = stubbed_lanes.flat_map { |id, lane, replaced_tasks| replaced_tasks.to_a }.to_h # DISCUSS: do we need this?

# message_flow = collaboration.to_h[:message_flow]
new_message_flow = message_flow.collect { |throw_evt, (activity, catch_evt)| [throw_evt, [old_activity_2_new_activity[activity], catch_evt]] }.to_h

new_start_position = Collaboration::Position.new(old_activity_2_new_activity.fetch(start_position.activity), start_position.task)
# raise start_position.inspect

new_initial_lane_positions = initial_lane_positions.collect do |position|
# TODO: make lane_positions {Position} instances, too.

raise position.inspect
Collaboration::Position.new(old_activity_2_new_activity[position[0]], position[1])
end

new_initial_lane_positions = Collaboration::Positions.new(new_initial_lane_positions)

return Collaboration::Schema.new(lanes: stubbed_lanes, message_flow: new_message_flow), new_message_flow, new_start_position, new_initial_lane_positions
end
end
Expand Down
31 changes: 20 additions & 11 deletions test/collaboration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ def build_schema()
ui: lane_activity_ui,
}

approver_activity, extended_message_flow, extended_initial_lane_positions = build_custom_editor_lane(lanes, message_flow)

lanes = lanes.merge(approver: approver_activity)

schema = Trailblazer::Workflow::Collaboration::Schema.new(
lanes: lanes,
message_flow: message_flow,
)

approver_activity, extended_message_flow, extended_initial_lane_positions = build_custom_editor_lane(lanes, message_flow)

return schema, lanes.merge(approver: approver_activity), extended_message_flow, extended_initial_lane_positions
return schema, lanes, extended_message_flow, extended_initial_lane_positions
end

# DISCUSS: this is mostly to play around with the "API" of building a Collaboration.
Expand All @@ -118,8 +120,9 @@ def build_custom_editor_lane(lanes, message_flow)

approver_start_suspend = nil
approver_activity = Class.new(Trailblazer::Activity::Railway) do
step task: approver_start_suspend = Trailblazer::Workflow::Event::Suspend.new(semantic: "invented_semantic", "resumes" => ["xxx"])
step task: approver_start_suspend = Trailblazer::Workflow::Event::Suspend.new(semantic: "invented_semantic", "resumes" => ["catch-before-decider-xxx"])

fail task: Trailblazer::Workflow::Event::Catch.new(semantic: "xxx --> decider"), id: "catch-before-decider-xxx", Output(:success) => Track(:failure)
fail :decider, id: "xxx",
Output(:failure) => Trailblazer::Activity::Railway.Id("xxx_reject")
fail task: decision_is_approve_throw = Trailblazer::Workflow::Event::Throw.new(semantic: "xxx_approve")
Expand All @@ -136,7 +139,7 @@ def decider(ctx, decision: true, **)

extended_message_flow = message_flow.merge(
# "throw-after-Activity_0wr78cv"
missing_throw_from_notify_approver => [approver_activity, Trailblazer::Activity::Introspect.Nodes(approver_activity, id: "xxx").task],
missing_throw_from_notify_approver => [approver_activity, Trailblazer::Activity::Introspect.Nodes(approver_activity, id: "catch-before-decider-xxx").task],
decision_is_approve_throw => [lifecycle_activity, Trailblazer::Activity::Introspect.Nodes(lifecycle_activity, id: "catch-before-#{approve_id}").task],
decision_is_reject_throw => [lifecycle_activity, Trailblazer::Activity::Introspect.Nodes(lifecycle_activity, id: "catch-before-#{reject_id}").task],
)
Expand Down Expand Up @@ -278,9 +281,6 @@ def render_states(states, lanes:, additional_state_data:, task_map:)
lane_activity_ui = lanes[:ui]
approver_activity = lanes[:approver]

# raise schema_hash.keys.inspect





Expand All @@ -297,13 +297,22 @@ def render_states(states, lanes:, additional_state_data:, task_map:)

run_multiple_times: {
# We're "clicking" the [Notify_approver] button again, this time to get rejected.
Trailblazer::Activity::Introspect.Nodes(lane_activity_ui, id: "catch-before-#{ui_notify_approver}").task => {ctx_merge: {decision: false}, config_payload: {outcome: :failure}},
Trailblazer::Activity::Introspect.Nodes(lane_activity_ui, id: "catch-before-#{ui_notify_approver}").task => {ctx_merge: {
# decision: false, # TODO: this is how it should be.
:"approver:xxx" => Trailblazer::Activity::Left, # FIXME: {:decision} must be translated to {:"approver:xxx"}
}, config_payload: {outcome: :failure}},

# Click [UI Create] again, with invalid data.
Trailblazer::Activity::Introspect.Nodes(lane_activity_ui, id: "catch-before-#{ui_create}").task => {ctx_merge: {create: false}, config_payload: {outcome: :failure}}, # lifecycle create is supposed to fail.
Trailblazer::Activity::Introspect.Nodes(lane_activity_ui, id: "catch-before-#{ui_create}").task => {ctx_merge: {
# create: false
:"lifecycle:Create" => Trailblazer::Activity::Left,
}, config_payload: {outcome: :failure}}, # lifecycle create is supposed to fail.

# Click [UI Update] again, with invalid data.
Trailblazer::Activity::Introspect.Nodes(lane_activity_ui, id: "catch-before-#{ui_update}").task => {ctx_merge: {update: false}, config_payload: {outcome: :failure}}, # lifecycle create is supposed to fail.
Trailblazer::Activity::Introspect.Nodes(lane_activity_ui, id: "catch-before-#{ui_update}").task => {ctx_merge: {
# update: false
:"lifecycle:Update" => Trailblazer::Activity::Left,
}, config_payload: {outcome: :failure}}, # lifecycle create is supposed to fail.
}
)

Expand Down

0 comments on commit 674cc52

Please # to comment.