-
Notifications
You must be signed in to change notification settings - Fork 19
/
machine_impl.go
433 lines (367 loc) · 10.7 KB
/
machine_impl.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package statemachine
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
"time"
"github.com/Gurpartap/statemachine-go/internal/dynafunc"
)
type machineImpl struct {
def *MachineDef
previousState string
currentState string
supermachine *machineImpl
submachines map[string][]*machineImpl
mutex sync.RWMutex
hasExited bool
ctxTimedEvents context.Context
stopTimedEvents context.CancelFunc
}
// NewMachine returns a zero-valued instance of machine, which implements
// Machine.
func NewMachine() Machine {
ctxTimedEvents, stopTimedEvents := context.WithCancel(context.Background())
return &machineImpl{
def: NewMachineDef(),
submachines: map[string][]*machineImpl{},
ctxTimedEvents: ctxTimedEvents,
stopTimedEvents: stopTimedEvents,
}
}
// BuildNewMachine creates a zero-valued instance of machine, and builds it
// using the passed machineBuilderFn arg.
func BuildNewMachine(machineBuilderFn func(machineBuilder MachineBuilder)) Machine {
machine := NewMachine()
machine.Build(machineBuilderFn)
return machine
}
func (m *machineImpl) Build(machineBuilderFn func(machineBuilder MachineBuilder)) {
machineBuilder := NewMachineBuilder()
machineBuilderFn(machineBuilder)
machineBuilder.Build(m)
}
// SetMachineDef implements MachineBuildable.
func (m *machineImpl) SetMachineDef(def *MachineDef) {
m.mutex.Lock()
defer m.mutex.Unlock()
// b, _ := json.MarshalIndent(def, "", " ")
// // b, _ := hclencoder.Encode(def)
// fmt.Printf("machine def = %s\n", string(b))
m.def = def
if err := m.setCurrentState(m.def.InitialState); err != nil {
panic(err)
}
m.restartTimedEventsLoops()
}
func (m *machineImpl) restartTimedEventsLoops() {
for event, eventDef := range m.def.Events {
if eventDef.TimedEvery > 0 {
go func(event string, timedEvery time.Duration) {
// fmt.Printf("event=%s timed_every=%d\n", event, timedEvery)
for {
select {
case <-time.After(timedEvery):
// fmt.Printf("firing timed event '%s'\n", event)
_ = m.Fire(event)
case <-m.ctxTimedEvents.Done():
// fmt.Printf("stopping timed event '%s'\n", event)
return
}
}
}(event, eventDef.TimedEvery)
}
}
}
// GetStateMap implements Machine.
func (m *machineImpl) GetStateMap() StateMap {
substate := StateMap{}
if submachines, ok := m.submachines[m.currentState]; ok {
for _, submachine := range submachines {
if _, ok := submachine.submachines[submachine.currentState]; ok {
substate[submachine.def.ID] = submachine.GetStateMap()
} else {
substate[submachine.def.ID] = submachine.currentState
}
}
}
return StateMap{
m.currentState: substate,
}
}
// GetState implements Machine.
func (m *machineImpl) GetState() string {
return m.currentState
}
// SetCurrentState implements Machine.
func (m *machineImpl) SetCurrentState(state interface{}) error {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.setCurrentState(state)
}
// IsState implements Machine.
func (m *machineImpl) IsState(state string) bool {
return m.GetState() == state
}
// Send implements Machine.
func (m *machineImpl) Send(signal Message) error {
switch signal.(type) {
case TriggerEvent:
return m.Fire(signal.(TriggerEvent).Event)
case OverrideState:
return m.SetCurrentState(signal.(OverrideState).State)
}
return errors.New("no such signal")
}
// Fire implements Machine.
func (m *machineImpl) Fire(event string) (err error) {
m.mutex.Lock()
defer func() {
if err != nil {
args := make(map[reflect.Type]interface{})
args[reflect.TypeOf(new(Event))] = &eventImpl{name: event}
args[reflect.TypeOf(new(error))] = err
for _, callbackDef := range m.def.FailureCallbacks {
if callbackDef.MatchesEvent(event) {
for _, callback := range callbackDef.Do {
m.exec(callback.Func, args)
}
}
}
}
m.mutex.Unlock()
if m.hasExited {
// TODO: should we wait for `<-m.stoppedTimedEvents`?
m.stopTimedEvents()
*m = machineImpl{}
}
}()
if m.IsState("") {
err = errors.New("state machine not initialized")
return
}
// fmt.Printf("\n---\n🔁 %s\n", event)
// defer func() { fmt.Printf("=> %s\n---\n", m.GetState()) }()
fromState := m.GetState()
var transition Transition
transition, err = m.findTransition(event, fromState)
if err != nil {
return
}
err = m.applyTransition(transition)
return
}
func (m *machineImpl) findTransition(event string, fromState string) (transition Transition, err error) {
eventDef, ok := m.def.Events[event]
if !ok {
err = errors.New("no such event")
return
}
transition, err = m.matchTransition(eventDef.Transitions, fromState)
if err == nil || eventDef.Choice == nil {
return
}
transition, err = m.findChoiceTransition(event, eventDef, fromState)
return
}
func (m *machineImpl) findChoiceTransition(event string, eventDef *EventDef, fromState string) (transition Transition, err error) {
args := make(map[reflect.Type]interface{})
args[reflect.TypeOf(new(Machine))] = m
args[reflect.TypeOf(new(Event))] = &eventImpl{name: event}
if eventDef.Choice.UnlessGuard != nil {
if ok := execGuard(eventDef.Choice.UnlessGuard.Guard, args); ok {
err = ErrTransitionNotAllowed
return
}
}
if execChoice(eventDef.Choice.Condition.Condition, args) {
if eventDef.Choice.OnTrue.Choice != nil {
transition, err = m.findChoiceTransition(event, eventDef.Choice.OnTrue, fromState)
return
}
transition, err = m.matchTransition(eventDef.Choice.OnTrue.Transitions, fromState)
return
}
if eventDef.Choice.OnFalse.Choice != nil {
transition, err = m.findChoiceTransition(event, eventDef.Choice.OnFalse, fromState)
return
}
transition, err = m.matchTransition(eventDef.Choice.OnFalse.Transitions, fromState)
return
}
func (m *machineImpl) matchTransition(transitions []*TransitionDef, fromState string) (transition Transition, err error) {
for _, transitionDef := range transitions {
matches := transitionDef.Matches(fromState)
if !matches {
err = ErrNoMatchingTransition
continue
}
if !transitionDef.IsAllowed(fromState, m) {
err = ErrTransitionNotAllowed
continue
}
transition = newTransitionImpl(fromState, transitionDef.To)
err = nil
return
}
err = ErrNoMatchingTransition
return
}
func (m *machineImpl) Submachine(idPath ...string) (Machine, error) {
for _, submachine := range m.submachines[m.currentState] {
if submachine.def.ID == idPath[0] {
if len(idPath) > 1 {
return submachine.Submachine(idPath[1:]...)
}
return submachine, nil
}
}
return nil, errors.New("submachine not active")
}
func (m *machineImpl) setCurrentStateMap(state StateMap) error {
for rootState, subStates := range state {
switch subStates.(type) {
case nil:
// fmt.Printf("setting state to '%s'\n", rootState)
return m.setCurrentState(rootState)
case StateMap:
// fmt.Printf("setting state to '%s'\n", rootState)
if err := m.setCurrentState(rootState); err != nil {
return err
}
for id, state := range subStates.(StateMap) {
for _, submachine := range m.submachines[rootState] {
if submachine.def.ID == id {
switch state.(type) {
case StateMap:
// fmt.Printf("nesting into submachine '%s'\n", id)
if err := submachine.setCurrentStateMap(state.(StateMap)); err != nil {
return err
}
case string:
// fmt.Printf("setting submachine '%s' to '%s'\n", id, state)
if err := submachine.SetCurrentState(state); err != nil {
return err
}
default:
return ErrStateTypeNotSupported
}
}
}
}
default:
if err := m.setCurrentState(rootState); err != nil {
return err
}
return ErrStateTypeNotSupported
}
// there is only one kv in any given StateMap
break
}
return nil
}
func (m *machineImpl) setCurrentState(state interface{}) error {
if state, ok := state.(StateMap); ok {
if err := m.setCurrentStateMap(state); err != nil {
return err
}
return nil
}
if state, ok := state.(string); ok {
for _, s := range m.def.States {
if s == state {
m.previousState = m.currentState
m.currentState = state
return nil
}
}
for s, submachineDefs := range m.def.Submachines {
if s == state {
m.submachines[state] = []*machineImpl{}
for _, submachineDef := range submachineDefs {
submachine := &machineImpl{
supermachine: m,
submachines: map[string][]*machineImpl{},
}
submachine.SetMachineDef(submachineDef)
m.submachines[state] = append(m.submachines[state], submachine)
}
m.previousState = m.currentState
m.currentState = state
return nil
}
}
}
return ErrStateTypeNotSupported
}
func (m *machineImpl) applyTransition(transition Transition) error {
fromState := m.GetState()
args := make(map[reflect.Type]interface{})
args[reflect.TypeOf(new(Transition))] = transition
for _, callbackDef := range m.def.BeforeCallbacks {
if callbackDef.Matches(fromState, transition.To()) {
for _, callback := range callbackDef.Do {
m.exec(callback.Func, args)
}
}
}
var matchingCallbacks []*TransitionCallbackFuncDef
for _, callbackDef := range m.def.AroundCallbacks {
if callbackDef.Matches(fromState, transition.To()) {
matchingCallbacks = append(matchingCallbacks, callbackDef.Do...)
}
}
applyTransition := func() {
m.setCurrentState(transition.To())
}
m.applyTransitionAroundCallbacks(matchingCallbacks, args, applyTransition)
for _, callbackDef := range m.def.AfterCallbacks {
if !callbackDef.Matches(fromState, transition.To()) {
continue
}
for _, callback := range callbackDef.Do {
m.exec(callback.Func, args)
}
if callbackDef.ExitToState != "" && m.supermachine != nil {
if err := m.supermachine.applyTransition(
newTransitionImpl(m.supermachine.currentState, callbackDef.ExitToState),
); err != nil {
return fmt.Errorf("could not exit submachine: %s", err)
}
m.hasExited = true
return nil
}
}
return nil
}
// callback1(next: {
// callback2(next: {
// callback3(next: {
// applyTransition()
// })
// })
// })
func (m *machineImpl) applyTransitionAroundCallbacks(callbacks []*TransitionCallbackFuncDef, args map[reflect.Type]interface{}, applyTransition func()) {
if len(callbacks) == 0 {
applyTransition()
return
}
calledBackNext := false
args[reflect.TypeOf(new(func()))] = func() {
calledBackNext = true
m.applyTransitionAroundCallbacks(callbacks[1:], args, applyTransition)
}
m.exec(callbacks[0].Func, args)
if !calledBackNext && len(callbacks) != 1 {
panic("non-last around callbacks must call next()")
}
return
}
func (m *machineImpl) exec(callback TransitionCallbackFunc, args map[reflect.Type]interface{}) {
args[reflect.TypeOf(new(Machine))] = m
fn := dynafunc.NewDynamicFunc(callback, args)
if err := fn.Call(); err != nil {
panic(err)
}
}