rachinations 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -16
  3. data/Gemfile.lock +2 -0
  4. data/lib/rachinations/domain/diagrams/diagram.rb +46 -14
  5. data/lib/rachinations/domain/edges/edge.rb +34 -20
  6. data/lib/rachinations/domain/modules/common/hash_init.rb +2 -1
  7. data/lib/rachinations/domain/modules/common/invariant.rb +2 -2
  8. data/lib/rachinations/domain/modules/common/refiners/number_modifiers.rb +22 -0
  9. data/lib/rachinations/domain/nodes/converter.rb +5 -5
  10. data/lib/rachinations/domain/nodes/gate.rb +77 -0
  11. data/lib/rachinations/domain/nodes/node.rb +69 -36
  12. data/lib/rachinations/domain/nodes/pool.rb +121 -74
  13. data/lib/rachinations/domain/nodes/resourceful_node.rb +0 -1
  14. data/lib/rachinations/domain/nodes/sink.rb +3 -0
  15. data/lib/rachinations/domain/nodes/source.rb +3 -2
  16. data/lib/rachinations/domain/resource_bag.rb +3 -4
  17. data/lib/rachinations/dsl/bad_dsl.rb +2 -0
  18. data/lib/rachinations/dsl/bootstrap.rb +59 -0
  19. data/lib/rachinations/dsl/diagram_shorthand_methods.rb +107 -0
  20. data/lib/rachinations/dsl/helpers/parser.rb +170 -0
  21. data/lib/rachinations/extras/constant_hash.rb +25 -0
  22. data/lib/rachinations/extras/fifo.rb +25 -19
  23. data/lib/rachinations/helpers/edge_helper.rb +40 -0
  24. data/lib/rachinations/utils/string_helper.rb +7 -0
  25. data/lib/rachinations/version.rb +1 -1
  26. data/lib/rachinations.rb +13 -5
  27. data/rachinations.gemspec +3 -2
  28. data/testing/simulations/modelo1.rb +1 -1
  29. data/testing/simulations/sequencial.rb +1 -1
  30. data/testing/simulations/sobonito.rb +1 -1
  31. data/testing/simulations/sobonitowhile.rb +1 -1
  32. data/testing/simulations/whatIwish1.rb +2 -2
  33. data/testing/spec/canon/conditions_spec.rb +3 -4
  34. data/testing/spec/converter_spec.rb +3 -4
  35. data/testing/spec/diagram_spec.rb +293 -238
  36. data/testing/spec/edge_spec.rb +28 -14
  37. data/testing/spec/gate_spec.rb +34 -0
  38. data/testing/spec/pool_spec.rb +8 -10
  39. data/testing/spec/release1/dsl_spec.rb +204 -0
  40. data/testing/spec/spec_helper.rb +1 -0
  41. data/testing/spec/xexeo_spec.rb +39 -40
  42. metadata +30 -8
  43. data/lib/rachinations/domain/edges/random_edge.rb +0 -4
  44. data/lib/rachinations/dsl/dsl.rb +0 -63
  45. data/testing/spec/canon/trigger_spec.rb +0 -128
@@ -1,404 +1,407 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe Diagram do
4
+ using NumberModifiers
5
+ using ProcConvenienceMethods
4
6
 
5
- it 'can be empty' do
6
- d = Diagram.new 'empty'
7
- expect(d.name).to eq 'empty'
8
- end
9
7
 
10
- it 'should be created with a source and a pool and run n times with no errors' do
8
+ context 'integration' do
11
9
 
12
- d=Diagram.new 'simple'
10
+ it 'can be empty' do
11
+ d = Diagram.new 'empty'
12
+ expect(d.name).to eq 'empty'
13
+ end
13
14
 
14
- d.add_node! Source, {
15
- :name => 'source'
16
- }
15
+ it 'should be created with a source and a pool and run n times with no errors' do
17
16
 
18
- d.add_node! Pool, {
19
- :name => 'deposit',
20
- :initial_value => 0
21
- }
17
+ d=Diagram.new 'simple'
22
18
 
