fsm

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 6 Imported by: 0

README

fsm

Table of contents

Summary

This package contains a simple Finite State Machine package.

How it works

  • sets the initial workflow
  • provides a current state
  • provides the next allowed states
  • sets a new state

Architecture

DSL

This package represents the workflow containing states and edges which also can be defined in a json file format.

Generator

This package contains a generator capable of converting input DSL into a PlantUML file, which with additional tool can be converted to an SVG file.

Example

The following example has been generated from the dsl example located in here.

The result can be converted to an SVG diagram presented below.

Dependencies

Documentation

Overview

Package fsm contains finite state machine source code.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPkg a Broker package error.
	ErrPkg = errors.New("fsm")
	// ErrUnmarshal error.
	ErrUnmarshal = fmt.Errorf("%w: unmarshal", ErrPkg)
)

Functions

func NewWorkflow

func NewWorkflow(input []byte) (*dsl.Workflow, error)

NewWorkflow a workflow factory.

Types

type EdgesGetter

type EdgesGetter interface {
	GetEdges() []*dsl.Edge
}

EdgesGetter returns all edges.

type FSM

FSM represents a finite state machine abstraction. It provides methods to set and get the current state, as well as query valid next states and transitions.

type FinalStateGetter

type FinalStateGetter interface {
	GetFinalStates() []*dsl.State
}

FinalStateGetter returns all the final states.

type FiniteStateMachine

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

FiniteStateMachine is a thread-safe finite state machine.

func New

func New(workflow *dsl.Workflow) *FiniteStateMachine

New is a factory function.

func (*FiniteStateMachine) GetEdges

func (d *FiniteStateMachine) GetEdges() []*dsl.Edge

GetEdges returns all edges.

func (*FiniteStateMachine) GetFinalStates

func (d *FiniteStateMachine) GetFinalStates() []*dsl.State

GetFinalStates returns all final states of the finite state machine. A state is final if no edges originate from it.

func (*FiniteStateMachine) GetInitialState

func (d *FiniteStateMachine) GetInitialState() *dsl.State

GetInitialState finds initial state.

func (*FiniteStateMachine) GetNextEdges

func (d *FiniteStateMachine) GetNextEdges() []*dsl.Edge

GetNextEdges returns all edges that can be taken from the current state.

func (*FiniteStateMachine) GetNextStates

func (d *FiniteStateMachine) GetNextStates() []*dsl.State

GetNextStates returns the list of next valid states from the current state.

func (*FiniteStateMachine) GetPreviousStates

func (d *FiniteStateMachine) GetPreviousStates() []*dsl.State

GetPreviousStates returns the list of valid previous states that can transition into the current state.

func (*FiniteStateMachine) GetState

func (d *FiniteStateMachine) GetState() *dsl.State

GetState returns the current state.

func (*FiniteStateMachine) GetStates

func (d *FiniteStateMachine) GetStates() []*dsl.State

GetStates returns all the states.

func (*FiniteStateMachine) SetState

func (d *FiniteStateMachine) SetState(name string) error

SetState sets the current state by name.

type InitialStateGetter

type InitialStateGetter interface {
	GetInitialState() *dsl.State
}

InitialStateGetter returns the initial state.

type NextEdgesGetter

type NextEdgesGetter interface {
	GetNextEdges() []*dsl.Edge
}

NextEdgesGetter defines the ability to retrieve all valid outgoing edges from the current state, representing possible transitions in the FSM.

type NextStatesGetter

type NextStatesGetter interface {
	GetNextStates() []*dsl.State
}

NextStatesGetter defines the ability to retrieve all valid next states that can be transitioned to from the current state.

type PreviousStatesGetter

type PreviousStatesGetter interface {
	GetPreviousStates() []*dsl.State
}

PreviousStatesGetter returns all previous states.

type StateGetter

type StateGetter interface {
	GetState() *dsl.State
}

StateGetter defines the ability to retrieve the current active state of the FSM. Returns a pointer to the current state, or nil if no state has been set.

type StateSetter

type StateSetter interface {
	SetState(name string) error
}

StateSetter defines the ability to transition the FSM to a new state by name. Returns an error if the provided state name is invalid or not part of the FSM definition.

type StatesGetter

type StatesGetter interface {
	GetStates() []*dsl.State
}

StatesGetter returns all states.

Directories

Path Synopsis
Package generator contains finite state machine diagram generator source code.
Package generator contains finite state machine diagram generator source code.

Jump to

Keyboard shortcuts

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