demoSimPyEvents.py :  » Development » SimPy » SimPy-2.1.0beta » SimPyModels » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SimPy 
SimPy » SimPy 2.1.0beta » SimPyModels » demoSimPyEvents.py
from SimPy.Simulation import *
import random
"""
   Demo of SimPy's event signalling synchronization constructs
"""

print 'demoSimPyEvents'

##Pavlov's dogs
"""Scenario:
Dogs start to drool when Pavlov rings the bell.
"""
class BellMan(Process):
    def ring(self):
        while True:
            bell.signal()
            print "%s %s rings bell"%(now(),self.name)
            yield hold,self,5

class PavlovDog(Process):
    def behave(self):
        while True:
            yield waitevent,self,bell
            print "%s %s drools"%(now(),self.name)

random.seed(111333555)
initialize()
bell=SimEvent("bell")
for i in range(4):
    p=PavlovDog("Dog %s"%(i+1))
    activate(p,p.behave())
b=BellMan("Pavlov")
activate(b,b.ring())
print "\n Pavlov's dogs"
simulate(until=10)

##PERT simulation
"""
Scenario:
A job (TotalJob) requires 10 parallel activities with random duration to be completed.
"""
class Activity(Process):
    def __init__(self,name):
        Process.__init__(self,name)
        self.event=SimEvent("completion of %s"%self.name)
        allEvents.append(self.event)
    def perform(self):
        yield hold,self,random.randint(1,100)
        self.event.signal()
        print "%s Event '%s' fired"%(now(),self.event.name)

class TotalJob(Process):
    def perform(self,allEvents):
        for e in allEvents:
            yield waitevent,self,e
        print now(),"All done"

random.seed(111333555)
initialize()
allEvents=[]
for i in range(10):
    a=Activity("Activity %s"%(i+1))
    activate(a,a.perform())
t=TotalJob()
activate(t,t.perform(allEvents))
print "\n PERT network simulation"
simulate(until=100)

## US-style4-way stop intersection
"""
Scenario:
At a US-style 4-way stop intersection, a car may only enter the intersection when it is free.
Cars enter in FIFO manner.
"""
class Car(Process):
    def drive(self):
        print "%4.1f %s waiting to enter intersection"%(now(),self.name)
        yield queueevent,self,intersectionFree
        # Intersection free, enter  . . 
        ### Begin Critical Section
        yield hold,self,1            #drive across
        print "%4.1f %s crossed intersection"%(now(),self.name)
        ### End Critical Section
        intersectionFree.signal()

random.seed(111333555)
initialize()
intersectionFree=SimEvent("Intersection free")
intersectionFree.signal()
arrtime=0.0
for i in range(20):
    c=Car("Car %s"%(i+1))
    activate(c,c.drive(),at=arrtime)
    arrtime+=0.2
print "\n 4-way stop intersection"
print simulate(until=100)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.