fsm

package
v0.0.0-...-96c78b7 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2016 License: MIT Imports: 1 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FSM

type FSM struct {
	Transitions []*Transition
	// contains filtered or unexported fields
}

FSM is a finite state machine. Transitions is a list of valid state transitions. The initial state is the empty string, and transitions must include at least one transition from this initial state. If multiple transitions exist for the same From-To pair then each of the corresponding actions will be taken in the order the appaer in the list.

func (*FSM) Current

func (f *FSM) Current() State

Current returns the current state.

func (*FSM) Goto

func (f *FSM) Goto(s State) error

Goto transitions to the new state given and calls the associated action callback if there is one. The callback takes place after the current and previous states have been updated. If the given state is not a valid transition then an error is returned. If the given state is the current state then this function does nothing.

func (*FSM) Previous

func (f *FSM) Previous() State

Previous returns the previous state. This will be initally be equal to the current state (the empty string), until the state has changed at least once.

func (*FSM) ValidTransitions

func (f *FSM) ValidTransitions(s State) []State

ValidTransitions returns a list of valid states that can be transitioned to from the given state.

type State

type State string

State is the name of a state in the finite state machine.

const (
	// InitialState is the initial state that an FSM is in.
	InitialState State = ""
)

type Transition

type Transition struct {
	From, To State
	Action   func()
}

Transition defines what action to take when transitioning between the states From and To.

Jump to

Keyboard shortcuts

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