Class: StatesLanguageMachine::States::Parallel
- Inherits:
-
Base
- Object
- StatesLanguageMachine::State
- Base
- StatesLanguageMachine::States::Parallel
- Defined in:
- lib/ruby_slm/states/parallel.rb
Instance Attribute Summary collapse
-
#branches ⇒ Array<Hash>
readonly
The list of branches to execute in parallel.
-
#max_concurrency ⇒ Integer
readonly
Maximum number of concurrent branches.
Attributes inherited from Base
Attributes inherited from StatesLanguageMachine::State
#end_state, #name, #next_state, #type
Instance Method Summary collapse
-
#execute(execution, input) ⇒ Hash
The output data from the state.
-
#initialize(name, definition) ⇒ Parallel
constructor
A new instance of Parallel.
Methods inherited from Base
Methods inherited from StatesLanguageMachine::State
Constructor Details
#initialize(name, definition) ⇒ Parallel
Returns a new instance of Parallel.
14 15 16 17 18 19 20 21 |
# File 'lib/ruby_slm/states/parallel.rb', line 14 def initialize(name, definition) # Ensure parallel states have End: true as required by base class definition_with_end = definition.merge('End' => true) super(name, definition_with_end) @branches = definition["Branches"] || [] @max_concurrency = definition["MaxConcurrency"] || @branches.size validate_parallel_specific! end |
Instance Attribute Details
#branches ⇒ Array<Hash> (readonly)
Returns the list of branches to execute in parallel.
7 8 9 |
# File 'lib/ruby_slm/states/parallel.rb', line 7 def branches @branches end |
#max_concurrency ⇒ Integer (readonly)
Returns maximum number of concurrent branches.
10 11 12 |
# File 'lib/ruby_slm/states/parallel.rb', line 10 def max_concurrency @max_concurrency end |
Instance Method Details
#execute(execution, input) ⇒ Hash
Returns the output data from the state.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruby_slm/states/parallel.rb', line 26 def execute(execution, input) execution.logger&.info("Executing parallel state: #{@name} with #{@branches.size} branches") # Execute branches concurrently using Fibers results = execute_branches_concurrently(execution, input) # Handle any branch failures handle_branch_errors(results) # Combine successful results final_result = merge_branch_results(results) process_result(execution, final_result) final_result end |