| 1 |
""" bank13: Wait for the doorman to give a signal: *waitevent*""" |
| 2 |
from SimPy.Simulation import * |
| 3 |
from random import * |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
dooropen = SimEvent("Door Open") |
| 8 |
|
| 9 |
|
| 10 |
class Doorman(Process): |
| 11 |
""" Doorman opens the door""" |
| 12 |
def openthedoor(self): |
| 13 |
""" He will opens the door at fixed intervals""" |
| 14 |
for i in range(5): |
| 15 |
yield hold, self, 30.0 |
| 16 |
dooropen.signal() |
| 17 |
print("%7.4f You may enter" % (now())) |
| 18 |
|
| 19 |
|
| 20 |
class Source(Process): |
| 21 |
""" Source generates customers randomly""" |
| 22 |
def generate(self, number, rate): |
| 23 |
for i in range(number): |
| 24 |
c = Customer(name="Customer%02d" % (i)) |
| 25 |
activate(c, c.visit(timeInBank=12.0)) |
| 26 |
yield hold, self, expovariate(rate) |
| 27 |
|
| 28 |
|
| 29 |
class Customer(Process): |
| 30 |
""" Customer arrives, is served and leaves """ |
| 31 |
def visit(self, timeInBank=10): |
| 32 |
arrive = now() |
| 33 |
|
| 34 |
if dooropen.occurred: |
| 35 |
msg = '.' |
| 36 |
else: |
| 37 |
msg = ' but the door is shut.' |
| 38 |
print("%7.4f %s: Here I am%s" % (now(), self.name, msg)) |
| 39 |
yield waitevent, self, dooropen |
| 40 |
|
| 41 |
print("%7.4f %s: The door is open!" % (now(), self.name)) |
| 42 |
|
| 43 |
wait = now() - arrive |
| 44 |
print("%7.4f %s: Waited %6.3f" % (now(), self.name, wait)) |
| 45 |
|
| 46 |
yield request, self, counter |
| 47 |
tib = expovariate(1.0 / timeInBank) |
| 48 |
yield hold, self, tib |
| 49 |
yield release, self, counter |
| 50 |
|
| 51 |
print("%7.4f %s: Finished " % (now(), self.name)) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
maxTime = 400.0 |
| 56 |
|
| 57 |
counter = Resource(1, name="Clerk") |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
def model(SEED=393939): |
| 63 |
seed(SEED) |
| 64 |
|
| 65 |
initialize() |
| 66 |
doorman = Doorman() |
| 67 |
activate(doorman, doorman.openthedoor()) |
| 68 |
source = Source() |
| 69 |
activate(source, |
| 70 |
source.generate(number=5, rate=0.1), at=0.0) |
| 71 |
simulate(until=maxTime) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
model() |