Replies: 2 comments 3 replies
-
Hi @hoderlump , how are you? Nice example! The hook This should do the trick... I've updated a few things on your example to take advantage of the latest features (like state names are now optional). class LogObserver(object):
def __init__(self):
self.counter = 0
def after_transition(self, machine, event, source, target):
self.counter += 1
print(f"StateMachine: {source.id}--({event})-->{target.id}")
machine._graph().write_svg(f"{machine.name}_{self.counter:0>5}.svg")
class mySuperStatemachine(StateMachine):
# initialization states:
my_init_State = State(initial=True)
my_worker_State = State(final=True)
# failure states:
sm_configuration_invalid_State = State(final=True)
# transitions:
run = (
my_init_State.to(my_worker_State, cond="valid_sm_configuration")
| my_init_State.to(sm_configuration_invalid_State, unless="valid_sm_configuration")
)
def __init__(self):
# conditions and guards:
self.valid_sm_configuration = False
def run_until_done(self):
while not self.current_state.final:
self.run()
def on_enter_my_init_State(self):
# do stuff that takes a lot of time...
self.valid_sm_configuration = True The main loop: sm = mySuperStatemachine()
sm.add_observer(LogObserver())
sm.run_until_done() |
Beta Was this translation helpful? Give feedback.
-
Hi Fernando, thanks for you quick help! :) class LogObserver(object):
def __init__(self):
self.counter = 0
def after_transition(self, machine, event, source, target):
self.counter += 1
print(f"StateMachine: {source.id}--({event})-->{target.id}")
machine._graph().write_svg(f"{machine.name}_{self.counter:0>5}.svg")
class mySuperStatemachine(StateMachine):
# initialization states:
not_being_pregnant = State(initial=True)
being_pregnant = State()
giving_birth = State(final=True) # Not the final state in reality. For illustration purposes only. Everyone life long!! :)
# transitions:
run = (
not_being_pregnant.to(being_pregnant)
| being_pregnant.to(giving_birth, cond="nine_months_pass")
)
def __init__(self):
# conditions and guards:
self.nine_months_pass = False
def run_until_done(self):
while not self.current_state.final:
self.run()
def on_enter_being_pregnant(self):
time.sleep(60*60*24*30*9)
self.self.nine_months_pass = True It's obvious that the "being_pregnant" state is active for 9 months in the example above. I could solve it now by changing the |
Beta Was this translation helpful? Give feedback.
-
Hi everone.
Thanks for this cool project! I really like the versability and the fact that it is very robust.
There is one thing that bothers me a little. Maybe my understanding is not correct here but I try to lllistrate it.
I have the following simplyfied state machine:
This is the main loop:
This is the observer; It prints the current state and creates a svg of the state machine and marks the current state:
It runs quite well!
But the graphical representation is always late. As all the graphical representation is being created before the state is entered (during the
run
transition), the previous state is always marked instead of the target state.I tried to change the things by executing all my worker code in
on_exit_
functions but this does not work well for me: I set validators inside the code (self.valid_sm_configuration = False
) and that is too late in theon_exit_
function.Is there any way, i can execute code during the state, so after entering and before exiting?
Maybe there is another way: I could to create the graph in the observer when the state is entered. But then i have 2
on_enter_
functions per state and i do not know which of those is executed first...Thx a lot!
Beta Was this translation helpful? Give feedback.
All reactions