fsm

package
v0.0.0-...-580f94b Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package fsm defines a finite state machine and has the ability to save this state machine to a graph file This graph file can be visualized using mermaid.js

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FSM

type FSM struct {
	// States is the map from state ID to states
	States States

	// Current is the current state represented by the identifier
	Current StateID

	// Name represents the descriptive name of this state machine
	Name string

	// StateCallback is the function ran when a transition occurs
	// It takes the old state, the new state and the data and returns if this is handled by the client
	StateCallback func(StateID, StateID, interface{}) bool

	// Directory represents the path where the state graph is stored
	Directory string

	// Generate represents whether we want to generate the graph
	Generate bool

	// GetStateName gets the name of a state as a string
	GetStateName func(StateID) string
	// contains filtered or unexported fields
}

FSM represents the total graph.

func (*FSM) CheckTransition

func (fsm *FSM) CheckTransition(desired StateID) error

CheckTransition returns an error whether or not a transition to state `desired` is possible

func (*FSM) GenerateGraph

func (fsm *FSM) GenerateGraph() string

GenerateGraph generates a mermaid graph if the state machine is initialized If the graph cannot be generated, it returns the empty string.

func (*FSM) GoTransition

func (fsm *FSM) GoTransition(newState StateID) (bool, error)

GoTransition is an alias to call GoTransitionWithData but have an empty string as data.

func (*FSM) GoTransitionRequired

func (fsm *FSM) GoTransitionRequired(newState StateID, data interface{}) error

GoTransitionRequired transitions the state machine to a new state with associated state data 'data' If this transition is not handled by the client, it returns an error.

func (*FSM) GoTransitionWithData

func (fsm *FSM) GoTransitionWithData(newState StateID, data interface{}) (bool, error)

GoTransitionWithData is a helper that transitions the state machine toward the 'newState' with associated state data 'data' It returns whether or not the transition is handled by the client.

func (*FSM) InState

func (fsm *FSM) InState(check StateID) bool

InState returns whether or not the state machine is in the given 'check' state.

func (*FSM) Init

func (fsm *FSM) Init(
	current StateID,
	states States,
	callback func(StateID, StateID, interface{}) bool,
	directory string,
	nameGen func(StateID) string,
	generate bool,
)

Init initializes the state machine and sets it to the given current state.

type State

type State struct {
	// Transitions indicates which out arrows this node has
	Transitions []Transition
}

State represents a single node in the graph.

type StateID

type StateID int8

StateID represents the Identifier of the state.

type StateIDSlice

type StateIDSlice []StateID

StateIDSlice represents the list of state identifiers.

func (StateIDSlice) Len

func (v StateIDSlice) Len() int

Len is defined here such that we can sort the slice

func (StateIDSlice) Less

func (v StateIDSlice) Less(i, j int) bool

Less is defined here such that we can sort the slice

func (StateIDSlice) Swap

func (v StateIDSlice) Swap(i, j int)

Swap is defined here such that we can sort the slice

type States

type States map[StateID]State

States is the map from state identifier to the state itself

type Transition

type Transition struct {
	// To represents the to-be-new state
	To StateID
	// Description is what type of message the arrow gets in the graph
	Description string
}

Transition indicates an arrow in the state graph.

Jump to

Keyboard shortcuts

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