0

I am using a GraphMachine to model a workflow of a MongoDB record.

I am only storing the state in MongoDB and when I am reloading at a later time, I use the set_state() option on the machine to force it back to where it was left off.

This all works correctly except when I try to show the state machine graph.

After loading it always shows itself in the initial state even though it seems it did accept the set_state because transitions are accepted as if it was in the restored state.

Lets say I have a simple linear FSM like: S0 -> S1 -> S2 -> S3 -> S3 -> S0.

S0 is the initial state, and S2 is where it was saved.

When I restore, it always graphs itself in S0, but if I try to make the S2->S3 transition, it accepts it. When I make the graph afterwards, it is in the correct S3 state.

Is there a way I can make the GraphMachine 'initialize' to the correct state?

Thanks

St1
  • 21
  • 3

1 Answers1

0

Machine.set_state will hard set the model state but won't call necessary callbacks to regenerate the graph. You can either pass the initial state to the constructor or force a recreation of the graph after set_state:

from transitions.extensions import GraphMachine

states = ["A", "B", "C"]
m1 = GraphMachine(states=states, initial="A", ordered_transitions=True, show_state_attributes=True)
m1.next_state()

m2 = GraphMachine(states=states, initial=m1.state, ordered_transitions=True)
m2.get_graph().draw("machine2.png")

m1.set_state("C")
m1.get_graph(force_new=True).draw("machine1.png")
aleneum
  • 2,083
  • 12
  • 29