hsm

package module
v0.0.0-...-69341d4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2020 License: MIT Imports: 3 Imported by: 12

README

gohsm

Introduction

HSM provides the framework for Hierarchical State Machine implementations.

Framework Overview

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

  • 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

How To Use

  1. Define the set of events that will be processed.
  2. Define all of the states that are possible. For each state, implement the methods required by the State interface
  3. Create the state machine, and hand it the starting state
  4. Call Run on the state machine

Example Usage

Two sample HSMs have been implemented in the example/ directory.

Documentation

Overview

Package hsm provides the framework for Hierarchical State Machine implementations.

Related Documents:

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

Constants

This section is empty.

Variables

View Source
var NopAction = func(logger *zap.Logger) {}

NopAction is a no-op action

View Source
var StartEvent = &BaseEvent{"Start"}

StartEvent is an event used to start a state machine evaluation.

Functions

func Assertion

func Assertion(logger *zap.Logger, expression bool, message string)

Assertion causes an runtime panic if expression is not true

func Precondition

func Precondition(logger *zap.Logger, expression bool, message string)

Precondition causes a panic if expression is not true

Types

type Action

type Action func(logger *zap.Logger)

Action is a type representing a function to be executed on a transition.

type BaseEvent

type BaseEvent struct {
	Name string
}

BaseEvent is a no-op event.

func (*BaseEvent) ID

func (be *BaseEvent) ID() string

ID returns the event ID.

type BaseState

type BaseState struct {
	// contains filtered or unexported fields
}

BaseState implements the InternalState interface

func NewBaseState

func NewBaseState(name string, parentState State, logger *zap.Logger) *BaseState

NewBaseState constructor

func (*BaseState) Entered

func (b *BaseState) Entered() bool

Entered getter

func (*BaseState) Exited

func (b *BaseState) Exited() bool

Exited getter

func (*BaseState) Logger

func (b *BaseState) Logger() *zap.Logger

Logger getter

func (*BaseState) Name

func (b *BaseState) Name() string

Name getter

func (*BaseState) ParentState

func (b *BaseState) ParentState() State

ParentState getter

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

func (*EndTransition) Execute

func (t *EndTransition) Execute(logger *zap.Logger, fromState State) State

Execute calls OnExit() for the fromState and all parent states up to the nil parent state Action() is then called and nil is returned for the new current state

type Event

type Event interface {
	ID() string
}

Event is an interface to an event.

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

func (*InternalTransition) Execute

func (t *InternalTransition) Execute(logger *zap.Logger, fromState State) State

Execute calls the transition's action and returns the same fromState that started the transition

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

func (*StateMachine) Run

func (sm *StateMachine) Run(ctx context.Context, events <-chan Event)

Run starts the StateMachine and processes incoming events until the StateMachine terminates (new currentState is nil after processing a transition) or the "done" event is received

type Transition

type Transition interface {
	Execute(logger *zap.Logger, fromState State) State
}

Transition defines the interface that is implemented by the different types of transitions

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL