-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrelations_playground.go
145 lines (128 loc) · 2.77 KB
/
relations_playground.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
package main
import am "github.com/pancsta/asyncmachine-go/pkg/machine"
const log = am.LogOps
func init() {
// am-dbg is required for debugging, go run it
// import amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
// go run github.com/pancsta/asyncmachine-go/tools/cmd/am-dbg@latest
// amhelp.EnableDebugging(false)
// amhelp.SetLogLevel(am.LogChanges)
}
func main() {
FooBar()
FileProcessed()
DryWaterWet()
RemoveByAdd()
AddOptionalRemoveMandatory()
Mutex()
Quiz()
}
func FooBar() {
mach := newMach("FooBar", am.Struct{
"Foo": {Require: am.S{"Bar"}},
"Bar": {},
})
mach.Add1("Foo", nil)
// TODO quiz: is Foo active?
}
func FileProcessed() {
mach := newMach("FileProcessed", am.Struct{
"ProcessingFile": { // async
Remove: am.S{"FileProcessed"},
},
"FileProcessed": { // async
Remove: am.S{"ProcessingFile"},
},
"InProgress": { // sync
Auto: true,
Require: am.S{"ProcessingFile"},
},
})
mach.Add1("ProcessingFile", nil)
// TODO quiz: is InProgress active?
mach.Add1("FileProcessed", nil)
}
func DryWaterWet() {
mach := newMach("DryWaterWet", am.Struct{
"Wet": {
Require: am.S{"Water"},
},
"Dry": {
Remove: am.S{"Water"},
},
"Water": {
Add: am.S{"Wet"},
Remove: am.S{"Dry"},
},
})
mach.Add1("Dry", nil)
mach.Add1("Water", nil)
mach.Add1("Dry", nil)
// TODO quiz: is Wet active?
}
func RemoveByAdd() {
mach := newMach("RemoveByNonCalled", am.Struct{
"A": {Add: am.S{"B"}},
"B": {Remove: am.S{"C"}},
"C": {},
})
mach.Add1("C", nil)
mach.Add1("A", nil)
// TODO quiz: is C active?
}
func AddOptionalRemoveMandatory() {
mach := newMach("AddIsOptional", am.Struct{
"A": {Add: am.S{"B"}},
"B": {},
"C": {Remove: am.S{"B"}},
})
mach.Add(am.S{"A", "C"}, nil)
// TODO quiz: is B active?
}
func Mutex() {
mach := newMach("Mutex", am.Struct{
"A": {Remove: am.S{"A", "B", "C"}},
"B": {Remove: am.S{"A", "B", "C"}},
"C": {Remove: am.S{"A", "B", "C"}},
})
mach.Add1("A", nil)
mach.Add1("B", nil)
mach.Add1("C", nil)
// TODO quiz: which one is active?
}
func Quiz() {
mach := newMach("Quiz", am.Struct{
"A": {Add: am.S{"B"}},
"B": {
Require: am.S{"D"},
Add: am.S{"C"},
},
"C": {},
"D": {Remove: am.S{"C"}},
"E": {Add: am.S{"D"}},
})
mach.Add(am.S{"A", "E"}, nil)
// TODO quiz: which one is active?
}
// playground helpers
func newMach(id string, machStruct am.Struct) *am.Machine {
mach := am.New(nil, machStruct, &am.Opts{
ID: id,
DontLogID: true,
Tracers: []am.Tracer{&Tracer{}},
LogLevel: log,
})
println("\n")
println("-----")
println("mach: " + mach.Id())
println("-----")
// DEBUG
// amhelp.MachDebugEnv(mach)
return mach
}
type Tracer struct {
*am.NoOpTracer
}
func (t *Tracer) TransitionEnd(tx *am.Transition) {
println("=> " + tx.Machine.String())
}