From 98b67b272bcbf137f5cb3cc8cf809d8e34b2272f Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Thu, 30 May 2024 17:15:19 +0200 Subject: FSM: Remove Mermaid graph generation Too complicated to be in the core codebase and I only sometimes need it to view the updated figure. Create a CLI tool some time later --- internal/fsm/fsm.go | 80 ++--------------------------------------------------- 1 file changed, 2 insertions(+), 78 deletions(-) (limited to 'internal/fsm/fsm.go') diff --git a/internal/fsm/fsm.go b/internal/fsm/fsm.go index 0fe3444..950f7cf 100644 --- a/internal/fsm/fsm.go +++ b/internal/fsm/fsm.go @@ -1,13 +1,7 @@ -// Package fsm defines a finite state machine and has the ability to save this state machine to a graph file -// This graph file can be visualized using mermaid.js +// Package fsm defines a finite state machine package fsm -import ( - "fmt" - "os" - "path" - "sort" -) +import "fmt" type ( // StateID represents the Identifier of the state. @@ -66,9 +60,6 @@ type FSM struct { // Directory represents the path where the state graph is stored Directory string - // Generate represents whether we want to generate the graph - Generate bool - // GetStateName gets the name of a state as a string GetStateName func(StateID) string @@ -83,14 +74,12 @@ func (fsm *FSM) Init( callback func(StateID, StateID, interface{}) bool, directory string, nameGen func(StateID) string, - generate bool, ) { fsm.States = states fsm.Current = current fsm.StateCallback = callback fsm.Directory = directory fsm.GetStateName = nameGen - fsm.Generate = generate fsm.initial = current } @@ -115,29 +104,6 @@ func (fsm *FSM) CheckTransition(desired StateID) error { return fmt.Errorf("fsm invalid transition attempt from '%s' to '%s'", fsm.GetStateName(fsm.Current), fsm.GetStateName(desired)) } -// graphFilename gets the full path to the graph filename including the .graph extension. -func (fsm *FSM) graphFilename(extension string) string { - pth := path.Join(fsm.Directory, "graph") - return fmt.Sprintf("%s%s", pth, extension) -} - -// writeGraph writes the state machine to a .graph file. -func (fsm *FSM) writeGraph() { - gph := fsm.GenerateGraph() - gf := fsm.graphFilename(".graph") - f, err := os.Create(gf) - if err != nil { - return - } - defer func() { - _ = f.Close() - }() - - // Writing the graph is best effort - // TODO: Return string instead, we do not want to write files in this library - _, _ = f.WriteString(gph) -} - // GoTransitionRequired transitions the state machine to a new state with associated state data 'data' // If this transition is not handled by the client, it returns an error. func (fsm *FSM) GoTransitionRequired(newState StateID, data interface{}) error { @@ -164,9 +130,6 @@ func (fsm *FSM) GoTransitionWithData(newState StateID, data interface{}) (bool, prev := fsm.Current fsm.Current = newState - if fsm.Generate { - fsm.writeGraph() - } return fsm.StateCallback(prev, newState, data), nil } @@ -175,42 +138,3 @@ func (fsm *FSM) GoTransition(newState StateID) (bool, error) { // No data means the callback is never required return fsm.GoTransitionWithData(newState, "") } - -// generateMermaidGraph generates a graph suitable to be converted by the mermaid.js tool -// it returns the graph as a string. -func (fsm *FSM) generateMermaidGraph() string { - gph := "graph TD\n" - sf := make(StateIDSlice, 0, len(fsm.States)) - for stateID := range fsm.States { - sf = append(sf, stateID) - } - sort.Sort(sf) - for _, state := range sf { - transitions := fsm.States[state].Transitions - for _, transition := range transitions { - if state == fsm.Current { - gph += "\nstyle " + fsm.GetStateName(state) + " fill:cyan\n" - } else { - gph += "\nstyle " + fsm.GetStateName(state) + " fill:white\n" - } - gph += fsm.GetStateName( - state, - ) + "(" + fsm.GetStateName( - state, - ) + ") " + "-->|" + transition.Description + "| " + fsm.GetStateName( - transition.To, - ) + "\n" - } - } - return gph -} - -// GenerateGraph generates a mermaid graph if the state machine is initialized -// If the graph cannot be generated, it returns the empty string. -func (fsm *FSM) GenerateGraph() string { - if fsm.GetStateName != nil { - return fsm.generateMermaidGraph() - } - - return "" -} -- cgit v1.2.3