Re: Use of filter function on store
Klaus G. Muller <kgmuller <at> xs4all.nl>
2009-10-08 06:30:21 GMT
Bonnie,
here is an example of how the filter function should be written:
from SimPy.Simulation import *
import random as rand
class Parcel:
def __init__(self,parcelType):
self.ptype = parcelType
class ParcelProducer(Process):
def produce(self,store):
while True:
yield hold,self,1
parcl = Parcel(parcelType = rand.choice(["Light","Heavy"]))
yield put,self,store,[parcl]
class ParcelConsumer(Process):
def consume(self,store):
while True:
yield get,self,store,heavies
found = self.got
print "Time %s: retrieved parcel type is %s"%(now(),[x.ptype
for x in found])
def heavies(buff):
"""filter function. Only get 'Heavy' parcels"""
res = []
for item in buff:
if item.ptype == "Heavy":
res.append(item)
return res
rand.seed(12345)
initialize()
s = Store(capacity = 1000)
pp = ParcelProducer()
activate(pp,pp.produce(store = s))
pc =ParcelConsumer()
activate(pc,pc.consume(store = s))
simulate(until=15)
print "Left in store: parcels of type: %s"%[x.ptype for x in s.theBuffer]
The filter function must have only one parameter (standing for the
Store's buffer). It must be in the scope of the "yield get" which uses it.
OK?
Klaus Müller
Bonnie Douglas wrote:
> Hello again group,
> Yet another question on a problem that I'm having that is likely a result of my
> inexperience with Python.
>
> I am trying to pull items (which are processes) out of a store based on specific
> item attributes. I do not understand the explanation of the use of filter
> functions in the in-depth SimPy manual. I have created a small test program
> (below).
>
> I do not understand:
> 1. whether the filter should be defined globally or locally (within the Unit
> process).
> 2. whether the attributes against which I'm testing must be defined globally or
> locally.
>
> I have tried several different configurations with no success.
>
> Any help would be greatly appreciated.
> Kind Regards,
> Bonnie Douglas
>
> test.py
> ----------------------------------------
> import csv
> from SimPy.Simulation import *
>
> wT = ""
> pS = ""
>
> class Unit(Process):
> def __init__(self):
> Process.__init__(self)
>
> def run(self): # the ProducerD PEM
> while True:
> for li in maintfault:
> wT = li[0]
> pS = li[1]
> print 'At', now(), ' I wanted type: ', wT, ' pack: ', pS
> yield get, self, sObj, wagType
> print 'At', now(), ' I wanted type: ', wT, ' pack: ', pS, ' I
> got:', self.got
> yield put, self, sObj, self.got #put them all back yield hold,
> self, 1 # wait for 10 units for the heck of it
> yield put, self, sObj, self.got #put them all back
>
> class Wagon(Process):
> def __init__(self,wType,pkSize):
> Process.__init__(self, wType, pkSize)
> self.moveSignal=SimEvent()
> self.pksize=pkSize
> self.wagType=wType
> def storeme(self):
> yield put,self,sObj,[self]
> yield waitevent,self,self.moveSignal
> yield hold, self, 10
>
> def wagType(buff):
> """ filter: get all wagons with .type = type from store. """
> result=[]
> print 'in wagType function wtype: ', wT, ' ps: ', pS
> if wT == "ALL" and pS == "ALL":
> result = buff
> else:
> if wT == "ALL":
> for i in buff:
> if i.pkSize == pS:
> result.append(i)
> if pS == "ALL":
> for i in buff:
> if i.wagType == wT:
> result.append(i)
> else:
> for i in buff:
> if i.wagType == wT and i.pkSize == pS:
> result.append(i)
> return result
>
> def read_as_dict(filename):
> """
> example
> =======
> toast = {}
> for row in read_as_dict("thing.csv"):
> toast[row["type"]] = Store(name=row["type"], cap=row['cap'], etc)
> """
> return csv.DictReader(open(filename, "rb"))
>
> initialize()
> ################################################
> ## read input data in from files
> ################################################
> sObj = Store(name='a_store',
> unitName='wagons',
> capacity=9999,
> monitored=True,
> monitorType=Monitor)
>
> #maintfault=[]
> #for row in read_as_dict("MaintenanceFault.csv"):
> # maintfault.append( [row["wagonType"],row["pkSize"],
> row["FaultType"],row["FaultDescr"], row["daysToSM"],
> row["pFaultLowUTM"],row["pFaultHighUTM"], \
> # row["pFaultLowFX1"], row["pFaultHighFX1"],
> row["repDistribution"],row["repDurMinMode"], \
> # row["repDurHigh"],row["repDurLow"],
> row["repDurMinAvg"], row["repDurMinStdDev"]] )
>
> maintfault = [["ALL","ALL"],["NHRH","ALL"],["NHRH",4],["TTTT",3]]
>
> w1 = Wagon("NHRH",8)
> activate(w1,w1.storeme())
> w2 = Wagon("NHPH",7)
> activate(w2,w2.storeme())
> w3 = Wagon("TTTT",3)
> activate(w3,w3.storeme())
>
>
> u = Unit()
> activate(u,u.run())
>
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Simpy-users mailing list
> Simpy-users <at> lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/simpy-users
>
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference