-
Notifications
You must be signed in to change notification settings - Fork 314
/
mermaid_visualizer.go
89 lines (70 loc) · 2.95 KB
/
mermaid_visualizer.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package fsm
import (
"bytes"
"fmt"
)
const highlightingColor = "#00AA00"
// MermaidDiagramType the type of the mermaid diagram type
type MermaidDiagramType string
const (
// FlowChart the diagram type for output in flowchart style (https://mermaid-js.github.io/mermaid/#/flowchart) (including current state)
FlowChart MermaidDiagramType = "flowChart"
// StateDiagram the diagram type for output in stateDiagram style (https://mermaid-js.github.io/mermaid/#/stateDiagram)
StateDiagram MermaidDiagramType = "stateDiagram"
)
// VisualizeForMermaidWithGraphType outputs a visualization of a FSM in Mermaid format as specified by the graphType.
func VisualizeForMermaidWithGraphType(fsm *FSM, graphType MermaidDiagramType) (string, error) {
switch graphType {
case FlowChart:
return visualizeForMermaidAsFlowChart(fsm), nil
case StateDiagram:
return visualizeForMermaidAsStateDiagram(fsm), nil
default:
return "", fmt.Errorf("unknown MermaidDiagramType: %s", graphType)
}
}
func visualizeForMermaidAsStateDiagram(fsm *FSM) string {
var buf bytes.Buffer
sortedTransitionKeys := getSortedTransitionKeys(fsm.transitions)
buf.WriteString("stateDiagram-v2\n")
buf.WriteString(fmt.Sprintln(` [*] -->`, fsm.current))
for _, k := range sortedTransitionKeys {
v := fsm.transitions[k]
buf.WriteString(fmt.Sprintf(` %s --> %s: %s`, k.src, v, k.event))
buf.WriteString("\n")
}
return buf.String()
}
// visualizeForMermaidAsFlowChart outputs a visualization of a FSM in Mermaid format (including highlighting of current state).
func visualizeForMermaidAsFlowChart(fsm *FSM) string {
var buf bytes.Buffer
sortedTransitionKeys := getSortedTransitionKeys(fsm.transitions)
sortedStates, statesToIDMap := getSortedStates(fsm.transitions)
writeFlowChartGraphType(&buf)
writeFlowChartStates(&buf, sortedStates, statesToIDMap)
writeFlowChartTransitions(&buf, fsm.transitions, sortedTransitionKeys, statesToIDMap)
writeFlowChartHighlightCurrent(&buf, fsm.current, statesToIDMap)
return buf.String()
}
func writeFlowChartGraphType(buf *bytes.Buffer) {
buf.WriteString("graph LR\n")
}
func writeFlowChartStates(buf *bytes.Buffer, sortedStates []string, statesToIDMap map[string]string) {
for _, state := range sortedStates {
buf.WriteString(fmt.Sprintf(` %s[%s]`, statesToIDMap[state], state))
buf.WriteString("\n")
}
buf.WriteString("\n")
}
func writeFlowChartTransitions(buf *bytes.Buffer, transitions map[eKey]string, sortedTransitionKeys []eKey, statesToIDMap map[string]string) {
for _, transition := range sortedTransitionKeys {
target := transitions[transition]
buf.WriteString(fmt.Sprintf(` %s --> |%s| %s`, statesToIDMap[transition.src], transition.event, statesToIDMap[target]))
buf.WriteString("\n")
}
buf.WriteString("\n")
}
func writeFlowChartHighlightCurrent(buf *bytes.Buffer, current string, statesToIDMap map[string]string) {
buf.WriteString(fmt.Sprintf(` style %s fill:%s`, statesToIDMap[current], highlightingColor))
buf.WriteString("\n")
}