robot

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const PreStatusKeepSize = 20

PreStatusKeepSize Size of history status.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultDelegate

type DefaultDelegate struct {
	P EventProcessor
}

DefaultDelegate is a default delegate.

func (*DefaultDelegate) HandleEvent

func (dd *DefaultDelegate) HandleEvent(action string, fromState string, toState string) error

HandleEvent implements Delegate interface and split HandleEvent into three actions

type Delegate

type Delegate interface {
	HandleEvent(action string, fromState string, toState string) error
}

Delegate is used to process actions

type Error

type Error interface {
	error
	BadEvent() string
	CurrentState() string
}

Error is an error when processing event and state changing

type EventProcessor

type EventProcessor interface {
	// OnExit Action handles exiting a state
	OnExit(fromState string)
	// Action is used to handle transitions
	Action(action string, fromState string, toState string) error
	// OnActionFailure failed to execute the Action
	OnActionFailure(action string, fromState string, toState string, err error)
	// OnExit Action handles entering a state
	OnEnter(toState string)
}

EventProcessor defines OnExit, Action and OnEnter actions

type StateMachine

type StateMachine struct {
	sync.Mutex

	State    StateType   `json:"state"`
	PreState []StateType `json:"preState"`

	Transitions []Transition `json:"transitions"`
	// contains filtered or unexported fields
}

StateMachine is a FSM that can handle transitions

func NewStateMachine

func NewStateMachine(defaultState string, delegate Delegate, transitions ...Transition) *StateMachine

NewStateMachine creates a new state machine.

func (*StateMachine) Export

func (m *StateMachine) Export(outfile string) error

Export exports the state diagram into a file

func (*StateMachine) ExportWithDetails

func (m *StateMachine) ExportWithDetails(outfile string, format string, layout string, scale string, more string) error

ExportWithDetails exports the state diagram with more graphviz options

func (*StateMachine) GetState

func (m *StateMachine) GetState() string

GetState exports the state

func (*StateMachine) LastingTime

func (m *StateMachine) LastingTime() time.Duration

LastingTime return current status lasting time.

func (*StateMachine) Reset

func (m *StateMachine) Reset()

Reset reset SM to default state.

func (*StateMachine) Trigger

func (m *StateMachine) Trigger(currentState string, event string) error

Trigger fires a event

type StateType

type StateType struct {
	State   string    `json:"state"`
	StartAt time.Time `json:"startAt"`
	EndAt   time.Time `json:"endAt"`
}

StateType is a type describe state info

type Transition

type Transition struct {
	From   string `json:"from"`
	Event  string `json:"event"`
	To     string `json:"to"`
	Action string `json:"action"`
}

Transition is a state transition

Jump to

Keyboard shortcuts

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