Skip to content

dipeshdulal/statemachine

Repository files navigation

Simple Finite State Machines in GO

In computation theory, the Finite State Machine (FSM) are those machines that can have finite number of states at a given time. These machines are used in our daily life such as; vending machines, elevators, traffic lights etc. Not only in hardware these machines are used in software also...

More Description in this Medium article

Learn more about finite state machines here

Inspired from @davidkpiano X-State library. (minimal finite state machine library in javascript) @xstate/fsm

basic-toggle Figure from xstate.js.org/viz/

Basic State Machine
machine := statemachine.Machine{
    ID:      "machine-1",
    Initial: "on",
    States: statemachine.StateMap{
        "on": statemachine.MachineState{
            On: statemachine.TransitionMap{
                "TOGGLE": statemachine.MachineTransition{
                    To: "off",
                },
            },
        },
        "off": statemachine.MachineState{
            On: statemachine.TransitionMap{
                "TOGGLE": statemachine.MachineTransition{
                    To: "on",
                },
            },
        },
    },
}
output := machine.Transition("TOGGLE")
// output will contain state of the machine
// after sending TOGGLE event
Parallel Machine

You can also build a parallel state machine, that bundles up multiple state machines together.

parallel := statemachine.ParallelMachine{
    Machines: statemachine.Machines{
        "machine-1": &machineOne,
        "machine-2": &machineTwo,
    },
    Subscribers: statemachine.ParallelSubscribers{
        // some state subscribers
    },
}

// Send machine-1 TOGGLE state  
next, err := parallel.Transition("machine-1.TOGGLE")
Checklist
  • Implement basic finite state machine with transition
  • Add condition cond function calling to get the output.
  • Add multiple actions and call the function on state change.
  • Add state change listeners.
  • Support for parallel state machines.
  • Implement Parallel State machines state listeners.

About

Simple Finite State Machine in go. Inspired from https://xstate.js.org

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages