-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm_unsafe.go
62 lines (58 loc) · 1.79 KB
/
fsm_unsafe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package fsm
import "golang.org/x/exp/constraints"
var _ IFsm[string, string] = (*Fsm[string, string])(nil)
var _ IFsm[int, string] = (*Fsm[int, string])(nil)
// Fsm is the state machine that holds the current state.
// E is the event
// S is the state
type Fsm[E constraints.Ordered, S constraints.Ordered] struct {
// Transition contain events and source states to destination states.
// This is immutable
ITransition[E, S]
// current is the state that the Fsm is currently in.
current S
}
// NewFsm constructs a generic Fsm with an initial state S and a transition.
// E is the event type
// S is the state type.
func NewFsm[E constraints.Ordered, S constraints.Ordered](initState S, ts ITransition[E, S]) IFsm[E, S] {
return &Fsm[E, S]{
current: initState,
ITransition: ts,
}
}
func (f *Fsm[E, S]) Clone() IFsm[E, S] {
return &Fsm[E, S]{
current: f.current,
ITransition: f.ITransition,
}
}
func (f *Fsm[E, S]) CloneNewState(newState S) IFsm[E, S] {
return &Fsm[E, S]{
current: newState,
ITransition: f.ITransition,
}
}
func (f *Fsm[E, S]) Current() S { return f.current }
func (f *Fsm[E, S]) Is(state S) bool { return state == f.current }
func (f *Fsm[E, S]) SetCurrent(state S) { f.current = state }
func (f *Fsm[E, S]) Trigger(event E) error {
dst, err := f.ITransition.Transform(f.current, event)
if err != nil {
return err
}
f.current = dst
return nil
}
func (f *Fsm[E, S]) MatchCurrentOccur(event E) bool {
return f.ITransition.MatchOccur(f.current, event)
}
func (f *Fsm[E, S]) MatchCurrentAllOccur(event ...E) bool {
return f.ITransition.MatchAllOccur(f.current, event...)
}
func (f *Fsm[E, S]) CurrentAvailEvents() []E {
return f.ITransition.AvailEvents(f.current)
}
func (f *Fsm[E, S]) Visualize(t VisualizeType) (string, error) {
return Visualize[E, S](t, f)
}