Skip to content

rianby64/kry

Repository files navigation

kry

A simple Go project for finite state machines (FSM). I got inspiration by my wife Kry. I really love her!

Thank you for using my code. Kry will be very happy if you like it.

Overview

kry is a Go-based library for building and running finite state machines (FSM) in your applications. It provides a simple API to define states, transitions, and handle events.

Getting Started

  1. Install the package:
go get github.com/rianby64/kry
  1. Import and use in your Go code:
package main

import (
	"context"
	"fmt"

	"github.com/rianby64/kry"
)

type CustomParam struct {
	Value string
}

func main() {
	const (
		initial int = iota
		close
		open
	)

	ctx := context.TODO()

	fsk, err := kry.New(initial, []kry.Transition[string, int, CustomParam]{
		{
			Name: "open",
			Src:  []int{initial, close},
			Dst:  open,
			Enter: func(ctx context.Context, instance kry.InstanceFSM[string, int, CustomParam], param CustomParam) error {
				fmt.Println("Opened with param:", param.Value)

				return nil
			},
		},
		{
			Name: "close",
			Src:  []int{open},
			Dst:  close,
		},
	}, kry.WithFullHistory())
	if err != nil {
		panic(err)
	}

	if err := fsk.Event(ctx, "open", CustomParam{Value: "example"}); err != nil {
		panic(err)
	}

	fmt.Println("Current state:", fsk.Current())

	if err := fsk.Apply(ctx, "close", close); err != nil {
		panic(err)
	}

	fmt.Println("Current state:", fsk.Current())
}

Requirements

  • Go 1.23 or higher

We have

  • Simple API
  • Support for source transitions matching via function. (All 5xx, 4xx, etc.)
  • Support for destination transitions matching via function. (All 5xx, 4xx, etc.)
  • Visualization tools for FSMs
  • From transition - do a call to another transition, and do not allow looping
  • History of transitions with safe for concurrent use

Wish list for future improvements

  • Add more examples and documentation
  • Implement more advanced features like state beforeEnter/exit actions. (should I?)
  • Improve the history with a better flow. At this moment it's basic.

Design considerations

  1. This library does not support ForceState as it is dangerous and breaks the FSM concept. So, if you encounter such a need, please rethink your design. (Experimenting...)
  2. Keep the API simple and easy to use.
  3. It's up to you to ensure that the FSM is used in a thread-safe manner if needed, so lock it in the callbacks Enter* if you need to.

License

This project is licensed under the MIT License.

About

Simplest finite-state-krychine implementation

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages