SimPy_worker_extend_OO.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 » SimPy_worker_extend_OO.py
from SimPy.Simulation import *
from random import uniform,seed
import string

## Model components ------------------------
def theTime(time):

    hrs=int(time/60)
    min=int(time-hrs*60)
    return "%s:%s" %(string.zfill(str(hrs),2),string.zfill(str(min),2))

class worker(Process):
    def __init__(self,id,sim):
        Process.__init__(self,sim=sim)
        self.id = id
        self.output = 0
        self.idle = 0
        self.total_idle = 0

    def working_day(self,foobar):
        print "%s Worker %s arrives in factory" %(theTime(self.sim.now()),self.id)
        while self.sim.now()<17*60: #work till 5 pm
            yield hold,self,uniform(3,10)
            #print "%s Widget completed" %theTime(now())
            foobar.queue.append(self)
            if foobar.idle:
                self.sim.reactivate(foobar)
            else:
                self.idle = 1 #worker has to wait for foobar service
                start_idle = self.sim.now()
                #print "%s Worker %s queuing for foobar machine" %(theTime(now()),self.id)
            yield passivate,self #waiting and foobar service
            self.output += 1
            if self.idle:
                self.total_idle += self.sim.now()-start_idle
            self.idle=0
        print "%s %s goes home, having built %d widgets today." %(theTime(self.sim.now()),self.id,self.output)
        print "Worker %s was idle waiting for foobar machine for %3.1f hours" %(self.id,self.total_idle/60)

class foobar_machine(Process):
    def __init__(self,sim):
        Process.__init__(self,sim=sim)
        self.queue=[]
        self.idle=1

    def foobar_Process(self):
        yield passivate,self
        while 1:
            while len(self.queue) > 0:
                self.idle=0
                yield hold,self,3
                served=self.queue.pop(0)
                self.sim.reactivate(served)
            self.idle=1
            yield passivate,self
            
## Model -----------------------------------
class SimPy_Worker_Extend_Model(Simulation):
    def run(self):
        print 'SimPy_worker_extend'
        self.initialize()
        foo=foobar_machine(sim=self)
        self.activate(foo,foo.foobar_Process(),delay=0)
        john=worker("John",sim=self)
        self.activate(john,john.working_day(foo),at=510) #start at 8:30 am
        eve=worker("Eve",sim=self)
        self.activate(eve,eve.working_day(foo),at=510)
        self.simulate(until=18*60) #run simulation from midnight till 6 pm

## Expriment -------------------------------
seed(111333555)
SimPy_Worker_Extend_Model().run()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.