transitions: Incorrect transition between nested states

Consider the following state machine:

class ServiceRestartState(Enum):
    starting = auto()
    stopping = auto()
    stopped = auto()


class ServiceState(Enum):
    initialized = auto()
    starting = auto()
    started = auto()
    restarting = ServiceRestartState
    stopping = auto()
    stopped = auto()


class Service(HierarchicalAsyncMachine):
    def __init__(self):
        transitions = [
            ['starting', [ServiceState.initialized, ServiceState.stopped], ServiceState.starting],
            ['started', [ServiceState.starting, ServiceRestartState.starting], ServiceState.started],
            ['restarting', ServiceState.started, 'restarting'],
            ['stopping', 'restarting', ServiceRestartState.stopping],
            ['stopped', ServiceRestartState.stopping, ServiceRestartState.stopped],
            ['starting', ServiceRestartState.stopped, ServiceRestartState.starting],
            ['stopping', ServiceState.started, ServiceState.stopping],
            ['stopped', ServiceState.stopping, ServiceState.stopped],
        ]
        super().__init__(states=ServiceState,
                         transitions=transitions,
                         initial=ServiceState.initialized,
                         auto_transitions=False)

If I am currently at the stopped stage I can’t transition to stopped.

However if you do attempt to do so the stopped trigger will transition the state machine to ServiceRestartState.stopped.

Try the code below:

m = Service()
m.add_transition('travel', ServiceState.initialized, ServiceState.stopped)
m.travel()
m.stopped()

I tried changing the name of the nested states, I tried to rename the triggers to not be the same. Nothing works.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

0.8.2 has been released. I will close this since it seems like the issue mentioned in the beginning has been solved. Feel free to comment though if you face further issues with enums and HSMs. I will reopen the issue if necessary. For unrelated problems, please open a new issue.