23
- d.add_edge! Edge, {
24
- :name => 'connector',
25
- :from => 'source',
26
- :to => 'deposit'
27
- }
19
+ d.add_node! Source, {
20
+ :name => 'source'
21
+ }
28
22
 
29
- d.run!(10)
23
+ d.add_node! Pool, {
24
+ :name => 'deposit',
25
+ :initial_value => 0
26
+ }
30
27
 
31
- expect(d.resource_count).to eq 10
32
- expect(d.get_node('deposit').resource_count).to eq 10
28
+ d.add_edge! Edge, {
29
+ :name => 'connector',
30
+ :from => 'source',
31
+ :to => 'deposit'
32
+ }
33
33
 
34
- end
34
+ d.run!(10)
35
35
 
36
- it "runs for 2 turns with two pools using PULL and there's the correct amount of resources at the end" do
36
+ expect(d.resource_count).to eq 10
37
+ expect(d.get_node('deposit').resource_count).to eq 10
37
38
 
38
- d = Diagram.new 'some_name'
39
+ end
39
40
 
40
- d.add_node! Pool, name: 'pool1', initial_value: 5
41
+ it "runs for 2 turns with two pools using PULL and there's the correct amount of resources at the end" do
41
42
 
42
- d.add_node! Pool, name: 'pool2', activation: :automatic
43
+ d = Diagram.new 'some_name'
43
44
 
44
- d.add_edge! Edge, name: 'edge', from: 'pool1', to: 'pool2'
45
+ d.add_node! Pool, name: 'pool1', initial_value: 5
45
46
 
46
- d.run!(2)
47
+ d.add_node! Pool, name: 'pool2', activation: :automatic
47
48
 
48
- expect(d.get_node('pool1').resource_count).to eq 3
49
- expect(d.get_node('pool2').resource_count).to eq 2
49
+ d.add_edge! Edge, name: 'edge', from: 'pool1', to: 'pool2'
50
50
 
51
- end
51
+ d.run!(2)
52
52
 
53
- it "runs for 2 turns with two pools using PULL and add the correct amount" do
53
+ expect(d.get_node('pool1').resource_count).to eq 3
54
+ expect(d.get_node('pool2').resource_count).to eq 2
54
55
 
55
- d = Diagram.new 'some_name'
56
+ end
56
57
 
57
- d.add_node! Pool, name: 'pool1', initial_value: 5
58
+ it "runs for 2 turns with two pools using PULL and add the correct amount" do
58
59
 
59
- d.add_node! Pool, name: 'pool2', activation: :automatic
60
+ d = Diagram.new
60
61
 
61
- d.add_edge! Edge, name: 'edge', from: 'pool1', to: 'pool2'
62
+ d.add_node! Pool, name: 'pool1', initial_value: 5
62
63
 
63
- d.run!(2)
64
+ d.add_node! Pool, name: 'pool2', activation: :automatic
64
65
 
65
- expect(d.get_node('pool1').resources_added).to eq 0
66
- expect(d.get_node('pool2').resources_added).to eq 2
66
+ d.add_edge! Edge, name: 'edge', from: 'pool1', to: 'pool2'
67
67
 
68
- expect(d.get_node('pool2').resources_removed).to eq 0
69
- expect(d.get_node('pool1').resources_removed).to eq 2
68
+ d.run!(2)
70
69
 
71
- end
70
+ expect(d.get_node('pool1').resources_added).to eq 0
71
+ expect(d.get_node('pool2').resources_added).to eq 2
72
+ expect(d.get_node('pool2').resources_removed).to eq 0
73
+ expect(d.get_node('pool1').resources_removed).to eq 2
72
74
 
73
- it "runs for 2 turns with source and pool using PULL and add and remove the correct amount" do
75
+ end
74
76
 
75
- d = Diagram.new 'some_name'
77
+ it "runs for 2 turns with source and pool using PULL and add and remove the correct amount" do
76
78
 
77
- d.add_node! Source, name: 's1', activation: :automatic
79
+ d = Diagram.new 'some_name'
78
80
 
79
- d.add_node! Pool, name: 'pool2'
81
+ d.add_node! Source, name: 's1', activation: :automatic
80
82
 
81
- d.add_edge! Edge, name: 'edge', from: 's1', to: 'pool2'
83
+ d.add_node! Pool, name: 'pool2'
82
84
 
83
- d.run!(2)
85
+ d.add_edge! Edge, name: 'edge', from: 's1', to: 'pool2'
84
86
 
87
+ d.run!(2)
85
88
 
86
- expect(d.get_node('s1').resources_added).to eq 0
87
- expect(d.get_node('pool2').resources_added).to eq 2
88
- expect(d.get_node('pool2').resources_removed).to eq 0
89
- expect(d.get_node('s1').resources_removed).to eq 2
90
89
 
91
- end
90
+ expect(d.get_node('s1').resources_added).to eq 0
91
+ expect(d.get_node('pool2').resources_added).to eq 2
92
+ expect(d.get_node('pool2').resources_removed).to eq 0
93
+ expect(d.get_node('s1').resources_removed).to eq 2
92
94
 
93
- it "runs for two turns with two pools using PUSH and there's the correct amount of resources at the end" do
95
+ end
94
96
 
95
- d = Diagram.new 'some_name'
97
+ it "runs for two turns with two pools using PUSH and there's the correct amount of resources at the end" do
96
98
 
97
- d.add_node! Pool, name: 'pool1', initial_value: 5, mode: :push_any, activation: :automatic
99
+ d = Diagram.new 'some_name'
98
100
 
99
- d.add_node! Pool, name: 'pool2'
101
+ d.add_node! Pool, name: 'pool1', initial_value: 5, mode: :push_any, activation: :automatic
100
102
 
101
- d.add_edge! Edge, name: 'edge', from: 'pool1', to: 'pool2'
103
+ d.add_node! Pool, name: 'pool2'
102
104
 
103
- d.run!(2)
105
+ d.add_edge! Edge, name: 'edge', from: 'pool1', to: 'pool2'
104
106
 
105
- expect(d.get_node('pool1').resource_count).to eq 3
106
- expect(d.get_node('pool2').resource_count).to eq 2
107
+ d.run!(2)
107
108
 
108
- end
109
+ expect(d.get_node('pool1').resource_count).to eq 3
110
+ expect(d.get_node('pool2').resource_count).to eq 2
109
111
 
110
- it "runs for a single turn with one source and two pools and there's the correct amount of resources at the end" do
111
- p = Diagram.new('one source two pools')
112
+ end
112
113
 
113
- p.add_node!(Source, {name: 'source'})
114
+ it "runs for a single turn with one source and two pools and there's the correct amount of resources at the end" do
115
+ p = Diagram.new('one source two pools')
114
116
 
115
- p.add_node!(Pool, {name: 'pool1'})
117
+ p.add_node!(Source, {name: 'source'})
116
118
 
117
- p.add_node!(Pool, {name: 'pool2', activation: :automatic})
119
+ p.add_node!(Pool, {name: 'pool1'})
118
120
 
119
- p.add_edge!(Edge, {name: 'edge1', from: 'source', to: 'pool1'})
121
+ p.add_node!(Pool, {name: 'pool2', activation: :automatic})
120
122
 
121
- p.add_edge!(Edge, {name: 'connector2', from: 'pool1', to: 'pool2'})
123
+ p.add_edge!(Edge, {name: 'edge1', from: 'source', to: 'pool1'})
122
124
 
123
- p.run!(1)
125
+ p.add_edge!(Edge, {name: 'connector2', from: 'pool1', to: 'pool2'})
124
126
 
125
- expect(p.get_node('pool1').resource_count).to eq 1
126
- expect(p.get_node('pool2').resource_count).to eq 0
127
+ p.run!(1)
127
128
 
128
- end
129
+ expect(p.get_node('pool1').resource_count).to eq 1
130
+ expect(p.get_node('pool2').resource_count).to eq 0
129
131
 
