Statifier.Actions.ForeachAction (statifier v1.9.0)
View SourceRepresents 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 iterationsource_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
Functions
@spec execute(Statifier.StateChart.t(), t()) :: Statifier.StateChart.t()
Execute the foreach action by iterating over the array and executing actions.
Implementation follows W3C SCXML specification:
- Evaluate array expression to get collection
- Validate collection is iterable
- Create variable scope snapshot
- Declare item/index variables if needed
- Iterate through collection, executing actions for each item
- Restore variable scope
- Handle errors by raising error.execution event
Returns the updated StateChart.
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)