fsm

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2021 License: Apache-2.0 Imports: 4 Imported by: 33

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExportDot

func ExportDot(fsm *FSM, outfile string) error

ExportDot export fsm in dot format to outfile, which can be visualize by graphviz

Types

type ArgsType

type ArgsType map[string]interface{}

type Callback

type Callback func(*State, EventType, ArgsType)

type Callbacks

type Callbacks map[StateType]Callback

type EventType

type EventType string
const (
	EntryEvent EventType = "Entry event"
	ExitEvent  EventType = "Exit event"
)

Entry/Exit event are defined by fsm package

type FSM

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

func NewFSM

func NewFSM(transitions Transitions, callbacks Callbacks) (*FSM, error)

NewFSM create a new FSM object then registers transitions and callbacks to it

func (*FSM) SendEvent

func (fsm *FSM) SendEvent(state *State, event EventType, args ArgsType) error

SendEvent triggers a callback with an event, and do transition after callback if need There are 3 types of callback:

  • on exit callback: call when fsm leave one state, with ExitEvent event
  • event callback: call when user trigger a user-defined event
  • on entry callback: call when fsm enter one state, with EntryEvent event

type State

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

State is a thread-safe structure that represents the state of a object and it can be used for FSM

func NewState

func NewState(initState StateType) *State

NewState create a State object with current state set to initState

func (*State) Current

func (state *State) Current() StateType

Current get the current state

func (*State) Is

func (state *State) Is(target StateType) bool

Is return true if the current state is equal to target

func (*State) Set

func (state *State) Set(next StateType)

Set current state to next

type StateType

type StateType string

type Transition

type Transition struct {
	Event EventType
	From  StateType
	To    StateType
}

Transition defines a transition that a Event is triggered at From state, and transfer to To state after the Event

type Transitions

type Transitions []Transition

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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