Simple ChoiceΒΆ

This example shows a simple state machine containing a single Choice state that maps to a small number of terminal states.

The enums version demonstrates how Enum members can be used rather than just primitive values.

start

if ($.value == "A") then (yes)
   :ResultA;
   stop
else if ($.value == "B") then (yes)
   :ResultB1;
   :ResultB2;
   stop
else
   #red:Unknown;
   end
endif

"""
Simple workflow using a choice with three possible branches.
"""
from rhodes.choice_rules import VariablePath
from rhodes.states import Choice, Fail, StateMachine, Task


def build() -> StateMachine:
    workflow = StateMachine(
        Comment="This is a simple state machine with a single choice and three end states."
    )

    decision = workflow.start_with(Choice("TheBeginning"))

    decision.if_(VariablePath("$.value") == "A").then(
        Task("ResultA", Resource="arn:aws:lambda:us-east-1:123456789012:function:A")
    ).end()

    decision.if_(VariablePath("$.value") == "B").then(
        Task("ResultB1", Resource="arn:aws:lambda:us-east-1:123456789012:function:B1")
    ).then(
        Task("ResultB2", Resource="arn:aws:lambda:us-east-1:123456789012:function:B2")
    ).end()

    decision.else_(Fail("Unknown", Error="Unhandled Case", Cause="Unknown Value"))

    return workflow
{
    "Comment": "This is a simple state machine with a single choice and three end states.",
    "StartAt": "TheBeginning",
    "States": {
        "TheBeginning": {
            "Type": "Choice",
            "Choices": [
                {
                    "Variable": "$.value",
                    "StringEquals": "A",
                    "Next": "ResultA"
                },
                {
                    "Variable": "$.value",
                    "StringEquals": "B",
                    "Next": "ResultB1"
                }
            ],
            "InputPath": "$",
            "OutputPath": "$",
            "Default": "Unknown"
        },
        "ResultA": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:A",
            "InputPath": "$",
            "OutputPath": "$",
            "ResultPath": "$",
            "End": true
        },
        "ResultB1": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:B1",
            "InputPath": "$",
            "OutputPath": "$",
            "ResultPath": "$",
            "Next": "ResultB2"
        },
        "ResultB2": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:B2",
            "InputPath": "$",
            "OutputPath": "$",
            "ResultPath": "$",
            "End": true
        },
        "Unknown": {
            "Type": "Fail",
            "Error": "Unhandled Case",
            "Cause": "Unknown Value"
        }
    }
}
"""
Simple workflow using a choice with three possible branches.
"""
from enum import Enum

from rhodes.choice_rules import VariablePath
from rhodes.states import Choice, Fail, StateMachine, Task


class Values(Enum):
    CHOICE_A = "A"
    CHOICE_B = "B"


def build() -> StateMachine:
    workflow = StateMachine(
        Comment="This is a simple state machine with a single choice and three end states."
    )

    decision = workflow.start_with(Choice("TheBeginning"))
    decision.if_(VariablePath("$.value") == Values.CHOICE_A).then(
        Task(
            "ResultA", Resource="arn:aws:lambda:us-east-1:123456789012:function:A"
        ).end()
    )
    result_b1 = decision.if_(VariablePath("$.value") == Values.CHOICE_B).then(
        Task("ResultB1", Resource="arn:aws:lambda:us-east-1:123456789012:function:B1")
    )
    result_b1.then(
        Task(
            "ResultB2", Resource="arn:aws:lambda:us-east-1:123456789012:function:B2"
        ).end()
    )
    decision.else_(Fail("Unknown", Error="Unhandled Case", Cause="Unknown Value"))

    return workflow
{
    "Comment": "This is a simple state machine with a single choice and three end states.",
    "StartAt": "TheBeginning",
    "States": {
        "TheBeginning": {
            "Type": "Choice",
            "Choices": [
                {
                    "Variable": "$.value",
                    "StringEquals": "A",
                    "Next": "ResultA"
                },
                {
                    "Variable": "$.value",
                    "StringEquals": "B",
                    "Next": "ResultB1"
                }
            ],
            "InputPath": "$",
            "OutputPath": "$",
            "Default": "Unknown"
        },
        "ResultA": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:A",
            "InputPath": "$",
            "OutputPath": "$",
            "ResultPath": "$",
            "End": true
        },
        "ResultB1": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:B1",
            "InputPath": "$",
            "OutputPath": "$",
            "ResultPath": "$",
            "Next": "ResultB2"
        },
        "ResultB2": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:B2",
            "InputPath": "$",
            "OutputPath": "$",
            "ResultPath": "$",
            "End": true
        },
        "Unknown": {
            "Type": "Fail",
            "Error": "Unhandled Case",
            "Cause": "Unknown Value"
        }
    }
}