pytransitions / transitions

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

one gramar doubt of the "before_state_change" #540

Closed LifeIsBinary closed 2 years ago

LifeIsBinary commented 2 years ago

thanks for this job of FSM library.

Maybe I do not understand some term and regular of FSM.

I have some doubt as fllow: `
//1. define a fsm self.machine = Machine(model=self, states=NaoController.states, before_state_change='set_IS_STAND', send_event=True, initial='init')

    // 2.  "init" to "prepared"
    self.machine.add_transition(trigger='control_start',
                                source='init', dest='prepared',before='robot_init',
                                conditions='is_stand')

`

above the code , I found one thing: state transition condtion is justify before the method: set_IS_STAND, if i want to change the parameter: is_stand before condition justify,i need to use ”before“ or ”prepare_event“ ?

thank your!

aleneum commented 2 years ago

Hello @LifeIsBinary,

the Callback execution order in the documentation states the following:

Callback | Current State | Comments -- | -- | -- 'machine.prepare_event' | source | executed once before individual transitions are processed 'transition.prepare' | source | executed as soon as the transition starts 'transition.conditions' | source | conditions may fail and halt the transition 'transition.unless' | source | conditions may fail and halt the transition 'machine.before_state_change' | source | default callbacks declared on model 'transition.before' | source |  

Thus, callbacks are executed in the order prepare_event, prepare, conditions/unless, before_state_change, before and so on. As failed conditions halt the transition, before and all following callbacks are only called when the state transition will actually happen. Conditions must had been processed and evaluated to True already. If you want callbacks to execute before conditions or unless you need to use either machine.prepare_event or transition.prepare.

Closing this since it's a question about how to use transitions.


If you have a question like "How do I do X with transitions" or "I have the following problem... Can transitions help me with that?", please consider Stack Overflow first.

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. Tag your question with [pytransitions] to make sure, that users of transitions will receive a notification. If the SO community cannot answer you question within a week, you can notify us by opening an issue here. Make sure to link your Stack Overflow post. We'd rather answer questions there.