pytransitions / transitions

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

A newbie question #439

Closed mniemotka closed 4 years ago

mniemotka commented 4 years ago

I have written a state machine for a pool controller, but I am struggling with something that is noted in the documentation about forcing the on_enter method to fire off when I initialize the model. The documentation says: "Note that onenter«state name» callback will not fire when a Machine is first initialized. For example if you have an on_enter_A() callback defined, and initialize the Machine with initial='A', on_enter_A() will not be fired until the next time you enter state A. (If you need to make sure on_enter_A() fires at initialization, you can simply create a dummy initial state and then explicitly call to_A() inside the init method.)"

But I am not sure where I need to call this out....I create a Class called Pool Controller, then create an instance of Pool Controller like this MyController=Pool_Controller

And then I create the state machine like this

machine = Machine(MyController, states=states, transitions, initial='dummy_state')

where do I need to put the to_initial_state() to get the dummy_state immediately jump to my initial_state so that on_enter_initial_state will execute when the model intializes?

Sorry if this is a basic question.

potens1 commented 4 years ago

Hi,

The easiest way is:

machine = Machine(MyController, states=states, transitions, initial='dummy_state')
machine.to_initial_state()

Hope it helps,

mniemotka commented 4 years ago

Thanks so much.  After I posted the question, I tried a bunch of things, and then the same solution hit me too.  I would still like to know the answer as to where in the code I would put the initi in case I need to do it that way in the future. Thanks Mike On Friday, May 29, 2020, 02:00:00 AM CDT, potens1 notifications@github.com wrote:

Hi,

The easiest way is: machine = Machine(MyController, states=states, transitions, initial='dummy_state') machine.to_initial_state()

Hope it helps,

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

aleneum commented 4 years ago

Hi @mniemotka,

this looks like a question about how to use transitions. Please refer to Stackoverflow for such matters. Posting there has several advantages:

Your question gains higher visibility since most developers look for help there. The targeted community is larger; Some people will even help you to formulate a good question People get 'rewarded' with 'reputation' to help you. You also gain reputation in case this questions pops up more frequently. It's a win-win situation.

I will close this issue for now. Feel free to comment nevertheless. If an issue/bug/feature request arises I will reopen the issue.

mniemotka commented 4 years ago

Thank you very much On Wednesday, June 10, 2020, 06:44:14 AM CDT, Alexander Neumann notifications@github.com wrote:

Hi @mniemotka,

this looks like a question about how to use transitions. Please refer to Stackoverflow for such matters. Posting there has several advantages:

Your question gains higher visibility since most developers look for help there. The targeted community is larger; Some people will even help you to formulate a good question People get 'rewarded' with 'reputation' to help you. You also gain reputation in case this questions pops up more frequently. It's a win-win situation.

I will close this issue for now. Feel free to comment nevertheless. If an issue/bug/feature request arises I will reopen the issue.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.