-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmodel.go
554 lines (467 loc) · 12.4 KB
/
model.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
// Copyright 2015 Alex Goussiatiner. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
//
// Godes is the general-purpose simulation library
// which includes the simulation engine and building blocks
// for modeling a wide variety of systems at varying levels of details.
//
// Godes model controls the runnners
// See examples for the usage.
//
package godes
import (
"container/list"
"fmt"
//"sync"
"time"
)
const simulationSecondScale = 100
const rUNNER_STATE_READY = 0
const rUNNER_STATE_ACTIVE = 1
const rUNNER_STATE_WAITING_COND = 2
const rUNNER_STATE_SCHEDULED = 3
const rUNNER_STATE_INTERRUPTED = 4
const rUNNER_STATE_TERMINATED = 5
var modl *model
var stime float64 = 0
// WaitUntilDone stops the main goroutine and waits
// until all the runners finished executing the Run()
func WaitUntilDone() {
if modl == nil {
panic(" not initilized")
}
modl.waitUntillDone()
}
//AddRunner adds the runner obejct into model
func AddRunner(runner RunnerInterface) {
if runner == nil {
panic("runner is nil")
}
if modl == nil {
createModel(false)
}
modl.add(runner)
}
//Interrupt holds the runner execution
func Interrupt(runner RunnerInterface) {
if runner == nil {
panic("runner is nil")
}
if modl == nil {
panic("model is nil")
}
modl.interrupt(runner)
}
//Resume restarts the runner execution
func Resume(runner RunnerInterface, timeChange float64) {
if runner == nil {
panic("runner is nil")
}
if modl == nil {
panic("model is nil")
}
modl.resume(runner, timeChange)
}
//Run starts the simulation model.
// Must be called explicitly.
func Run() {
if modl == nil {
createModel(false)
}
//assuming that it comes from the main go routine
if modl.activeRunner == nil {
panic("runner is nil")
}
if modl.activeRunner.getInternalId() != 0 {
panic("it comes from not from the main go routine")
}
modl.simulationActive = true
modl.control()
}
//Advance the simulation time
func Advance(interval float64) {
if modl == nil {
createModel(false)
}
modl.advance(interval)
}
// Verbose sets the model in the verbose mode
func Verbose(v bool) {
if modl == nil {
createModel(v)
}
modl.DEBUG = v
}
// Clear the model between the runs
func Clear() {
if modl == nil {
panic(" No model exist")
} else {
stime = 0
modl = newModel(modl.DEBUG)
//model.simulationActive = true
//model.control()
}
}
// GetSystemTime retuns the current simulation time
func GetSystemTime() float64 {
return stime
}
// Yield stops the runner for short time
func Yield() {
Advance(0.01)
}
// createModel
func createModel(verbose bool) {
if modl != nil {
panic("model is already active")
}
stime = 0
modl = newModel(verbose)
//model.simulationActive = true
//model.control()
//assuming that it comes from the main go routine
}
type model struct {
//mu sync.RWMutex
activeRunner RunnerInterface
movingList *list.List
scheduledList *list.List
waitingList *list.List
waitingConditionMap map[int]RunnerInterface
interruptedMap map[int]RunnerInterface
terminatedList *list.List
currentId int
controlChannel chan int
simulationActive bool
DEBUG bool
}
//newModel initilizes the model
func newModel(verbose bool) *model {
var ball *Runner = newRunner()
ball.channel = make(chan int)
ball.markTime = time.Now()
ball.internalId = 0
ball.state = rUNNER_STATE_ACTIVE //that is bypassing READY
ball.priority = 100
ball.setMarkTime(time.Now())
var runner RunnerInterface = ball
mdl := model{activeRunner: runner, controlChannel: make(chan int), DEBUG: verbose, simulationActive: false}
mdl.addToMovingList(runner)
return &mdl
}
func (mdl *model) advance(interval float64) bool {
ch := mdl.activeRunner.getChannel()
mdl.activeRunner.setMovingTime(stime + interval)
mdl.activeRunner.setState(rUNNER_STATE_SCHEDULED)
mdl.removeFromMovingList(mdl.activeRunner)
mdl.addToSchedulledList(mdl.activeRunner)
//restart control channel and freez
mdl.controlChannel <- 100
<-ch
return true
}
func (mdl *model) waitUntillDone() {
if mdl.activeRunner.getInternalId() != 0 {
panic("waitUntillDone initiated for not main ball")
}
mdl.removeFromMovingList(mdl.activeRunner)
mdl.controlChannel <- 100
for {
if !modl.simulationActive {
break
} else {
if mdl.DEBUG {
fmt.Println("waiting", mdl.movingList.Len())
}
time.Sleep(time.Millisecond * simulationSecondScale)
}
}
}
func (mdl *model) add(runner RunnerInterface) bool {
mdl.currentId++
runner.setChannel(make(chan int))
runner.setMovingTime(stime)
runner.setInternalId(mdl.currentId)
runner.setState(rUNNER_STATE_READY)
mdl.addToMovingList(runner)
go func() {
<-runner.getChannel()
runner.setMarkTime(time.Now())
runner.Run()
if mdl.activeRunner == nil {
panic("remove: activeRunner == nil")
}
mdl.removeFromMovingList(mdl.activeRunner)
mdl.activeRunner.setState(rUNNER_STATE_TERMINATED)
mdl.activeRunner = nil
mdl.controlChannel <- 100
}()
return true
}
func (mdl *model) interrupt(runner RunnerInterface) {
if runner.getState() != rUNNER_STATE_SCHEDULED {
panic("It is not rUNNER_STATE_SCHEDULED")
}
mdl.removeFromSchedulledList(runner)
runner.setState(rUNNER_STATE_INTERRUPTED)
mdl.addToInterruptedMap(runner)
}
func (mdl *model) resume(runner RunnerInterface, timeChange float64) {
if runner.getState() != rUNNER_STATE_INTERRUPTED {
panic("It is not rUNNER_STATE_INTERRUPTED")
}
mdl.removeFromInterruptedMap(runner)
runner.setState(rUNNER_STATE_SCHEDULED)
runner.setMovingTime(runner.getMovingTime() + timeChange)
//mdl.addToMovingList(runner)
mdl.addToSchedulledList(runner)
}
func (mdl *model) booleanControlWait(b *BooleanControl, val bool) {
ch := mdl.activeRunner.getChannel()
if mdl.activeRunner == nil {
panic("booleanControlWait - no runner")
}
mdl.removeFromMovingList(mdl.activeRunner)
mdl.activeRunner.setState(rUNNER_STATE_WAITING_COND)
mdl.activeRunner.setWaitingForBool(val)
mdl.activeRunner.setWaitingForBoolControl(b)
mdl.addToWaitingConditionMap(mdl.activeRunner)
mdl.controlChannel <- 100
<-ch
}
func (mdl *model) booleanControlWaitAndTimeout(b *BooleanControl, val bool, timeout float64) {
ri := &TimeoutRunner{&Runner{}, mdl.activeRunner, timeout}
AddRunner(ri)
mdl.activeRunner.setWaitingForBoolControlTimeoutId(ri.getInternalId())
mdl.booleanControlWait(b, val)
}
func (mdl *model) booleanControlSet(b *BooleanControl) {
ch := mdl.activeRunner.getChannel()
if mdl.activeRunner == nil {
panic("booleanControlSet - no runner")
}
mdl.controlChannel <- 100
<-ch
}
func (mdl *model) control() bool {
if mdl.activeRunner == nil {
panic("control: activeBall == nil")
}
go func() {
var runner RunnerInterface
for {
<-mdl.controlChannel
if mdl.waitingConditionMap != nil && len(mdl.waitingConditionMap) > 0 {
for key, temp := range mdl.waitingConditionMap {
if temp.getWaitingForBoolControl() == nil {
panic(" no BoolControl")
}
if temp.getWaitingForBool() == temp.getWaitingForBoolControl().GetState() {
temp.setState(rUNNER_STATE_READY)
temp.setWaitingForBoolControl(nil)
temp.setWaitingForBoolControlTimeoutId(-1)
mdl.addToMovingList(temp)
delete(mdl.waitingConditionMap, key)
break
}
}
}
//finding new runner
runner = nil
if mdl.movingList != nil && mdl.movingList.Len() > 0 {
runner = mdl.getFromMovingList()
}
if runner == nil && mdl.scheduledList != nil && mdl.scheduledList.Len() > 0 {
runner = mdl.getFromSchedulledList()
if runner.getMovingTime() < stime {
panic("control is seting simulation time in the past")
} else {
stime = runner.getMovingTime()
}
mdl.addToMovingList(runner)
}
if runner == nil {
break
}
//restarting
mdl.activeRunner = runner
mdl.activeRunner.setState(rUNNER_STATE_ACTIVE)
runner.setWaitingForBoolControl(nil)
mdl.activeRunner.getChannel() <- -1
}
if mdl.DEBUG {
fmt.Println("Finished")
}
mdl.simulationActive = false
}()
return true
}
/*
MovingList
This list is sorted in descending order according to the value of the ball priority attribute.
Balls with identical priority values are sorted according to the FIFO principle.
|
| |
| |
| | |
<----- | | | |
*/
func (mdl *model) addToMovingList(runner RunnerInterface) bool {
if mdl.DEBUG {
fmt.Printf("addToMovingList %v\n", runner)
}
if mdl.movingList == nil {
mdl.movingList = list.New()
mdl.movingList.PushFront(runner)
return true
}
insertedSwt := false
for element := mdl.movingList.Front(); element != nil; element = element.Next() {
if runner.getPriority() > element.Value.(RunnerInterface).getPriority() {
mdl.movingList.InsertBefore(runner, element)
insertedSwt = true
break
}
}
if !insertedSwt {
mdl.movingList.PushBack(runner)
}
return true
}
func (mdl *model) getFromMovingList() RunnerInterface {
if mdl.movingList == nil {
panic("MovingList was not initilized")
}
if mdl.DEBUG {
runner := mdl.movingList.Front().Value.(RunnerInterface)
fmt.Printf("getFromMovingList %v\n", runner)
}
runner := mdl.movingList.Front().Value.(RunnerInterface)
mdl.movingList.Remove(mdl.movingList.Front())
return runner
}
func (mdl *model) removeFromMovingList(runner RunnerInterface) {
if mdl.movingList == nil {
panic("MovingList was not initilized")
}
if mdl.DEBUG {
fmt.Printf("removeFromMovingList %v\n", runner)
}
var found bool
for e := mdl.movingList.Front(); e != nil; e = e.Next() {
if e.Value == runner {
mdl.movingList.Remove(e)
found = true
break
}
}
if !found {
//panic("not found in MovingList")
}
}
/*
SchedulledList
This list is sorted in descending order according to the schedulled time
Priorites are not used
|
| |
| |
| | |
| | | |--->
*/
func (mdl *model) addToSchedulledList(runner RunnerInterface) bool {
if mdl.scheduledList == nil {
mdl.scheduledList = list.New()
mdl.scheduledList.PushFront(runner)
return true
}
insertedSwt := false
for element := mdl.scheduledList.Back(); element != nil; element = element.Prev() {
if runner.getMovingTime() < element.Value.(RunnerInterface).getMovingTime() {
mdl.scheduledList.InsertAfter(runner, element)
insertedSwt = true
break
}
}
if !insertedSwt {
mdl.scheduledList.PushFront(runner)
}
if mdl.DEBUG {
fmt.Println("===")
fmt.Printf("addToSchedulledList %v\n", runner)
for element := mdl.scheduledList.Front(); element != nil; element = element.Next() {
fmt.Printf("elem %v\n", element.Value.(RunnerInterface))
}
fmt.Println("===")
}
return true
}
func (mdl *model) getFromSchedulledList() RunnerInterface {
if mdl.scheduledList == nil {
panic(" SchedulledList was not initilized")
}
if mdl.DEBUG {
runner := mdl.scheduledList.Back().Value.(RunnerInterface)
fmt.Printf("getFromSchedulledList %v\n", runner)
}
runner := mdl.scheduledList.Back().Value.(RunnerInterface)
mdl.scheduledList.Remove(mdl.scheduledList.Back())
return runner
}
func (mdl *model) removeFromSchedulledList(runner RunnerInterface) {
if mdl.scheduledList == nil {
panic("schedulledList was not initilized")
}
if modl.DEBUG {
fmt.Printf("removeFrom schedulledListt %v\n", runner)
}
var found bool
for e := mdl.scheduledList.Front(); e != nil; e = e.Next() {
if e.Value == runner {
mdl.scheduledList.Remove(e)
found = true
break
}
}
if !found {
panic("not found in scheduledList")
}
return
}
func (mdl *model) addToWaitingConditionMap(runner RunnerInterface) bool {
if runner.getWaitingForBoolControl() == nil {
panic(" addToWaitingConditionMap - no control ")
}
if mdl.DEBUG {
fmt.Printf("addToWaitingConditionMap %v\n", runner)
}
if mdl.waitingConditionMap == nil {
mdl.waitingConditionMap = make(map[int]RunnerInterface)
}
mdl.waitingConditionMap[runner.getInternalId()] = runner
return true
}
func (mdl *model) addToInterruptedMap(runner RunnerInterface) bool {
if mdl.DEBUG {
fmt.Printf("addToInterruptedMap %v\n", runner)
}
if mdl.interruptedMap == nil {
mdl.interruptedMap = make(map[int]RunnerInterface)
}
mdl.interruptedMap[runner.getInternalId()] = runner
return true
}
func (mdl *model) removeFromInterruptedMap(runner RunnerInterface) bool {
if mdl.DEBUG {
fmt.Printf("removeFromInterruptedMap %v\n", runner)
}
_, ok := mdl.interruptedMap[runner.getInternalId()]
if ok {
delete(mdl.interruptedMap, runner.getInternalId())
} else {
panic("not found in interruptedMap")
}
return true
}