Documentation
¶
Overview ¶
Package state_machine provides a state machine implementation.
Index ¶
- type AnyMapStateMachine
- type State
- type StateMachine
- func (sm *StateMachine[T]) GetCurrentState() *State
- func (sm *StateMachine[T]) Reset() error
- func (sm *StateMachine[T]) SortedStates() []*State
- func (sm *StateMachine[T]) ToDOT() string
- func (sm *StateMachine[T]) ToMermaid() string
- func (sm *StateMachine[T]) ToPlantUML() string
- func (sm *StateMachine[T]) Trigger(event string) error
- func (sm *StateMachine[T]) Validate() error
- type StateRef
- type Transition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnyMapStateMachine ¶
type AnyMapStateMachine = StateMachine[map[string]interface{}]
For backward compatibility
func CreateAnyMapStateMachine ¶
func CreateAnyMapStateMachine(name string) *AnyMapStateMachine
CreateAnyMapStateMachine creates a new state machine with map[string]interface{} context for backward compatibility with code that expects the old untyped approach
type State ¶
type State struct { ID string Name string // Human-readable name Description string IsInitial bool IsFinal bool Data map[string]interface{} // Additional metadata }
State represents a single state in a state machine
type StateMachine ¶
type StateMachine[T any] struct { Name string States map[string]*State Transitions []Transition[T] CurrentState *State Context T // Shared context for the state machine }
StateMachine represents a complete state machine
func NewStateMachine ¶
func NewStateMachine[T any](name string, states map[string]*State, transitions []Transition[T]) *StateMachine[T]
NewStateMachine creates a new state machine with the given name, states, and transitions
func (*StateMachine[T]) GetCurrentState ¶
func (sm *StateMachine[T]) GetCurrentState() *State
GetCurrentState returns the current state of the state machine
func (*StateMachine[T]) Reset ¶
func (sm *StateMachine[T]) Reset() error
Reset resets the state machine to its initial state
func (*StateMachine[T]) SortedStates ¶ added in v0.0.14
func (sm *StateMachine[T]) SortedStates() []*State
SortedStates returns states in a topological order starting from initial states. This ensures consistent visualization output regardless of Go's map iteration order.
func (*StateMachine[T]) ToDOT ¶
func (sm *StateMachine[T]) ToDOT() string
ToDOT generates a DOT representation of the state machine for visualization with Graphviz
func (*StateMachine[T]) ToMermaid ¶
func (sm *StateMachine[T]) ToMermaid() string
ToMermaid generates a Mermaid.js representation of the state machine
func (*StateMachine[T]) ToPlantUML ¶ added in v0.0.13
func (sm *StateMachine[T]) ToPlantUML() string
ToPlantUML generates a PlantUML representation of the state machine
func (*StateMachine[T]) Trigger ¶
func (sm *StateMachine[T]) Trigger(event string) error
Trigger attempts to trigger a transition with the given event
func (*StateMachine[T]) Validate ¶
func (sm *StateMachine[T]) Validate() error
Validate checks if the state machine is valid
type StateRef ¶
type StateRef[T any] interface { // ID returns the unique identifier of the state ID() string // Name returns the human-readable name of the state Name() string // Description returns the human-readable description of the state Description() string // IsInitial returns whether this is an initial state IsInitial() bool // IsFinal returns whether this is a final state IsFinal() bool // GetData returns the data associated with the state GetData() map[string]interface{} // SetData sets data for the state SetData(key string, value interface{}) }
StateRef is an interface that represents a reference to a state in a state machine