This topic describes Choice states and provides related examples. This topic also describes how to use conditional expressions in Choice states.
Overview
A Choice state allows the flow to execute different states based on conditions, and is similar to the switch-case
statement in programming languages. A Choice state contains multiple choice branches and a default branch. Each choice branch contains the Condition field that specifies a conditional expression and the Next field that specifies the state to execute if the condition is met.
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 branch returns
True
, the states (if any) and the Next instruction (if any) defined in the choice branch are executed.If no conditional expressions of the choice branches return
True
, the states and the Next instruction defined in the default branch are executed.
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. | See InputConstructor. |
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 | The value of this attribute is a conditional expression. | $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 value of
status
in the input isready
,Pass2
is executed, as specified in the first choice branch.If the value of
status
in the input is notready
,Pass1
is executed, as specified in the default branch.
Type: StateMachine
Name: my-wkfl
SpecVersion: v1
StartAt: Choice1
States:
- Type: Choice
Name: Choice1
Branches:
- Condition:$Input.status=="ready"
Next: Pass2
Default: Pass1
- Type: Pass
Name: Pass1
End: true
- Type: Pass
Name: Pass2
End: true
Conditional 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! =
. The comparison operations are suitable for strings and numbers.Logic operations:
||
and&&
.String constants: A string constant is enclosed in double quotation marks (") or grave accents (`). Examples: "foobar" or `foobar`.
Numeric constants: Examples:
1
and12.5
.Boolean constants:
true
andfalse
.Prefixes:
!
and-
.Contain:
in
, which is used to determine whether an array contains a value or whether an object contains a key value.
Sample expression
The following example shows the results of different conditional expressions based on the specified input. You can use the $Context
and $Input
variables to reference data in the context and input of a state in conditional expressions. 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 |