| 1 |
from SimPy.Simulation import (Process, activate, hold, initialize, now, |
| 2 |
reactivate, simulate) |
| 3 |
|
| 4 |
|
| 5 |
class Bus(Process): |
| 6 |
|
| 7 |
def operate(self, repairduration, triplength): |
| 8 |
tripleft = triplength |
| 9 |
while tripleft > 0: |
| 10 |
yield hold, self, tripleft |
| 11 |
if self.interrupted(): |
| 12 |
print('%s at %s' % (self.interruptCause.name, now())) |
| 13 |
tripleft = self.interruptLeft |
| 14 |
self.interruptReset() |
| 15 |
reactivate(br, delay=repairduration) |
| 16 |
yield hold, self, repairduration |
| 17 |
print('Bus repaired at %s' % now()) |
| 18 |
else: |
| 19 |
break |
| 20 |
print('Bus has arrived at %s' % now()) |
| 21 |
|
| 22 |
|
| 23 |
class Breakdown(Process): |
| 24 |
def __init__(self, myBus): |
| 25 |
Process.__init__(self, name='Breakdown ' + myBus.name) |
| 26 |
self.bus = myBus |
| 27 |
|
| 28 |
def breakBus(self, interval): |
| 29 |
while True: |
| 30 |
yield hold, self, interval |
| 31 |
if self.bus.terminated(): |
| 32 |
break |
| 33 |
self.interrupt(self.bus) |
| 34 |
|
| 35 |
|
| 36 |
initialize() |
| 37 |
b = Bus('Bus') |
| 38 |
activate(b, b.operate(repairduration=20, triplength=1000)) |
| 39 |
br = Breakdown(b) |
| 40 |
activate(br, br.breakBus(300)) |
| 41 |
simulate(until=4000) |
| 42 |
print('SimPy: No more events at time %s' % now()) |