-
Notifications
You must be signed in to change notification settings - Fork 127
/
components_for_test.go
217 lines (181 loc) · 3.59 KB
/
components_for_test.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
package goflow
import (
"sync"
)
// doubler doubles its input.
type doubler struct {
In <-chan int
Out chan<- int
}
func (c *doubler) Process() {
for i := range c.In {
c.Out <- 2 * i
}
}
// doubleOnce is a non-resident version of doubler.
type doubleOnce struct {
In <-chan int
Out chan<- int
}
func (c *doubleOnce) Process() {
i := <-c.In
c.Out <- 2 * i
}
// A component with two inputs and one output.
type adder struct {
Op1 <-chan int
Op2 <-chan int
Sum chan<- int
}
func (c *adder) Process() {
op1Buf := make([]int, 0, 10)
op2Buf := make([]int, 0, 10)
addOp := func(op int, buf, otherBuf *[]int) {
if len(*otherBuf) > 0 {
otherOp := (*otherBuf)[0]
*otherBuf = (*otherBuf)[1:]
c.Sum <- (op + otherOp)
} else {
*buf = append(*buf, op)
}
}
for c.Op1 != nil || c.Op2 != nil {
select {
case op1, ok := <-c.Op1:
if !ok {
c.Op1 = nil
break
}
addOp(op1, &op1Buf, &op2Buf)
case op2, ok := <-c.Op2:
if !ok {
c.Op2 = nil
break
}
addOp(op2, &op2Buf, &op1Buf)
}
}
}
// echo passes input to the output.
type echo struct {
In <-chan int
Out chan<- int
}
func (c *echo) Process() {
for i := range c.In {
c.Out <- i
}
}
// repeater repeats an input string a given number of times.
type repeater struct {
Word <-chan string
Times <-chan int
Words chan<- string
}
func (c *repeater) Process() {
times := 0
word := ""
for c.Times != nil || c.Word != nil {
select {
case t, ok := <-c.Times:
if !ok {
c.Times = nil
break
}
times = t
c.repeat(word, times)
case w, ok := <-c.Word:
if !ok {
c.Word = nil
break
}
word = w
c.repeat(word, times)
}
}
}
func (c *repeater) repeat(word string, times int) {
if word == "" || times <= 0 {
return
}
for i := 0; i < times; i++ {
c.Words <- word
}
}
// router routes input map port to output.
type router struct {
In map[string]<-chan int
Out map[string]chan<- int
}
// Process routes incoming packets to the output by sending them to the same
// outport key as the inport key they arrived at.
func (c *router) Process() {
wg := new(sync.WaitGroup)
for k, ch := range c.In {
k := k
ch := ch
wg.Add(1)
go func() {
for n := range ch {
c.Out[k] <- n
}
close(c.Out[k])
wg.Done()
}()
}
wg.Wait()
}
// irouter routes input array port to output.
type irouter struct {
In [](<-chan int)
Out [](chan<- int)
}
// Process routes incoming packets to the output by sending them to the same
// outport key as the inport key they arrived at.
func (c *irouter) Process() {
wg := new(sync.WaitGroup)
for k, ch := range c.In {
k := k
ch := ch
wg.Add(1)
go func() {
for n := range ch {
c.Out[k] <- n
}
close(c.Out[k])
wg.Done()
}()
}
wg.Wait()
}
func RegisterTestComponents(f *Factory) error {
f.Register("echo", func() (interface{}, error) {
return new(echo), nil
})
f.Annotate("echo", Annotation{
Description: "Passes an int from in to out without changing it",
Icon: "arrow-right",
})
f.Register("doubler", func() (interface{}, error) {
return new(doubler), nil
})
f.Annotate("doubler", Annotation{
Description: "Doubles its input",
Icon: "times-circle",
})
f.Register("repeater", func() (interface{}, error) {
return new(repeater), nil
})
f.Annotate("repeater", Annotation{
Description: "Repeats Word given numer of Times",
Icon: "times-circle",
})
f.Register("adder", func() (interface{}, error) {
return new(adder), nil
})
f.Annotate("adder", Annotation{
Description: "Sums integers coming to its inports",
Icon: "plus-circle",
})
return nil
}