fsm

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InvalidTransition = errors.New("invalid transition")
)

Functions

func WithRules

func WithRules(r Ruleset) func(*Machine)

WithRules is intended to be passed to New to set the Rules

func WithSubject

func WithSubject(s Stater) func(*Machine)

WithSubject is intended to be passed to New to set the Subject

Types

type Guard

type Guard func(subject Stater, goal State) bool

Guard provides protection against transitioning to the goal State. Returning true/false indicates if the transition is permitted or not.

type Machine

type Machine struct {
	Rules   *Ruleset
	Subject Stater
}

Machine is a pairing of Rules and a Subject. The subject or rules may be changed at any time within the machine's lifecycle.

func New

func New(opts ...func(*Machine)) Machine

New initializes a machine

func (Machine) Transition

func (m Machine) Transition(goal State) error

Transition attempts to move the Subject to the Goal state.

type Ruleset

type Ruleset map[Transition][]Guard

Ruleset stores the rules for the state machine.

func CreateRuleset

func CreateRuleset(transitions ...Transition) Ruleset

CreateRuleset will establish a ruleset with the provided transitions. This eases initialization when storing within another structure.

func (Ruleset) AddRule

func (r Ruleset) AddRule(t Transition, guards ...Guard)

AddRule adds Guards for the given Transition

func (Ruleset) AddTransition

func (r Ruleset) AddTransition(t Transition)

AddTransition adds a transition with a default rule

func (Ruleset) Permitted

func (r Ruleset) Permitted(subject Stater, goal State) bool

Permitted determines if a transition is allowed. This occurs in parallel. NOTE: Guards are not halted if they are short-circuited for some transition. They may continue running *after* the outcome is determined.

type State

type State string

type Stater

type Stater interface {
	CurrentState() State
	SetState(State)
}

Stater can be passed into the FSM. The Stater is reponsible for setting its own default state. Behavior of a Stater without a State is undefined.

type T

type T struct {
	O, E State
}

T implements the Transition interface; it provides a default implementation of a Transition.

func (T) Exit

func (t T) Exit() State

func (T) Origin

func (t T) Origin() State

type Transition

type Transition interface {
	Origin() State
	Exit() State
}

Transition is the change between States

Jump to

Keyboard shortcuts

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