forked from peterbourgon/goop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
328 lines (272 loc) · 6.72 KB
/
field.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 main
import (
"fmt"
)
type Field map[string]Node
func NewField() Field {
return map[string]Node{}
}
func (f Field) Add(n Node) error {
defer writeDotfile(f)
name := n.Name()
if n, _ := f.Get(name); n != nil {
return fmt.Errorf("already exists")
}
f[name] = n
return nil
}
func (f Field) Get(name string) (Node, error) {
if n, ok := f[name]; ok {
return n, nil
}
return nil, fmt.Errorf("not found")
}
func (f Field) Delete(name string) error {
defer writeDotfile(f)
n, err := f.Get(name)
if err != nil {
return fmt.Errorf("not found")
}
for _, parent := range n.Parents() {
if err := f.Disconnect(parent.Name(), name); err != nil {
panic(fmt.Errorf("delete(%s): %s", name, err))
}
}
for _, child := range n.Children() {
if err := f.Disconnect(name, child.Name()); err != nil {
panic(fmt.Errorf("delete(%s): %s", name, err))
}
}
n.Events() <- KillEvent()
delete(f, name)
return nil
}
func (f Field) Connect(src, dst string) error {
D("Connect(%s, %s)", src, dst)
defer writeDotfile(f)
parent, err := f.Get(src)
if err != nil {
return err
}
child, err := f.Get(dst)
if err != nil {
return err
}
if reachable(child, parent) {
return fmt.Errorf("cycle detected")
}
parent.Events() <- ConnectEvent(child)
child.Events() <- ConnectionEvent(parent)
return nil
}
func (f Field) Disconnect(src, dst string) error {
defer writeDotfile(f)
parent, err := f.Get(src)
if err != nil {
return err
}
var child Node
for _, n := range parent.Children() {
if n.Name() == dst {
child = n
break
}
}
if child == nil {
return fmt.Errorf("'%s' not a child of '%s'", dst, src)
}
parent.Events() <- DisconnectEvent(child)
child.Events() <- DisconnectionEvent(parent)
return nil
}
func (f Field) DisconnectAll(src string) error {
defer writeDotfile(f)
parent, err := f.Get(src)
if err != nil {
return err
}
for _, child := range parent.Children() {
parent.Events() <- DisconnectEvent(child)
child.Events() <- DisconnectionEvent(parent)
}
return nil
}
func (f Field) Broadcast(ev Event) {
for _, n := range f {
n.Events() <- ev
}
}
func (f *Field) Dot() string {
s := "digraph G {\n"
// nodes
for _, n := range *f {
s += fmt.Sprintf(
"\t%s [shape=box,label=\"%s\"];\n",
n.Name(),
NodeLabel(n),
//n,
)
}
s += "\n"
// edges
for _, n := range *f {
D("Dot: adding edges for %d children of %s", len(n.Children()), n.Name())
for _, child := range n.Children() {
s += fmt.Sprintf("\t%s -> %s;\n", n.Name(), child.Name())
}
}
s += "}"
return s
}
//
//
//
type Node interface {
Name() string
Parents() []Node
Children() []Node
EventReceiver
}
var nilNode Node
type Typed interface {
Kind() string
}
func NodeLabel(n Node) string {
if typed, ok := n.(Typed); ok {
return fmt.Sprintf("%s '%s'", typed.Kind(), n.Name())
}
return n.Name()
}
func reachable(n, tgt Node) bool {
D("reachable(\n\t%6s %v,\n\t%6s %v\n)", n.Name(), n, tgt.Name(), tgt)
if n == tgt {
D(" reachable because %v == %v", n, tgt)
return true
}
for i, child := range n.Children() {
if child == n {
D(" reachable because %s Child[%d] == %v", n.Name(), i, tgt)
return true
}
if reachable(child, tgt) {
D(" recursive return reachable")
return true
}
}
D(" not reachable!")
return false
}
// A nodeName may be embedded into any type to satisfy
// the Name() method of the Node interface.
type nodeName string
func (nn nodeName) Name() string { return string(nn) }
//
//
//
// singleParent may be embedded into any type to satisfy
// the Parents() method of the Node interface, with arity=1.
//
// To set, do myStruct.ParentNode = n.
// To clear, do myStruct.ParentNode = nilNode.
type singleParent struct{ ParentNode Node }
func (sp singleParent) Parents() []Node {
if sp.ParentNode == nilNode {
return []Node{}
}
return []Node{sp.ParentNode}
}
func (sp *singleParent) processEvent(ev Event, container Node) {
switch ev.Type {
case Connection: // upstream
node, nodeOk := ev.Arg.(Node)
if !nodeOk {
break
}
if sp.ParentNode != nilNode {
// This means x=>a, y=>a has been executed without an intermediate
// x≠>a. Since we have only one parent, the original necessarily
// must be disconnected.
sp.ParentNode.Events() <- DisconnectEvent(container)
}
sp.ParentNode = node
case Disconnection: // upstream
sp.ParentNode = nilNode
}
}
// singleChild may be embedded into any type to satisfy
// the Children() method of the Node interface, with arity=1.
//
// To set, do myStruct.ChildNode = n.
// To clear, do myStruct.ChildNode = nilNode.
type singleChild struct{ ChildNode Node }
func (sc singleChild) Children() []Node {
if sc.ChildNode == nilNode {
return []Node{}
}
return []Node{sc.ChildNode}
}
func (sc *singleChild) processEvent(ev Event, container Node) {
switch ev.Type {
case Connect: // downstream
node, nodeOk := ev.Arg.(Node)
if !nodeOk {
break
}
if sc.ChildNode != nilNode {
// This means a=>b, a=>c has been executed without an intermediate
// a≠>b. Since we have only one child, the original necessarily
// must be disconnected.
sc.ChildNode.Events() <- DisconnectionEvent(container)
}
sc.ChildNode = node
case Disconnect: // downstream
sc.ChildNode = nilNode // TODO could do more thorough checking
}
}
// singleAncestry combines singleParent + singleChild.
// It should be embedded into a concrete struct.
// It requires no explicit initialization.
type singleAncestry struct {
singleParent
singleChild
}
func (sa *singleAncestry) processEvent(ev Event, container Node) {
switch ev.Type {
case Connect, Disconnect: // downstream
sa.singleChild.processEvent(ev, container)
case Connection, Disconnection: // upstream
sa.singleParent.processEvent(ev, container)
case Kill:
sa.singleChild.ChildNode = nilNode
sa.singleParent.ParentNode = nilNode
}
}
// multipleParents may be embedded into any type to satisfy
// the Parents() method of the Node interface, with arity=N.
type multipleParents struct{ m map[string]Node }
func newMultipleParents() *multipleParents {
return &multipleParents{
m: map[string]Node{},
}
}
func (mp *multipleParents) Parents() []Node {
parents := []Node{}
for _, n := range mp.m {
parents = append(parents, n)
}
return parents
}
func (mp *multipleParents) AddParent(n Node) {
mp.m[n.Name()] = n
}
func (mp *multipleParents) DeleteParent(name string) {
delete(mp.m, name)
}
// noParents may be embedded into any type to satisfy
// the Parents() method of the Node interface, with arity=0.
type noParents struct{}
func (np noParents) Parents() []Node { return []Node{} }
// noChildren may be embedded into any type to satisfy
// the Children() method of the Node interface, with arity=0.
type noChildren struct{}
func (nc noChildren) Children() []Node { return []Node{} }