130
- it 'takes staging and commit steps into account when run with 3 pools for 1 turn only' do
132
+ end
131
133
 
132
- d = Diagram.new 'some_name'
134
+ it 'takes staging and commit steps into account when run with 3 pools for 1 turn only' do
133
135
 
134
- d.add_node! Pool, name: 'pool1', initial_value: 2, mode: :push_any, activation: :automatic
136
+ d = Diagram.new
135
137
 
136
- d.add_node! Pool, name: 'pool2'
138
+ d.add_node! Pool, name: 'pool1', initial_value: 2, mode: :push_any, activation: :automatic
137
139
 
138
- d.add_node! Pool, name: 'pool3', activation: :automatic
140
+ d.add_node! Pool, name: 'pool2'
139
141
 
140
- d.add_edge! Edge, name: 'edge1', from: 'pool1', to: 'pool2'
142
+ d.add_node! Pool, name: 'pool3', activation: :automatic
141
143
 
142
- d.add_edge! Edge, name: 'edge2', from: 'pool2', to: 'pool3'
144
+ d.add_edge! Edge, name: 'edge1', from: 'pool1', to: 'pool2'
143
145
 
144
- d.run!(1)
146
+ d.add_edge! Edge, name: 'edge2', from: 'pool2', to: 'pool3'
145
147
 
146
- expect(d.get_node('pool1').resource_count).to eq 1
147
- expect(d.get_node('pool2').resource_count).to eq 1
148
- expect(d.get_node('pool3').resource_count).to eq 0
148
+ d.run!(1)
149
149
 
150
- end
150
+ expect(d.get_node('pool1').resource_count).to eq 1
151
+ expect(d.get_node('pool2').resource_count).to eq 1
152
+ expect(d.get_node('pool3').resource_count).to eq 0
151
153
 
152
- it 'takes staging and commit steps into account when run with 3 pools for 4 turns' do
154
+ end
153
155
 
154
- d = Diagram.new 'some_name'
156
+ it 'takes staging and commit steps into account when run with 3 pools for 4 turns' do
155
157
 
156
- d.add_node! Pool, name: 'pool1', initial_value: 10, mode: :push_any, activation: :automatic
158
+ d = Diagram.new
157
159
 
158
- d.add_node! Pool, name: 'pool2'
160
+ d.add_node! Pool, name: 'pool1', initial_value: 10, mode: :push_any, activation: :automatic
159
161
 
160
- d.add_node! Pool, name: 'pool3', activation: :automatic
162
+ d.add_node! Pool, name: 'pool2'
161
163
 
162
- d.add_edge! Edge, name: 'edge1', from: 'pool1', to: 'pool2'
164
+ d.add_node! Pool, name: 'pool3', activation: :automatic
163
165
 
164
- d.add_edge! Edge, name: 'edge2', from: 'pool2', to: 'pool3'
166
+ d.add_edge! Edge, name: 'edge1', from: 'pool1', to: 'pool2'
165
167
 
168
+ d.add_edge! Edge, name: 'edge2', from: 'pool2', to: 'pool3'
166
169
 
167
- d.run!(4)
168
170
 
169
- expect(d.get_node('pool1').resource_count).to eq 6
170
- expect(d.get_node('pool2').resource_count).to eq 1
171
- expect(d.get_node('pool3').resource_count).to eq 3
171
+ d.run!(4)
172
172
 
173
+ expect(d.get_node('pool1').resource_count).to eq 6
174
+ expect(d.get_node('pool2').resource_count).to eq 1
175
+ expect(d.get_node('pool3').resource_count).to eq 3
173
176
 
174
- end
175
177
 
176
- it 'runs with a source and a pool and have the expected amount of resources at the end' do
178
+ end
177
179
 
178
- d=Diagram.new 'simple'
180
+ it 'runs with a source and a pool and have the expected amount of resources at the end' do
179
181
 
182
+ d=Diagram.new
180
183
 
