-
Notifications
You must be signed in to change notification settings - Fork 18
/
viz.py
68 lines (54 loc) · 1.52 KB
/
viz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# https://plantweb.readthedocs.io/#python-api
# https://plantuml.com/state-diagram
from plantweb.render import render
from xstate.machine import Machine
# just a test
simple_machine = Machine(
{
"id": "simple",
"initial": "green",
"states": {
"green": {"on": {"JENNY_EVENT": "yellow"}},
"yellow": {"on": {"NEXT_EVENT": "red"}},
"red": {"on": {"NEXT_EVENT": "green"}},
},
}
)
def state_node_to_viz(state_node):
result = ""
if not state_node.parent:
result += """
@startuml
skinparam state {
ArrowColor Black
ArrowThickness 2
BorderThickness 5
BorderColor Blue
BackgroundColor White
}
"""
if state_node.initial:
initial_state = state_node.initial.target[0].id
initial_string = f"[*] --> {initial_state}\n"
result += initial_string
transitions = state_node.transitions
for t in transitions:
t_string = f"{t.source.id} --> {t.target[0].id} : {t.event}\n"
result += t_string
children = state_node.states.values()
for c in children:
child_string = state_node_to_viz(c)
result += child_string
if not state_node.parent:
result += "@enduml\n"
return result
if __name__ == "__main__":
output = render(
state_node_to_viz(simple_machine.root),
engine="plantuml",
format="svg",
cacheopts={"use_cache": False},
)[0]
file1 = open("test.svg", "w")
file1.write(output.decode("utf-8"))
file1.close()