Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stateless
import (
"context"
"fmt"
"image/color"
"sort"
"strings"
"text/template"
Expand All @@ -12,6 +13,18 @@ import (
type graph struct {
}

// GraphConfiguration holds the configuration that is used to render
// graphs with the ToGraph method.
type GraphConfiguration struct {
OmitIgnoredTransitions bool // OmitIgnoredTransitions can be used to omit ignored transition from graphs.
OmitReentrantTransitions bool // OmitReentrantTransitions can be used to omit reentrant transition from graphs.
OmitInternalTransitions bool // OmitInternalTransitions can be used to omit internal transition from graphs.

IgnoredTransitionColor color.Color // IgnoredTransitionColor is the color of ignored transitions. nil represents the default color.
ReentrantTransitionColor color.Color // ReentrantTransitionColor is the color of reentrant transitions. nil represents the default color.
InternalTransitionColor color.Color // InternalTransitionColor is the color of internal transitions. nil represents the default color.
}

func (g *graph) formatStateMachine(sm *StateMachine) string {
var sb strings.Builder
sb.WriteString("digraph {\n\tcompound=true;\n\tnode [shape=Mrecord];\n\trankdir=\"LR\";\n\n")
Expand All @@ -34,7 +47,7 @@ func (g *graph) formatStateMachine(sm *StateMachine) string {
dest := sm.stateConfig[sr.InitialTransitionTarget]
if dest != nil {
src := clusterStr(sr.State, true, true)
sb.WriteString(g.formatOneLine(src, str(dest.State, true), ""))
sb.WriteString(g.formatOneLine(src, str(dest.State, true), "", nil))
}
}
}
Expand Down Expand Up @@ -126,13 +139,19 @@ func (g *graph) formatAllStateTransitions(sm *StateMachine, sr *stateRepresentat
for _, trigger := range triggerList {
switch t := trigger.(type) {
case *ignoredTriggerBehaviour:
sb.WriteString(g.formatOneTransition(sm, sr.State, sr.State, t.Trigger, nil, t.Guard))
if !sm.graphConfig.OmitIgnoredTransitions {
sb.WriteString(g.formatOneTransition(sm, sr.State, sr.State, t, nil, t.Guard))
}
case *reentryTriggerBehaviour:
actions := g.getEntryActions(sr.EntryActions, t.Trigger)
sb.WriteString(g.formatOneTransition(sm, sr.State, t.Destination, t.Trigger, actions, t.Guard))
if !sm.graphConfig.OmitReentrantTransitions {
actions := g.getEntryActions(sr.EntryActions, t.Trigger)
sb.WriteString(g.formatOneTransition(sm, sr.State, t.Destination, t, actions, t.Guard))
}
case *internalTriggerBehaviour:
actions := g.getEntryActions(sr.EntryActions, t.Trigger)
sb.WriteString(g.formatOneTransition(sm, sr.State, sr.State, t.Trigger, actions, t.Guard))
if !sm.graphConfig.OmitInternalTransitions {
actions := g.getEntryActions(sr.EntryActions, t.Trigger)
sb.WriteString(g.formatOneTransition(sm, sr.State, sr.State, t, actions, t.Guard))
}
case *transitioningTriggerBehaviour:
src := sm.stateConfig[sr.State]
if src == nil {
Expand All @@ -149,17 +168,17 @@ func (g *graph) formatAllStateTransitions(sm *StateMachine, sr *stateRepresentat
} else {
destState = dest.State
}
sb.WriteString(g.formatOneTransition(sm, src.State, destState, t.Trigger, actions, t.Guard))
sb.WriteString(g.formatOneTransition(sm, src.State, destState, t, actions, t.Guard))
case *dynamicTriggerBehaviour:
// TODO: not supported yet
}
}
return sb.String()
}

func (g *graph) formatOneTransition(sm *StateMachine, source, destination State, trigger Trigger, actions []string, guards transitionGuard) string {
func (g *graph) formatOneTransition(sm *StateMachine, source, destination State, tb triggerBehaviour, actions []string, guards transitionGuard) string {
var sb strings.Builder
sb.WriteString(str(trigger, false))
sb.WriteString(str(tb.GetTrigger(), false))
if len(actions) > 0 {
sb.WriteString(" / ")
sb.WriteString(strings.Join(actions, ", "))
Expand All @@ -170,16 +189,38 @@ func (g *graph) formatOneTransition(sm *StateMachine, source, destination State,
}
sb.WriteString(fmt.Sprintf("[%s]", esc(info.Description.String(), false)))
}
return g.formatOneLine(str(source, true), str(destination, true), sb.String())
color := findColorForTrigger(sm, tb)
return g.formatOneLine(str(source, true), str(destination, true), sb.String(), color)
}

func (g *graph) formatOneLine(fromNodeName, toNodeName, label string) string {
func findColorForTrigger(sm *StateMachine, tb triggerBehaviour) color.Color {
switch tb.(type) {
case *ignoredTriggerBehaviour:
return sm.graphConfig.IgnoredTransitionColor
case *reentryTriggerBehaviour:
return sm.graphConfig.ReentrantTransitionColor
case *internalTriggerBehaviour:
return sm.graphConfig.InternalTransitionColor
}
return nil
}

func (g *graph) formatOneLine(fromNodeName, toNodeName, label string, color color.Color) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("\t%s -> %s [label=\"%s\"", fromNodeName, toNodeName, label))
if color != nil {
graphvizColor := toGraphvizColor(color)
sb.WriteString(fmt.Sprintf(` color="%s" fontcolor="%s"`, graphvizColor, graphvizColor))
}
sb.WriteString("];\n")
return sb.String()
}

func toGraphvizColor(color color.Color) string {
r, g, b, a := color.RGBA()
return fmt.Sprintf("#%02x%02x%02x%02x", r>>8, g>>8, b>>8, a>>8)
}

func clusterStr(state any, quote, init bool) string {
s := fmt.Sprint(state)
if init {
Expand Down
7 changes: 7 additions & 0 deletions statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func callEvents(events []TransitionFunc, ctx context.Context, transition Transit
type StateMachine struct {
stateConfig map[State]*stateRepresentation
triggerConfig map[Trigger]triggerWithParameters
graphConfig GraphConfiguration
stateAccessor func(context.Context) (State, error)
stateMutator func(context.Context, State) error
unhandledTriggerAction UnhandledTriggerActionFunc
Expand Down Expand Up @@ -228,6 +229,12 @@ func (sm *StateMachine) SetTriggerParameters(trigger Trigger, argumentTypes ...r
sm.triggerConfig[trigger] = config
}

// SetGraphConfiguration sets a new configuration for rendering graphs
// using the ToGraph function.
func (sm *StateMachine) SetGraphConfiguration(config GraphConfiguration) {
sm.graphConfig = config
}

// Fire see FireCtx
func (sm *StateMachine) Fire(trigger Trigger, args ...any) error {
return sm.FireCtx(context.Background(), trigger, args...)
Expand Down