-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
650 lines (574 loc) · 21.7 KB
/
builder.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package fsm
// FromStep Step marker interfaces to enforce the correct order of method calls
type FromStep interface{}
type ToStep interface{}
type OnStep interface{}
type WhenStep interface{}
type PerformStep interface{}
type ToAmongStep interface{}
type WithinStep interface{}
// TransitionStarterInterface is the interface for starting a transition definition
type TransitionStarterInterface[S comparable, E comparable, P any] interface {
// ExternalTransition starts defining an external transition
ExternalTransition() ExternalTransitionBuilderInterface[S, E, P]
// InternalTransition starts defining an internal transition
InternalTransition() InternalTransitionBuilderInterface[S, E, P]
// ExternalTransitions starts defining multiple external transitions
ExternalTransitions() ExternalTransitionsBuilderInterface[S, E, P]
// ExternalParallelTransition starts defining an external parallel transition
ExternalParallelTransition() ExternalParallelTransitionBuilderInterface[S, E, P]
}
// ExternalTransitionBuilderInterface is the interface for building external transitions
type ExternalTransitionBuilderInterface[S comparable, E comparable, P any] interface {
// From specifies the source state
From(state S) FromInterface[S, E, P]
}
// FromInterface is the interface for specifying the source state of a transition
type FromInterface[S comparable, E comparable, P any] interface {
// To specifies the target state
To(state S) ToInterface[S, E, P]
}
// ToInterface is the interface for specifying the target state of a transition
type ToInterface[S comparable, E comparable, P any] interface {
// On specifies the triggering event
On(event E) OnInterface[S, E, P]
}
// OnInterface is the interface for specifying the triggering event of a transition
type OnInterface[S comparable, E comparable, P any] interface {
// When specifies the condition for the transition
When(condition Condition[P]) WhenInterface[S, E, P]
// WhenFunc specifies a function as the condition for the transition
WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P]
}
// WhenInterface is the interface for specifying the condition of a transition
type WhenInterface[S comparable, E comparable, P any] interface {
// Perform specifies the action to execute during the transition
Perform(action Action[S, E, P])
// PerformFunc specifies a function as the action to execute during the transition
PerformFunc(actionFunc func(from, to S, event E, payload P) error)
}
// InternalTransitionBuilderInterface is the interface for building internal transitions
type InternalTransitionBuilderInterface[S comparable, E comparable, P any] interface {
// Within specifies the state where the internal transition occurs
Within(state S) ToInterface[S, E, P]
}
// ExternalTransitionsBuilderInterface is the interface for building multiple external transitions
type ExternalTransitionsBuilderInterface[S comparable, E comparable, P any] interface {
// FromAmong specifies multiple source states
FromAmong(states ...S) FromInterface[S, E, P]
}
// ExternalParallelTransitionBuilderInterface is the interface for building external parallel transitions
type ExternalParallelTransitionBuilderInterface[S comparable, E comparable, P any] interface {
// From specifies the source state
From(state S) ParallelFromInterface[S, E, P]
}
// ParallelFromInterface is the interface for specifying the source state of a parallel transition
type ParallelFromInterface[S comparable, E comparable, P any] interface {
// ToAmong specifies multiple target states
ToAmong(states ...S) ToInterface[S, E, P]
}
// Type assertions to ensure implementations satisfy interfaces
var (
_ TransitionStarterInterface[string, string, any] = (*StateMachineBuilder[string, string, any])(nil)
_ ExternalTransitionBuilderInterface[string, string, any] = (*TransitionBuilder[string, string, any, FromStep])(nil)
_ ExternalTransitionsBuilderInterface[string, string, any] = (*ExternalTransitionsBuilder[string, string, any])(nil)
_ ExternalParallelTransitionBuilderInterface[string, string, any] = (*ExternalParallelTransitionBuilder[string, string, any])(nil)
_ InternalTransitionBuilderInterface[string, string, any] = (*InternalTransitionBuilder[string, string, any])(nil)
_ ParallelFromInterface[string, string, any] = (*ParallelFromBuilder[string, string, any, ToAmongStep])(nil)
_ ToInterface[string, string, any] = (*ParallelFromBuilder[string, string, any, OnStep])(nil)
_ OnInterface[string, string, any] = (*ParallelFromBuilder[string, string, any, WhenStep])(nil)
_ WhenInterface[string, string, any] = (*ParallelFromBuilder[string, string, any, PerformStep])(nil)
_ FromInterface[string, string, any] = (*FromBuilder[string, string, any, ToStep])(nil)
_ ToInterface[string, string, any] = (*FromBuilder[string, string, any, OnStep])(nil)
_ OnInterface[string, string, any] = (*FromBuilder[string, string, any, WhenStep])(nil)
_ WhenInterface[string, string, any] = (*FromBuilder[string, string, any, PerformStep])(nil)
_ ToInterface[string, string, any] = (*OnTransitionBuilder[string, string, any, OnStep])(nil)
_ OnInterface[string, string, any] = (*OnTransitionBuilder[string, string, any, WhenStep])(nil)
_ WhenInterface[string, string, any] = (*OnTransitionBuilder[string, string, any, PerformStep])(nil)
)
// StateMachineBuilder builds state machines with a fluent API
type StateMachineBuilder[S comparable, E comparable, P any] struct {
stateMachine *StateMachineImpl[S, E, P]
}
// NewStateMachineBuilder creates a new builder
// Returns:
//
// A new state machine builder instance
func NewStateMachineBuilder[S comparable, E comparable, P any]() *StateMachineBuilder[S, E, P] {
return &StateMachineBuilder[S, E, P]{
stateMachine: newStateMachine[S, E, P](""),
}
}
// ExternalTransition starts defining an external transition
// Returns:
//
// A transition builder for configuring the external transition
func (b *StateMachineBuilder[S, E, P]) ExternalTransition() ExternalTransitionBuilderInterface[S, E, P] {
return &TransitionBuilder[S, E, P, FromStep]{
stateMachine: b.stateMachine,
transitionType: External,
}
}
// InternalTransition starts defining an internal transition
// Returns:
//
// A internal transition builder for configuring the internal transition
func (b *StateMachineBuilder[S, E, P]) InternalTransition() InternalTransitionBuilderInterface[S, E, P] {
return &InternalTransitionBuilder[S, E, P]{
stateMachine: b.stateMachine,
}
}
// ExternalTransitions starts defining multiple external transitions from different source states to the same target state
// Returns:
//
// A external transitions builder for configuring the transitions
func (b *StateMachineBuilder[S, E, P]) ExternalTransitions() ExternalTransitionsBuilderInterface[S, E, P] {
return &ExternalTransitionsBuilder[S, E, P]{
stateMachine: b.stateMachine,
}
}
// ExternalParallelTransition starts defining an external parallel transition
// Returns:
//
// A external parallel transition builder for configuring the parallel transition
func (b *StateMachineBuilder[S, E, P]) ExternalParallelTransition() ExternalParallelTransitionBuilderInterface[S, E, P] {
return &ExternalParallelTransitionBuilder[S, E, P]{
stateMachine: b.stateMachine,
}
}
// Build finalizes the state machine with the given ID
// Parameters:
//
// machineId: Unique identifier for the state machine
//
// Returns:
//
// The built state machine and possible error
func (b *StateMachineBuilder[S, E, P]) Build(machineId string) (StateMachine[S, E, P], error) {
b.stateMachine.id = machineId
b.stateMachine.SetReady(true)
// Register the state machine in a factory
err := RegisterStateMachine[S, E, P](machineId, b.stateMachine)
if err != nil {
return nil, err
}
return b.stateMachine, nil
}
// ExternalTransitionsBuilder builds external transitions from multiple source states to a single target state
type ExternalTransitionsBuilder[S comparable, E comparable, P any] struct {
stateMachine *StateMachineImpl[S, E, P]
}
// FromAmong specifies multiple source states
// Parameters:
//
// states: Multiple source states
//
// Returns:
//
// The from builder for method chaining
func (b *ExternalTransitionsBuilder[S, E, P]) FromAmong(states ...S) FromInterface[S, E, P] {
return &FromBuilder[S, E, P, ToStep]{
stateMachine: b.stateMachine,
sourceIds: states,
transitionType: External,
}
}
// ExternalParallelTransitionBuilder builds external parallel transitions
type ExternalParallelTransitionBuilder[S comparable, E comparable, P any] struct {
stateMachine *StateMachineImpl[S, E, P]
}
// From specifies the source state
// Parameters:
//
// state: Source state
//
// Returns:
//
// The parallel from builder for method chaining
func (b *ExternalParallelTransitionBuilder[S, E, P]) From(state S) ParallelFromInterface[S, E, P] {
return &ParallelFromBuilder[S, E, P, ToAmongStep]{
stateMachine: b.stateMachine,
sourceId: state,
transitionType: External,
}
}
// ParallelFromBuilder builds the "from" part of a parallel transition
type ParallelFromBuilder[S comparable, E comparable, P any, Next any] struct {
stateMachine *StateMachineImpl[S, E, P]
transitionType TransitionType
sourceId S
targetIds []S
event E
condition Condition[P]
action Action[S, E, P]
}
// ToAmong specifies multiple target states
// Parameters:
//
// states: Multiple target states
//
// Returns:
//
// The parallel from builder for method chaining
func (b *ParallelFromBuilder[S, E, P, Next]) ToAmong(states ...S) ToInterface[S, E, P] {
b.targetIds = states
return (*ParallelFromBuilder[S, E, P, OnStep])(b)
}
// On specifies the triggering event
// Parameters:
//
// event: The event that triggers these transitions
//
// Returns:
//
// The parallel from builder for method chaining
func (b *ParallelFromBuilder[S, E, P, Next]) On(event E) OnInterface[S, E, P] {
b.event = event
return (*ParallelFromBuilder[S, E, P, WhenStep])(b)
}
// When specifies the condition for all transitions
// Parameters:
//
// condition: The condition that must be satisfied for the transitions to occur
//
// Returns:
//
// The parallel from builder for method chaining
func (b *ParallelFromBuilder[S, E, P, Next]) When(condition Condition[P]) WhenInterface[S, E, P] {
b.condition = condition
return (*ParallelFromBuilder[S, E, P, PerformStep])(b)
}
// WhenFunc specifies a function as the condition for all transitions
// Parameters:
//
// conditionFunc: The function that must return true for the transitions to occur
//
// Returns:
//
// The parallel from builder for method chaining
func (b *ParallelFromBuilder[S, E, P, Next]) WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P] {
b.condition = ConditionFunc[P](conditionFunc)
return (*ParallelFromBuilder[S, E, P, PerformStep])(b)
}
// Perform specifies the action to execute during all transitions
// Parameters:
//
// action: The action to execute when the transitions occur
func (b *ParallelFromBuilder[S, E, P, Next]) Perform(action Action[S, E, P]) {
b.action = action
// Get or create source state
sourceState := b.stateMachine.GetState(b.sourceId)
// Create transitions to all target states
for _, targetId := range b.targetIds {
targetState := b.stateMachine.GetState(targetId)
transition := sourceState.AddTransition(b.event, targetState, b.transitionType)
transition.Condition = b.condition
transition.Action = b.action
}
}
// PerformFunc specifies a function as the action to execute during all transitions
// Parameters:
//
// actionFunc: The function to execute when the transitions occur
func (b *ParallelFromBuilder[S, E, P, Next]) PerformFunc(actionFunc func(from, to S, event E, payload P) error) {
b.action = ActionFunc[S, E, P](actionFunc)
// Get or create source state
sourceState := b.stateMachine.GetState(b.sourceId)
// Create transitions to all target states
for _, targetId := range b.targetIds {
targetState := b.stateMachine.GetState(targetId)
transition := sourceState.AddTransition(b.event, targetState, b.transitionType)
transition.Condition = b.condition
transition.Action = b.action
}
}
// TransitionBuilder builds individual transitions
type TransitionBuilder[S comparable, E comparable, P any, Next any] struct {
stateMachine *StateMachineImpl[S, E, P]
transitionType TransitionType
sourceId S
targetId S
event E
condition Condition[P]
action Action[S, E, P]
}
// From specifies the source state
// Parameters:
//
// state: Source state
//
// Returns:
//
// The transition builder for method chaining
func (b *TransitionBuilder[S, E, P, Next]) From(state S) FromInterface[S, E, P] {
b.sourceId = state
return (*TransitionBuilder[S, E, P, ToStep])(b)
}
// To specifies the target state
// Parameters:
//
// state: Target state
//
// Returns:
//
// The transition builder for method chaining
func (b *TransitionBuilder[S, E, P, Next]) To(state S) ToInterface[S, E, P] {
b.targetId = state
return (*TransitionBuilder[S, E, P, OnStep])(b)
}
// On specifies the triggering event
// Parameters:
//
// event: The event that triggers this transition
//
// Returns:
//
// The transition builder for method chaining
func (b *TransitionBuilder[S, E, P, Next]) On(event E) OnInterface[S, E, P] {
b.event = event
return (*TransitionBuilder[S, E, P, WhenStep])(b)
}
// When specifies the condition for the transition
// Parameters:
//
// condition: The condition that must be satisfied for the transition to occur
//
// Returns:
//
// The transition builder for method chaining
func (b *TransitionBuilder[S, E, P, Next]) When(condition Condition[P]) WhenInterface[S, E, P] {
b.condition = condition
return (*TransitionBuilder[S, E, P, PerformStep])(b)
}
// WhenFunc specifies a function as the condition for the transition
// Parameters:
//
// conditionFunc: The function that must return true for the transition to occur
//
// Returns:
//
// The transition builder for method chaining
func (b *TransitionBuilder[S, E, P, Next]) WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P] {
b.condition = ConditionFunc[P](conditionFunc)
return (*TransitionBuilder[S, E, P, PerformStep])(b)
}
// Perform specifies the action to execute during the transition
// Parameters:
//
// action: The action to execute when the transition occurs
func (b *TransitionBuilder[S, E, P, Next]) Perform(action Action[S, E, P]) {
b.action = action
// Get or create states
sourceState := b.stateMachine.GetState(b.sourceId)
targetState := b.stateMachine.GetState(b.targetId)
// Create transition
transition := sourceState.AddTransition(b.event, targetState, b.transitionType)
transition.Condition = b.condition
transition.Action = b.action
}
// PerformFunc specifies a function as the action to execute during the transition
// Parameters:
//
// actionFunc: The function to execute when the transition occurs
func (b *TransitionBuilder[S, E, P, Next]) PerformFunc(actionFunc func(from, to S, event E, payload P) error) {
b.action = ActionFunc[S, E, P](actionFunc)
// Get or create states
sourceState := b.stateMachine.GetState(b.sourceId)
targetState := b.stateMachine.GetState(b.targetId)
// Create transition
transition := sourceState.AddTransition(b.event, targetState, b.transitionType)
transition.Condition = b.condition
transition.Action = b.action
}
// FromBuilder builds the "from" part of multiple transitions
type FromBuilder[S comparable, E comparable, P any, Next any] struct {
stateMachine *StateMachineImpl[S, E, P]
transitionType TransitionType
sourceIds []S
targetId S
event E
condition Condition[P]
action Action[S, E, P]
}
// To specifies the target state
// Parameters:
//
// state: Target state
//
// Returns:
//
// The from builder for method chaining
func (b *FromBuilder[S, E, P, Next]) To(state S) ToInterface[S, E, P] {
b.targetId = state
return (*FromBuilder[S, E, P, OnStep])(b)
}
// On specifies the triggering event
// Parameters:
//
// event: The event that triggers these transitions
//
// Returns:
//
// The from builder for method chaining
func (b *FromBuilder[S, E, P, Next]) On(event E) OnInterface[S, E, P] {
b.event = event
return (*FromBuilder[S, E, P, WhenStep])(b)
}
// When specifies the condition for all transitions
// Parameters:
//
// condition: The condition that must be satisfied for the transitions to occur
//
// Returns:
//
// The from builder for method chaining
func (b *FromBuilder[S, E, P, Next]) When(condition Condition[P]) WhenInterface[S, E, P] {
b.condition = condition
return (*FromBuilder[S, E, P, PerformStep])(b)
}
// WhenFunc specifies a function as the condition for all transitions
// Parameters:
//
// conditionFunc: The function that must return true for the transitions to occur
//
// Returns:
//
// The from builder for method chaining
func (b *FromBuilder[S, E, P, Next]) WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P] {
b.condition = ConditionFunc[P](conditionFunc)
return (*FromBuilder[S, E, P, PerformStep])(b)
}
// Perform specifies the action to execute during all transitions
// Parameters:
//
// action: The action to execute when the transitions occur
func (b *FromBuilder[S, E, P, Next]) Perform(action Action[S, E, P]) {
b.action = action
// Get or create target state
targetState := b.stateMachine.GetState(b.targetId)
// Create transitions from all source states
for _, sourceId := range b.sourceIds {
sourceState := b.stateMachine.GetState(sourceId)
transition := sourceState.AddTransition(b.event, targetState, b.transitionType)
transition.Condition = b.condition
transition.Action = b.action
}
}
// PerformFunc specifies a function as the action to execute during all transitions
// Parameters:
//
// actionFunc: The function to execute when the transitions occur
func (b *FromBuilder[S, E, P, Next]) PerformFunc(actionFunc func(from, to S, event E, payload P) error) {
b.action = ActionFunc[S, E, P](actionFunc)
// Get or create target state
targetState := b.stateMachine.GetState(b.targetId)
// Create transitions from all source states
for _, sourceId := range b.sourceIds {
sourceState := b.stateMachine.GetState(sourceId)
transition := sourceState.AddTransition(b.event, targetState, b.transitionType)
transition.Condition = b.condition
transition.Action = b.action
}
}
// InternalTransitionBuilder builds internal transitions
type InternalTransitionBuilder[S comparable, E comparable, P any] struct {
stateMachine *StateMachineImpl[S, E, P]
}
// Within specifies the state where the internal transition occurs
// Parameters:
//
// state: The state where the internal transition occurs
//
// Returns:
//
// The internal transition builder for method chaining
func (b *InternalTransitionBuilder[S, E, P]) Within(state S) ToInterface[S, E, P] {
return &OnTransitionBuilder[S, E, P, OnStep]{
stateMachine: b.stateMachine,
stateId: state,
transitionType: Internal,
}
}
// OnTransitionBuilder builds the "on" part of an internal transition
type OnTransitionBuilder[S comparable, E comparable, P any, Next any] struct {
stateMachine *StateMachineImpl[S, E, P]
stateId S
event E
condition Condition[P]
action Action[S, E, P]
transitionType TransitionType
}
// To specifies the target state
// Parameters:
//
// state: Target state
//
// Returns:
//
// The on transition builder for method chaining
func (b *OnTransitionBuilder[S, E, P, Next]) To(state S) ToInterface[S, E, P] {
// Currently not used
return nil
}
// On specifies the triggering event
// Parameters:
//
// event: The event that triggers this transition
//
// Returns:
//
// The on transition builder for method chaining
func (b *OnTransitionBuilder[S, E, P, Next]) On(event E) OnInterface[S, E, P] {
b.event = event
return (*OnTransitionBuilder[S, E, P, WhenStep])(b)
}
// When specifies the condition for the transition
// Parameters:
//
// condition: The condition that must be satisfied for the transition to occur
//
// Returns:
//
// The on transition builder for method chaining
func (b *OnTransitionBuilder[S, E, P, Next]) When(condition Condition[P]) WhenInterface[S, E, P] {
b.condition = condition
return (*OnTransitionBuilder[S, E, P, PerformStep])(b)
}
// WhenFunc specifies a function as the condition for the transition
// Parameters:
//
// conditionFunc: The function that must return true for the transition to occur
//
// Returns:
//
// The on transition builder for method chaining
func (b *OnTransitionBuilder[S, E, P, Next]) WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P] {
b.condition = ConditionFunc[P](conditionFunc)
return (*OnTransitionBuilder[S, E, P, PerformStep])(b)
}
// Perform specifies the action to execute during the transition
// Parameters:
//
// action: The action to execute when the transition occurs
func (b *OnTransitionBuilder[S, E, P, Next]) Perform(action Action[S, E, P]) {
b.action = action
// Get or create state
state := b.stateMachine.GetState(b.stateId)
// Create internal transition
transition := state.AddTransition(b.event, state, b.transitionType)
transition.Condition = b.condition
transition.Action = b.action
}
// PerformFunc specifies a function as the action to execute during the transition
// Parameters:
//
// actionFunc: The function to execute when the transition occurs
func (b *OnTransitionBuilder[S, E, P, Next]) PerformFunc(actionFunc func(from, to S, event E, payload P) error) {
b.action = ActionFunc[S, E, P](actionFunc)
// Get or create state
state := b.stateMachine.GetState(b.stateId)
// Create internal transition
transition := state.AddTransition(b.event, state, b.transitionType)
transition.Condition = b.condition
transition.Action = b.action
}