GitHub - looplab/fsm: Finite State Machine for Go
2019-08-31 19:58:39 Author: github.com(查看原文) 阅读量:677 收藏

Join GitHub today

GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Sign up

Finite State Machine for Go

Go Makefile

Permalink

Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Examples import correction Jul 22, 2015
.gitignore Update wercker.yml to use the Docker pipeline Sep 17, 2016
.travis.yml Add Makefile, go.mod and travis.yml Jan 16, 2019
LICENSE Initial commit Aug 21, 2013
Makefile Add Makefile, go.mod and travis.yml Jan 16, 2019
README.md Add Makefile, go.mod and travis.yml Jan 16, 2019
errors.go ISSUE-15 / Fix double transitions (#22) Mar 10, 2017
errors_test.go Full unit test coverage on errors.go Jul 14, 2015
event.go Restructure code for better maintainability Sep 5, 2014
fsm.go Added an AvailableTransitions func to FSM (#32) May 15, 2018
fsm_test.go Added an AvailableTransitions func to FSM (#32) May 15, 2018
go.mod Add Makefile, go.mod and travis.yml Jan 16, 2019
utils.go Fix CI test coverage with race detection (#31) Jan 16, 2018

Build Status Coverage Status GoDoc Go Report Card

FSM is a finite state machine for Go.

It is heavily based on two FSM implementations:

For API docs and examples see http://godoc.org/github.com/looplab/fsm

From examples/simple.go:

package main

import (
    "fmt"
    "github.com/looplab/fsm"
)

func main() {
    fsm := fsm.NewFSM(
        "closed",
        fsm.Events{
            {Name: "open", Src: []string{"closed"}, Dst: "open"},
            {Name: "close", Src: []string{"open"}, Dst: "closed"},
        },
        fsm.Callbacks{},
    )

    fmt.Println(fsm.Current())

    err := fsm.Event("open")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(fsm.Current())

    err = fsm.Event("close")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(fsm.Current())
}

From examples/struct.go:

package main

import (
    "fmt"
    "github.com/looplab/fsm"
)

type Door struct {
    To  string
    FSM *fsm.FSM
}

func NewDoor(to string) *Door {
    d := &Door{
        To: to,
    }

    d.FSM = fsm.NewFSM(
        "closed",
        fsm.Events{
            {Name: "open", Src: []string{"closed"}, Dst: "open"},
            {Name: "close", Src: []string{"open"}, Dst: "closed"},
        },
        fsm.Callbacks{
            "enter_state": func(e *fsm.Event) { d.enterState(e) },
        },
    )

    return d
}

func (d *Door) enterState(e *fsm.Event) {
    fmt.Printf("The door to %s is %s\n", d.To, e.Dst)
}

func main() {
    door := NewDoor("heaven")

    err := door.FSM.Event("open")
    if err != nil {
        fmt.Println(err)
    }

    err = door.FSM.Event("close")
    if err != nil {
        fmt.Println(err)
    }
}

FSM is licensed under Apache License 2.0

http://www.apache.org/licenses/LICENSE-2.0


文章来源: https://github.com/looplab/fsm
如有侵权请联系:admin#unsafe.sh