| 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(whichsim): |
| 10 |
evlist = whichsim._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 |
whichsim.step() |
| 19 |
print "\nTime now: %s, next event at: %s for process: %s " %(whichsim.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 whichsim.peek() < nexttime: |
| 27 |
whichsim.step() |
| 28 |
except: |
| 29 |
if cmd == 's': |
| 30 |
whichsim.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"%whichsim.allEventNotices() |
| 38 |
elif cmd[0] == 'p': |
| 39 |
while evlist and evlist[0][2].name <> cmd[1:]: |
| 40 |
whichsim.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 self.sim.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 self.sim.now() > 30 |
| 58 |
|
| 59 |
yield waituntil,self,gt30 |
| 60 |
print "now() is past 30" |
| 61 |
self.sim.stopSimulation() |
| 62 |
|
| 63 |
until = 100 |
| 64 |
s = SimulationTrace() |
| 65 |
s.initialize() |
| 66 |
evt = SimEvent(sim=s) |
| 67 |
t=Test("Test1", sim=s) |
| 68 |
s.activate(t,t.run()) |
| 69 |
t2=Test("Test2", sim=s) |
| 70 |
s.activate(t2,t2.run()) |
| 71 |
w=Waiter("Waiter", sim=s) |
| 72 |
s.activate(w,w.run(evt=evt)) |
| 73 |
stepper(whichsim=s) |