| // Visualize outputs a visualization of a FSM in Graphviz format. |
| func Visualize(fsm *FSM) string { |
| states := make(map[string]int) |
| buf.WriteString(fmt.Sprintf(`digraph fsm {`)) |
| // make sure the initial state is at top |
| for k, v := range fsm.transitions { |
| if k.src == fsm.current { |
| buf.WriteString(fmt.Sprintf(` "%s" -> "%s" [ label = "%s" ];`, k.src, v, k.event)) |
| for k, v := range fsm.transitions { |
| if k.src != fsm.current { |
| buf.WriteString(fmt.Sprintf(` "%s" -> "%s" [ label = "%s" ];`, k.src, v, k.event)) |
| buf.WriteString(fmt.Sprintf(` "%s";`, k)) |
| buf.WriteString(fmt.Sprintln("}")) |