forked from qmuntal/stateless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.go
273 lines (246 loc) · 7.65 KB
/
states.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
package stateless
import (
"context"
"fmt"
)
type actionBehaviour struct {
Action ActionFunc
Description invocationInfo
Trigger *Trigger
}
func (a actionBehaviour) Execute(ctx context.Context, transition Transition, args ...interface{}) (err error) {
if a.Trigger == nil || *a.Trigger == transition.Trigger {
ctx = withTransition(ctx, transition)
err = a.Action(ctx, args...)
}
return
}
type actionBehaviourSteady struct {
Action func(ctx context.Context) error
Description invocationInfo
}
func (a actionBehaviourSteady) Execute(ctx context.Context) error {
return a.Action(ctx)
}
type stateRepresentation struct {
State State
InitialTransitionTarget State
Superstate *stateRepresentation
EntryActions []actionBehaviour
ExitActions []actionBehaviour
ActivateActions []actionBehaviourSteady
DeactivateActions []actionBehaviourSteady
Substates []*stateRepresentation
TriggerBehaviours map[Trigger][]triggerBehaviour
HasInitialState bool
}
func newstateRepresentation(state State) *stateRepresentation {
return &stateRepresentation{
State: state,
TriggerBehaviours: make(map[Trigger][]triggerBehaviour),
}
}
func (sr *stateRepresentation) SetInitialTransition(state State) {
sr.InitialTransitionTarget = state
sr.HasInitialState = true
}
func (sr *stateRepresentation) state() State {
return sr.State
}
func (sr *stateRepresentation) CanHandle(ctx context.Context, trigger Trigger, args ...interface{}) (ok bool) {
_, ok = sr.FindHandler(ctx, trigger, args...)
return
}
func (sr *stateRepresentation) FindHandler(ctx context.Context, trigger Trigger, args ...interface{}) (handler triggerBehaviourResult, ok bool) {
handler, ok = sr.findHandler(ctx, trigger, args...)
if ok || sr.Superstate == nil {
return
}
handler, ok = sr.Superstate.FindHandler(ctx, trigger, args...)
return
}
func (sr *stateRepresentation) findHandler(ctx context.Context, trigger Trigger, args ...interface{}) (result triggerBehaviourResult, ok bool) {
var (
possibleBehaviours []triggerBehaviour
)
if possibleBehaviours, ok = sr.TriggerBehaviours[trigger]; !ok {
return
}
allResults := make([]triggerBehaviourResult, 0, len(possibleBehaviours))
for _, behaviour := range possibleBehaviours {
allResults = append(allResults, triggerBehaviourResult{
Handler: behaviour,
UnmetGuardConditions: behaviour.UnmetGuardConditions(ctx, args...),
})
}
metResults := make([]triggerBehaviourResult, 0, len(allResults))
unmetResults := make([]triggerBehaviourResult, 0, len(allResults))
for _, result := range allResults {
if len(result.UnmetGuardConditions) == 0 {
metResults = append(metResults, result)
} else {
unmetResults = append(unmetResults, result)
}
}
if len(metResults) > 1 {
panic(fmt.Sprintf("stateless: Multiple permitted exit transitions are configured from state '%v' for trigger '%v'. Guard clauses must be mutually exclusive.", sr.State, trigger))
}
if len(metResults) == 1 {
result, ok = metResults[0], true
} else if len(unmetResults) > 0 {
result, ok = unmetResults[0], false
}
return
}
func (sr *stateRepresentation) Activate(ctx context.Context) error {
if sr.Superstate != nil {
if err := sr.Superstate.Activate(ctx); err != nil {
return err
}
}
return sr.executeActivationActions(ctx)
}
func (sr *stateRepresentation) Deactivate(ctx context.Context) error {
if err := sr.executeDeactivationActions(ctx); err != nil {
return err
}
if sr.Superstate != nil {
return sr.Superstate.Deactivate(ctx)
}
return nil
}
func (sr *stateRepresentation) Enter(ctx context.Context, transition Transition, args ...interface{}) error {
if transition.IsReentry() {
return sr.executeEntryActions(ctx, transition, args...)
}
if sr.IncludeState(transition.Source) {
return nil
}
if sr.Superstate != nil && !transition.isInitial {
if err := sr.Superstate.Enter(ctx, transition, args...); err != nil {
return err
}
}
return sr.executeEntryActions(ctx, transition, args...)
}
func (sr *stateRepresentation) Exit(ctx context.Context, transition Transition, args ...interface{}) (err error) {
isReentry := transition.IsReentry()
if !isReentry && sr.IncludeState(transition.Destination) {
return
}
err = sr.executeExitActions(ctx, transition, args...)
// Must check if there is a superstate, and if we are leaving that superstate
if err == nil && !isReentry && sr.Superstate != nil {
// Check if destination is within the state list
if sr.IsIncludedInState(transition.Destination) {
// Destination state is within the list, exit first superstate only if it is NOT the the first
if sr.Superstate.state() != transition.Destination {
err = sr.Superstate.Exit(ctx, transition, args...)
}
} else {
// Exit the superstate as well
err = sr.Superstate.Exit(ctx, transition, args...)
}
}
return
}
func (sr *stateRepresentation) InternalAction(ctx context.Context, transition Transition, args ...interface{}) error {
var internalTransition *internalTriggerBehaviour
var stateRep *stateRepresentation = sr
for stateRep != nil {
if result, ok := stateRep.findHandler(ctx, transition.Trigger, args...); ok {
switch t := result.Handler.(type) {
case *internalTriggerBehaviour:
internalTransition = t
}
break
}
stateRep = stateRep.Superstate
}
if internalTransition == nil {
panic("stateless: The configuration is incorrect, no action assigned to this internal transition.")
}
return internalTransition.Execute(ctx, transition, args...)
}
func (sr *stateRepresentation) IncludeState(state State) bool {
if state == sr.State {
return true
}
for _, substate := range sr.Substates {
if substate.IncludeState(state) {
return true
}
}
return false
}
func (sr *stateRepresentation) IsIncludedInState(state State) bool {
if state == sr.State {
return true
}
if sr.Superstate != nil {
return sr.Superstate.IsIncludedInState(state)
}
return false
}
func (sr *stateRepresentation) AddTriggerBehaviour(tb triggerBehaviour) {
trigger := tb.GetTrigger()
sr.TriggerBehaviours[trigger] = append(sr.TriggerBehaviours[trigger], tb)
}
func (sr *stateRepresentation) PermittedTriggers(ctx context.Context, args ...interface{}) (triggers []Trigger) {
for key, value := range sr.TriggerBehaviours {
for _, tb := range value {
if len(tb.UnmetGuardConditions(ctx, args...)) == 0 {
triggers = append(triggers, key)
break
}
}
}
if sr.Superstate != nil {
triggers = append(triggers, sr.Superstate.PermittedTriggers(ctx, args...)...)
// remove duplicated
seen := make(map[Trigger]struct{}, len(triggers))
j := 0
for _, v := range triggers {
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
triggers[j] = v
j++
}
triggers = triggers[:j]
}
return
}
func (sr *stateRepresentation) executeActivationActions(ctx context.Context) error {
for _, a := range sr.ActivateActions {
if err := a.Execute(ctx); err != nil {
return err
}
}
return nil
}
func (sr *stateRepresentation) executeDeactivationActions(ctx context.Context) error {
for _, a := range sr.DeactivateActions {
if err := a.Execute(ctx); err != nil {
return err
}
}
return nil
}
func (sr *stateRepresentation) executeEntryActions(ctx context.Context, transition Transition, args ...interface{}) error {
for _, a := range sr.EntryActions {
if err := a.Execute(ctx, transition, args...); err != nil {
return err
}
}
return nil
}
func (sr *stateRepresentation) executeExitActions(ctx context.Context, transition Transition, args ...interface{}) error {
for _, a := range sr.ExitActions {
if err := a.Execute(ctx, transition, args...); err != nil {
return err
}
}
return nil
}