Statifier.Actions.IfAction (statifier v1.9.0)
View SourceRepresents SCXML <if>, <elseif>, and <else> conditional execution blocks.
The if element provides conditional execution of actions within executable content. It contains conditional blocks that are evaluated in document order, and the first block whose condition evaluates to true will have its actions executed.
Structure
An if element can contain:
- Zero or more
<elseif>elements withcondattributes - Zero or one
<else>element (no condition, catches all remaining cases) - Executable content (assign, log, raise, etc.) within each conditional block
Examples
<if cond="x === 0">
<assign location="result" expr="'zero'"/>
</if>
<if cond="x > 10">
<assign location="category" expr="'high'"/>
<elseif cond="x > 5"/>
<assign location="category" expr="'medium'"/>
<else/>
<assign location="category" expr="'low'"/>
</if>SCXML Specification
From the W3C SCXML specification:
- The if element and its children are executable content
- Conditional blocks are evaluated in document order
- The first block whose condition evaluates to true is executed
- If no conditions are true and an else block exists, the else block is executed
- If no conditions are true and no else block exists, no actions are executed
Summary
Functions
Execute the if action by evaluating conditions and executing the first true block.
Create a new IfAction from conditional blocks.
Types
@type t() :: %Statifier.Actions.IfAction{ conditional_blocks: [conditional_block()], source_location: map() | nil }
Functions
@spec execute(Statifier.StateChart.t(), t()) :: Statifier.StateChart.t()
Execute the if action by evaluating conditions and executing the first true block.
Processes conditional blocks in document order:
- Evaluate each condition until one returns true
- Execute all actions in the first true block
- If no conditions are true, execute else block if present
- Return the updated StateChart
@spec new([conditional_block()], map() | nil) :: t()
Create a new IfAction from conditional blocks.
Takes a list of conditional blocks, each containing type, condition, and actions. Conditions are compiled for performance during creation.
Examples
blocks = [
%{type: :if, cond: "x > 0", actions: [assign_action1]},
%{type: :else, cond: nil, actions: [assign_action2]}
]
action = Statifier.Actions.IfAction.new(blocks)