-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
328 lines (276 loc) · 9.24 KB
/
component.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* Package statemachine
* @Author 砚池/Ivan
* @Date 2024/04/08
* @Description:
*/
package statemachine
import (
"fmt"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
)
// Action 是状态机规则命中后,执行的业务逻辑
type Action[S, E comparable, C any] func(from S, to S, event E, context *C)
// Condition 是状态机路由后在执行 Action 之前的前置校验,在配置状态机时可以为空
type Condition[C any] func(ctx *C) bool
// TransitionType TransitionType 状态转移类型
// 当前只有 EXTERNAL 与 INTERNAL 两种
type TransitionType string
const (
//INTERNAL if triggered, occurs without exiting or entering the source state
//(i.e., it does not cause a state change). This means that the entry or exit condition of the source
//state will not be invoked. An internal transition can be taken even if the SateMachine is in one or
//more Regions nested within the associated state.
INTERNAL TransitionType = "INTERNAL"
LOCAL TransitionType = "LOCAL"
//EXTERNAL if triggered, will exit the composite (source) state.
EXTERNAL TransitionType = "EXTERNAL"
)
// transition is something what a state machine associates with a state
type transition[S, E comparable, C any] struct {
source state[S, E, C]
target state[S, E, C]
event E
condition Condition[C]
t TransitionType
action Action[S, E, C]
}
// transit Do transition from source state to target state.
func (t *transition[S, E, C]) transit(ctx *C, checkCondition bool) state[S, E, C] {
log.Debugf("Do t: %s\n", t.String())
err := t.verify()
if err != nil {
log.Errorf("err:%s,stay at the %v state\n", err.Error(), t.source)
return t.source
}
if !checkCondition || t.condition == nil || t.condition(ctx) {
if t.action != nil {
t.action(t.source.id, t.target.id, t.event, ctx)
}
return t.target
}
log.Debugf("Condition is not satisfied, stay at the %v state\n", t.source)
return t.source
}
// verify transition correctness
func (t *transition[S, E, C]) verify() error {
if t.t == INTERNAL && t.source.id != t.target.id {
return fmt.Errorf("Internal t source state '%v' and target state '%v' must be same.\n", t.source.id, t.target.id)
}
return nil
}
// String 打印状态机
func (t *transition[S, E, C]) String() string {
return fmt.Sprintf("%v-[%v+%v]->%v", t.source.id, t.event, t.t, t.target.id)
}
func sameTransition[S, E comparable, C any](first, second *transition[S, E, C]) bool {
fs := first.source.id
ft := first.target.id
fe := first.event
ss := second.source.id
st := second.target.id
se := second.event
return fs == ss && ft == st && fe == se
}
// state 状态
type state[S, E comparable, C any] struct {
id S
eventTransitions *eventTransitions[S, E, C]
}
func (s *state[S, E, C]) String() string {
return fmt.Sprintf("%v", s.id)
}
// addTransition to the state
func (s *state[S, E, C]) addTransition(event E, target state[S, E, C], transitionType TransitionType) *transition[S, E, C] {
t := &transition[S, E, C]{
source: *s,
target: target,
event: event,
t: transitionType,
}
s.transitions().put(event, t)
return t
}
func (s *state[S, E, C]) transitions() *eventTransitions[S, E, C] {
if s.eventTransitions == nil {
s.eventTransitions = &eventTransitions[S, E, C]{}
}
return s.eventTransitions
}
func (s *state[S, E, C]) transitionsOfEvent(event E) []*transition[S, E, C] {
return s.transitions().get(event)
}
func (s *state[S, E, C]) allTransitions() []*transition[S, E, C] {
return s.transitions().allTransitions()
}
type CurrentStateFetcher[S comparable, C any] func(ctx *C) S
type eventTransitions[S, E comparable, C any] struct {
transitions map[E][]*transition[S, E, C]
}
func (et *eventTransitions[S, E, C]) put(event E, t *transition[S, E, C]) {
if et.transitions == nil {
et.transitions = make(map[E][]*transition[S, E, C])
}
if et.transitions[event] == nil {
var transitions []*transition[S, E, C]
transitions = append(transitions, t)
et.transitions[event] = transitions
} else {
existingTransitions := et.transitions[event]
err := et.verify(existingTransitions, t)
if err != nil {
log.Fatal(err)
}
existingTransitions = append(existingTransitions, t)
et.transitions[event] = existingTransitions
}
}
func (et *eventTransitions[S, E, C]) get(event E) []*transition[S, E, C] {
return et.transitions[event]
}
func (et *eventTransitions[S, E, C]) allTransitions() []*transition[S, E, C] {
var all []*transition[S, E, C]
for _, transitions := range et.transitions {
all = append(all, transitions...)
}
return all
}
func (et *eventTransitions[S, E, C]) verify(existingTransitions []*transition[S, E, C], newTransition *transition[S, E, C]) error {
for _, transition := range existingTransitions {
if sameTransition(transition, newTransition) {
return fmt.Errorf("%v already Exist, you can not add another one\n", transition)
}
}
return nil
}
type fromInterface[S, E comparable, C any] interface {
To(stateId S) toInterface[S, E, C]
}
type toInterface[S, E comparable, C any] interface {
On(event E) onInterface[S, E, C]
}
type onInterface[S, E comparable, C any] interface {
When(condition Condition[C]) whenInterface[S, E, C]
whenInterface[S, E, C]
}
type whenInterface[S, E comparable, C any] interface {
Perform(action Action[S, E, C])
}
type ExternalTransitionInterface[S, E comparable, C any] interface {
From(stateId S) fromInterface[S, E, C]
}
type ExternalTransitionsInterface[S, E comparable, C any] interface {
FromAmong(stateIds ...S) fromInterface[S, E, C]
}
type InternalTransitionInterface[S, E comparable, C any] interface {
Within(stateId S) toInterface[S, E, C]
}
type transitionBuilder[S, E comparable, C any] struct {
stateMap map[S]state[S, E, C]
target state[S, E, C]
t TransitionType
source state[S, E, C]
transition *transition[S, E, C]
}
func newExternalTransitionBuilder[S, E comparable, C any](stateMap map[S]state[S, E, C], t TransitionType) ExternalTransitionInterface[S, E, C] {
return &transitionBuilder[S, E, C]{
stateMap: stateMap,
t: t,
}
}
func newInternalTransitionBuilder[S, E comparable, C any](stateMap map[S]state[S, E, C], t TransitionType) InternalTransitionInterface[S, E, C] {
return &transitionBuilder[S, E, C]{
stateMap: stateMap,
t: t,
}
}
func (tb *transitionBuilder[S, E, C]) Perform(action Action[S, E, C]) {
tb.transition.action = action
}
func (tb *transitionBuilder[S, E, C]) On(event E) onInterface[S, E, C] {
tb.transition = tb.source.addTransition(event, tb.target, tb.t)
return tb
}
func (tb *transitionBuilder[S, E, C]) When(condition Condition[C]) whenInterface[S, E, C] {
tb.transition.condition = condition
return tb
}
func (tb *transitionBuilder[S, E, C]) To(stateId S) toInterface[S, E, C] {
tb.target = getState(tb.stateMap, stateId)
return tb
}
func (tb *transitionBuilder[S, E, C]) From(stateId S) fromInterface[S, E, C] {
tb.source = getState(tb.stateMap, stateId)
return tb
}
func (tb *transitionBuilder[S, E, C]) Within(stateId S) toInterface[S, E, C] {
tb.source = getState(tb.stateMap, stateId)
tb.target = getState(tb.stateMap, stateId)
return tb
}
func getState[S, E comparable, C any](stateMap map[S]state[S, E, C], stateId S) state[S, E, C] {
if !lo.Contains(lo.Keys(stateMap), stateId) {
s := state[S, E, C]{
id: stateId,
eventTransitions: &eventTransitions[S, E, C]{},
}
stateMap[stateId] = s
}
return stateMap[stateId]
}
type transitionsBuilder[S, E comparable, C any] struct {
stateMap map[S]state[S, E, C]
target state[S, E, C]
t TransitionType
sources []state[S, E, C]
transitions []*transition[S, E, C]
}
func newExternalTransitionsBuilder[S, E comparable, C any](stateMap map[S]state[S, E, C], t TransitionType) ExternalTransitionsInterface[S, E, C] {
return &transitionsBuilder[S, E, C]{
stateMap: stateMap,
t: t,
}
}
func (tb *transitionsBuilder[S, E, C]) To(stateId S) toInterface[S, E, C] {
tb.target = getState(tb.stateMap, stateId)
return tb
}
func (tb *transitionsBuilder[S, E, C]) On(event E) onInterface[S, E, C] {
transitions := tb.transitions
for _, source := range tb.sources {
transition := source.addTransition(event, tb.target, tb.t)
transitions = append(transitions, transition)
}
tb.transitions = transitions
return tb
}
func (tb *transitionsBuilder[S, E, C]) FromAmong(stateIds ...S) fromInterface[S, E, C] {
sources := tb.sources
for _, stateId := range stateIds {
sources = append(sources, getState(tb.stateMap, stateId))
}
tb.sources = sources
return tb
}
func (tb *transitionsBuilder[S, E, C]) Perform(action Action[S, E, C]) {
for _, transition := range tb.transitions {
transition.action = action
}
}
func (tb *transitionsBuilder[S, E, C]) When(condition Condition[C]) whenInterface[S, E, C] {
for _, transition := range tb.transitions {
transition.condition = condition
}
return tb
}
type FailCallbackInterface[S, E comparable, C any] interface {
OnFail(source S, event E, ctx *C)
}
type NumbFailCallback[S, E comparable, C any] struct{}
func (n *NumbFailCallback[S, E, C]) OnFail(source S, event E, ctx *C) {
//do nothing
}
type AlertFailCallback[S, E comparable, C any] struct{}
func (a *AlertFailCallback[S, E, C]) OnFail(source S, event E, ctx *C) {
log.Fatalf("Cannot fire event [%v] on current state [%v] with context [%v]\n", event, source, ctx)
}