go-fsm: github.com/iotexproject/go-fsm Index | Files

package fsm

import "github.com/iotexproject/go-fsm"

Index

Package Files

fsm.go

Variables

var (
    // ErrBuild represents the error of building an FSM.
    ErrBuild = errors.New("error when building an FSM")
    // ErrTransitionNotFound indicates that there doesn't exist a transition defined on the source state and the event.
    ErrTransitionNotFound = errors.New("unable to find a valid transition")
    // ErrInvalidTransition indicates that the actual destination state after transition is not defined.
    ErrInvalidTransition = errors.New("invalid transition")
)

type Builder Uses

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

Builder is an FSM builder to help construct an FSM.

func NewBuilder Uses

func NewBuilder() *Builder

NewBuilder creates an FSM builder instance with empty setup.

func (*Builder) AddInitialState Uses

func (b *Builder) AddInitialState(s State) *Builder

AddInitialState adds an initial state

func (*Builder) AddStates Uses

func (b *Builder) AddStates(states ...State) *Builder

AddStates adds the non-initial state(s)

func (*Builder) AddTransition Uses

func (b *Builder) AddTransition(src State, et EventType, trans Transition, dsts []State) *Builder

AddTransition adds a transition setup, including the source state, the event to trigger the transition, the transition callback, and the legal destination transitions.

func (*Builder) Build Uses

func (b *Builder) Build() (FSM, error)

Build builds an FSM instance based on the configured states and transition setup.

type Event Uses

type Event interface {
    // Type returns the event type
    Type() EventType
}

Event is the interface of the events that could be handled by an FSM

type EventType Uses

type EventType string

EventType represents a event type and it is a string.

type FSM Uses

type FSM interface {
    // CurrentState returns the current state.
    CurrentState() State
    // Handle handles an event and return error if there is any.
    Handle(Event) error
}

FSM is the interface of an FSM (finite state machine). It allows to define the transition logic and destinations after the transition, and intake the event to trigger the transition. The event handling is synchronized, so that it guarantee always processing one event at any time. An FSM must have exactly one initial state.

type State Uses

type State string

State represents the state and it is a string.

type Transition Uses

type Transition func(Event) (State, error)

Transition is the interface of the transition that would happen on a certain state when receiving a certain event. The transition returns a destination state where an FSM should transit into or an error.

Package fsm imports 2 packages (graph) and is imported by 3 packages. Updated 2019-01-30. Refresh now. Tools for package owners. This is a dead-end fork (no commits since the fork).