pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.49k stars 525 forks source link

Function may_<trigger>() always returns false on an internal transition. #594

Closed a-schade closed 1 year ago

a-schade commented 1 year ago

Describe the bug A call to the may_() function returns always false on an internal transition.

Minimal working example

from transitions import Machine, State

class InactiveStateHandler:

    def __init__(self, model):
        self._model = model

    def on_enter(self) -> None:
        print("enter inactive")

class ReadyStateHandler:

    def __init__(self, model):
        self._model = model

    def on_enter(self) -> None:
        print("enter ready")

class TheMachine(Machine):

    # trigger, source, target, condition, unless, before, after, prepare
    transitions = [
        ["start", "inactive", "ready"],
        ["stop", "ready", "inactive"]
    ]

    def __init__(self):
        Machine.__init__(self, transitions=self.transitions, initial='inactive')
        self.ready_state_handler = ReadyStateHandler(self)
        self.inactive_state_handler = InactiveStateHandler(self)
        self.add_state("inactive", on_enter=self.inactive_state_handler.on_enter)
        self.add_state("ready", on_enter=self.ready_state_handler.on_enter)
        self.add_transition("start", "inactive", "ready")
        self.add_transition("stop", "ready", "inactive")
        self.add_transition("do", "ready", None, after="on_do")

    def on_do(self):
        print("Done")

m = TheMachine()
print(f"State: {m.state} Start: {m.may_start()} Stop: {m.may_stop()} Do: {m.may_do()}")
m.start()
print(f"State: {m.state} Start: {m.may_start()} Stop: {m.may_stop()} Do: {m.may_do()}")
m.dispatch("do")
markus-work commented 1 month ago

Thanks for solving this! 😃 When is this planned to be released?

aleneum commented 1 month ago

Hello @markus-work,

Thanks for solving this! 😃 When is this planned to be released?

end of this week. I need to check open issues and see whether there are some major issues to be tackled. features will be postponed to the next release though.

markus-work commented 1 month ago

Great to hear! 😄

aleneum commented 1 month ago

Hello @markus-work,

FYI: version 0.9.1 has been released