Statifier.Actions.IfAction (statifier v1.9.0)

View Source

Represents 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 with cond attributes
  • 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

conditional_block()

@type conditional_block() :: %{
  type: :if | :elseif | :else,
  cond: String.t() | nil,
  compiled_cond: term() | nil,
  actions: [term()]
}

t()

@type t() :: %Statifier.Actions.IfAction{
  conditional_blocks: [conditional_block()],
  source_location: map() | nil
}

Functions

execute(state_chart, if_action)

Execute the if action by evaluating conditions and executing the first true block.

Processes conditional blocks in document order:

  1. Evaluate each condition until one returns true
  2. Execute all actions in the first true block
  3. If no conditions are true, execute else block if present
  4. Return the updated StateChart

new(conditional_blocks, source_location \\ nil)

@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)