All Projects → calebwin → go-sm

calebwin / go-sm

Licence: MIT license
A finite-state machine library for the Go programming language

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-sm

Finity
A finite state machine library for Node.js and the browser with a friendly configuration DSL.
Stars: ✭ 88 (+528.57%)
Mutual labels:  state-machine, finite-state-machine
flviz
FLVIz - Finite Automata Simulator written in QT/Graphviz
Stars: ✭ 36 (+157.14%)
Mutual labels:  state-machine, finite-state-machine
Hsm
Finite state machine library based on the boost hana meta programming library. It follows the principles of the boost msm and boost sml libraries, but tries to reduce own complex meta programming code to a minimum.
Stars: ✭ 106 (+657.14%)
Mutual labels:  state-machine, finite-state-machine
Hal
🔴 A non-deterministic finite-state machine for Android & JVM that won't let you down
Stars: ✭ 63 (+350%)
Mutual labels:  state-machine, finite-state-machine
StateBuilder
State machine code generator for C++ and Java.
Stars: ✭ 30 (+114.29%)
Mutual labels:  state-machine, finite-state-machine
Xstateful
A wrapper for xstate that stores state, handles transitions, emits events for state changes and actions/activities, and includes an optional reducer framework for updating state and invoking side-effects
Stars: ✭ 81 (+478.57%)
Mutual labels:  state-machine, finite-state-machine
Django Fsm
Django friendly finite state machine support
Stars: ✭ 1,898 (+13457.14%)
Mutual labels:  state-machine, finite-state-machine
SimpleStateMachineLibrary
📚 A simple library for realization state machines in C# code
Stars: ✭ 30 (+114.29%)
Mutual labels:  state-machine, finite-state-machine
Easy States
The simple, stupid state machine for Java
Stars: ✭ 167 (+1092.86%)
Mutual labels:  state-machine, finite-state-machine
Nanostate
🚦- Small Finite State Machines
Stars: ✭ 151 (+978.57%)
Mutual labels:  state-machine, finite-state-machine
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (+3085.71%)
Mutual labels:  state-machine, finite-state-machine
Awesome Fsm
🤖 A curated list of awesome resources related to finite state machines and statecharts.
Stars: ✭ 189 (+1250%)
Mutual labels:  state-machine, finite-state-machine
Statecharts.github.io
There is no state but what we make. Feel free to pitch in.
Stars: ✭ 265 (+1792.86%)
Mutual labels:  state-machine, finite-state-machine
Jstate
Advanced state machines in Java.
Stars: ✭ 84 (+500%)
Mutual labels:  state-machine, finite-state-machine
use-tiny-state-machine
A tiny (~700 bytes) react hook to help you write finite state machines
Stars: ✭ 37 (+164.29%)
Mutual labels:  state-machine, finite-state-machine
Afsm
C++14 Finite State Machine library
Stars: ✭ 113 (+707.14%)
Mutual labels:  state-machine, finite-state-machine
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (+1635.71%)
Mutual labels:  state-machine, finite-state-machine
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (+235.71%)
Mutual labels:  state-machine, finite-state-machine
Sm
🚀 SM – a static State Machine library
Stars: ✭ 149 (+964.29%)
Mutual labels:  state-machine, finite-state-machine
Python Statemachine
Python Finite State Machines made easy.
Stars: ✭ 184 (+1214.29%)
Mutual labels:  state-machine, finite-state-machine

What it is

go-sm is a library for generating persistent finite-state machines in the Go programming language. go-sm currently supports lifecycle callbacks, state history storage, generated .dot graph visualizations.

How to use it

A basic finite-state machine with two states and two transitions can be created as follows with a simple 3-step process.

import "github.com/calebwin/go-sm/fsm"

myFSM := fsm.Generate("locked") // 1) generate a new finite-state machine with an inital state of "locked"

myTransitions := []fsm.Transition{
  fsm.Transition{
  	"coin", 
	[]fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, 
	fsm.State{"un-locked"}
  },
  fsm.Transition{
  	"push", 
	[]fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, 
	fsm.State{"locked"}
  },
}
myFSM = fsm.SetTransitions(myFSM, myTransitions) // 2) define possible transitions within the finite-state machine

myFSM = fsm.Execute(myFSM, "coin") // 3) execute the transition named "coin"

myFSM.state // "un-locked"

Callback Functions

Callback functions can be defined for the following 4 lifecycle events.

  • onBeforeTransition
  • onAfterTransition
  • onEnterState
  • onLeaveState They can be defined with go-sm as follows.
myFSM := fsm.Generate("locked")

myTransitions := []fsm.Transition{
  fsm.Transition{"coin", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"un-locked"}},
  fsm.Transition{"push", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"locked"}},
}
myFSM = fsm.SetTransitions(myFSM, myTransitions) 

coins := 0
myFSM = fsm.SetCallbacks(myFSM,
  func(transition string) {}, // onBeforeTransition
  func(transition string) { // onAfterTransition
    if transition == "coin" {
      money += 1
    }
  },
  func(state string) {}, // onEnterState
  func(state string) {}, // onLeaveState
)

myFSM = fsm.Execute(myFSM, "coin") 

State History

State history can be maintained with go-sm as follows.

myFSM := fsm.Generate("locked", true) // first flag set to true to indicate history should be maintained

myTransitions := []fsm.Transition{
  fsm.Transition{"coin", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"un-locked"}},
  fsm.Transition{"push", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"locked"}},
}
myFSM = fsm.SetTransitions(myFSM, myTransitions) 

myFSM = fsm.Execute(myFSM, "coin") 

myFSM.history // {"locked", "un-locked",}

State history can be used to undo/redo state transitions.

myFSM := fsm.Generate("locked", true) // first flag set to true to indicate history should be maintained

myTransitions := []fsm.Transition{
  fsm.Transition{"coin", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"un-locked"}},
  fsm.Transition{"push", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"locked"}},
}
myFSM = fsm.SetTransitions(myFSM, myTransitions) 

myFSM = fsm.Execute(myFSM, "coin")
myFSM = fsm.Execute(myFSM, "coin")

myFSM = fsm.HistoryBack(myFSM, 2) // undo 2 state transitions

myFSM.state // "locked"

State history can also be cleared.

myFSM := fsm.Generate("locked", true) // first flag set to true to indicate history should be maintained

myTransitions := []fsm.Transition{
  fsm.Transition{"coin", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"un-locked"}},
  fsm.Transition{"push", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"locked"}},
}
myFSM = fsm.SetTransitions(myFSM, myTransitions) 

myFSM = fsm.Execute(myFSM, "coin")
myFSM = fsm.Execute(myFSM, "coin")

myFSM = fsm.ClearHistory(myFSM)

myFSM.history // {"un-locked",}

Visualizations

Visualizations of finite-state machines can be generated as .dot files as follows.

myFSM := fsm.Generate("locked", true) // first flag set to true to indicate history should be maintained

myTransitions := []fsm.Transition{
  fsm.Transition{"coin", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"un-locked"}},
  fsm.Transition{"push", []fsm.State{fsm.State{"locked"}, fsm.State{"un-locked"},}, fsm.State{"locked"}},
}
myFSM = fsm.SetTransitions(myFSM, myTransitions) 

fsm.GenerateVisualization(myFSM, "myVisualization.dot")

The above call to GenerateVisualization will result in the following file created.

digraph {
	locked;
	un-locked;

	"locked" -> "un-locked" [ label=" coin " ];
	"un-locked" -> "un-locked" [ label=" coin " ];
	"locked" -> "locked" [ label=" push " ];
	"un-locked" -> "locked" [ label=" push " ];
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].