Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guard_with = Guard.silence doesn't seem to work for me #20

Open
tiborkiss opened this issue Mar 25, 2021 · 0 comments
Open

guard_with = Guard.silence doesn't seem to work for me #20

tiborkiss opened this issue Mar 25, 2021 · 0 comments

Comments

@tiborkiss
Copy link

tiborkiss commented Mar 25, 2021

I don't understand how to work with

    class Config:
        guard_with = statesman.Guard.silence

For example, the following code throws
RuntimeError: event trigger failed: the "run" event cannot be triggered from the current state of "stopped"
when I try to call run in stopped state. I would like to silently continue, but not execute the state if it is illegal.
Or how to use this state machine in a user interaction, when the user may send commands which are not yet allowed?

import statesman
import asyncio

class StateMachine(statesman.StateMachine):
    class States(statesman.StateEnum):
        waiting = 'Waiting'
        running = 'Running'
        stopped = 'Stopped'
        aborted = 'Aborted'

    class Config:
        guard_with = statesman.Guard.silence

    @statesman.event(None, States.waiting)
    async def start(self) -> None:
        ...

    @statesman.event(States.waiting, States.running)
    async def run(self) -> None:
        ...

    @statesman.event(States.running, States.stopped)
    async def stop(self) -> None:
        ...

    @statesman.event(States.__any__, States.aborted)
    async def abort(self) -> None:
        ...

    @statesman.event(
        States.__any__,
        States.__active__,
        type=statesman.Transition.Types.self
    )
    async def check(self) -> None:
        print("Exiting and reentering active state!")



async def _example() -> None:
    state_machine = await StateMachine.create()
    await state_machine.start()
    await state_machine.run()
    await state_machine.stop()
    # I would like to silently keep the last state for the following
    await state_machine.run()


if __name__ == '__main__':
    asyncio.run(_example())
@tiborkiss tiborkiss changed the title guard_with = Guard.silence doesn't seem to work for m guard_with = Guard.silence doesn't seem to work for me Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant