fsm

package
v0.0.0-...-9dfb24e Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2024 License: MIT Imports: 3 Imported by: 0

README

FSM

This Go package provides a simple yet powerful implementation of a Finite State Machine (FSM). It's designed to be easy to use and integrate into Go applications where state management is needed, such as workflow engines, user interfaces, or gaming logic.

References
  • go-fsm - A Finite State Machine library for Go
  • fsm - Finite State Machine for Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event string

Event represents an event that can trigger a state transition.

type FSM

type FSM struct {
	CurrentState    State
	TransitionRules TransitionRuleSet

	// Callbacks triggered on state transitions.
	OnEnterState func(ctx context.Context, from State, to State, event Event)
	OnExitState  func(ctx context.Context, from State, to State, event Event)
	// contains filtered or unexported fields
}

FSM represents a finite state machine.

func New

func New(initialState State) *FSM

New creates a new FSM with the given initial state. It initializes the TransitionRules to prevent nil map assignments.

func (*FSM) AddTransitionRule

func (f *FSM) AddTransitionRule(from State, event Event, to State)

AddTransitionRule adds a transition rule to the FSM. It defines that when in the 'from' state, upon receiving 'event', the FSM should transition to the 'to' state.

func (*FSM) GetCurrentState

func (f *FSM) GetCurrentState() State

GetCurrentState returns the current state of the FSM. It uses a read lock to allow concurrent reads.

func (*FSM) SetOnEnterState

func (f *FSM) SetOnEnterState(callback func(ctx context.Context, from State, to State, event Event))

SetOnEnterState sets the callback function to be called when entering a new state. The callback receives a context, the previous state, the new state, and the triggering event.

func (*FSM) SetOnExitState

func (f *FSM) SetOnExitState(callback func(ctx context.Context, from State, to State, event Event))

SetOnExitState sets the callback function to be called when exiting a state. The callback receives a context, the previous state, the new state, and the triggering event.

func (*FSM) TriggerEvent

func (f *FSM) TriggerEvent(ctx context.Context, event Event) error

TriggerEvent triggers an event and attempts to transition the FSM to the next state. It accepts a context for handling cancellation, deadlines, and passing request-scoped values. It returns an error if the transition is invalid.

type State

type State string

State represents a state in the FSM.

func (State) String

func (s State) String() string

String returns the string representation of the state.

type TransitionRuleSet

type TransitionRuleSet map[State]map[Event]State

TransitionRuleSet defines the allowed state transitions. It maps a State to a map of Events and their resulting States.

Jump to

Keyboard shortcuts

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