| 1 |
""" |
| 2 |
A utility module for stepping through the events of a simulation |
| 3 |
under user control. |
| 4 |
|
| 5 |
REQUIRES SimPy 2.1 |
| 6 |
""" |
| 7 |
from SimPy.SimulationTrace import * |
| 8 |
|
| 9 |
def stepper(): |
| 10 |
evlist = Globals.sim._timestamps |
| 11 |
while True: |
| 12 |
if not evlist: |
| 13 |
print "No more events." |
| 14 |
break |
| 15 |
tEvt = evlist[0][0] |
| 16 |
who = evlist[0][2] |
| 17 |
while evlist[0][3]: |
| 18 |
step() |
| 19 |
print "\nTime now: %s, next event at: %s for process: %s " %(now(),tEvt,who.name) |
| 20 |
|
| 21 |
cmd = raw_input( |
| 22 |
"'s' next event,'r' run to end,'e' to end run, " |
| 23 |
"<time> skip to event at <time>, 'l' show eventlist, 'p<name>' skip to event for <name>: ") |
| 24 |
try: |
| 25 |
nexttime = float(cmd) |
| 26 |
while peek() < nexttime: |
| 27 |
step() |
| 28 |
except: |
| 29 |
if cmd == 's': |
| 30 |
step() |
| 31 |
elif cmd == 'r': |
| 32 |
break |
| 33 |
elif cmd == 'e': |
| 34 |
stopSimulation() |
| 35 |
break |
| 36 |
elif cmd == 'l': |
| 37 |
print "Events scheduled: \n%s"%allEventNotices() |
| 38 |
elif cmd[0] == 'p': |
| 39 |
while evlist and evlist[0][2].name <> cmd[1:]: |
| 40 |
step() |
| 41 |
else: |
| 42 |
print "%s not a valid command" % cmd |
| 43 |
|
| 44 |
|
| 45 |
if __name__ == "__main__": |
| 46 |
import random as r |
| 47 |
r.seed(1234567) |
| 48 |
|
| 49 |
class Test(Process): |
| 50 |
def run(self): |
| 51 |
while now() < until: |
| 52 |
yield hold,self,r.uniform(1,10) |
| 53 |
|
| 54 |
class Waiter(Process): |
| 55 |
def run(self,evt): |
| 56 |
def gt30(): |
| 57 |
return now()>30 |
| 58 |
|
| 59 |
yield waituntil,self,gt30 |
| 60 |
print "now() is past 30" |
| 61 |
stopSimulation() |
| 62 |
|
| 63 |
until = 100 |
| 64 |
initialize() |
| 65 |
evt = SimEvent() |
| 66 |
t=Test("Test1") |
| 67 |
activate(t,t.run()) |
| 68 |
t2=Test("Test2") |
| 69 |
activate(t2,t2.run()) |
| 70 |
w=Waiter("Waiter") |
| 71 |
activate(w,w.run(evt=evt)) |
| 72 |
stepper() |