fsm

package module
v0.1.5-0...-4b2249e Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2020 License: MIT Imports: 6 Imported by: 0

README

FSM for golang

Documentation

Index

Constants

View Source
const (
	ShouldNotReEnterPanic = "the process event should not re-enter. " +
		"i.e., ProcessEvent should not be invoked in action/guard"
)

Variables

View Source
var (
	AlreadyExists = errors.New("already exists")
)

Functions

This section is empty.

Types

type ActionHookArgs

type ActionHookArgs struct {
	FromState State
	ToState   State
	Event     Event
	Payload   interface{}
}

type Event

type Event interface {
	FSMEventID() string
}

Event defines the FSM event. NOTE: the `FSMEventID` in one FSM should be unique. NOTE: the Event may carry data and can be mutable for each `ProcessEvent`.

FSM support filter event by its carrying data. See `AddTransition` for details.

type FSM

type FSM struct {
	GlobalBeforeAction delegate.Delegate
	GlobalAfterAction  delegate.Delegate
	// contains filtered or unexported fields
}

FSM is a finite state machine. NOTE: It is not thread-safe. It is caller's duty to add mutex/shared mutex when calling FSM concurrently.

func NewFSM

func NewFSM(initState State, payload interface{}) *FSM

NewFSM will create a new fsm with initialize state. The nullable `payload` will pass to each `action`/`guard` methods.

func (*FSM) AddEvent

func (fsm *FSM) AddEvent(eventID string) error

func (*FSM) AddState

func (fsm *FSM) AddState(state State) error

func (*FSM) AddTransition

func (fsm *FSM) AddTransition(from State, evId string, to State,
	action func(interface{}, Event) error, guard func(interface{}, Event) bool) error

AddTransition will append a transition to fsm.

  • The states and event should be added before.
  • The `guard` will invoke when the current state is from, and the event is triggered. The action will be invoked if the `guard` returns true, otherwise, the next transition guard for the same state/event will be invoked.
  • The fsm.ProcessEvent should not be invoked in action/guard
  • If the action returns an error, the state will be not changed and the process event will returns that error.

func (*FSM) CurrentState

func (fsm *FSM) CurrentState() State

func (*FSM) DumpGraphviz

func (fsm *FSM) DumpGraphviz() string

func (*FSM) HasEvent

func (fsm *FSM) HasEvent(evID string) bool

func (*FSM) HasState

func (fsm *FSM) HasState(state State) bool

func (*FSM) ProcessEvent

func (fsm *FSM) ProcessEvent(ev Event) error

ProcessEvent will invoke the binding transition and change the current state. See `AddTransition` for more information. It may return NoTransition when there is no binding transition for this event.

type PreemptiveFSM

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

PreemptiveFSM is a thread safe FSM. If there is a processing event, the `ProcessEvent` will be wait until the processing complete. If `ProcessEvent` is invoked more than once together, old events will be ignored and ProcessEvent will return error. i.e., the event is preemptive.

func NewPreemptiveFSM

func NewPreemptiveFSM(initState State, payload interface{}) *PreemptiveFSM

func (*PreemptiveFSM) Close

func (p *PreemptiveFSM) Close() error

func (*PreemptiveFSM) ProcessEvent

func (p *PreemptiveFSM) ProcessEvent(event Event) error

type QueuedFSM

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

func NewQueuedFSM

func NewQueuedFSM(initState State, payload interface{}) *QueuedFSM

func (*QueuedFSM) Close

func (q *QueuedFSM) Close() error

func (*QueuedFSM) ProcessEvent

func (q *QueuedFSM) ProcessEvent(ev Event) (errResult error)

type State

type State interface {
	FSMStateID() string
}

State defines the FSM state. NOTE: the `FSMStateID` in one FSM should be unique. NOTE: the State should be immutable. Maybe a global variable is good for state.

type StringEvent

type StringEvent string

func (StringEvent) FSMEventID

func (s StringEvent) FSMEventID() string

type StringState

type StringState string

func (StringState) FSMStateID

func (s StringState) FSMStateID() string

Jump to

Keyboard shortcuts

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