Statifier.Actions.ForeachAction (statifier v1.9.0)

View Source

Represents an SCXML <foreach> action for iterating over collections.

The foreach action iterates over a collection (array), executing a block of actions for each item. It provides both the item value and optionally the index to the actions.

Attributes

  • array - The expression that evaluates to the collection to iterate over (required)
  • item - The variable name to assign each collection item to (required)
  • index - The variable name to assign the current index to (optional)
  • actions - List of executable actions to run for each iteration
  • source_location - Location in the source SCXML for error reporting

Examples

<foreach array="myArray" item="currentItem" index="currentIndex">
    <assign location="sum" expr="sum + currentItem"/>
    <assign location="indexSum" expr="indexSum + currentIndex"/>
</foreach>

<foreach array="users" item="user">
    <log expr="user.name"/>
</foreach>

SCXML Specification

From the W3C SCXML specification:

  • The foreach element must have 'array' and 'item' attributes
  • The 'index' attribute is optional
  • The processor must declare new variables if item/index don't exist
  • Variables are restored to their previous values after foreach completes
  • If 'array' doesn't evaluate to an iterable collection, error.execution is raised
  • Iteration proceeds from first to last item in collection order

Variable Scoping

The foreach element implements proper variable scoping:

  • Creates snapshot of current datamodel before execution
  • Declares item/index variables if they don't exist
  • Restores original variable values after foreach completion
  • Inner foreach loops properly nest variable scopes

Summary

Functions

Execute the foreach action by iterating over the array and executing actions.

Create a new ForeachAction from parsed attributes.

Types

t()

@type t() :: %Statifier.Actions.ForeachAction{
  actions: [term()],
  array: String.t(),
  compiled_array: term() | nil,
  index: String.t() | nil,
  item: String.t(),
  source_location: map() | nil
}

Functions

execute(state_chart, foreach_action)

Execute the foreach action by iterating over the array and executing actions.

Implementation follows W3C SCXML specification:

  1. Evaluate array expression to get collection
  2. Validate collection is iterable
  3. Create variable scope snapshot
  4. Declare item/index variables if needed
  5. Iterate through collection, executing actions for each item
  6. Restore variable scope
  7. Handle errors by raising error.execution event

Returns the updated StateChart.

new(array, item, index \\ nil, actions, source_location \\ nil)

@spec new(String.t(), String.t(), String.t() | nil, [term()], map() | nil) :: t()

Create a new ForeachAction from parsed attributes.

The array expression is compiled for performance during creation.

Examples

actions = [assign_action1, log_action]
action = Statifier.Actions.ForeachAction.new("myArray", "item", "index", actions)