Documentation ¶
Overview ¶
Package hsm provides the framework for Hierarchical State Machine implementations.
Related Documents:
- Introduction to Hierarchical State Machines: https://barrgroup.com/Embedded-Systems/How-To/Introduction-Hierarchical-State-Machines
- Yet Another Hierarchical State Machine: https://accu.org/index.php/journals/252
- State Diagram: https://en.wikipedia.org/wiki/State_diagram
- gohsm Object Model: go_state_machine_framework.png
Included in this framework are the following components:
StateMachine: Machine that controls the event processing
State: Interface that must be implemented by all States in the StateMachine Composed of an InternalState interface that is implemented by BaseState and an ExternalState interface whose methods must be implemented by each state in the state machine
Transition: Interface that is implemented by each of the different types of transitions:
ExternalTransition: Transition from current state to a different state. On execution the following takes place: 1. OnExit is called on the current state and all parent states up to the parent state that owns the new state (or the parent state is nil) 2. action() associated with the the transition is called 3. OnEnter() is called on the new state which may call OnEnter() for a sub-state. The final new current state is returned by the OnEnter() call
InternalTransition: Transition within the current state. On execution the following takes place: 1. action() associated with the the transition is called
EndTransition: Transition from current state that terminates the state machine. On execution the following takes place: 1. OnExit is called on the current state and all parent states until there are no more parent states 2. action() associated with the the transition is called
Event: An event represents something that has happened (login, logout, newCall, networkChange, etc.) that might drive a change in the state machine
Index ¶
- Variables
- func Assertion(logger *zap.Logger, expression bool, message string)
- func Precondition(logger *zap.Logger, expression bool, message string)
- type Action
- type BaseEvent
- type BaseState
- type EndTransition
- type Event
- type ExternalState
- type ExternalTransition
- type InternalState
- type InternalTransition
- type State
- type StateMachine
- type Transition
Constants ¶
This section is empty.
Variables ¶
var NopAction = func(logger *zap.Logger) {}
NopAction is a no-op action
var StartEvent = &BaseEvent{"Start"}
StartEvent is an event used to start a state machine evaluation.
Functions ¶
Types ¶
type BaseState ¶
type BaseState struct {
// contains filtered or unexported fields
}
BaseState implements the InternalState interface
func NewBaseState ¶
NewBaseState constructor
func (*BaseState) VerifyNotEntered ¶
func (b *BaseState) VerifyNotEntered()
VerifyNotEntered makes sure that this state is only entered once
func (*BaseState) VerifyNotExited ¶
func (b *BaseState) VerifyNotExited()
VerifyNotExited make sure that this state is only exited once
type EndTransition ¶
type EndTransition struct {
// contains filtered or unexported fields
}
EndTransition is the final transition that terminates the state machine
func NewEndTransition ¶
func NewEndTransition(event Event, action Action) *EndTransition
NewEndTransition constructor
type ExternalState ¶
type ExternalState interface { OnEnter(e Event) State OnExit(e Event) State EventHandler(e Event) Transition }
ExternalState interface used by the StateMachine Framework and must be implemented by each State
type ExternalTransition ¶
type ExternalTransition struct {
// contains filtered or unexported fields
}
ExternalTransition is a transition from one state to a different state
func NewExternalTransition ¶
func NewExternalTransition(event Event, toState State, action Action) *ExternalTransition
NewExternalTransition constructor
func (*ExternalTransition) Execute ¶
func (t *ExternalTransition) Execute(logger *zap.Logger, fromState State) State
Execute calls OnExit() for the fromState and all parent states up to the nil parent state or the parentState that is shared by the new toState. Action() is then called and OnEnter() is finally called on the new toState.
type InternalState ¶
type InternalState interface { Name() string ParentState() State VerifyNotEntered() VerifyNotExited() Entered() bool Exited() bool Logger() *zap.Logger }
InternalState interface used by the StateMachine Framework
type InternalTransition ¶
type InternalTransition struct {
// contains filtered or unexported fields
}
InternalTransition is a transition within a state where only the action() is performed. OnEnter() and OnExit() are not called.
func NewInternalTransition ¶
func NewInternalTransition(event Event, action Action) *InternalTransition
NewInternalTransition constructor
type State ¶
type State interface { InternalState ExternalState }
State interface that must be implemented by all states in a StateMachine States can use the BaseState for the InternalState implementation
type StateMachine ¶
type StateMachine struct {
// contains filtered or unexported fields
}
StateMachine manages event processing as implemented by each State
func NewStateMachine ¶
func NewStateMachine(logger *zap.Logger, startState State, startEvent Event) *StateMachine
NewStateMachine constructor
func (*StateMachine) CurrentState ¶
func (sm *StateMachine) CurrentState() State
CurrentState getter
func (*StateMachine) HandleEvent ¶
func (sm *StateMachine) HandleEvent(e Event) bool
HandleEvent executes the event handler for the current state or parent state if found. If no event handler is found then the event is dropped