181
- d.add_node! Source, {
182
- :name => 'source'
183
- }
184
184
 
185
- d.add_node! Pool, {
186
- :name => 'deposit',
187
- }
185
+ d.add_node! Source, {
186
+ :name => 'source'
187
+ }
188
188
 
189
- d.add_edge! Edge, {
190
- :name => 'connector',
191
- :from => 'source',
192
- :to => 'deposit'
193
- }
189
+ d.add_node! Pool, {
190
+ :name => 'deposit',
191
+ }
194
192
 
195
- d.run!(10)
193
+ d.add_edge! Edge, {
194
+ :name => 'connector',
195
+ :from => 'source',
196
+ :to => 'deposit'
197
+ }
196
198
 
197
- expect(d.get_node('deposit').resource_count).to eq 10
199
+ d.run!(10)
198
200
 
199
- end
201
+ expect(d.get_node('deposit').resource_count).to eq 10
200
202
 
201
- it 'can be run until a given condition is true' do
202
- d=Diagram.new 'simple'
203
- d.add_node! Pool, {
204
- :name => 'deposit',
205
- :initial_value => 0
206
- }
207
- d.add_node! Source, {
208
- :name => 'source'
209
- }
210
- d.add_edge! Edge, {
211
- :name => 'connector',
212
- :from => 'source',
213
- :to => 'deposit'
214
- }
215
-
216
- d.run_while! { d.get_node('deposit').resource_count < 10 }
217
- expect(d.get_node('deposit').resource_count).to eq 10
203
+ end
218
204
 
219
- end
205
+ it 'can be run until a given condition is true' do
220
206
 
221
- it 'aborts after specified turns as a safeguard against infinite loops given as stopping condition' do
207
+ d=Diagram.new
222
208
 
223
- d=Diagram.new 'simple'
224
- d.max_iterations=9
209
+ d.add_node! Pool, {
210
+ :name => 'deposit',
211
+ :initial_value => 0
212
+ }
213
+ d.add_node! Source, {
214
+ :name => 'source'
215
+ }
216
+ d.add_edge! Edge, {
217
+ :name => 'connector',
218
+ :from => 'source',
219
+ :to => 'deposit'
220
+ }
225
221
 
226
- d.add_node! Pool, {
227
- :name => 'deposit',
228
- :initial_value => 0
229
- }
230
- d.add_node! Source, {
231
- :name => 'source'
232
- }
233
- d.add_edge! Edge, {
234
- :name => 'connector',
235
- :from => 'source',
236
- :to => 'deposit'
237
- }
222
+ d.run_while! { d.get_node('deposit').resource_count < 10 }
223
+ expect(d.get_node('deposit').resource_count).to eq 10
238
224
 
239
- d.run_while! { true == true }
225
+ end
240
226
 
241
- #not hanging on forever is the success condition.
242
- expect(d.get_node('deposit').resource_count).to eq 9
227
+ it 'aborts after specified turns as a safeguard against infinite loops given as stopping condition' do
243
228
 
244
- end
229
+ d=Diagram.new
245
230
 
246
- it 'does not raise errors when active pushes or pulls are not possible' do
231
+ d.max_iterations=9
247
232
 
248
- d = Diagram.new 'no errors'
233
+ d.add_node! Pool, {
234
+ :name => 'deposit',
235
+ :initial_value => 0
236
+ }
237
+ d.add_node! Source, {
238
+ :name => 'source'
239
+ }
240
+ d.add_edge! Edge, {
241
+ :name => 'connector',
242
+ :from => 'source',
243
+ :to => 'deposit'
244
+ }
249
245
 
250
- d.add_node! Pool, name: 'Poor fella', initial_value: 5
246
+ d.run_while! { true == true }
251
247
 
252
- d.add_node! Pool, name: 'Hungry fella', activation: :automatic
248
+ #not hanging on forever is the success condition.
249
+ expect(d.get_node('deposit').resource_count).to eq 9
253
250
 
254
- d.add_edge! Edge, name: 'edge1', from: 'Poor fella', to: 'Hungry fella'
251
+ end
255
252
 
