sam

package module
v0.0.0-...-b5f1bde Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2020 License: MIT Imports: 1 Imported by: 1

README

Sam

Sam is the finite state machine engine.

You can define state diagram by adding Transitions and add transition hooks.

Usage

Add to project using go get

go get git.ooo.ua/pub/sam

Examples

  • Without hooks
package main

import (
	"fmt"
	"git.ooo.ua/pub/sam"
)

const (
	WStateDisabled    sam.State = "Disabled"
	WStateEnabled     sam.State = "Enabled"
	WStateInitialized sam.State = "Initialized"
	WStateRun         sam.State = "Run"
	WStateStopped     sam.State = "Stopped"
	WStateFailed      sam.State = "Failed"
)

// newWorkerSM returns filled state machine of worker lifecycle
//
// (*) -> [Disabled] -> [Enabled] -> [Initialized] -> [Run] <-> [Stopped]
//          ↑ ↑____________|  |          |  |  ↑         |
//          |_________________|__________|  |  |------|  ↓
//                            |-------------|-----> [Failed]
func main() {
	var err error
	workerSM := sam.NewStateMachine()
	workerSM.AddTransitions(WStateDisabled, WStateEnabled).
		AddTransitions(WStateEnabled, WStateInitialized, WStateFailed, WStateDisabled).
		AddTransitions(WStateInitialized, WStateRun, WStateFailed, WStateDisabled).
		AddTransitions(WStateRun, WStateStopped, WStateFailed).
		AddTransitions(WStateStopped, WStateRun).
		AddTransitions(WStateFailed, WStateInitialized, WStateDisabled)
	worker, err := workerSM.Finalize(WStateDisabled)
	if err != nil || worker == nil {
		fmt.Println("init failed: ", err)
	}
	clone := worker.Clone()

	err = clone.GoTo(WStateEnabled)
	if err != nil {
		fmt.Println("error: ", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook func(from, to State) error

type HookList

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

func (*HookList) Execute

func (hl *HookList) Execute(from, to State) (err error)

func (*HookList) ExecuteRollback

func (hl *HookList) ExecuteRollback(from, to State) (err error)

func (HookList) New

func (HookList) New() HookList

type State

type State string

type StateMachine

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

func NewStateMachine

func NewStateMachine() StateMachine

func (*StateMachine) AddAfterAllHook

func (sm *StateMachine) AddAfterAllHook(hook Hook) *StateMachine

func (*StateMachine) AddBeforeAllHook

func (sm *StateMachine) AddBeforeAllHook(hook Hook) *StateMachine

func (*StateMachine) AddTransition

func (sm *StateMachine) AddTransition(from, to State) *StateMachine

func (*StateMachine) AddTransitions

func (sm *StateMachine) AddTransitions(from State, to ...State) *StateMachine

func (*StateMachine) Clone

func (sm *StateMachine) Clone() StateMachine

func (*StateMachine) Error

func (sm *StateMachine) Error() error

func (*StateMachine) Finalize

func (sm *StateMachine) Finalize(state State) (*StateMachine, error)

func (*StateMachine) GoBack

func (sm *StateMachine) GoBack() error

func (*StateMachine) GoTo

func (sm *StateMachine) GoTo(toState State) error

func (StateMachine) New

func (StateMachine) New() StateMachine

func (*StateMachine) RegisterState

func (sm *StateMachine) RegisterState(state State, before, after Hook) *StateMachine

func (*StateMachine) SetAfterHook

func (sm *StateMachine) SetAfterHook(state State, hook Hook) *StateMachine

func (*StateMachine) SetBeforeHook

func (sm *StateMachine) SetBeforeHook(state State, hook Hook) *StateMachine

func (*StateMachine) SetState

func (sm *StateMachine) SetState(state State) error

func (*StateMachine) State

func (sm *StateMachine) State() State

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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