Assembly updated to the new signalling approach

parent 38d2047a
...@@ -27,15 +27,19 @@ it gathers frames and parts which are loaded to the frames ...@@ -27,15 +27,19 @@ it gathers frames and parts which are loaded to the frames
''' '''
from SimPy.Simulation import Process, Resource from SimPy.Simulation import Process, Resource
from SimPy.Simulation import waituntil, now, hold from SimPy.Simulation import waitevent, now, hold
import xlwt import xlwt
from RandomNumberGenerator import RandomNumberGenerator from RandomNumberGenerator import RandomNumberGenerator
from CoreObject import CoreObject from CoreObject import CoreObject
#the Assembly object #===============================================================================
# the Assembly object
#===============================================================================
class Assembly(CoreObject): class Assembly(CoreObject):
class_name = 'Dream.Assembly' class_name = 'Dream.Assembly'
#initialize the object #===========================================================================
# initialize the object
#===========================================================================
def __init__(self, id, name, processingTime=None): def __init__(self, id, name, processingTime=None):
if not processingTime: if not processingTime:
processingTime = {'distributionType': 'Fixed', processingTime = {'distributionType': 'Fixed',
...@@ -72,6 +76,9 @@ class Assembly(CoreObject): ...@@ -72,6 +76,9 @@ class Assembly(CoreObject):
# then the giverObjects have to be blocked for the time # then the giverObjects have to be blocked for the time
# that the machine is being loaded # that the machine is being loaded
#===========================================================================
# initialize method
#===========================================================================
def initialize(self): def initialize(self):
Process.__init__(self) Process.__init__(self)
CoreObject.initialize(self) CoreObject.initialize(self)
...@@ -108,15 +115,35 @@ class Assembly(CoreObject): ...@@ -108,15 +115,35 @@ class Assembly(CoreObject):
self.Res.activeQ=[] self.Res.activeQ=[]
self.Res.waitQ=[] self.Res.waitQ=[]
#===========================================================================
# class generator
#===========================================================================
def run(self): def run(self):
activeObjectQueue=self.getActiveObjectQueue()
# check if there is WIP and signal receiver
self.initialSignalReceiver()
while 1: while 1:
yield waituntil, self, self.canAcceptAndIsRequested #wait until the Assembly can accept a frame self.printTrace(self.id, 'will wait for frame event')
#and one "frame" giver requests it # wait until the Queue can accept an entity and one predecessor requests it
yield waitevent, self, self.isRequested #[self.isRequested,self.canDispose, self.loadOperatorAvailable]
# yield waituntil, self, self.canAcceptAndIsRequested #wait until the Assembly can accept a frame
# #and one "frame" giver requests it
if self.isRequested.signalparam:
self.printTrace(self.id, 'received a isRequested event from '+self.isRequested.signalparam.id)
# reset the isRequested signal parameter
self.isRequested.signalparam=None
self.getEntity("Frame") #get the Frame self.getEntity("Frame") #get the Frame
for i in range(self.getActiveObjectQueue()[0].capacity): #this loop will be carried until the Frame is full with the parts for i in range(self.getActiveObjectQueue()[0].capacity): #this loop will be carried until the Frame is full with the parts
yield waituntil, self, self.isRequestedFromPart #wait until a part is requesting for the assembly self.printTrace(self.id, 'will wait for part event')
yield waitevent, self, self.isRequested
if self.isRequested.signalparam:
self.printTrace(self.id, 'received a isRequested event from '+self.isRequested.signalparam.id)
# reset the isRequested signal parameter
self.isRequested.signalparam=None
# yield waituntil, self, self.isRequestedFromPart #wait until a part is requesting for the assembly
# TODO: fix the getEntity 'Part' case
self.getEntity("Part") self.getEntity("Part")
self.outputTrace(self.getActiveObjectQueue()[0].name, "is now full in "+ self.objName) self.outputTrace(self.getActiveObjectQueue()[0].name, "is now full in "+ self.objName)
...@@ -136,82 +163,146 @@ class Assembly(CoreObject): ...@@ -136,82 +163,146 @@ class Assembly(CoreObject):
startBlockageTime=now() startBlockageTime=now()
self.completedJobs+=1 #Assembly completed a job self.completedJobs+=1 #Assembly completed a job
self.waitToDispose=True #since all the frame is full self.waitToDispose=True #since all the frame is full
self.printTrace(self.id, 'will try to signal a receiver from generator')
# signal the receiver that the activeObject has something to dispose of
if not self.signalReceiver():
# if there was no available receiver, get into blocking control
while 1: while 1:
yield waituntil, self, self.next[0].canAccept #wait until the next object is free # wait the event canDispose, this means that the station can deliver the item to successor
if self.next[0].getGiverObject()==self: #if the free object can accept from this Assembly event=yield waitevent, self, self.canDispose #[self.canDispose, self.interruptionStart]
#break. Else continue # try to signal a receiver, if successful then proceed to get an other entity
if self.signalReceiver():
break
# TODO: router most probably should signal givers and not receivers in order to avoid this hold,self,0
# As the receiver (e.g.) a machine that follows the machine receives an loadOperatorAvailable event,
# signals the preceding station (e.g. self.machine) and immediately after that gets the entity.
# the preceding machine gets the canDispose signal which is actually useless, is emptied by the following station
# and then cannot exit an infinite loop.
yield hold, self, 0
# if while waiting (for a canDispose event) became free as the machines that follows emptied it, then proceed
if not self.haveToDispose():
break break
#self.totalBlockageTime+=now()-startBlockageTime #add the blockage time #self.totalBlockageTime+=now()-startBlockageTime #add the blockage time
#checks if the Assembly can accept an entity #===========================================================================
# checks if the Assembly can accept an entity
#===========================================================================
def canAccept(self, callerObject=None): def canAccept(self, callerObject=None):
return len(self.getActiveObjectQueue())==0 # get active and giver objects
activeObject=self.getActiveObject()
#checks if the Assembly can accept an entity and there is a Frame waiting for it
def canAcceptAndIsRequested(self):
activeObjectQueue=self.getActiveObjectQueue() activeObjectQueue=self.getActiveObjectQueue()
#if there is no caller then perform the default
#loop through the possible givers if(callerObject==None):
for object in self.previous: return len(activeObjectQueue)==0
#activate only if the possible giver is not empty thecaller=callerObject
if(len(object.getActiveObjectQueue())>0): # if the object holds nothing then return true
#activate only if the caller carries Frame if len(self.getActiveObjectQueue())==0:
if(object.getActiveObjectQueue()[0].type=='Frame'): return not activeObject.entryIsAssignedTo()
#update the giver # if it holds already a frame then return true only for parts and if the frame has still space
self.giver=object if len(activeObjectQueue[0].getFrameQueue())<activeObjectQueue[0].capacity:
return len(activeObjectQueue)==0 and object.haveToDispose(self) if callerObject.getActiveObjectQueue()[0].type=='Part':
return not activeObject.entryIsAssignedTo()
return False return False
#checks if the Assembly can accept an entity and there is a Frame waiting for it #===========================================================================
def isRequestedFromPart(self): # checks if the Assembly can accept a part or a Frame
#===========================================================================
def canAcceptAndIsRequested(self, callerObject=None):
# get the active and the giver objects
activeObject=self.getActiveObject()
activeObjectQueue=self.getActiveObjectQueue() activeObjectQueue=self.getActiveObjectQueue()
# giverObject=self.getGiverObject()
#loop through the possible givers giverObject=callerObject
for object in self.previous: assert giverObject, 'there must be a caller for canAcceptAndIsRequested'
#activate only if the possible giver is not empty if(len(giverObject.getActiveObjectQueue())>0):
if(len(object.getActiveObjectQueue())>0): #activate only if the caller carries Frame
if(giverObject.getActiveObjectQueue()[0].type=='Frame'):
return len(activeObjectQueue)==0 and giverObject.haveToDispose(activeObject)
#activate only if the caller carries Part #activate only if the caller carries Part
if(object.getActiveObjectQueue()[0].type=='Part'): if(giverObject.getActiveObjectQueue()[0].type=='Part'):
#update giver return len(activeObjectQueue)==1 and giverObject.haveToDispose(activeObject)
self.giver=object
return len(activeObjectQueue)==1 and object.haveToDispose(self)
return False return False
#checks if the Assembly can dispose an entity to the following object #===========================================================================
# checks if the Assembly can dispose an entity to the following object
#===========================================================================
def haveToDispose(self, callerObject=None): def haveToDispose(self, callerObject=None):
return len(self.getActiveObjectQueue())>0 and self.waitToDispose # get active object and its queue
activeObject=self.getActiveObject()
activeObjectQueue=self.getActiveObjectQueue()
#if we have only one possible receiver just check if the Queue holds one or more entities
if(callerObject==None):
return len(activeObjectQueue)>0
#removes an entity from the Assembly thecaller=callerObject
return len(activeObjectQueue)>0 and (thecaller in activeObject.next) and activeObject.waitToDispose
#===========================================================================
# removes an entity from the Assembly
#===========================================================================
def removeEntity(self, entity=None): def removeEntity(self, entity=None):
activeObject=self.getActiveObject()
activeEntity=CoreObject.removeEntity(self, entity) #run the default method activeEntity=CoreObject.removeEntity(self, entity) #run the default method
self.waitToDispose=False self.waitToDispose=False
return activeEntity #the object does not wait to dispose now if self.canAccept():
self.printTrace(self.id, 'will try signalling a giver from removeEntity')
self.signalGiver()
return activeEntity
#gets an entity from the giver #===========================================================================
#it may handle both Parts and Frames # gets an entity from the giver
# it may handle both Parts and Frames
#===========================================================================
def getEntity(self, type): def getEntity(self, type):
activeObject=self.getActiveObject() activeObject=self.getActiveObject()
activeObjectQueue=self.getActiveObjectQueue() activeObjectQueue=self.getActiveObjectQueue()
giverObject=self.getGiverObject() giverObject=self.getGiverObject()
giverObject.sortEntities() #sort the Entities of the giver according to the scheduling rule if applied giverObject.sortEntities() #sort the Entities of the giver according to the scheduling rule if applied
giverObjectQueue=self.getGiverObjectQueue() giverObjectQueue=self.getGiverObjectQueue()
activeEntity=giverObjectQueue[0] # if the giverObject is blocked then unBlock it
if giverObject.exitIsAssignedTo():
if(type=="Part"): giverObject.unAssignExit()
activeObjectQueue[0].getFrameQueue().append(activeEntity) #get the part from the giver and append it to the frame! # if the activeObject entry is blocked then unBlock it
giverObject.removeEntity(activeEntity) #remove the part from the previews object if activeObject.entryIsAssignedTo():
self.outputTrace(activeEntity.name, "got into "+ self.objName) activeObject.unAssignEntry()
elif(type=="Frame"):
activeObjectQueue.append(giverObjectQueue[0]) #get the frame from the giver activeEntity=self.identifyEntityToGet()
giverObject.removeEntity(activeEntity) #remove the frame from the previews object assert activeEntity.type==type, 'the type of the entity to get must be of type '+type+' while it is '+activeEntity.type
#remove the entity from the previews object
giverObject.removeEntity(activeEntity)
self.printTrace(activeEntity.name, "got into "+self.id)
self.outputTrace(activeEntity.name, "got into "+ self.objName) self.outputTrace(activeEntity.name, "got into "+ self.objName)
# if the type is Frame
if(activeEntity.type=="Frame"):
self.nameLastEntityEntered=activeEntity.name self.nameLastEntityEntered=activeEntity.name
self.timeLastEntityEntered=now() self.timeLastEntityEntered=now()
activeEntity.currentStation=self activeEntity.currentStation=self
# if the frame is not fully loaded then signal a giver
if len(activeObjectQueue[0].getFrameQueue())<activeObjectQueue[0].capacity:
self.printTrace(self.id, 'will try signalling a giver from getEntity')
self.signalGiver()
return activeEntity return activeEntity
#actions to be taken after the simulation ends #===========================================================================
# appends entity to the receiver object. to be called by the removeEntity of the giver
# this method is created to be overridden by the Assembly class in its getEntity where Frames are loaded
#===========================================================================
def appendEntity(self,entity=None):
activeObject=self.getActiveObject()
activeObjectQueue=self.getActiveObjectQueue()
assert entity, 'the entity to be appended cannot be None'
if entity.type=='Part':
activeObjectQueue[0].getFrameQueue().append(entity) #get the part from the giver and append it to the frame!
elif entity.type=='Frame':
activeObjectQueue.append(entity) #get the frame and append it to the internal queue
#===========================================================================
# actions to be taken after the simulation ends
#===========================================================================
def postProcessing(self, MaxSimtime=None): def postProcessing(self, MaxSimtime=None):
if MaxSimtime==None: if MaxSimtime==None:
...@@ -241,7 +332,9 @@ class Assembly(CoreObject): ...@@ -241,7 +332,9 @@ class Assembly(CoreObject):
self.Working.append(100*self.totalWorkingTime/MaxSimtime) self.Working.append(100*self.totalWorkingTime/MaxSimtime)
self.Blockage.append(100*self.totalBlockageTime/MaxSimtime) self.Blockage.append(100*self.totalBlockageTime/MaxSimtime)
#outputs data to "output.xls" #===========================================================================
# outputs data to "output.xls"
#===========================================================================
def outputResultsXL(self, MaxSimtime=None): def outputResultsXL(self, MaxSimtime=None):
from Globals import G from Globals import G
from Globals import getConfidenceIntervals from Globals import getConfidenceIntervals
...@@ -280,7 +373,9 @@ class Assembly(CoreObject): ...@@ -280,7 +373,9 @@ class Assembly(CoreObject):
G.outputIndex+=1 G.outputIndex+=1
G.outputIndex+=1 G.outputIndex+=1
#outputs results to JSON File #===========================================================================
# outputs results to JSON File
#===========================================================================
def outputResultsJSON(self): def outputResultsJSON(self):
from Globals import G from Globals import G
from Globals import getConfidenceIntervals from Globals import getConfidenceIntervals
......
...@@ -262,7 +262,7 @@ class CoreObject(Process): ...@@ -262,7 +262,7 @@ class CoreObject(Process):
# and if preemption is required # and if preemption is required
#=========================================================================== #===========================================================================
def preemptReceiver(self): def preemptReceiver(self):
self.printTrace(self.id, 'trying to preempt a receiver') # self.printTrace(self.id, 'trying to preempt a receiver')
activeObject=self.getActiveObject() activeObject=self.getActiveObject()
activeObjectQueue=self.getActiveObjectQueue() activeObjectQueue=self.getActiveObjectQueue()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment