Sympy_ex1

#The Store object sObj is established by a statement like the following:

#sObj = Store(name='a_store', #             unitName='units', #             capacity='unbounded', #             initialBuffered=None, #             putQType=FIFO, #             getQType=FIFO, #             monitored=False, #             monitorType=Monitor)

from SimPy.Simulation import *

class Widget(Lister):

    def __init__(self,weight=0):

        self.weight=weight

class ProducerD(Process):

    def __init__(self):

        Process.__init__(self)

    def produce(self):           # the ProducerD PEM

        while True:

            yield put,self,buf,[Widget(9),Widget(7)]

            yield hold,self,10

class ConsumerD(Process):       

    def __init__(self):

        Process.__init__(self)

    def consume(self):           # the ConsumerD PEM

        while True:

            toGet=3

            yield get,self,buf,toGet

            assert len(self.got)==toGet

            print now(),'Get widget weights',\

                 [x.weight for x in self.got]

            yield hold,self,11

widgbuf=[]

for i in range(10):

    widgbuf.append(Widget(5))

initialize()

buf=Store(capacity=11,initialBuffered=widgbuf,monitored=True)

for i in range(3):       # define and activate 3 producer objects

    p=ProducerD()

    activate(p,p.produce())

for i in range(3):       # define and activate 3 consumer objects

    c=ConsumerD()

    activate(c,c.consume())

simulate(until=50)

print 'LenBuffer:',buf.bufferMon     # length of buffer

print 'getQ:',buf.getQMon            # length of getQ

print 'putQ',buf.putQMon             # length of putQ

===================================================

0 Get widget weights [5, 5, 5]

0 Get widget weights [5, 5, 5]

0 Get widget weights [5, 5, 5]

11 Get widget weights [5, 9, 7]

11 Get widget weights [9, 7, 9]

11 Get widget weights [7, 9, 7]

22 Get widget weights [9, 7, 9]

22 Get widget weights [7, 9, 7]

22 Get widget weights [9, 7, 9]

33 Get widget weights [7, 9, 7]

33 Get widget weights [9, 7, 9]

40 Get widget weights [7, 9, 7]

44 Get widget weights [9, 7, 9]

50 Get widget weights [7, 9, 7]

LenBuffer: [[0, 10], [0, 7], [0, 9], [0, 11], [0, 8], [0, 10], [0, 7], [10, 9], [10, 11], [11, 8], [11, 10], [11, 7], [11, 4], [20, 6], [20, 8], [21, 10], [22, 7], [22, 4], [22, 1], [30, 3], [30, 5], [31, 7], [33, 4], [33, 1], [40, 3], [40, 0], [40, 2], [41, 4], [44, 1], [50, 3], [50, 0], [50, 2]]

getQ: [[0, 0], [33, 1], [40, 0], [44, 1], [50, 0]]

putQ [[0, 0], [0, 1], [0, 2], [0, 3], [0, 2], [0, 1], [0, 0], [10, 1], [11, 0]]