-
I have a script which runs a machine and I wanted to check if the machine was configured the way I expected. I thought using the diagrams extension would allow me to generate a diagram for inspection. But all the use cases in the documentation show how to create an object of class |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @leingang,
unfortunately, transitions does not feature a copy constructor or method (yet). from transitions.extensions import GraphMachine
from transitions import Machine
m = Machine(states=["A", "B", "C", "D"], initial="C")
m.add_ordered_transitions(trigger="next", conditions=["is_A", "is_B", "is_C", "is_D"])
# do not initialise a model and replace events and states
tmp = GraphMachine(model=None, initial=None, show_conditions=True)
tmp.states = m.states
tmp.events = m.events
# we can pass machine as a model to our graph machine
tmp._get_graph(m).draw("graph_simple.png", prog="dot") Alternatively, you could use from transitions.extensions import GraphMachine
from transitions.extensions.markup import MarkupMachine as Machine
mm = Machine(states=["A", "B", "C", "D"], initial="A")
mm.add_ordered_transitions(trigger="next", conditions=["is_A", "is_B", "is_C", "is_D"])
mm.next()
gm = GraphMachine(markup=mm.markup, show_conditions=True)
gm.next()
gm.get_graph().draw("graph_markup.png", prog="dot") This will create a new copy of all states and transitions. Thus, if DEBUG:
from transitions.extensions import GraphMachine as Machine
else:
from transitions import Machine
[...]
if DEBUG:
machine.get_graph()... Last but not least: Many people do not add states and transitions via states = ["A", "B", "C", "D"]
transitions = [
["go", "A", "B"],
["check", "B", "C"],
{"trigger": "undo", "source": "C", "dest": "D", "conditions": "has_failed"}
]
# ...
m = Machine(states=states, transitions=transitions, initial="A")
# ...
gm = GraphMachine(states=states, transitions=transitions, initial=m.state) |
Beta Was this translation helpful? Give feedback.
Hello @leingang,
unfortunately, transitions does not feature a copy constructor or method (yet).
HierarchicalMachine
does some shallow copying but expects the copied/embedded machine to be of the same class.GraphMachine
andMachine
are mostly the same. You could get a diagram from a temporaryGraphMachine
by shallow copying/assigning the relevant properties of your machine to the graph machine: