有限狀態機(Finite State Machine,FSM)是一種數學模型,用於描述系統在不同狀態下的行為和轉移條件。
狀態機有三個組成部分:狀態(State)、事件(Event)、動作(Action)
,事件(轉移條件)觸發狀態的轉移和動作的執行。動作的執行不是必須的,可以只轉移狀態,不指定任何動作。總體而言,狀態機是一種用以表示有限個狀態以及這些狀態之間的轉移和動作的執行等行為的數學模型。
狀態機可以用公式 State(S) , Event(E) -> Actions (A), State(S’)
表示,即在處於狀態S的情況下,接收到了事件E,使得狀態轉移到了S’,同時伴隨著動作A的執行。
Event(事件)
是指觸發狀態轉換的輸入訊號或條件。它可以是任何型別的輸入,例如感測器資料、使用者輸入、網路訊息等。在程式設計中,Event通常是一個列舉型別,每個列舉值代表一個特定的事件。
State(狀態)
是指系統在某一時刻所處的狀態,它是系統的一種抽象描述。在有限狀態機中,狀態是由一組狀態變數來描述的,這些狀態變數的取值決定了系統的狀態。狀態可以是離散的,也可以是連續的。在有限狀態機中,狀態通常用一個圓圈來表示,圓圈內部寫上狀態的名稱。例如,一個簡單的有限狀態機可以有兩個狀態:開和關,它們可以用以下方式表示:
Action(動作)
是指在狀態轉移時執行的操作或動作。當有限狀態機從一個狀態轉移到另一個狀態時,可以執行一個或多個action來改變系統的狀態或執行某些操作。例如,當有限狀態機從「待機」狀態轉移到「執行」狀態時,可以執行一個action來啟動系統。在實際應用中,action可以是任何有效的程式碼,例如函數呼叫、變數賦值、列印輸出等。
FSM 通常用於程式設計中,用於實現狀態轉移和控制流程。
注意:
在任何時刻,FSM 只能處於一種狀態。
通過上面關於有限狀態機的定義,我們大概知道了狀態機是個什麼東西,那麼Golang中是怎麼實現的呢。不用慌,已經有大佬實現好了,只管用就好了。
安裝:
go get github.com/looplab/[email protected]
接下來一起看看github.com/looplab/fsm
是如何使用的。
注意:
不同版本的 fsm 使用方式,可能不太一樣,最好是看下
NewFSM
函數的註釋,看下具體的細節。 本篇文章以:github.com/looplab/[email protected]
為例。
這裡把官方的例子改了下,感覺官方的例子不是很清晰。程式碼如下:
package main
import (
"context"
"fmt"
"github.com/looplab/fsm"
)
type Door struct {
Name string
FSM *fsm.FSM
}
func NewDoor(name string) *Door {
d := &Door{
Name: name,
}
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(_ context.Context, e *fsm.Event) { d.enterState(e) },
},
)
return d
}
func (d *Door) enterState(e *fsm.Event) {
fmt.Printf("The door's name:%s , current state:%s\n", d.Name, e.Dst)
}
func main() {
door := NewDoor("測試")
fmt.Printf("fsm current state: %s \n", door.FSM.Current())
err := door.FSM.Event(context.Background(), "open")
if err != nil {
fmt.Println(err)
}
fmt.Printf("fsm current state: %s \n", door.FSM.Current())
err = door.FSM.Event(context.Background(), "close")
if err != nil {
fmt.Println(err)
}
fmt.Printf("fsm current state: %s \n", door.FSM.Current())
}
執行結果:
fsm current state: closed
The door's name:測試 , current state:open
fsm current state: open
The door's name:測試 , current state:closed
fsm current state: closed
這裡就通過Event
改變FSM中的狀態。轉移公式為:Src,Event -> Dst,d.enterState
。大意就是接受到了輸入Event
,狀態機的State
由Src->Dst
,並且執行了Action
:d.enterState。
剛開始使用的時候,好奇d.enterState(e)
是什麼時候呼叫的,我們一起看看 NewFSM
中的註釋就清楚了。
// NewFSM constructs a FSM from events and callbacks.
//
// The events and transitions are specified as a slice of Event structs
// specified as Events. Each Event is mapped to one or more internal
// transitions from Event.Src to Event.Dst.
// Callbacks are added as a map specified as Callbacks where the key is parsed
// as the callback event as follows, and called in the same order:
//
// 1. before_<EVENT> - called before event named <EVENT>
//
// 2. before_event - called before all events
//
// 3. leave_<OLD_STATE> - called before leaving <OLD_STATE>
//
// 4. leave_state - called before leaving all states
//
// 5. enter_<NEW_STATE> - called after entering <NEW_STATE>
//
// 6. enter_state - called after entering all states
//
// 7. after_<EVENT> - called after event named <EVENT>
//
// 8. after_event - called after all events
//
// There are also two short form versions for the most commonly used callbacks.
// They are simply the name of the event or state:
//
// 1. <NEW_STATE> - called after entering <NEW_STATE>
//
// 2. <EVENT> - called after event named <EVENT>
//
// If both a shorthand version and a full version is specified it is undefined
// which version of the callback will end up in the internal map. This is due
// to the pseudo random nature of Go maps. No checking for multiple keys is
// currently performed.
從上面我們知道了,d.enterState(e)
是在called after entering all states
時執行的。
從上面的註釋能知道完整版書寫的Callbacks
的執行順序如下:
雖然Callbacks
的寫法有兩種,但是不能同時使用完整版和簡寫版,否則最終使用那個版本是不確定的。
package main
import (
"context"
"fmt"
"github.com/looplab/fsm"
)
type Door struct {
Name string
FSM *fsm.FSM
}
func NewDoor(name string) *Door {
d := &Door{
Name: name,
}
d.FSM = fsm.NewFSM(
"closed",
fsm.Events{
{Name: "open", Src: []string{"closed"}, Dst: "open"},
{Name: "close", Src: []string{"open"}, Dst: "closed"},
},
fsm.Callbacks{
"before_open": func(_ context.Context, e *fsm.Event) { d.beforeOpen(e) },
"before_event": func(_ context.Context, e *fsm.Event) { d.beforeEvent(e) },
"leave_closed": func(_ context.Context, e *fsm.Event) { d.leaveClosed(e) },
"leave_state": func(_ context.Context, e *fsm.Event) { d.leaveState(e) },
"enter_open": func(_ context.Context, e *fsm.Event) { d.enterOpen(e) },
"enter_state": func(_ context.Context, e *fsm.Event) { d.enterState(e) },
"after_open": func(_ context.Context, e *fsm.Event) { d.afterOpen(e) },
"after_event": func(_ context.Context, e *fsm.Event) { d.afterEvent(e) },
},
)
return d
}
func (d *Door) beforeOpen(e *fsm.Event) {
fmt.Printf("beforeOpen, current state:%s, Dst:%s \n", d.FSM.Current(), e.Dst)
}
func (d *Door) beforeEvent(e *fsm.Event) {
fmt.Printf("beforeEvent, current state:%s, Dst:%s \n", d.FSM.Current(), e.Dst)
}
func (d *Door) leaveClosed(e *fsm.Event) {
fmt.Printf("leaveClosed, current state:%s, Dst:%s \n", d.FSM.Current(), e.Dst)
}
func (d *Door) leaveState(e *fsm.Event) {
fmt.Printf("leaveState, current state:%s, Dst:%s \n", d.FSM.Current(), e.Dst)
}
func (d *Door) enterOpen(e *fsm.Event) {
fmt.Printf("enterOpen, current state:%s, Dst:%s \n", d.FSM.Current(), e.Dst)
}
func (d *Door) enterState(e *fsm.Event) {
fmt.Printf("enterState, current state:%s, Dst:%s \n", d.FSM.Current(), e.Dst)
}
func (d *Door) afterOpen(e *fsm.Event) {
fmt.Printf("afterOpen, current state:%s, Dst:%s \n", d.FSM.Current(), e.Dst)
}
func (d *Door) afterEvent(e *fsm.Event) {
fmt.Printf("afterEvent, current state:%s, Dst:%s \n", d.FSM.Current(), e.Dst)
}
func main() {
door := NewDoor("測試")
fmt.Printf("fsm current state: %s \n", door.FSM.Current())
err := door.FSM.Event(context.Background(), "open")
if err != nil {
fmt.Println(err)
}
fmt.Printf("fsm current state: %s \n", door.FSM.Current())
err = door.FSM.Event(context.Background(), "close")
if err != nil {
fmt.Println(err)
}
fmt.Printf("fsm current state: %s \n", door.FSM.Current())
}
執行結果:大家重點看current state
何時發生的變化。
fsm current state: closed
beforeOpen, current state:closed, Dst:open
beforeEvent, current state:closed, Dst:open
leaveClosed, current state:closed, Dst:open
leaveState, current state:closed, Dst:open
enterOpen, current state:open, Dst:open
enterState, current state:open, Dst:open
afterOpen, current state:open, Dst:open
afterEvent, current state:open, Dst:open
fsm current state: open
beforeEvent, current state:open, Dst:closed
leaveState, current state:open, Dst:closed
enterState, current state:closed, Dst:closed
afterEvent, current state:closed, Dst:closed
fsm current state: closed
參考資料:
[有限狀態機](