fsm

package module
v0.0.0-...-40bf467 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2018 License: MIT Imports: 2 Imported by: 8

README

fsm GoDoc Build Status

Why another FSM implementation? Because we didn't see one suited for smaller-scale, programmatic use in Go which was very efficient. Example use:

bp := fsm.New()
bp.Start(0)
bp.From(0).To(1)
bp.From(1).To(2).Then(func (m *fsm.Machine) { fmt.Println("hola!") })

m := bp.Machine()
m.State() // => a
m.Goto(1) // => error(nil)
m.State() // => b
m.Goto(2) // => error(nil)
// => hola!
m.Goto(1) // => error, "Transition 2 to 1 not permitted."

See the godocs for more information.

Benchmarks well, especially against comparable solutions.

➜  fsm git:(fsm) ✗ go test -bench=.
PASS
PASS
BenchmarkTransitions    100000000           20.8 ns/op
BenchmarkAllows          50000000           20.6 ns/op
BenchmarkGetState      2000000000           0.48 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Blueprint

type Blueprint struct {
	// contains filtered or unexported fields
}

func New

func New() *Blueprint

New creates a new finite state machine blueprint.

func (*Blueprint) Add

func (b *Blueprint) Add(t *Transition)

Add adds a complete transition to the blueprint.

func (*Blueprint) From

func (b *Blueprint) From(start uint8) *Transition

From returns a new transition for the blueprint. The transition will be added to the blueprint automatically when it has both "from" and "to" values.

func (*Blueprint) Machine

func (b *Blueprint) Machine() *Machine

Machine returns a new machine created from the blueprint.

func (*Blueprint) Start

func (b *Blueprint) Start(state uint8)

Start sets the start state for the machine.

type Handler

type Handler func(m *Machine)

Handler represents a callback to be called when the machine performs a certain transition between states.

type Machine

type Machine struct {
	// contains filtered or unexported fields
}

Machine represents a finite state machine. Instances of this struct should not be constructed by hand and instead should be created using a blueprint.

func (*Machine) Allows

func (f *Machine) Allows(b uint8) bool

Allows returns whether or not this machine can transition to the state b.

func (*Machine) Disallows

func (f *Machine) Disallows(b uint8) bool

Disallows returns whether or not this machine can't transition to the state b.

func (*Machine) Goto

func (f *Machine) Goto(state uint8) error

Goto moves the machine to the specified state. An error is returned if the transition is not valid.

func (*Machine) State

func (f *Machine) State() uint8

State returns the current state.

type Transition

type Transition struct {
	// contains filtered or unexported fields
}

Transition represents a transition between two states.

func (*Transition) From

func (t *Transition) From(from uint8) *Transition

From sets the source state of the transition.

func (*Transition) Then

func (t *Transition) Then(fn Handler) *Transition

Then sets the callback function for when the transition has occurred.

func (*Transition) To

func (t *Transition) To(to uint8) *Transition

To sets the destination state of the transition.

Jump to

Keyboard shortcuts

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