pytransitions / transitions

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

Plot the diagram without initial state #562

Closed remz1337 closed 2 years ago

remz1337 commented 2 years ago

Hi,

I am building simple binary state machines and plotting them using graphviz as documented, however the initial state of the model is required in order to build the machine. In my case, the initial state is irrelevant. It would be nice to have the option to not display any initial state when calling machine.get_graph().draw(...). Something like machine.get_graph().draw(show_initial_state=False, ...)

aleneum commented 2 years ago

Hello @remz1337,

styling Graphviz is a can of worms and contains a lot of individual requirement transitions cannot tackle in a satisfying manner.

Depending on the backend you use, machine.get_graph() will return a AGraph (pygraphviz) object or a bit simpler text representation of the underlying dot (graphviz). You can alter the content of the graph before you call draw to adjust the output to your liking. With AGraph it's a bit more convenient:

from transitions.extensions.diagrams import GraphMachine
from pygraphviz import AGraph

machine = GraphMachine(states=["initial", "A", "B"], transitions=[["go", "A", "B"], ["init", "initial", "A"]],
                       use_pygraphviz=True)
graph: AGraph = machine.get_graph()
graph.remove_edge("initial", "A")
graph.remove_node("initial")
graph.draw("test.png", prog="dot")
remz1337 commented 2 years ago

Thanks for the example! I ended up using graphviz directly for this specific use case. Closing this issue.