def createInCapacityStationBuffers(self):
        # loop through the exits
        for exit in G.CapacityStationExitList:
            # if the exit received nothing currently there is nothing to do
            if exit.currentlyObtainedEntities == []:
                continue
            buffer = exit.nextCapacityStationBuffer  # the next buffer
            # if it is the end of the system there is nothing to do
            if not buffer:
                exit.currentlyObtainedEntities = []
                continue
            previousStation = exit.previous[
                0]  # the station the the entity just finished from
            previousBuffer = previousStation.previous[
                0]  # the buffer of the station
            nextStation = buffer.next[0]  # the next processing station
            # for every entity calculate the new entity to be created in the next station and create it
            for entity in exit.currentlyObtainedEntities:
                project = entity.capacityProject
                # if the entity exits from an assembly station
                # and not all project is finished there, then do not create anything in the next
                if previousBuffer.requireFullProject:
                    projectFinishedFromLast = True
                    for e in previousBuffer.getActiveObjectQueue():
                        if e.capacityProject == project:
                            projectFinishedFromLast = False
                            break
                    if not projectFinishedFromLast:
                        continue

                entityCapacity = entity.requiredCapacity
                previousRequirement = float(
                    project.capacityRequirementDict[previousStation.id])
                nextRequirement = float(
                    project.capacityRequirementDict[nextStation.id])
                # if the previous station was assembly then in the next the full project arrives
                # so requires whatever the project requires
                if previousBuffer.requireFullProject:
                    nextStationCapacityRequirement = nextRequirement
                # else calculate proportionally
                else:
                    proportion = nextRequirement / previousRequirement
                    nextStationCapacityRequirement = proportion * entityCapacity
                entityToCreateName = entity.capacityProjectId + '_' + nextStation.objName + '_' + str(
                    nextStationCapacityRequirement)
                entityToCreate = CapacityEntity(
                    name=entityToCreateName,
                    capacityProjectId=entity.capacityProjectId,
                    requiredCapacity=nextStationCapacityRequirement)
                entityToCreate.currentStation = buffer
                entityToCreate.initialize()
                import dream.simulation.Globals as Globals
                Globals.setWIP([entityToCreate
                                ])  #set the new components as wip
            # reset the currently obtained entities list to empty
            exit.currentlyObtainedEntities = []
 def createInCapacityStationBuffers(self): 
     # loop through the exits   
     for exit in G.CapacityStationExitList:
         # if the exit received nothing currently there is nothing to do
         if exit.currentlyObtainedEntities==[]:
             continue
         buffer=exit.nextCapacityStationBuffer   # the next buffer
         # if it is the end of the system there is nothing to do
         if not buffer:
             exit.currentlyObtainedEntities=[]
             continue
         previousStation=exit.previous[0]  # the station the the entity just finished from
         previousBuffer=previousStation.previous[0]  # the buffer of the station
         nextStation=buffer.next[0]        # the next processing station
         # for every entity calculate the new entity to be created in the next station and create it  
         for entity in exit.currentlyObtainedEntities:
             project=entity.capacityProject
             # if the entity exits from an assembly station 
             # and not all project is finished there, then do not create anything in the next
             if previousBuffer.requireFullProject:
                 projectFinishedFromLast=True
                 for e in previousBuffer.getActiveObjectQueue():
                     if e.capacityProject==project:
                         projectFinishedFromLast=False
                         break
                 if not projectFinishedFromLast:
                     continue
                 
             entityCapacity=entity.requiredCapacity
             previousRequirement=float(project.capacityRequirementDict[previousStation.id])
             nextRequirement=float(project.capacityRequirementDict[nextStation.id])
             # if the previous station was assembly then in the next the full project arrives
             # so requires whatever the project requires
             if previousBuffer.requireFullProject:
                 nextStationCapacityRequirement=nextRequirement
             # else calculate proportionally
             else:
                 proportion=nextRequirement/previousRequirement
                 nextStationCapacityRequirement=proportion*entityCapacity
             entityToCreateName=entity.capacityProjectId+'_'+nextStation.objName+'_'+str(nextStationCapacityRequirement)
             entityToCreate=CapacityEntity(name=entityToCreateName, capacityProjectId=entity.capacityProjectId, 
                                           requiredCapacity=nextStationCapacityRequirement)
             entityToCreate.currentStation=buffer
             entityToCreate.initialize()
             import dream.simulation.Globals as Globals
             Globals.setWIP([entityToCreate])     #set the new components as wip                
         # reset the currently obtained entities list to empty
         exit.currentlyObtainedEntities=[]
 def mergeEntities(self):
     # loop through the capacity station buffers
     for buffer in G.CapacityStationBufferList:
         nextStation = buffer.next[0]
         projectList = []
         # loop through the entities to see what projects lie in the buffer
         for entity in buffer.getActiveObjectQueue():
             if entity.capacityProject not in projectList:
                 projectList.append(entity.capacityProject)
         for project in projectList:
             entitiesToBeMerged = []
             for entity in buffer.getActiveObjectQueue():
                 if entity.capacityProject == project:
                     entitiesToBeMerged.append(entity)
             totalCapacityRequirement = 0
             # if the buffer acts as assembly there is no need to calculate the total capacity requirement,
             # it will be the one that the project has as a total for this station
             if buffer.requireFullProject:
                 # find what has been already processed
                 alreadyProcessed = 0
                 for record in nextStation.detailedWorkPlan:
                     if record['project'] == project.id:
                         alreadyProcessed += float(record['allocation'])
                 totalCapacityRequirement = project.capacityRequirementDict[
                     nextStation.id] - alreadyProcessed
             # else calculate the total capacity requirement by adding the one each entity requires
             else:
                 for entity in entitiesToBeMerged:
                     totalCapacityRequirement += entity.requiredCapacity
             # erase the Entities to create the merged one
             for entity in entitiesToBeMerged:
                 buffer.getActiveObjectQueue().remove(entity)
             # create the merged entity
             entityToCreateName = entity.capacityProjectId + '_' + nextStation.objName + '_' + str(
                 totalCapacityRequirement)
             entityToCreate = CapacityEntity(
                 name=entityToCreateName,
                 capacityProjectId=project.id,
                 requiredCapacity=totalCapacityRequirement)
             entityToCreate.currentStation = buffer
             entityToCreate.initialize()
             import dream.simulation.Globals as Globals
             Globals.setWIP([entityToCreate
                             ])  #set the new components as wip
 def mergeEntities(self):
     # loop through the capacity station buffers
     for buffer in G.CapacityStationBufferList:
         nextStation=buffer.next[0]
         projectList=[]
         # loop through the entities to see what projects lie in the buffer
         for entity in buffer.getActiveObjectQueue():
             if entity.capacityProject not in projectList:
                 projectList.append(entity.capacityProject)
         for project in projectList:
             entitiesToBeMerged=[]
             for entity in buffer.getActiveObjectQueue():
                 if entity.capacityProject==project:
                     entitiesToBeMerged.append(entity)
             totalCapacityRequirement=0
             # if the buffer acts as assembly there is no need to calculate the total capacity requirement, 
             # it will be the one that the project has as a total for this station
             if buffer.requireFullProject:
                 # find what has been already processed
                 alreadyProcessed=0     
                 for record in nextStation.detailedWorkPlan:
                     if record['project']==project.id:
                         alreadyProcessed+=float(record['allocation'])
                 totalCapacityRequirement=project.capacityRequirementDict[nextStation.id]-alreadyProcessed
             # else calculate the total capacity requirement by adding the one each entity requires
             else:
                 for entity in entitiesToBeMerged:
                     totalCapacityRequirement+=entity.requiredCapacity
             # erase the Entities to create the merged one
             for entity in entitiesToBeMerged:
                 buffer.getActiveObjectQueue().remove(entity)
             # create the merged entity
             entityToCreateName=entity.capacityProjectId+'_'+nextStation.objName+'_'+str(totalCapacityRequirement)
             entityToCreate=CapacityEntity(name=entityToCreateName, capacityProjectId=project.id, 
                                           requiredCapacity=totalCapacityRequirement)
             entityToCreate.currentStation=buffer
             entityToCreate.initialize()
             import dream.simulation.Globals as Globals
             Globals.setWIP([entityToCreate])     #set the new components as wip                                            
 def breakEntity(self, entity, buffer, station, totalAvailableCapacity, totalRequestedCapacity):
     # calculate what is the capacity that should proceed and what that should remain
     capacityToMove=totalAvailableCapacity*(entity.requiredCapacity)/float(totalRequestedCapacity)
     capacityToStay=entity.requiredCapacity-capacityToMove
     # if capacityToMove is equal to 0 no need to break. Return false.
     if capacityToMove==0:
         return False
     else:
         # remove the capacity entity by the buffer so that the broken ones are created
         buffer.getActiveObjectQueue().remove(entity)
         entityToMoveName=entity.capacityProjectId+'_'+station.objName+'_'+str(capacityToMove)
         entityToMove=CapacityEntity(name=entityToMoveName, capacityProjectId=entity.capacityProjectId, requiredCapacity=capacityToMove)
         entityToMove.initialize()
         entityToMove.currentStation=buffer
         entityToMove.shouldMove=True
         entityToStayName=entity.capacityProjectId+'_'+station.objName+'_'+str(capacityToStay)
         entityToStay=CapacityEntity(name=entityToStayName, capacityProjectId=entity.capacityProjectId, requiredCapacity=capacityToStay)
         entityToStay.initialize()
         entityToStay.currentStation=buffer
         import dream.simulation.Globals as Globals
         Globals.setWIP([entityToMove,entityToStay])     #set the new components as wip    
         return True   
 def breakEntity(self, entity, buffer, station, totalAvailableCapacity,
                 totalRequestedCapacity):
     # calculate what is the capacity that should proceed and what that should remain
     capacityToMove = totalAvailableCapacity * (
         entity.requiredCapacity) / float(totalRequestedCapacity)
     capacityToStay = entity.requiredCapacity - capacityToMove
     # if capacityToMove is equal to 0 no need to break. Return false.
     if capacityToMove == 0:
         return False
     else:
         # remove the capacity entity by the buffer so that the broken ones are created
         buffer.getActiveObjectQueue().remove(entity)
         entityToMoveName = entity.capacityProjectId + '_' + station.objName + '_' + str(
             capacityToMove)
         entityToMove = CapacityEntity(
             name=entityToMoveName,
             capacityProjectId=entity.capacityProjectId,
             requiredCapacity=capacityToMove)
         entityToMove.initialize()
         entityToMove.currentStation = buffer
         entityToMove.shouldMove = True
         entityToStayName = entity.capacityProjectId + '_' + station.objName + '_' + str(
             capacityToStay)
         entityToStay = CapacityEntity(
             name=entityToStayName,
             capacityProjectId=entity.capacityProjectId,
             requiredCapacity=capacityToStay)
         entityToStay.initialize()
         entityToStay.currentStation = buffer
         import dream.simulation.Globals as Globals
         Globals.setWIP([entityToMove,
                         entityToStay])  #set the new components as wip
         return True