256
- expect { d.run! 10 }.not_to raise_error
253
+ it 'does not raise errors when active pushes or pulls are not possible' do
257
254
 
258
- expect(d.get_node('Hungry fella').resource_count).to eq 5
255
+ d = Diagram.new 'no errors'
259
256
 
260
- end
257
+ d.add_node! Pool, name: 'Poor fella', initial_value: 5
261
258
 
262
- it "raises an error in case users try to access a node that doesn't exist" do
259
+ d.add_node! Pool, name: 'Hungry fella', activation: :automatic
263
260
 
264
- p = Diagram.new('get invalid node')
261
+ d.add_edge! Edge, from: 'Poor fella', to: 'Hungry fella'
265
262
 
266
- p.add_node! Source, name: 'source'
263
+ expect { d.run! 10 }.not_to raise_error
267
264
 
268
- p.add_node! Pool, name: 'pool1'
265
+ expect(d.get_node('Hungry fella').resource_count).to eq 5
269
266
 
270
- p.add_edge! Edge, {
271
- name: 'connector1',
272
- from: 'source',
273
- to: 'pool1'
274
- }
267
+ end
275
268
 
276
- # use curly brackets instead of parentheses here.
277
- expect { p.get_node('pool') }.to raise_error RuntimeError
269
+ it "raises an error in case users try to access a node that doesn't exist" do
278
270
 
279
- end
271
+ p = Diagram.new('get invalid node')
280
272
 
281
- it 'runs with typed nodes connected by typeless edges' do
273
+ p.add_node! Source, name: 'source'
282
274
 
283
- p = Diagram.new('one source one pool typed')
275
+ p.add_node! Pool, name: 'pool1'
284
276
 
285
- p.add_node!(Source, name: 'source', :type => Green)
286
- p.add_node!(Pool, name: 'pool1', :types => [Green, Red])
287
- p.add_edge!(Edge, name: 'connector1', from: 'source', to: 'pool1')
277
+ p.add_edge! Edge, {
278
+ name: 'connector1',
279
+ from: 'source',
280
+ to: 'pool1'
281
+ }
288
282
 
289
- p.run!(5)
283
+ # use curly brackets instead of parentheses here.
284
+ expect { p.get_node('pool') }.to raise_error RuntimeError
290
285
 
291
- expect(p.get_node('pool1').resource_count(Green)).to eq 5
292
- expect(p.get_node('pool1').resource_count(Red)).to eq 0
286
+ end
293
287
 
294
- end
288
+ it 'runs with typed nodes connected by typeless edges' do
295
289
 
296
- it "allows untyped nodes to receive typed resources sent to them via untyped edges" do
290
+ p = Diagram.new('one source one pool typed')
297
291
 
298
- p = Diagram.new 'balls'
292
+ p.add_node!(Source, name: 'source', :type => Green)
293
+ p.add_node!(Pool, name: 'pool1', :types => [Green, Red])
294
+ p.add_edge!(Edge, name: 'connector1', from: 'source', to: 'pool1')
299
295
 
300
- p.add_node!(Source, name: 'source', :type => Football)
296
+ p.run!(5)
301
297
 
302
- p.add_node!(Pool, name: 'pool1')
298
+ expect(p.get_node('pool1').resource_count(type: Green)).to eq 5
299
+ expect(p.get_node('pool1').resource_count(type: Red)).to eq 0
303
300
 
304
- p.add_edge!(Edge, name: 'connector1', from: 'source', to: 'pool1')
301
+ end
305
302
 
306
- p.run!(5)
303
+ it "allows untyped nodes to receive typed resources sent to them via untyped edges" do
307
304
 
308
- expect(p.get_node('pool1').resource_count(Football)).to eq 5
305
+ p = Diagram.new 'balls'
309
306
 
310
- end
307
+ p.add_node!(Source, name: 'source', :type => Football)
311
308
 
312
- it "allows untyped nodes to receive typed resources sent to them via typed edges" do
309
+ p.add_node!(Pool, name: 'pool1')
313
310
 
