A Choice state directs a flow into different branches based on conditional logic. This topic describes the basic concepts of the Choice state, provides usage examples, and explains how to use conditional expressions for flow control.
Overview
A Choice state is a node in a flow that executes different states based on specified conditions. It contains multiple choices and a default option. Each choice has a conditional expression (Condition) and a transition instruction (Next). Similar to a switch-case statement in a programming language, a Choice state uses conditional expressions to determine the execution path of the flow.
When a flow proceeds to a Choice state, the system evaluates whether the conditional expressions of the choice branches return True in the defined sequence.
If the conditional expression of a choice returns
True, the flow executes the state and transition defined for that choice.If no conditional expressions return
True, the flow executes the state and transition defined in the default option.
The following table describes the attributes that a Choice state contains.
Attribute | Type | Required | Description | Example |
Name | string | Yes | The name of the state. | my state |
Description | string | No | The description of the state. | describe it here |
Type | string | Yes | The type of the state. | Choice |
InputConstructor | map[string]any | No | The input constructor. | For more information, see Inputs and Outputs. |
Default | string | Yes | The default branch. | my next task |
Branches | ChoiceBranch | Yes | The choice branches. | See ChoiceBranch. |
ChoiceBranch
Attribute | Type | Required | Description | Example |
Condition | string | Yes | A conditional expression. For more information, see Expression examples. | $Input.status=="ready" |
Next | string | Yes | The name of the next state that the workflow transits to if the condition is met. | my next task |
Choice states do not support the End and Next attributes. The Default attribute of Choice states specifies the state that the workflow transits to by default when no condition in Choice branches is met. The feature of the Default attribute is the same as the feature of the Next attribute. ChoiceBranch can contain the Next attribute.
Example
The following sample flow contains a Choice state.
If the input
statusvalue isready, thePass2state of the first choice is executed.If the input
statusvalue is notready, the default optionPass1is executed.
Type: StateMachine
Name: MyWorkflow
SpecVersion: v1
StartAt: Choice
States:
- Type: Choice
Name: Choice
Branches:
- Condition: $Input.data == "ready"
Next: Pass1
Default: Pass2
- Type: Pass
Name: Pass2
End: true
- Type: Pass
Name: Pass1
End: trueConditional expressions
The conditional expressions supported by the system consist of the following operations and variables. You can determine whether the conditions specified by a conditional expression are met based on the Boolean value returned by the expression.
Comparison operations:
>,>=,<,<=,==, and!=. These operations apply to strings and numbers.Logical operations:
||and&&.String constants: A string constant is enclosed in double quotation marks (") or grave accents (`). Examples: "foobar" or `foobar`.
Numeric constants:
1and12.5.Boolean constants:
trueandfalse.Prefixes:
!and-.Inclusion:
in. This is used to determine whether an array contains a value or an object contains a key.
Sample expression
The following table shows the results of different conditional expressions based on the sample state input $Input. You can use the $Context and $Input variables in a conditional expression. For more information, see Data passing.
{
"a": 1,
"b": {
"b1": true,
"b2": "ready"
},
"c": [1, 2, 3],
"d": 1,
"e": 1,
"f": {
"f1": false,
"f2": "inprogress"
}
}Conditional expression | Execution results |
| true |
| false |
| true |
| true |
| true |
| true |
| true |
| true |
| false |
| true |
| false |
| true |
| false |
| true |
| true |
| true |
| false |
| true |