Documentation
¶
Index ¶
- Variables
- func AssertContextValue(t *testing.T, ctx Context, key string, expected interface{})
- func AssertEventProcessed(t *testing.T, result *EventResult, shouldProcess bool)
- func AssertGuardEvaluationCount(t *testing.T, count int, expected int)
- func AssertObserverCalled(t *testing.T, observer *TestObserver, transitions, enters, exits int)
- func AssertState(t *testing.T, machine Machine, expectedState string)
- func AssertStateChanged(t *testing.T, result *EventResult, expectedPrevious, expectedCurrent string)
- func AssertTransitionSequence(t *testing.T, machine Machine, expectedStates []string)
- func ConcurrentEventSender(machine Machine, eventName string, count int, done chan bool)
- func ConcurrentStateChecker(machine Machine, checks int, results chan string)
- func IsActionError(err error) bool
- func IsConfigurationError(err error) bool
- func IsGuardError(err error) bool
- func IsMachineError(err error) bool
- func IsStateError(err error) bool
- func IsTransitionError(err error) bool
- func ResetTestAction()
- func SetTestGuard(result bool)
- func TestAction(ctx Context) error
- func TestGuard(ctx Context) bool
- type ActionError
- type ActionEvent
- type ActionFunc
- type AtomicState
- type AtomicStateImpl
- func (s *AtomicStateImpl) Enter(ctx Context)
- func (s *AtomicStateImpl) Exit(ctx Context)
- func (s *AtomicStateImpl) ID() string
- func (s *AtomicStateImpl) IsComposite() bool
- func (s *AtomicStateImpl) IsFinal() bool
- func (s *AtomicStateImpl) IsParallel() bool
- func (s *AtomicStateImpl) IsPseudo() bool
- func (s *AtomicStateImpl) Parent() State
- func (s *AtomicStateImpl) WithEntryAction(action ActionFunc) *AtomicStateImpl
- func (s *AtomicStateImpl) WithExitAction(action ActionFunc) *AtomicStateImpl
- func (s *AtomicStateImpl) WithParent(parent State) *AtomicStateImpl
- type BaseEvent
- type BaseObserver
- func (o *BaseObserver) OnActionExecution(actionType string, state string, event Event, ctx Context)
- func (o *BaseObserver) OnError(err error, ctx Context)
- func (o *BaseObserver) OnEventRejected(event Event, reason string, ctx Context)
- func (o *BaseObserver) OnGuardEvaluation(from string, to string, event Event, result bool, ctx Context)
- func (o *BaseObserver) OnMachineStarted(ctx Context)
- func (o *BaseObserver) OnMachineStopped(ctx Context)
- func (o *BaseObserver) OnStateEnter(state string, ctx Context)
- func (o *BaseObserver) OnStateExit(state string, ctx Context)
- func (o *BaseObserver) OnTransition(from string, to string, event Event, ctx Context)
- type ChoiceBuilder
- type ChoiceCondition
- type ChoiceTransitionBuilder
- type CompositeState
- type CompositeStateBuilder
- type CompositeStateImpl
- func (s *CompositeStateImpl) AddSubstate(state State)
- func (s *CompositeStateImpl) InitialState() State
- func (s *CompositeStateImpl) IsComposite() bool
- func (s *CompositeStateImpl) Substates() []State
- func (s *CompositeStateImpl) WithInitialState(state State) *CompositeStateImpl
- func (s *CompositeStateImpl) WithParent(parent State) *CompositeStateImpl
- type ConfigurationError
- type Context
- type ContextEvent
- type ErrorCode
- type ErrorEvent
- type Event
- type EventRejectEvent
- type EventResult
- type ExtendedObserver
- type ForkBuilder
- type GuardError
- type GuardEvent
- type GuardFunc
- type HistoryBuilder
- type JoinBuilder
- type JunctionBuilder
- type Machine
- type MachineBuilder
- type MachineDefinition
- type MachineError
- type MachineState
- type Observer
- type ObserverManager
- func (om *ObserverManager) AddObserver(observer Observer)
- func (om *ObserverManager) NotifyActionExecution(actionType string, state string, event Event, ctx Context)
- func (om *ObserverManager) NotifyError(err error, ctx Context)
- func (om *ObserverManager) NotifyEventRejected(event Event, reason string, ctx Context)
- func (om *ObserverManager) NotifyGuardEvaluation(from string, to string, event Event, result bool, ctx Context)
- func (om *ObserverManager) NotifyMachineStarted(ctx Context)
- func (om *ObserverManager) NotifyMachineStopped(ctx Context)
- func (om *ObserverManager) NotifyStateEnter(state string, ctx Context)
- func (om *ObserverManager) NotifyStateExit(state string, ctx Context)
- func (om *ObserverManager) NotifyTransition(from string, to string, event Event, ctx Context)
- func (om *ObserverManager) RemoveObserver(observer Observer)
- type ParallelState
- type ParallelStateBuilder
- type ParallelStateImpl
- type PseudoState
- type PseudoStateImpl
- func (s *PseudoStateImpl) AddChoiceCondition(guard GuardFunc, target string, action ActionFunc)
- func (s *PseudoStateImpl) AddForkTarget(target string)
- func (s *PseudoStateImpl) IsPseudo() bool
- func (s *PseudoStateImpl) Kind() PseudoStateKind
- func (s *PseudoStateImpl) SetDefaultTarget(target string)
- func (s *PseudoStateImpl) SetForkTargets(targets []string)
- func (s *PseudoStateImpl) SetHistoryDefault(target string)
- func (s *PseudoStateImpl) SetJoinSources(sources []string)
- func (s *PseudoStateImpl) SetJoinTarget(target string)
- type PseudoStateKind
- type Region
- type RegionBuilder
- type RegionImpl
- func (r *RegionImpl) AddState(state State)
- func (r *RegionImpl) CurrentState() State
- func (r *RegionImpl) HasFinalState() bool
- func (r *RegionImpl) ID() string
- func (r *RegionImpl) InitialState() State
- func (r *RegionImpl) IsComplete() bool
- func (r *RegionImpl) ParentState() ParallelState
- func (r *RegionImpl) States() []State
- func (r *RegionImpl) WithInitialState(state State) *RegionImpl
- type SequentialState
- type SequentialStateImpl
- type State
- type StateBuilder
- type StateError
- type StateEvent
- type StateMachine
- func (sm *StateMachine) AddObserver(observer Observer)
- func (sm *StateMachine) Context() Context
- func (sm *StateMachine) CurrentState() string
- func (sm *StateMachine) GetActiveStates() []string
- func (sm *StateMachine) GetParallelRegions() map[string][]string
- func (sm *StateMachine) GetStateHierarchy() []string
- func (sm *StateMachine) HandleEvent(eventName string, eventData any) *EventResult
- func (sm *StateMachine) HandleEventWithContext(ctx context.Context, eventName string, eventData any) *EventResult
- func (sm *StateMachine) IsInState(stateID string) bool
- func (sm *StateMachine) IsStateActive(stateID string) bool
- func (sm *StateMachine) MarshalJSON() ([]byte, error)
- func (sm *StateMachine) RegionState(regionID string) string
- func (sm *StateMachine) RemoveObserver(observer Observer)
- func (sm *StateMachine) Reset() error
- func (sm *StateMachine) SendEvent(eventName string, eventData any) *EventResult
- func (sm *StateMachine) SendEventWithContext(ctx context.Context, eventName string, eventData any) *EventResult
- func (sm *StateMachine) SetRegionState(regionID string, stateID string) error
- func (sm *StateMachine) SetState(state string) error
- func (sm *StateMachine) Start() error
- func (sm *StateMachine) Stop() error
- func (sm *StateMachine) UnmarshalJSON(data []byte) error
- func (sm *StateMachine) WithContext(ctx Context) Machine
- type StateMachineContext
- func (ctx *StateMachineContext) Fork() Context
- func (ctx *StateMachineContext) Get(key string) (any, bool)
- func (ctx *StateMachineContext) GetAll() map[string]any
- func (ctx *StateMachineContext) GetCurrentEvent() Event
- func (ctx *StateMachineContext) GetCurrentState() string
- func (ctx *StateMachineContext) GetEventData() any
- func (ctx *StateMachineContext) GetEventDataAs(target any) bool
- func (ctx *StateMachineContext) GetEventName() string
- func (ctx *StateMachineContext) GetMachine() Machine
- func (ctx *StateMachineContext) GetPreviousState() string
- func (ctx *StateMachineContext) GetSourceState() string
- func (ctx *StateMachineContext) GetTargetState() string
- func (ctx *StateMachineContext) Set(key string, value any)
- func (ctx *StateMachineContext) WithValue(key string, value any) Context
- type TestObserver
- func (o *TestObserver) LastStateEnter() *StateEvent
- func (o *TestObserver) LastTransition() *TransitionEvent
- func (o *TestObserver) OnActionExecution(actionType string, state string, event Event, ctx Context)
- func (o *TestObserver) OnError(err error, ctx Context)
- func (o *TestObserver) OnEventRejected(event Event, reason string, ctx Context)
- func (o *TestObserver) OnGuardEvaluation(from string, to string, event Event, result bool, ctx Context)
- func (o *TestObserver) OnMachineStarted(ctx Context)
- func (o *TestObserver) OnMachineStopped(ctx Context)
- func (o *TestObserver) OnStateEnter(state string, ctx Context)
- func (o *TestObserver) OnStateExit(state string, ctx Context)
- func (o *TestObserver) OnTransition(from string, to string, event Event, ctx Context)
- func (o *TestObserver) Reset()
- func (o *TestObserver) StateEnterCount() int
- func (o *TestObserver) StateExitCount() int
- func (o *TestObserver) TransitionCount() int
- type Transition
- type TransitionBuilder
- type TransitionError
- type TransitionEvent
Constants ¶
This section is empty.
Variables ¶
var TestActionCalled bool
Test action functions for testing
var TestActionError error
var TestGuardResult bool
Test guard functions for testing
Functions ¶
func AssertContextValue ¶
AssertContextValue checks if context contains expected value
func AssertEventProcessed ¶
func AssertEventProcessed(t *testing.T, result *EventResult, shouldProcess bool)
AssertEventProcessed checks if event was processed successfully
func AssertGuardEvaluationCount ¶
AssertGuardEvaluationCount checks if guard was evaluated expected number of times
func AssertObserverCalled ¶
func AssertObserverCalled(t *testing.T, observer *TestObserver, transitions, enters, exits int)
AssertObserverCalled checks if observer methods were called expected number of times
func AssertState ¶
AssertState checks if machine is in expected state
func AssertStateChanged ¶
func AssertStateChanged(t *testing.T, result *EventResult, expectedPrevious, expectedCurrent string)
AssertStateChanged checks if state transition occurred
func AssertTransitionSequence ¶
AssertTransitionSequence checks if machine followed expected state sequence
func ConcurrentEventSender ¶
ConcurrentEventSender sends events concurrently for testing thread safety
func ConcurrentStateChecker ¶
ConcurrentStateChecker checks states concurrently for testing thread safety
func IsActionError ¶
IsActionError checks if an error is an ActionError
func IsConfigurationError ¶
IsConfigurationError checks if an error is a ConfigurationError
func IsGuardError ¶
IsGuardError checks if an error is a GuardError
func IsMachineError ¶
IsMachineError checks if an error is a MachineError
func IsStateError ¶
IsStateError checks if an error is a StateError
func IsTransitionError ¶
IsTransitionError checks if an error is a TransitionError
func ResetTestAction ¶
func ResetTestAction()
func SetTestGuard ¶
func SetTestGuard(result bool)
func TestAction ¶
Types ¶
type ActionError ¶
ActionError represents action execution errors
func NewActionError ¶
func NewActionError(action, state string, err error) *ActionError
NewActionError creates a new action execution error
func (*ActionError) Error ¶
func (e *ActionError) Error() string
func (*ActionError) Unwrap ¶
func (e *ActionError) Unwrap() error
type ActionEvent ¶
type ActionFunc ¶
ActionFunc represents an enhanced action function with error support
type AtomicStateImpl ¶
type AtomicStateImpl struct {
// contains filtered or unexported fields
}
AtomicStateImpl implements the AtomicState interface
func NewAtomicState ¶
func NewAtomicState(id string) *AtomicStateImpl
NewAtomicState creates a new atomic state
func NewFinalState ¶
func NewFinalState(id string) *AtomicStateImpl
NewFinalState creates a new final state
func (*AtomicStateImpl) Enter ¶
func (s *AtomicStateImpl) Enter(ctx Context)
Enter executes the entry action
func (*AtomicStateImpl) Exit ¶
func (s *AtomicStateImpl) Exit(ctx Context)
Exit executes the exit action
func (*AtomicStateImpl) IsComposite ¶
func (s *AtomicStateImpl) IsComposite() bool
IsComposite returns false for atomic states
func (*AtomicStateImpl) IsFinal ¶
func (s *AtomicStateImpl) IsFinal() bool
IsFinal returns whether this is a final state
func (*AtomicStateImpl) IsParallel ¶
func (s *AtomicStateImpl) IsParallel() bool
IsParallel returns false for atomic states
func (*AtomicStateImpl) IsPseudo ¶
func (s *AtomicStateImpl) IsPseudo() bool
IsPseudo returns false for atomic states
func (*AtomicStateImpl) Parent ¶
func (s *AtomicStateImpl) Parent() State
Parent returns the parent state
func (*AtomicStateImpl) WithEntryAction ¶
func (s *AtomicStateImpl) WithEntryAction(action ActionFunc) *AtomicStateImpl
WithEntryAction sets the entry action for the state
func (*AtomicStateImpl) WithExitAction ¶
func (s *AtomicStateImpl) WithExitAction(action ActionFunc) *AtomicStateImpl
WithExitAction sets the exit action for the state
func (*AtomicStateImpl) WithParent ¶
func (s *AtomicStateImpl) WithParent(parent State) *AtomicStateImpl
WithParent sets the parent state
type BaseEvent ¶
type BaseEvent struct {
// contains filtered or unexported fields
}
BaseEvent provides a basic implementation of the Event interface
func (*BaseEvent) GetMetadata ¶
GetMetadata returns the event metadata
func (*BaseEvent) GetTimestamp ¶
GetTimestamp returns the event timestamp
type BaseObserver ¶
type BaseObserver struct{}
BaseObserver provides a default implementation with no-op methods
func (*BaseObserver) OnActionExecution ¶
func (o *BaseObserver) OnActionExecution(actionType string, state string, event Event, ctx Context)
OnActionExecution implements the optional ExtendedObserver method
func (*BaseObserver) OnError ¶
func (o *BaseObserver) OnError(err error, ctx Context)
OnError implements the optional ExtendedObserver method
func (*BaseObserver) OnEventRejected ¶
func (o *BaseObserver) OnEventRejected(event Event, reason string, ctx Context)
OnEventRejected implements the optional ExtendedObserver method
func (*BaseObserver) OnGuardEvaluation ¶
func (o *BaseObserver) OnGuardEvaluation(from string, to string, event Event, result bool, ctx Context)
OnGuardEvaluation implements the optional ExtendedObserver method
func (*BaseObserver) OnMachineStarted ¶
func (o *BaseObserver) OnMachineStarted(ctx Context)
OnMachineStarted implements the optional ExtendedObserver method
func (*BaseObserver) OnMachineStopped ¶
func (o *BaseObserver) OnMachineStopped(ctx Context)
OnMachineStopped implements the optional ExtendedObserver method
func (*BaseObserver) OnStateEnter ¶
func (o *BaseObserver) OnStateEnter(state string, ctx Context)
OnStateEnter implements the required Observer method
func (*BaseObserver) OnStateExit ¶
func (o *BaseObserver) OnStateExit(state string, ctx Context)
OnStateExit implements the optional ExtendedObserver method
func (*BaseObserver) OnTransition ¶
func (o *BaseObserver) OnTransition(from string, to string, event Event, ctx Context)
OnTransition implements the required Observer method
type ChoiceBuilder ¶
type ChoiceBuilder interface {
When(condition GuardFunc) ChoiceTransitionBuilder
Otherwise(target string) ChoiceBuilder
Do(action ActionFunc) ChoiceBuilder
OnEntry(action ActionFunc) ChoiceBuilder
// Navigation back
State(id string) StateBuilder
Build() MachineDefinition
}
ChoiceBuilder handles choice pseudostate with conditions
type ChoiceCondition ¶
type ChoiceCondition struct {
// Guard condition for this choice branch
Guard GuardFunc
// Target state for this choice branch
Target string
// Action to execute for this specific branch
Action ActionFunc
}
ChoiceCondition represents a condition and target for a choice pseudostate
type ChoiceTransitionBuilder ¶
type ChoiceTransitionBuilder interface {
To(target string) ChoiceBuilder
Do(action ActionFunc) ChoiceTransitionBuilder
}
ChoiceTransitionBuilder handles conditional transitions from choice
type CompositeState ¶
type CompositeState interface {
State
InitialState() State
Substates() []State
AddSubstate(state State)
}
CompositeState represents a state with substates
type CompositeStateBuilder ¶
type CompositeStateBuilder interface {
// Child states
State(id string) StateBuilder
CompositeState(id string) CompositeStateBuilder
// Child pseudostates
Choice(id string) ChoiceBuilder
Junction(id string) JunctionBuilder
Fork(id string) ForkBuilder
Join(id string) JoinBuilder
History(id string) HistoryBuilder
DeepHistory(id string) HistoryBuilder
// State actions
OnEntry(action ActionFunc) CompositeStateBuilder
OnExit(action ActionFunc) CompositeStateBuilder
// Transitions from this composite state
To(target string) TransitionBuilder
ToParent(target string) TransitionBuilder
// Navigation back to parent
End() MachineBuilder
Build() MachineDefinition
}
CompositeStateBuilder handles hierarchical states
type CompositeStateImpl ¶
type CompositeStateImpl struct {
AtomicStateImpl
// contains filtered or unexported fields
}
CompositeStateImpl implements the CompositeState interface
func NewCompositeState ¶
func NewCompositeState(id string) *CompositeStateImpl
NewCompositeState creates a new composite state
func (*CompositeStateImpl) AddSubstate ¶
func (s *CompositeStateImpl) AddSubstate(state State)
AddSubstate adds a substate to the composite state
func (*CompositeStateImpl) InitialState ¶
func (s *CompositeStateImpl) InitialState() State
InitialState returns the initial substate
func (*CompositeStateImpl) IsComposite ¶
func (s *CompositeStateImpl) IsComposite() bool
IsComposite returns true for composite states
func (*CompositeStateImpl) Substates ¶
func (s *CompositeStateImpl) Substates() []State
Substates returns all substates
func (*CompositeStateImpl) WithInitialState ¶
func (s *CompositeStateImpl) WithInitialState(state State) *CompositeStateImpl
WithInitialState sets the initial substate
func (*CompositeStateImpl) WithParent ¶
func (s *CompositeStateImpl) WithParent(parent State) *CompositeStateImpl
WithParent sets the parent state
type ConfigurationError ¶
ConfigurationError represents machine configuration issues
func NewConfigurationError ¶
func NewConfigurationError(component, issue string) *ConfigurationError
NewConfigurationError creates a new configuration error
func (*ConfigurationError) Error ¶
func (e *ConfigurationError) Error() string
type Context ¶
type Context interface {
context.Context
Get(key string) (any, bool)
Set(key string, value any)
GetAll() map[string]any
GetMachine() Machine
GetCurrentState() string
GetSourceState() string
GetTargetState() string
GetCurrentEvent() Event
GetEventName() string
GetEventData() any
GetEventDataAs(target any) bool
GetPreviousState() string
WithValue(key string, value any) Context
Fork() Context
}
Context provides access to data and information during state machine execution
func CreateTestContext ¶
func CreateTestContext() Context
CreateTestContext creates a simple test context
func NewContext ¶
NewContext creates a new state machine context
func NewSimpleContext ¶
func NewSimpleContext() Context
NewSimpleContext creates a simple context for testing
type ContextEvent ¶
type ContextEvent struct {
Ctx Context
}
type ErrorCode ¶
type ErrorCode int
ErrorCode represents specific error conditions in the state machine
const ( // No error occurred ErrCodeNone ErrorCode = iota // State was not found in the machine ErrCodeStateNotFound // Transition is not allowed from current state ErrCodeTransitionNotAllowed // Guard condition rejected the transition ErrCodeGuardRejected // Event is invalid for current context ErrCodeInvalidEvent // Machine is not in started state ErrCodeMachineNotStarted // Action execution failed ErrCodeActionFailed // Machine configuration is invalid ErrCodeInvalidConfiguration // State is in invalid condition ErrCodeInvalidState // Concurrent modification detected ErrCodeConcurrentModification )
func GetErrorCode ¶
GetErrorCode returns the error code for known error types
type ErrorEvent ¶
type Event ¶
type Event interface {
GetName() string
GetData() any
GetTimestamp() time.Time
GetMetadata() map[string]any
}
Event represents a trigger for transitions in the state machine
func CreateTestEvent ¶
CreateTestEvent creates a test event
func NewEventWithMetadata ¶
NewEventWithMetadata creates a new event with metadata
func NewTypedEvent ¶
NewTypedEvent creates a new event with typed data
type EventRejectEvent ¶
type EventResult ¶
type EventResult struct {
Processed bool
StateChanged bool
PreviousState string
CurrentState string
Error error
RejectionReason string
}
EventResult represents the result of processing an event
func NewEventResult ¶
func NewEventResult(processed, stateChanged bool, prevState, currentState string) *EventResult
NewEventResult creates a new event result
func (*EventResult) Success ¶
func (r *EventResult) Success() bool
Success returns true if the event was processed successfully
func (*EventResult) WithError ¶
func (r *EventResult) WithError(err error) *EventResult
WithError adds an error to the event result
func (*EventResult) WithRejection ¶
func (r *EventResult) WithRejection(reason string) *EventResult
WithRejection adds a rejection reason to the event result
type ExtendedObserver ¶
type ExtendedObserver interface {
Observer
// OnStateExit is called when exiting a state
OnStateExit(state string, ctx Context)
// OnGuardEvaluation is called when a guard condition is evaluated
OnGuardEvaluation(from string, to string, event Event, result bool, ctx Context)
// OnEventRejected is called when an event is rejected (no valid transition)
OnEventRejected(event Event, reason string, ctx Context)
// OnError is called when an error occurs during processing
OnError(err error, ctx Context)
// OnActionExecution is called when an action is executed
OnActionExecution(actionType string, state string, event Event, ctx Context)
// OnMachineStarted is called when the state machine starts
OnMachineStarted(ctx Context)
// OnMachineStopped is called when the state machine stops
OnMachineStopped(ctx Context)
}
ExtendedObserver provides additional optional observation methods
type ForkBuilder ¶
type ForkBuilder interface {
To(targets ...string) ForkBuilder
Do(action ActionFunc) ForkBuilder
OnEntry(action ActionFunc) ForkBuilder
// Navigation back
State(id string) StateBuilder
Build() MachineDefinition
}
ForkBuilder handles splitting to parallel targets
type GuardError ¶
GuardError represents guard condition failures
func NewGuardRejectedError ¶
func NewGuardRejectedError(from, to, event string, guardName string) *GuardError
NewGuardRejectedError creates a new guard rejected error
func (*GuardError) Error ¶
func (e *GuardError) Error() string
type GuardEvent ¶
type HistoryBuilder ¶
type HistoryBuilder interface {
Default(target string) HistoryBuilder
Do(action ActionFunc) HistoryBuilder
OnEntry(action ActionFunc) HistoryBuilder
// Navigation back
State(id string) StateBuilder
Build() MachineDefinition
}
HistoryBuilder handles history pseudostates
type JoinBuilder ¶
type JoinBuilder interface {
From(sources ...string) JoinBuilder
To(target string) JoinBuilder
Do(action ActionFunc) JoinBuilder
OnEntry(action ActionFunc) JoinBuilder
// Navigation back
State(id string) StateBuilder
Build() MachineDefinition
}
JoinBuilder handles synchronization from multiple sources
type JunctionBuilder ¶
type JunctionBuilder interface {
To(target string) JunctionBuilder
Do(action ActionFunc) JunctionBuilder
OnEntry(action ActionFunc) JunctionBuilder
// Navigation back
State(id string) StateBuilder
Build() MachineDefinition
}
JunctionBuilder handles simple merge points
type Machine ¶
type Machine interface {
Start() error
Stop() error
Reset() error
CurrentState() string
SetState(state string) error
SetRegionState(regionID string, stateID string) error
RegionState(regionID string) string
GetStateHierarchy() []string
IsInState(stateID string) bool
GetActiveStates() []string
IsStateActive(stateID string) bool
GetParallelRegions() map[string][]string
SendEvent(eventName string, eventData any) *EventResult
SendEventWithContext(ctx context.Context, eventName string, eventData any) *EventResult
HandleEvent(eventName string, eventData any) *EventResult
HandleEventWithContext(ctx context.Context, eventName string, eventData any) *EventResult
AddObserver(observer Observer)
RemoveObserver(observer Observer)
Context() Context
WithContext(ctx Context) Machine
MarshalJSON() ([]byte, error)
UnmarshalJSON(data []byte) error
}
Machine represents a state machine instance
func CreateEdgeCaseMachine ¶
func CreateEdgeCaseMachine() Machine
CreateEdgeCaseMachine creates a machine configured for edge case testing
func CreateHierarchicalMachine ¶
func CreateHierarchicalMachine() Machine
CreateHierarchicalMachine creates a hierarchical state machine for testing
func CreateParallelMachine ¶
func CreateParallelMachine() Machine
CreateParallelMachine creates a parallel state machine for testing
func CreatePseudostateMachine ¶
func CreatePseudostateMachine() Machine
CreatePseudostateMachine creates a machine with pseudostates for testing
func CreateSimpleMachine ¶
func CreateSimpleMachine() Machine
CreateSimpleMachine creates a basic state machine for testing
type MachineBuilder ¶
type MachineBuilder interface {
State(id string) StateBuilder
CompositeState(id string) CompositeStateBuilder
ParallelState(id string) ParallelStateBuilder
Choice(id string) ChoiceBuilder
Junction(id string) JunctionBuilder
Fork(id string) ForkBuilder
Join(id string) JoinBuilder
History(id string) HistoryBuilder
DeepHistory(id string) HistoryBuilder
Build() MachineDefinition
}
MachineBuilder provides the main entry point for building state machines
func NewMachine ¶
func NewMachine() MachineBuilder
NewMachine creates a new machine builder with the new fluent API
func NewMachineDefinition ¶
func NewMachineDefinition() MachineBuilder
NewMachineDefinition creates a new machine definition builder
type MachineDefinition ¶
type MachineDefinition interface {
CreateInstance() Machine
Build() MachineDefinition
GetInitialState() string
GetStates() map[string]State
GetTransitions() map[string][]Transition
}
MachineDefinition represents the configuration of a state machine
type MachineError ¶
MachineError represents state machine operation errors
func NewMachineError ¶
func NewMachineError(code ErrorCode, operation string, message string) *MachineError
NewMachineError creates a new machine error
func NewMachineNotStartedError ¶
func NewMachineNotStartedError(operation string) *MachineError
NewMachineNotStartedError creates a new machine not started error
func (*MachineError) Error ¶
func (e *MachineError) Error() string
type MachineState ¶
type MachineState int
MachineState represents the current state of the machine
const ( // Machine is stopped and not processing events MachineStateStopped MachineState = iota // Machine is running and processing events MachineStateStarted // Machine is in error state MachineStateError )
type Observer ¶
type Observer interface {
// OnTransition is called when a state transition occurs
OnTransition(from string, to string, event Event, ctx Context)
// OnStateEnter is called when entering a new state
OnStateEnter(state string, ctx Context)
}
Observer represents an entity that observes state machine lifecycle
type ObserverManager ¶
type ObserverManager struct {
// contains filtered or unexported fields
}
ObserverManager manages a collection of observers
func NewObserverManager ¶
func NewObserverManager() *ObserverManager
NewObserverManager creates a new observer manager
func (*ObserverManager) AddObserver ¶
func (om *ObserverManager) AddObserver(observer Observer)
AddObserver adds an observer to the manager
func (*ObserverManager) NotifyActionExecution ¶
func (om *ObserverManager) NotifyActionExecution(actionType string, state string, event Event, ctx Context)
NotifyActionExecution notifies all observers of action execution
func (*ObserverManager) NotifyError ¶
func (om *ObserverManager) NotifyError(err error, ctx Context)
NotifyError notifies all observers of errors
func (*ObserverManager) NotifyEventRejected ¶
func (om *ObserverManager) NotifyEventRejected(event Event, reason string, ctx Context)
NotifyEventRejected notifies all observers of event rejection
func (*ObserverManager) NotifyGuardEvaluation ¶
func (om *ObserverManager) NotifyGuardEvaluation(from string, to string, event Event, result bool, ctx Context)
NotifyGuardEvaluation notifies all observers of guard evaluation
func (*ObserverManager) NotifyMachineStarted ¶
func (om *ObserverManager) NotifyMachineStarted(ctx Context)
NotifyMachineStarted notifies all observers that the machine has started
func (*ObserverManager) NotifyMachineStopped ¶
func (om *ObserverManager) NotifyMachineStopped(ctx Context)
NotifyMachineStopped notifies all observers that the machine has stopped
func (*ObserverManager) NotifyStateEnter ¶
func (om *ObserverManager) NotifyStateEnter(state string, ctx Context)
NotifyStateEnter notifies all observers of state entry
func (*ObserverManager) NotifyStateExit ¶
func (om *ObserverManager) NotifyStateExit(state string, ctx Context)
NotifyStateExit notifies all observers of state exit
func (*ObserverManager) NotifyTransition ¶
func (om *ObserverManager) NotifyTransition(from string, to string, event Event, ctx Context)
NotifyTransition notifies all observers of a state transition
func (*ObserverManager) RemoveObserver ¶
func (om *ObserverManager) RemoveObserver(observer Observer)
RemoveObserver removes an observer from the manager
type ParallelState ¶
type ParallelState interface {
CompositeState
Regions() []Region
AddRegion(region Region)
}
ParallelState represents a parallel composite state
type ParallelStateBuilder ¶
type ParallelStateBuilder interface {
// Regions
Region(id string) RegionBuilder
// State actions
OnEntry(action ActionFunc) ParallelStateBuilder
OnExit(action ActionFunc) ParallelStateBuilder
// Transitions from this parallel state
To(target string) TransitionBuilder
ToParent(target string) TransitionBuilder
// Navigation back
End() MachineBuilder
Build() MachineDefinition
}
ParallelStateBuilder handles parallel regions
type ParallelStateImpl ¶
type ParallelStateImpl struct {
CompositeStateImpl
// contains filtered or unexported fields
}
ParallelStateImpl implements the ParallelState interface
func NewParallelState ¶
func NewParallelState(id string) *ParallelStateImpl
NewParallelState creates a new parallel composite state
func (*ParallelStateImpl) AddRegion ¶
func (s *ParallelStateImpl) AddRegion(region Region)
AddRegion adds a parallel region
func (*ParallelStateImpl) IsParallel ¶
func (s *ParallelStateImpl) IsParallel() bool
IsParallel returns true for parallel states
func (*ParallelStateImpl) Regions ¶
func (s *ParallelStateImpl) Regions() []Region
Regions returns all parallel regions
type PseudoState ¶
type PseudoState interface {
State
Kind() PseudoStateKind
}
PseudoState represents a transient state
type PseudoStateImpl ¶
type PseudoStateImpl struct {
AtomicStateImpl
// contains filtered or unexported fields
}
PseudoStateImpl implements the PseudoState interface
func NewHistoryState ¶
func NewHistoryState(id string, deep bool) *PseudoStateImpl
NewHistoryState creates a new history pseudostate
func NewPseudoState ¶
func NewPseudoState(id string, kind PseudoStateKind) *PseudoStateImpl
NewPseudoState creates a new pseudostate
func (*PseudoStateImpl) AddChoiceCondition ¶
func (s *PseudoStateImpl) AddChoiceCondition(guard GuardFunc, target string, action ActionFunc)
AddChoiceCondition adds a condition for Choice pseudostates
func (*PseudoStateImpl) AddForkTarget ¶
func (s *PseudoStateImpl) AddForkTarget(target string)
AddForkTarget adds a target state for Fork pseudostates
func (*PseudoStateImpl) IsPseudo ¶
func (s *PseudoStateImpl) IsPseudo() bool
IsPseudo returns true for pseudostates
func (*PseudoStateImpl) Kind ¶
func (s *PseudoStateImpl) Kind() PseudoStateKind
Kind returns the pseudostate kind
func (*PseudoStateImpl) SetDefaultTarget ¶
func (s *PseudoStateImpl) SetDefaultTarget(target string)
SetDefaultTarget sets the default target for Choice/Junction pseudostates
func (*PseudoStateImpl) SetForkTargets ¶
func (s *PseudoStateImpl) SetForkTargets(targets []string)
SetForkTargets sets the target states for Fork pseudostates
func (*PseudoStateImpl) SetHistoryDefault ¶
func (s *PseudoStateImpl) SetHistoryDefault(target string)
SetHistoryDefault sets the default target for History pseudostates
func (*PseudoStateImpl) SetJoinSources ¶
func (s *PseudoStateImpl) SetJoinSources(sources []string)
SetJoinSources adds a combination of source states for Join pseudostates Each call to this method represents one valid source combination
func (*PseudoStateImpl) SetJoinTarget ¶
func (s *PseudoStateImpl) SetJoinTarget(target string)
SetJoinTarget sets the target state for Join pseudostates
type PseudoStateKind ¶
type PseudoStateKind int
PseudoStateKind enumerates the types of pseudostates
const ( // Initial pseudostate marks the starting point Initial PseudoStateKind = iota // Choice pseudostate for conditional branching Choice // Junction pseudostate for merge points Junction // Fork pseudostate for parallel splitting Fork // Join pseudostate for parallel synchronization Join // Terminate pseudostate for machine termination Terminate // History pseudostate for shallow history History // DeepHistory pseudostate for deep history DeepHistory )
type Region ¶
type Region interface {
ID() string
ParentState() ParallelState
CurrentState() State
InitialState() State
States() []State
IsComplete() bool // Check if region has reached final state
HasFinalState() bool // Check if region contains a final state
}
Region represents a parallel region
type RegionBuilder ¶
type RegionBuilder interface {
// States within this region
State(id string) StateBuilder
CompositeState(id string) CompositeStateBuilder
// Pseudostates within this region
Choice(id string) ChoiceBuilder
Junction(id string) JunctionBuilder
Fork(id string) ForkBuilder
Join(id string) JoinBuilder
History(id string) HistoryBuilder
DeepHistory(id string) HistoryBuilder
// Navigation
Region(id string) RegionBuilder // Sibling region
End() ParallelStateBuilder // Back to parallel state
Build() MachineDefinition
}
RegionBuilder handles parallel region configuration
type RegionImpl ¶
type RegionImpl struct {
// contains filtered or unexported fields
}
RegionImpl implements the Region interface
func NewRegion ¶
func NewRegion(id string, parentState ParallelState) *RegionImpl
NewRegion creates a new parallel region
func (*RegionImpl) AddState ¶
func (r *RegionImpl) AddState(state State)
AddState adds a state to this region
func (*RegionImpl) CurrentState ¶
func (r *RegionImpl) CurrentState() State
CurrentState returns the current state in this region
func (*RegionImpl) HasFinalState ¶
func (r *RegionImpl) HasFinalState() bool
HasFinalState checks if this region contains at least one final state
func (*RegionImpl) InitialState ¶
func (r *RegionImpl) InitialState() State
InitialState returns the initial state of this region
func (*RegionImpl) IsComplete ¶
func (r *RegionImpl) IsComplete() bool
IsComplete checks if this region has reached a final state
func (*RegionImpl) ParentState ¶
func (r *RegionImpl) ParentState() ParallelState
ParentState returns the parent parallel state
func (*RegionImpl) States ¶
func (r *RegionImpl) States() []State
States returns all states in this region
func (*RegionImpl) WithInitialState ¶
func (r *RegionImpl) WithInitialState(state State) *RegionImpl
WithInitialState sets the initial state for this region
type SequentialState ¶
type SequentialState interface {
CompositeState
}
SequentialState represents a sequential composite state
type SequentialStateImpl ¶
type SequentialStateImpl struct {
CompositeStateImpl
}
SequentialStateImpl implements the SequentialState interface
func NewSequentialState ¶
func NewSequentialState(id string) *SequentialStateImpl
NewSequentialState creates a new sequential composite state
type State ¶
type State interface {
ID() string
Enter(ctx Context)
Exit(ctx Context)
Parent() State
IsComposite() bool
IsParallel() bool
IsPseudo() bool
IsFinal() bool
}
State represents a state in the state machine
type StateBuilder ¶
type StateBuilder interface {
To(target string) TransitionBuilder
ToSelf() TransitionBuilder
ToParent(target string) TransitionBuilder
OnEntry(action ActionFunc) StateBuilder
OnExit(action ActionFunc) StateBuilder
Final() StateBuilder
Initial() StateBuilder
State(id string) StateBuilder
CompositeState(id string) CompositeStateBuilder
ParallelState(id string) ParallelStateBuilder
Choice(id string) ChoiceBuilder
Junction(id string) JunctionBuilder
Fork(id string) ForkBuilder
Join(id string) JoinBuilder
History(id string) HistoryBuilder
DeepHistory(id string) HistoryBuilder
Build() MachineDefinition
}
StateBuilder handles regular atomic state configuration
type StateError ¶
StateError represents state-related errors
func NewInvalidStateError ¶
func NewInvalidStateError(stateID string, reason string) *StateError
NewInvalidStateError creates a new invalid state error
func NewStateError ¶
func NewStateError(code ErrorCode, stateID string, message string) *StateError
NewStateError creates a new state error with custom values
func NewStateNotFoundError ¶
func NewStateNotFoundError(stateID string) *StateError
NewStateNotFoundError creates a new state not found error
func (*StateError) Error ¶
func (e *StateError) Error() string
type StateEvent ¶
type StateMachine ¶
type StateMachine struct {
// contains filtered or unexported fields
}
StateMachine implements the Machine interface
func (*StateMachine) AddObserver ¶
func (sm *StateMachine) AddObserver(observer Observer)
AddObserver adds an observer to the machine
func (*StateMachine) Context ¶
func (sm *StateMachine) Context() Context
Context returns the machine's context
func (*StateMachine) CurrentState ¶
func (sm *StateMachine) CurrentState() string
CurrentState returns the current state
func (*StateMachine) GetActiveStates ¶
func (sm *StateMachine) GetActiveStates() []string
GetActiveStates returns all currently active states (including parallel regions)
func (*StateMachine) GetParallelRegions ¶
func (sm *StateMachine) GetParallelRegions() map[string][]string
GetParallelRegions returns the current parallel regions and their active states
func (*StateMachine) GetStateHierarchy ¶
func (sm *StateMachine) GetStateHierarchy() []string
GetStateHierarchy returns the full hierarchical path of the current state
func (*StateMachine) HandleEvent ¶
func (sm *StateMachine) HandleEvent(eventName string, eventData any) *EventResult
HandleEvent handles an event synchronously
func (*StateMachine) HandleEventWithContext ¶
func (sm *StateMachine) HandleEventWithContext(ctx context.Context, eventName string, eventData any) *EventResult
HandleEventWithContext handles an event synchronously with context
func (*StateMachine) IsInState ¶
func (sm *StateMachine) IsInState(stateID string) bool
IsInState checks if the machine is currently in the specified state or any of its substates
func (*StateMachine) IsStateActive ¶
func (sm *StateMachine) IsStateActive(stateID string) bool
IsStateActive checks if a specific state is currently active
func (*StateMachine) MarshalJSON ¶
func (sm *StateMachine) MarshalJSON() ([]byte, error)
MarshalJSON serializes the machine state to JSON
func (*StateMachine) RegionState ¶
func (sm *StateMachine) RegionState(regionID string) string
RegionState returns the current state of a specific region
func (*StateMachine) RemoveObserver ¶
func (sm *StateMachine) RemoveObserver(observer Observer)
RemoveObserver removes an observer from the machine
func (*StateMachine) SendEvent ¶
func (sm *StateMachine) SendEvent(eventName string, eventData any) *EventResult
SendEvent sends an event asynchronously
func (*StateMachine) SendEventWithContext ¶
func (sm *StateMachine) SendEventWithContext(ctx context.Context, eventName string, eventData any) *EventResult
SendEventWithContext sends an event asynchronously with context
func (*StateMachine) SetRegionState ¶
func (sm *StateMachine) SetRegionState(regionID string, stateID string) error
SetRegionState sets the state of a specific region in a parallel state
func (*StateMachine) SetState ¶
func (sm *StateMachine) SetState(state string) error
SetState sets the current state
func (*StateMachine) UnmarshalJSON ¶
func (sm *StateMachine) UnmarshalJSON(data []byte) error
UnmarshalJSON deserializes the machine state from JSON
func (*StateMachine) WithContext ¶
func (sm *StateMachine) WithContext(ctx Context) Machine
WithContext sets the machine's context
type StateMachineContext ¶
StateMachineContext implements the Context interface
func (*StateMachineContext) Fork ¶
func (ctx *StateMachineContext) Fork() Context
Fork creates a new context with copied data
func (*StateMachineContext) Get ¶
func (ctx *StateMachineContext) Get(key string) (any, bool)
Get retrieves a value from the context
func (*StateMachineContext) GetAll ¶
func (ctx *StateMachineContext) GetAll() map[string]any
GetAll returns all context data
func (*StateMachineContext) GetCurrentEvent ¶
func (ctx *StateMachineContext) GetCurrentEvent() Event
GetCurrentEvent returns the current event being processed
func (*StateMachineContext) GetCurrentState ¶
func (ctx *StateMachineContext) GetCurrentState() string
GetCurrentState returns the current state ID
func (*StateMachineContext) GetEventData ¶
func (ctx *StateMachineContext) GetEventData() any
GetEventData returns the data of the current event
func (*StateMachineContext) GetEventDataAs ¶
func (ctx *StateMachineContext) GetEventDataAs(target any) bool
GetEventDataAs attempts to cast event data to the target type
func (*StateMachineContext) GetEventName ¶
func (ctx *StateMachineContext) GetEventName() string
GetEventName returns the name of the current event
func (*StateMachineContext) GetMachine ¶
func (ctx *StateMachineContext) GetMachine() Machine
GetMachine returns the associated state machine
func (*StateMachineContext) GetPreviousState ¶
func (ctx *StateMachineContext) GetPreviousState() string
GetPreviousState returns the previous state
func (*StateMachineContext) GetSourceState ¶
func (ctx *StateMachineContext) GetSourceState() string
GetSourceState returns the source state of the current transition
func (*StateMachineContext) GetTargetState ¶
func (ctx *StateMachineContext) GetTargetState() string
GetTargetState returns the target state of the current transition
func (*StateMachineContext) Set ¶
func (ctx *StateMachineContext) Set(key string, value any)
Set stores a value in the context
type TestObserver ¶
type TestObserver struct {
Transitions []TransitionEvent
StateEnters []StateEvent
StateExits []StateEvent
EventRejects []EventRejectEvent
Errors []ErrorEvent
Actions []ActionEvent
Started []ContextEvent
Stopped []ContextEvent
Guards []GuardEvent
// contains filtered or unexported fields
}
TestObserver is a mock observer for testing that captures all observer events
func NewTestObserver ¶
func NewTestObserver() *TestObserver
NewTestObserver creates a new test observer
func (*TestObserver) LastStateEnter ¶
func (o *TestObserver) LastStateEnter() *StateEvent
func (*TestObserver) LastTransition ¶
func (o *TestObserver) LastTransition() *TransitionEvent
func (*TestObserver) OnActionExecution ¶
func (o *TestObserver) OnActionExecution(actionType string, state string, event Event, ctx Context)
func (*TestObserver) OnError ¶
func (o *TestObserver) OnError(err error, ctx Context)
func (*TestObserver) OnEventRejected ¶
func (o *TestObserver) OnEventRejected(event Event, reason string, ctx Context)
func (*TestObserver) OnGuardEvaluation ¶
func (*TestObserver) OnMachineStarted ¶
func (o *TestObserver) OnMachineStarted(ctx Context)
func (*TestObserver) OnMachineStopped ¶
func (o *TestObserver) OnMachineStopped(ctx Context)
func (*TestObserver) OnStateEnter ¶
func (o *TestObserver) OnStateEnter(state string, ctx Context)
func (*TestObserver) OnStateExit ¶
func (o *TestObserver) OnStateExit(state string, ctx Context)
ExtendedObserver interface implementations
func (*TestObserver) OnTransition ¶
func (o *TestObserver) OnTransition(from string, to string, event Event, ctx Context)
Observer interface implementations
func (*TestObserver) StateEnterCount ¶
func (o *TestObserver) StateEnterCount() int
func (*TestObserver) StateExitCount ¶
func (o *TestObserver) StateExitCount() int
func (*TestObserver) TransitionCount ¶
func (o *TestObserver) TransitionCount() int
type Transition ¶
type Transition struct {
SourceState string
TargetState string
EventName string
Guard GuardFunc
Action ActionFunc
}
Transition represents a state transition
func NewTransition ¶
func NewTransition(sourceState, targetState, eventName string) *Transition
NewTransition creates a new transition
func (*Transition) WithAction ¶
func (t *Transition) WithAction(action ActionFunc) *Transition
WithAction adds an action to the transition
func (*Transition) WithGuard ¶
func (t *Transition) WithGuard(guard GuardFunc) *Transition
WithGuard adds a guard condition to the transition
type TransitionBuilder ¶
type TransitionBuilder interface {
// Event binding
On(event string) TransitionBuilder
OnCompletion() TransitionBuilder // Completion transition (automatic when state completes)
// Conditions
When(guard GuardFunc) TransitionBuilder
Unless(guard GuardFunc) TransitionBuilder
// Actions
Do(action ActionFunc) TransitionBuilder
DoIf(condition GuardFunc, action ActionFunc) TransitionBuilder
DoAsync(action ActionFunc) TransitionBuilder
// Error handling
OnError(errorState string) TransitionBuilder
OnTimeout(timeoutState string) TransitionBuilder
// Multiple transitions from same state
To(target string) TransitionBuilder
ToSelf() TransitionBuilder
ToParent(target string) TransitionBuilder
// Navigation back
State(id string) StateBuilder
CompositeState(id string) CompositeStateBuilder
Build() MachineDefinition
}
TransitionBuilder handles transition configuration with inline actions
type TransitionError ¶
TransitionError represents transition-related errors
func NewNoTransitionError ¶
func NewNoTransitionError(from, event string) *TransitionError
NewNoTransitionError creates a new no transition found error
func NewTransitionError ¶
func NewTransitionError(code ErrorCode, from, to, event, reason string) *TransitionError
NewTransitionError creates a new transition error with custom values
func NewTransitionNotAllowedError ¶
func NewTransitionNotAllowedError(from, to, event string) *TransitionError
NewTransitionNotAllowedError creates a new transition not allowed error
func (*TransitionError) Error ¶
func (e *TransitionError) Error() string
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
document-approval
command
|
|
|
order-pipeline
command
|
|
|
smart-home
command
|
|
|
traffic-light
command
|
|