314
- p = Diagram.new 'fruits'
311
+ p.add_edge!(Edge, name: 'connector1', from: 'source', to: 'pool1')
315
312
 
316
- #by declaring initial values, we're implicitly declaring types.
317
- p.add_node! Pool, name: 'pool1', initial_value: {Peach => 20, Mango => 99}
313
+ p.run!(5)
318
314
 
319
- p.add_node! Pool, name: 'pool2', activation: :automatic
315
+ expect(p.get_node('pool1').resource_count(type: Football)).to eq 5
320
316
 
321
- p.add_edge!(Edge, name: 'connector1', from: 'pool1', to: 'pool2', types: [Peach])
317
+ end
322
318
 
323
- p.run!(5)
319
+ it "allows untyped nodes to receive typed resources sent to them via typed edges" do
324
320
 
325
- expect(p.get_node('pool1').resource_count(Peach)).to eq 15
326
- expect(p.get_node('pool1').resource_count(Mango)).to eq 99
327
- expect(p.get_node('pool2').resource_count(Peach)).to eq 5
321
+ p = Diagram.new 'fruits'
328
322
 
329
- end
323
+ #by declaring initial values, we're implicitly declaring types.
324
+ p.add_node! Pool, name: 'pool1', initial_value: {Peach => 20, Mango => 99}
330
325
 
331
- it "executes start nodes only once" do
326
+ p.add_node! Pool, name: 'pool2', activation: :automatic
332
327
 
333
- d=Diagram.new 'simple'
328
+ p.add_edge!(Edge, name: 'connector1', from: 'pool1', to: 'pool2', types: [Peach])
334
329
 
335
- d.add_node! Source, {
336
- :name => 'source',
337
- :activation => :start
338
- }
330
+ p.run!(5)
339
331
 
340
- d.add_node! Pool, {
341
- :name => 'deposit',
342
- :initial_value => 0
343
- }
332
+ expect(p.get_node('pool1').resource_count(type: Peach)).to eq 15
333
+ expect(p.get_node('pool1').resource_count(type: Mango)).to eq 99
334
+ expect(p.get_node('pool2').resource_count(type: Peach)).to eq 5
344
335
 
345
- d.add_edge! Edge, {
346
- :name => 'connector',
347
- :from => 'source',
348
- :to => 'deposit'
349
- }
336
+ end
350
337
 
351
- d.run!(10)
338
+ it "executes start nodes only once" do
352
339
 
340
+ d=Diagram.new 'simple'
353
341
 
354
- expect(d.get_node('deposit').resource_count).to eq 1
342
+ d.add_node! Source, {
343
+ :name => 'source',
344
+ :activation => :start
345
+ }
355
346
 
356
- end
347
+ d.add_node! Pool, {
348
+ :name => 'deposit',
349
+ :initial_value => 0
350
+ }
357
351
 
352
+ d.add_edge! Edge, {
353
+ :name => 'connector',
354
+ :from => 'source',
355
+ :to => 'deposit'
356
+ }
358
357
 
358
+ d.run!(10)
359
+
360
+ expect(d.get_node('deposit').resource_count).to eq 1
361
+
362
+ end
359
363
 
360
- context 'integration' do
361
364
 
362
365
  it ' makes a train run' do
363
366
 
364
367
  d = Diagram.new 'dia'
365
368
 
366
369
  d.add_node! Pool, {
367
- name: 'p1',
368
- mode: :push_any,
369
- activation: :automatic,
370
- initial_value: 8
370
+ name: 'p1',
371
+ mode: :push_any,
372
+ activation: :automatic,
373
+ initial_value: 8
371
374
  }
372
375
 
373
376
  d.add_node! Pool, {
374
- name: 'p2',
375
- mode: :push_any,
376
- activation: :automatic
377
+ name: 'p2',
378
+ mode: :push_any,
379
+ activation: :automatic
377
380
  }
378
381
 
