Debian Codegraph @

docs/examples/demoSimPyEvents.py

raw symbols SCIP
1 from SimPy.Simulation import *
2 import random
3 """
4 Demo of SimPy's event signalling synchronization constructs
5 """
6
7 print 'demoSimPyEvents'
8
9 ##Pavlov's dogs
10 """Scenario:
11 Dogs start to drool when Pavlov rings the bell.
12 """
13 class BellMan(Process):
14 def ring(self):
15 while True:
16 bell.signal()
17 print "%s %s rings bell"%(now(),self.name)
18 yield hold,self,5
19
20 class PavlovDog(Process):
21 def behave(self):
22 while True:
23 yield waitevent,self,bell
24 print "%s %s drools"%(now(),self.name)
25
26 random.seed(111333555)
27 initialize()
28 bell=SimEvent("bell")
29 for i in range(4):
30 p=PavlovDog("Dog %s"%(i+1))
31 activate(p,p.behave())
32 b=BellMan("Pavlov")
33 activate(b,b.ring())
34 print "\n Pavlov's dogs"
35 simulate(until=10)
36
37 ##PERT simulation
38 """
39 Scenario:
40 A job (TotalJob) requires 10 parallel activities with random duration to be completed.
41 """
42 class Activity(Process):
43 def __init__(self,name):
44 Process.__init__(self,name)
45 self.event=SimEvent("completion of %s"%self.name)
46 allEvents.append(self.event)
47 def perform(self):
48 yield hold,self,random.randint(1,100)
49 self.event.signal()
50 print "%s Event '%s' fired"%(now(),self.event.name)
51
52 class TotalJob(Process):
53 def perform(self,allEvents):
54 for e in allEvents:
55 yield waitevent,self,e
56 print now(),"All done"
57
58 random.seed(111333555)
59 initialize()
60 allEvents=[]
61 for i in range(10):
62 a=Activity("Activity %s"%(i+1))
63 activate(a,a.perform())
64 t=TotalJob()
65 activate(t,t.perform(allEvents))
66 print "\n PERT network simulation"
67 simulate(until=100)
68
69 ## US-style4-way stop intersection
70 """
71 Scenario:
72 At a US-style 4-way stop intersection, a car may only enter the intersection when it is free.
73 Cars enter in FIFO manner.
74 """
75 class Car(Process):
76 def drive(self):
77 print "%4.1f %s waiting to enter intersection"%(now(),self.name)
78 yield queueevent,self,intersectionFree
79 # Intersection free, enter . .
80 ### Begin Critical Section
81 yield hold,self,1 #drive across
82 print "%4.1f %s crossed intersection"%(now(),self.name)
83 ### End Critical Section
84 intersectionFree.signal()
85
86 random.seed(111333555)
87 initialize()
88 intersectionFree=SimEvent("Intersection free")
89 intersectionFree.signal()
90 arrtime=0.0
91 for i in range(20):
92 c=Car("Car %s"%(i+1))
93 activate(c,c.drive(),at=arrtime)
94 arrtime+=0.2
95 print "\n 4-way stop intersection"
96 print simulate(until=100)
97