Comments (10)
Hello @hwerbi !
I think this should work (over the former snippet):
def plot_state_machine(state_machine, name='state_machine'):
dg = Digraph(comment=name)
for s in state_machine.states:
for t in s.transitions:
dg.edge(t.source.name, t.destinations[0].name, label=t.identifier)
dg.render('folder/{}.gv'.format(name), format='png')
I just added label=t.identifier
when calling dg.edge
.
Cheers!
EDIT:
Whole script:
from graphviz import Digraph
from statemachine import StateMachine, State
class TrafficLightMachine(StateMachine):
green = State('Green', initial=True)
yellow = State('Yellow')
red = State('Red')
slowdown = green.to(yellow)
stop = yellow.to(red)
go = red.to(green)
def plot_state_machine(state_machine, name='state_machine'):
dg = Digraph(comment=name)
for s in state_machine.states:
for t in s.transitions:
dg.edge(t.source.name, t.destinations[0].name, label=t.identifier)
dg.render('folder/{}.gv'.format(name), format='png')
if __name__ == '__main__':
st = TrafficLightMachine()
plot_state_machine(st, 'my_state_machine')
from python-statemachine.
Hello @caiolopes, @fgmacedo,
I have implemented this solution:
from graphviz import Digraph
def plot_state_machine(state_machine, name='state_machine'):
dg = Digraph(comment=name)
for s in state_machine.states:
for t in s.transitions:
dg.edge(t.source.name, t.destinations[0].name)
dg.render('folder/{}.gv'.format(name), format='png')
I can make a PR if you want. If not, maybe adding this example in the docs.
from python-statemachine.
I packaged the plot function into a program that plots the statemachine from a separate module, creating the graph folder if not present and writing the plots to ./graphs/modname.machinename.png and .gv
https://github.com/slowrunner/Carl/blob/master/Examples/statemachine/plotsm.py
usage: plotsm.py [-h] -m MODULE -sm STATEMACHINE
optional arguments:
-h, --help show this help message and exit
-m MODULE, --module MODULE
module name to import from
-sm STATEMACHINE, --statemachine STATEMACHINE
State Machine Type Name
./plotsm.py -m stoplight -sm TraficLightMachine
will output ./graphs/stoplight.TrafficLightMachine.gv and
./graphs/stoplight.TrafficLightMachine.png
for the module stoplight.py containing the class TrafficLightMachine(StateMachine)
from python-statemachine.
It would be nice! But I don't like the idea of adding a required dependency for this. Maybe this should be in a contrib module.
This way, what do you think about doing an intermediate graph representation, in a way that can be consumed easily for specific parsers?
from python-statemachine.
@claverru nice snippet, thanks. graphviz is not a very good dependency to have as required to this lib, IMHO.
I think we can follow two approaches:
- as you suggested, we can just have an example on the README. I would suggest to do it with
TrafficLightMachine
to be friendly with the reader. - or, having this code as a method that raises an error if graphviz is not available. This is a common approach like with
pandas
when you read an excel file without the required dependency.
I am really tempted to say that we can do both, since they are not mutual exclusive 🎉
What do you think @fgmacedo?
from python-statemachine.
Thanks for pointing out this idea @caiolopes . Thanks for your snippet @claverru !
Let me share what I'm thinking about.
I'm reading about statecharts
since SCXML (Statechart XML), is a W3C standard and it defines a lot of the semantics and specifies how to deal with certain edge cases. A statechart is essentially a state machine with extra-powers, that allows any state to include more machines, in a hierarchical fashion. This is already a feature request at #246.
With that in mind, my goal to this library may turn to be as much as possible compatible with statecharts
but using a different syntax. A pythonic interface to statecharts.
A SCXML
representation may be the answer. Or even an xstate compatible object representation
to leverage their nice statechart viz tool .
from python-statemachine.
Hello @caiolopes, @fgmacedo,
I have implemented this solution:
from graphviz import Digraph def plot_state_machine(state_machine, name='state_machine'): dg = Digraph(comment=name) for s in state_machine.states: for t in s.transitions: dg.edge(t.source.name, t.destinations[0].name) dg.render('folder/{}.gv'.format(name), format='png')
I can make a PR if you want. If not, maybe adding this example in the docs.
@claverru thanks for sharing. Is there a way to add the name of the transition ?
from python-statemachine.
This issue seems to have stalled for while. Think about closing it.
from python-statemachine.
Hello! Nice updates! I've just merged support for diagrams at #300
from python-statemachine.
You can see the preview on the develop
branch: https://python-statemachine.readthedocs.io/en/develop/diagram.html
I'm planning a release soon.
from python-statemachine.
Related Issues (20)
- How to run transition HOT 8
- A small question about docs of `Actions` HOT 4
- Consider returning all values from actions for a triggered transition HOT 18
- Is there a way to make a transition from two or more states to one state instead of writing one by one? HOT 2
- Initialising multiple states by assigning them HOT 1
- Problem with pip when installing under Python 3.10.11 HOT 2
- Multiple Observers not working on singular State machine HOT 4
- Event keyword arguments are silently overwritten when particular keyword argument names are used HOT 4
- Finalize action after transition success or failure. HOT 1
- feature request: async callbacks HOT 3
- Incompatible with spy wrapper from pytest-mock HOT 3
- Add automation (GH action) to publish a new version on pypi HOT 1
- Simple question HOT 10
- Generate the code from PlantUML diagrams HOT 2
- Type annotations missing for `initial_state` and `final_states`; leading to linting errors with Pylint/Pyright HOT 3
- Expensive instantiation of StateMachine HOT 3
- AttributeError: object has no attribute 'model' HOT 3
- Enable Multithreading HOT 2
- [Feature request] State machine based workflow for database storage on entity state tracking HOT 1
- Action callback gets called twice when mixing definition methods HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-statemachine.