yafsm

package module
v0.0.0-...-5dcdb43 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2018 License: MIT Imports: 4 Imported by: 0

README

Yet Another Finite State Machine

A toy, DO NOT USE IN PRODUCTION.
Examples:
  • Creating a transition handler
const (
    green  yafsm.State = "green"
    yellow yafsm.State = "yellow"
    red    yafsm.State = "red"
)
handler := yafsm.CreateTransitionHandler([]yafsm.Transition{
    yafsm.NewTransition(yafsm.NewStates(red), green),
    yafsm.NewTransition(yafsm.NewStates(green), yellow),
    yafsm.NewTransition(yafsm.NewStates(yellow), red),
})

err := handler(green, red)
if err != nil {
    fmt.Println(err)
}

err := handler(green, yellow)
if err == nil {
    fmt.Println("Yay~")
}
  • Creating a DOT string from list of transitions
const (
    todo       yafsm.State = "todo"
    inprogress yafsm.State = "inprogress"
    verify     yafsm.State = "verify"
    done       yafsm.State = "done"
)
transitions := []yafsm.Transition{
    yafsm.NewTransition(yafsm.NewStates(todo, inprogress, verify), todo),
    yafsm.NewTransition(yafsm.NewStates(todo, inprogress, verify), inprogress),
    yafsm.NewTransition(yafsm.NewStates(inprogress, verify), verify),
    yafsm.NewTransition(yafsm.NewStates(verify), done),
}

dot := yafsm.CreateDOT(transitions)
fmt.Println(dot)

Output:

digraph  {
        todo->todo;
        inprogress->todo;
        verify->todo;
        todo->inprogress;
        inprogress->inprogress;
        verify->inprogress;
        inprogress->verify;
        verify->verify;
        verify->done;
        done;
        inprogress;
        todo;
        verify;

}

Documentation

Overview

Package yafsm is a simple library for building finite state machine.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDOT

func CreateDOT(transitions []Transition) string
Example
package main

import (
	"fmt"

	"github.com/bcen/yafsm"
)

func main() {
	const (
		todo       yafsm.State = "todo"
		inprogress yafsm.State = "inprogress"
		verify     yafsm.State = "verify"
		done       yafsm.State = "done"
	)
	transitions := []yafsm.Transition{
		yafsm.NewTransition(yafsm.NewStates(todo, inprogress, verify), todo),
		yafsm.NewTransition(yafsm.NewStates(todo, inprogress, verify), inprogress),
		yafsm.NewTransition(yafsm.NewStates(inprogress, verify), verify),
		yafsm.NewTransition(yafsm.NewStates(verify), done, yafsm.WithName("Mark Done")),
	}

	dot := yafsm.CreateDOT(transitions)
	fmt.Println(dot)
}
Output:

digraph  {
	rankdir=LR;
	todo->todo;
	inprogress->todo;
	verify->todo;
	todo->inprogress;
	inprogress->inprogress;
	verify->inprogress;
	inprogress->verify;
	verify->verify;
	verify->done[ label="Mark Done" ];
	done;
	inprogress;
	todo;
	verify;

}

func CreateTransitionHandler

func CreateTransitionHandler(trans []Transition) func(State, State, ...TransitionConfig) error

CreateTransitionHandler binds and returns an action handler for the given transitions.

Example
package main

import (
	"fmt"

	"github.com/bcen/yafsm"
)

func main() {
	const (
		green  yafsm.State = "green"
		yellow yafsm.State = "yellow"
		red    yafsm.State = "red"
	)

	cb := func(t yafsm.Transition, from, to yafsm.State) error {
		fmt.Printf("From callback: %s -> %s\n", from, to)
		return nil
	}

	handler := yafsm.CreateTransitionHandler([]yafsm.Transition{
		yafsm.NewTransition(yafsm.NewStates(red), green),
		yafsm.NewTransition(yafsm.NewStates(green), yellow, yafsm.WithCallback(cb)),
		yafsm.NewTransition(yafsm.NewStates(yellow), red),
	})

	err := handler(green, red)
	fmt.Println(err)

	err = handler(green, yellow)
	fmt.Println(err)
}
Output:

"Green" -> "Red" is not a valid transition
From callback: Green -> Yellow
<nil>

func CreateTransitionsFromDOT

func CreateTransitionsFromDOT(dot string) (States, []Transition, error)
Example
package main

import (
	"fmt"

	"github.com/bcen/yafsm"
)

func main() {
	dot := `
	digraph {
	    green -> yellow [label = "an optional label"];
	    yellow -> red;
	    red -> green;
	}
	`
	states, trans, _ := yafsm.CreateTransitionsFromDOT(dot)
	handler := yafsm.CreateTransitionHandler(trans)

	fmt.Println("States:")
	for _, s := range states.AsSortedStrings() {
		fmt.Println(s)
	}

	fmt.Println("Transitions:")

	if err := handler("green", "red"); err != nil {
		fmt.Println(err)
	}

	if err := handler("green", "yellow"); err == nil {
		fmt.Println(`"Green" -> "Yellow": OK`)
	}
}
Output:

States:
green
red
yellow
Transitions:
"Green" -> "Red" is not a valid transition
"Green" -> "Yellow": OK

Types

type Callback

type Callback func(t Transition, from State, to State) error

type State

type State string

func (State) String

func (s State) String() string

type States

type States []State

func NewStates

func NewStates(states ...State) States

NewStates creates a list of State from input.

func (States) AsSortedStrings

func (states States) AsSortedStrings() []string

func (States) Has

func (states States) Has(s State) bool

Has checks existence of a given State.

type Transition

type Transition interface {
	Name() string
	From() States
	To() State
	TransitionFrom(State, ...TransitionConfig) error
	GetCallback() Callback
}

func NewTransition

func NewTransition(from States, to State, options ...TransitionConfig) Transition

NewTransition creates a new transition.

type TransitionConfig

type TransitionConfig func(*config)

func WithCallback

func WithCallback(cb Callback) TransitionConfig

WithCallback sets a callback for a given transition.

func WithName

func WithName(name string) TransitionConfig

WithName sets an optional name for the transition.

Jump to

Keyboard shortcuts

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