Code Monkey home page Code Monkey logo

go-fsm's Introduction

gobus

Finite State Machine for Go

Build Status Go Report Card Software License Build Status

Package go-fsm is a zero-alloc, high performance, pure go Finite State Machine implementation.

Install

go get github.com/zerjioang/go-fsm

TL;DR

machine := fsm.New()

machine.AddState("start", fsm.NoStateEvents)
machine.AddState("a", fsm.NoStateEvents)
machine.AddState("b", fsm.NoStateEvents)
machine.AddState("c", fsm.NoStateEvents)
machine.AddState("finish", fsm.NoStateEvents)

machine.AddTransaction("toA", "start", "a")
machine.AddTransaction("toB", "a", "b")
machine.AddTransaction("toC", "a", "c")
machine.AddTransaction("backToA", "c", "a")
machine.AddTransaction("moveToC", "b", "c")
machine.AddTransaction("toFinish", "b", "finish")
machine.AddTransaction("toFinish", "c", "finish")

machine.SetInitialState("start")
machine.SetFinalState("finish")

machine.ChangeStateTo("a")

dot := machine.DotGraph()
fmt.Println(dot)

Graphviz dot view

This library also allows developers to create raw .dot files as following, in an automatic way:

digraph fsm {
    size ="4,4";
    node [shape=circle,fontsize=12,fixedsize=true,width=0.8];
    edge [fontsize=6];
    rankdir=LR;
    "b" -> "c" [ label = "moveToC" ];
    "b" -> "finish" [ label = "toFinish" ];
    "c" -> "finish" [ label = "toFinish" ];
    "start" -> "a" [ label = "toA" ];
    "a" -> "b" [ label = "toB" ];
    "a" -> "c" [ label = "toC" ];
    "c" -> "a" [ label = "backToA" ];

    "start" [shape="doublecircle" color="blue"];
    "a" [shape="circle" color="black"];
    "b" [shape="circle" color="black"];
    "c" [shape="circle" color="black"];
    "finish" [shape="doublecircle" color="red"];
}

Benchmarking

Always do benchmarking with your own data. Here are mine:

For single thread version

BenchmarkFsm/instantiation-4         	30000000	        47.7 ns/op	  20.95 MB/s	       0 B/op	       0 allocs/op
BenchmarkFsm/instantiation-ptr-4     	10000000	       139 ns/op	   7.15 MB/s	      96 B/op	       2 allocs/op
BenchmarkFsm/add-state-4             	50000000	        24.2 ns/op	  41.36 MB/s	       0 B/op	       0 allocs/op
BenchmarkFsm/add-state-no-name-4     	100000000	        20.2 ns/op	  49.41 MB/s	       0 B/op	       0 allocs/op
BenchmarkFsm/add-state-no-event-4    	100000000	        23.7 ns/op	  42.12 MB/s	       0 B/op	       0 allocs/op
BenchmarkFsm/change-state-empty-4    	100000000	        22.2 ns/op	  45.00 MB/s	       0 B/op	       0 allocs/op
BenchmarkFsm/change-state-4          	30000000	        56.4 ns/op	  17.72 MB/s	       0 B/op	       0 allocs/op
BenchmarkFsm/has-state-false-4       	300000000	         4.37 ns/op	 228.80 MB/s	       0 B/op	       0 allocs/op
BenchmarkFsm/has-state-true-4        	200000000	         7.58 ns/op	 131.93 MB/s	       0 B/op	       0 allocs/op
BenchmarkFsm/to-dot-4                	  300000	      5310 ns/op	   0.19 MB/s	    2210 B/op	      29 allocs/op
PASS

License

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Uses GPL license described below

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

go-fsm's People

Contributors

zerjioang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-fsm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.