Class: StatesLanguageMachine::States::Parallel

Inherits:
Base show all
Defined in:
lib/ruby_slm/states/parallel.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#comment, #definition

Attributes inherited from StatesLanguageMachine::State

#end_state, #name, #next_state, #type

Instance Method Summary collapse

Methods inherited from Base

#validate!

Methods inherited from StatesLanguageMachine::State

#end_state?, #next_state_name

Constructor Details

#initialize(name, definition) ⇒ Parallel

Returns a new instance of Parallel.

Parameters:

  • name (String)

    the name of the state

  • definition (Hash)

    the state definition



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

#branchesArray<Hash> (readonly)

Returns the list of branches to execute in parallel.

Returns:

  • (Array<Hash>)

    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_concurrencyInteger (readonly)

Returns maximum number of concurrent branches.

Returns:

  • (Integer)

    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.

Parameters:

  • execution (Execution)

    the current execution

  • input (Hash)

    the input data for the state

Returns:

  • (Hash)

    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