-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsm_instance.go
More file actions
executable file
·123 lines (104 loc) · 2.77 KB
/
Copy pathfsm_instance.go
File metadata and controls
executable file
·123 lines (104 loc) · 2.77 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package msm
import (
"reflect"
"time"
"go.uber.org/zap"
)
type FSMState struct {
Id int64 `gorm:"primaryKey;autoIncrement"`
CreatedAt time.Time // special name handled by GORM
UpdatedAt time.Time // special name handled by GORM
Fsm string `gorm:"index:idx_fsm_term"`
State string
Storage string `gorm:"type:json"`
Terminated bool `gorm:"index:idx_fsm_term"`
Description string `gorm:"index"`
}
func (f *FSMState) FsmID() FsmID {
return FsmID{
Fsm: f.Fsm,
Id: f.Id,
}
}
type FSMInstance struct {
FSMState
Manager *FSM
Storage reflect.Value // To be casted on a call
}
func (fi *FSMInstance) iProcess(response Response) {
eventKey := TransitionKey{
State: State(fi.State),
Event: reflect.TypeOf(response.Event),
}
eventValue := reflect.ValueOf(response.Event)
if eventKey.Event.Kind() != reflect.Ptr {
eventKey.Event = reflect.PointerTo(eventKey.Event)
eventValue = ptr(eventValue)
}
transforms, ok := fi.Manager.Table[eventKey]
if !ok {
// Silently quit if it was an anonymous event
switch response.Event.(type) {
case *None:
return
default:
}
for _, callback := range fi.Manager.Manager.Callbacks {
callback.NoTransition(fi, State(fi.State), response.Source, response.Event)
}
return
}
for _, transform := range transforms {
guardOut := reflect.ValueOf(transform.Guard).Call([]reflect.Value{
reflect.ValueOf(fi),
fi.Storage,
reflect.ValueOf(State(fi.State)),
reflect.ValueOf(response.Source),
eventValue,
})
guardAllow := guardOut[0].Bool()
if guardAllow {
for _, callback := range fi.Manager.Manager.Callbacks {
callback.ChangeState(fi, State(fi.State), response.Source, response.Event, transform.NewState)
}
reflect.ValueOf(transform.Action).Call([]reflect.Value{
reflect.ValueOf(fi),
fi.Storage,
reflect.ValueOf(State(fi.State)),
reflect.ValueOf(response.Source),
eventValue,
})
fi.State = string(transform.NewState)
fi.Terminated = fi.Manager.Terminals[State(fi.State)]
fi.Manager.SaveFSM(fi)
// Check if there is anonymous transition
fi.iProcess(Response{
Source: "internal",
Event: &None{},
})
return
}
}
for _, callback := range fi.Manager.Manager.Callbacks {
callback.NoTransition(fi, State(fi.State), response.Source, response.Event)
}
}
func (fi *FSMInstance) Request(dest string, request RequestPayload) {
fi.Manager.request(fi, dest, request)
}
func (fi *FSMInstance) IsTerminated() bool {
isTerminated := fi.Manager.Terminals[State(fi.State)]
return isTerminated
}
func (fi *FSMInstance) SetDescription(name string) {
fi.Description = name
}
func (fi *FSMInstance) ID() FsmID {
return FsmID{
Fsm: fi.Manager.Name,
Id: fi.Id,
}
}
func (fi *FSMInstance) Log() *zap.Logger {
return fi.Manager.Log()
}