Debian Codegraph @

docs/source/Manuals/programs/breakdown.py

raw symbols SCIP
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): # PEM
8 tripleft = triplength # time needed to finish trip
9 while tripleft > 0:
10 yield hold, self, tripleft # try to finish the trip
11 if self.interrupted(): # if another breakdown occurs
12 print('%s at %s' % (self.interruptCause.name, now()))
13 tripleft = self.interruptLeft # time to finish the trip
14 self.interruptReset() # end interrupt state
15 reactivate(br, delay=repairduration) # restart breakdown br
16 yield hold, self, repairduration # delay for repairs
17 print('Bus repaired at %s' % now())
18 else:
19 break # no more breakdowns, bus finished trip
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): # process execution method
29 while True:
30 yield hold, self, interval # breakdown interarrivals
31 if self.bus.terminated():
32 break
33 self.interrupt(self.bus) # breakdown to myBus
34
35
36 initialize()
37 b = Bus('Bus') # create a bus object
38 activate(b, b.operate(repairduration=20, triplength=1000))
39 br = Breakdown(b) # create breakdown br to bus b
40 activate(br, br.breakBus(300))
41 simulate(until=4000)
42 print('SimPy: No more events at time %s' % now())