379
382
  d.add_node! Pool, {
380
- name: 'p3',
381
- mode: :push_any,
382
- activation: :automatic
383
+ name: 'p3',
384
+ mode: :push_any,
385
+ activation: :automatic
383
386
  }
384
387
 
385
388
  d.add_node! Pool, {
386
- name: 'p4',
387
- mode: :push_any,
388
- activation: :automatic
389
+ name: 'p4',
390
+ mode: :push_any,
391
+ activation: :automatic
389
392
  }
390
393
 
391
- d.add_edge! Edge,{
394
+ d.add_edge! Edge, {
392
395
  name: 'e1',
393
396
  from: 'p1',
394
397
  to: 'p2'
395
398
  }
396
- d.add_edge! Edge,{
399
+ d.add_edge! Edge, {
397
400
  name: 'e2',
398
401
  from: 'p2',
399
402
  to: 'p3'
400
403
  }
401
- d.add_edge! Edge,{
404
+ d.add_edge! Edge, {
402
405
  name: 'e3',
403
406
  from: 'p3',
404
407
  to: 'p4'
@@ -413,9 +416,6 @@ describe Diagram do
413
416
 
414
417
  end
415
418
 
416
- end
417
-
418
- context 'simple converter behaviour' do
419
419
  it 'runs an untyped converter connected to two pools' do
420
420
 
421
421
  d=Diagram.new 'simple'
@@ -430,7 +430,7 @@ describe Diagram do
430
430
  :initial_value => 0
431
431
  }
432
432
 
433
- d.add_node! Converter,{
433
+ d.add_node! Converter, {
434
434
  :name => 'c',
435
435
  :activation => :automatic
436
436
  }
@@ -453,8 +453,63 @@ describe Diagram do
453
453
 
454
454
  end
455
455
 
456
+ describe 'diagrams with probabilistic edges' do
456
457
 
457
- end
458
+ it 'accepts random edges with percentages' do
459
+
460
+ d = Diagram.new
461
+
462
+ d.add_node! Source, name: 's'
463
+ d.add_node! Gate, name: 'g'
464
+ d.add_node! Pool, name: 'p'
465
+ d.add_edge! Edge, from: 's', to: 'g'
466
+ d.add_edge! Edge, from: 'g', to: 'p', label: 50.percent
467
+
468
+ d.run!(20)
469
+
470
+ expect(d.p.resource_count).to (be > 1).and(be < 20)
471
+
472
+ end
473
+ end
474
+
475
+
476
+ describe 'Comprehensive examples'do
477
+
478
+ it 'example using pull_all and activators' do
479
+
480
+ d = Diagram.new
481
+
482
+ d.add_node! Pool, mode: :pull_all, name: 'p1', activation: :automatic
483
+ d.add_node! Pool, name: 'p2', initial_value: 7
484
+ d.add_node! Pool, name: 'p3', initial_value: 2
485
+ d.add_node! Pool, name: 'p4', initial_value: 2
486
+ d.add_node! Pool, name: 'p5', activation: :automatic, initial_value: 6, mode: :push_any
487
+
488
+ d.add_edge! Edge, from: 'p2',to:'p1'
489
+ d.add_edge! Edge, from: 'p3',to:'p1'
490
+ d.add_edge! Edge, from: 'p4',to:'p1'
458
491
 
492
+ d.add_edge! Edge, from: 'p5',to:'p3'
493
+ d.add_edge! Edge, from: 'p5',to:'p4'
494
+
495
+ d.p5.attach_condition{ d.p3.resource_count < 1 && d.p4.resource_count < 1 }
496
+
497
+ d.run! 8
498
+
499
+ expect(d.p1.resource_count).to eq 15
500
+ expect(d.p2.resource_count).to eq 2
501
+ expect(d.p3.resource_count).to eq 0
502
+ expect(d.p4.resource_count).to eq 0
503
+ expect(d.p5.resource_count).to eq 0
504
+
505
+ end
506
+
507
+
508
+
509
+
510
+
511
+ end
512
+
513
+ end
459
514
 
460
515
  end