Skip to content

from_enum in Compound States #606

@vincedbowen

Description

@vincedbowen

Just wanted to first say thanks so much for making such an awesome library! 😄

I have been using version 3.0.0 a bunch, and I was wondering if it is possible to use from_enum() in a compound state?

Using an Enum in the compound state:

from enum import Enum, auto
from statemachine import State, StateChart
from statemachine.states import States

class outer_sts(Enum):
    FOO = auto()
    BAR = auto()

class inner_sts(Enum):
    FIZZ = auto()
    BUZZ = auto()

class SC(StateChart):
    baz = States.from_enum(outer_sts, initial = outer_sts.FOO, final = outer_sts.BAR)

    class inner(State.Compound):
        qux = States.from_enum(inner_sts, initial = inner_sts.FIZZ, final = inner_sts.BUZZ)
        fizz_to_buzz = qux.FIZZ.to(qux.BUZZ)
        
    baz_foo_to_inner = baz.FOO.to(inner)
    inner_to_baz_bar = inner.to(baz.BAR)

sm = SC()
print(sm.configuration)
sm.send("baz_foo_to_inner")
print(sm.configuration)
sm.send("fizz_to_buzz")
print(sm.configuration)
sm.send("inner_to_baz_bar")
print(sm.configuration)

Output:

{Foo}
{Inner}
{Inner}
{Bar}

Using Declarative states in the compound state

from enum import Enum, auto
from statemachine import State, StateChart
from statemachine.states import States

class outer_sts(Enum):
    FOO = auto()
    BAR = auto()

class inner_sts(Enum):
    FIZZ = auto()
    BUZZ = auto()

class SC(StateChart):
    baz = States.from_enum(outer_sts, initial = outer_sts.FOO, final = outer_sts.BAR)

    class inner(State.Compound):
        fizz = State("fizz", initial=True)
        buzz = State("buzz", final=True)
        fizz_to_buzz = fizz.to(buzz)
        
    baz_foo_to_inner = baz.FOO.to(inner)
    inner_to_baz_bar = inner.to(baz.BAR)

sm = SC()
print(sm.configuration)
sm.send("baz_foo_to_inner")
print(sm.configuration)
sm.send("fizz_to_buzz")
print(sm.configuration)
sm.send("inner_to_baz_bar")
print(sm.configuration)

Output:

{Foo}
{Inner, fizz}
{Inner, buzz}
{Bar}

This is the output I think I would expect from using an Enum with the child in the configuration. Is this something that is just not supported, or am I using it incorrectly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions