Router findCandidateReceivers clean-up

parent 0357c825
...@@ -112,15 +112,15 @@ class Operator(ObjectResource): ...@@ -112,15 +112,15 @@ class Operator(ObjectResource):
#=========================================================================== #===========================================================================
# method that finds a candidate entity for an operator # method that finds a candidate entity for an operator
#=========================================================================== #===========================================================================
def findCandidateStation(self, conflictingStations=[]): def findCandidateStation(self):
conflicting=conflictingStations
from Globals import G from Globals import G
candidateStation=next(x for x in self.candidateStations if not x in conflicting) router=G.Router
if not G.Router.sorting: candidateStation=next(x for x in self.candidateStations if not x in router.conflictingStations)
if not router.sorting:
if not candidateStation: if not candidateStation:
candidateStation=next(x for x in self.candidateStations) candidateStation=next(x for x in self.candidateStations)
conflicting.append(candidateStation) router.conflictingStations.append(candidateStation)
return candidateStation, conflictingStations return candidateStation
#=========================================================================== #===========================================================================
# recursive method that searches for entities with available receivers # recursive method that searches for entities with available receivers
......
...@@ -64,6 +64,7 @@ class Router(ObjectInterruption): ...@@ -64,6 +64,7 @@ class Router(ObjectInterruption):
self.conflictingOperators=[] # list with the operators that have candidateEntity with conflicting candidateReceivers self.conflictingOperators=[] # list with the operators that have candidateEntity with conflicting candidateReceivers
self.conflictingEntities=[] # entities with conflictingReceivers self.conflictingEntities=[] # entities with conflictingReceivers
self.conflictingStations=[] # stations with conflicting operators
self.occupiedReceivers=[] # occupied candidateReceivers of a candidateEntity self.occupiedReceivers=[] # occupied candidateReceivers of a candidateEntity
self.entitiesWithOccupiedReceivers=[] # list of entities that have no available receivers self.entitiesWithOccupiedReceivers=[] # list of entities that have no available receivers
...@@ -90,6 +91,7 @@ class Router(ObjectInterruption): ...@@ -90,6 +91,7 @@ class Router(ObjectInterruption):
self.toBeSignalled=[] self.toBeSignalled=[]
self.conflictingOperators=[] self.conflictingOperators=[]
self.conflictingEntities=[] self.conflictingEntities=[]
self.conflictingStations=[]
self.occupiedReceivers=[] self.occupiedReceivers=[]
self.entitiesWithOccupiedReceivers=[] self.entitiesWithOccupiedReceivers=[]
...@@ -97,10 +99,8 @@ class Router(ObjectInterruption): ...@@ -97,10 +99,8 @@ class Router(ObjectInterruption):
# the run method # the run method
# ======================================================================= # =======================================================================
''' '''
all the pendingEntities that are hot should be examined if they can proceed to the next after the events are over, assign the operators to machines for loading or simple processing
step of their route as they may not be first in the activeQueue of their currentStations (QueueManagedJob). read the pendingEntities currentStations, these are the stations (queues) that may be signalled
If they can be disposed to the next object then the router should wait again till the machine to receive them
returns canAcceptAndIsRequested (inPositionToGet is True)
''' '''
def run(self): def run(self):
from Globals import G from Globals import G
...@@ -124,9 +124,7 @@ class Router(ObjectInterruption): ...@@ -124,9 +124,7 @@ class Router(ObjectInterruption):
self.printTrace('','there are NO more events for now') self.printTrace('','there are NO more events for now')
break break
self.printTrace('','=-'*15) self.printTrace('','=-'*15)
# after the events are over, assign the operators to machines for loading or simple processing
# read the pendingEntities currentStations, these are the stations (queues) that may be signalled
# the activeCallers list is updated each time the canAcceptAndIsRequested returns true even if the machine is not signalled
# find the pending objects # find the pending objects
self.findPendingObjects() self.findPendingObjects()
...@@ -227,7 +225,7 @@ class Router(ObjectInterruption): ...@@ -227,7 +225,7 @@ class Router(ObjectInterruption):
del self.toBeSignalled[:] del self.toBeSignalled[:]
del self.multipleCriterionList[:] del self.multipleCriterionList[:]
del self.conflictingOperators[:] del self.conflictingOperators[:]
del self.conflictingOperators[:] del self.conflictingStations[:]
del self.conflictingEntities[:] del self.conflictingEntities[:]
del self.occupiedReceivers[:] del self.occupiedReceivers[:]
del self.entitiesWithOccupiedReceivers[:] del self.entitiesWithOccupiedReceivers[:]
...@@ -478,53 +476,38 @@ class Router(ObjectInterruption): ...@@ -478,53 +476,38 @@ class Router(ObjectInterruption):
# TODO: have to sort again after choosing candidateEntity # TODO: have to sort again after choosing candidateEntity
#======================================================================= #=======================================================================
def findCandidateReceivers(self): def findCandidateReceivers(self):
# finally we have to sort before giving the entities to the operators
# If there is an entity which must have priority then it should be assigned first
# TODO: sorting after choosing candidateEntity
if not self.managed: if not self.managed:
# initialise local variables occupiedReceivers and entitiesWithOccupiedReceivers
conflictingStations=[] # list with the operators that have candidateEntity with conflicting candidateReceivers
conflictingOperators=[]
# finally we have to sort before giving the entities to the operators
# If there is an entity which must have priority then it should be assigned first
# # TODO: sorting after choosing candidateEntity
# for the candidateOperators that do have candidateEntities pick a candidateEntity # for the candidateOperators that do have candidateEntities pick a candidateEntity
for operator in [x for x in self.candidateOperators if x.candidateStations]: for operator in [x for x in self.candidateOperators if x.candidateStations]:
# find the first available entity that has no occupied receivers # find the first available entity that has no occupied receivers
operator.candidateStation, conflictingStations = operator.findCandidateStation(conflictingStations) operator.candidateStation = operator.findCandidateStation()
# find the resources that are 'competing' for the same station # find the resources that are 'competing' for the same station
if not self.sorting: if not self.sorting:
# if there are entities that have conflicting receivers # if there are entities that have conflicting receivers
if len(conflictingStations): if len(self.conflictingStations):
for operator in self.candidateOperators: self.conflictingOperators=[operator for operator in self.candidateOperators\
if operator.candidateStation in conflictingStations: if operator.candidateStation in self.conflictingStations]
conflictingOperators.append(operator) # keep the sorting provided by the queues if there is conflict between operators
self.conflictingOperators=conflictingOperators conflictingGroup=[] # list that holds the operators that have the same recipient
# keep the sorting provided by the queues if there is conflict between operators if self.conflictingOperators:
conflictingGroup=[] # list that holds the operators that have the same recipient # for each of the candidateReceivers
if not self.sorting and self.conflictingOperators: for station in self.conflictingStations:
# for each of the candidateReceivers # find the group of operators that compete for this station
for station in conflictingStations: conflictingGroup=[operator for operator in self.conflictingOperators if operator.candidateStation==station]
# find the group of operators that compete for this station # the operator that can proceed is the manager of the entity as sorted by the queue that holds them
conflictingGroup=[operator for operator in self.conflictingOperators if operator.candidateStation==station] conflictingGroup.sort()
# the operator that can proceed is the manager of the entity as sorted by the queue that holds them # the operators that are not first in the list cannot proceed
conflictingGroup.sort() for operator in conflitingGroup:
# the operators that are not first in the list cannot proceed if conflictingGroup.index(operator)!=0:
for operator in conflitingGroup: self.candidateOperators.remove(operator)
if conflictingGroup.index(operator)!=0:
self.candidateOperators.remove(operator)
self.calledOperators.remove(operator)
# if the moving entities are managed # if the moving entities are managed
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
else: else:
# initialise local variable conflictingOperators
conflictingOperators=[] # list with the operators that have candidateEntity with conflicting candidateReceivers
# # finally we have to sort before giving the entities to the operators
# # If there is an entity which must have priority then it should be assigned first
# # TODO: sorting after choosing candidateEntity
# for the candidateOperators that do have candidateEntities pick a candidateEntity # for the candidateOperators that do have candidateEntities pick a candidateEntity
for operator in [x for x in self.candidateOperators if x.candidateEntities]: for operator in [x for x in self.candidateOperators if x.candidateEntities]:
# find the first available entity that has no occupied receivers # find the first available entity that has no occupied receivers
...@@ -534,36 +517,34 @@ class Router(ObjectInterruption): ...@@ -534,36 +517,34 @@ class Router(ObjectInterruption):
operator.candidateEntity.candidateReceiver=operator.candidateEntity.currentStation operator.candidateEntity.candidateReceiver=operator.candidateEntity.currentStation
else: else:
operator.candidateEntity.candidateReceiver=operator.candidateEntity.findCandidateReceiver() operator.candidateEntity.candidateReceiver=operator.candidateEntity.findCandidateReceiver()
# find the resources that are 'competing' for the same station # find the resources that are 'competing' for the same station
if not self.sorting: if not self.sorting:
# if there are entities that have conflicting receivers # if there are entities that have conflicting receivers
if len(self.conflictingEntities): if len(self.conflictingEntities):
for operator in self.candidateOperators: # find the conflictingOperators
if operator.candidateEntity in self.conflictingEntities: self.conflictingOperators=[operator for operator in self.candidateOperators\
conflictingOperators.append(operator) if operator.candidateEntity in self.conflictingEntities or\
elif operator.candidateEntity.candidateReceiver in [x.candidateReceiver for x in self.conflictingEntities]: operator.candidateEntity.candidateReceiver in [x.candidateReceiver for x in self.conflictingEntities]]
conflictingOperators.append(operator) # keep the sorting provided by the queues if there is conflict between operators
self.conflictingOperators=conflictingOperators conflictingGroup=[] # list that holds the operators that have the same recipient
# keep the sorting provided by the queues if there is conflict between operators if len(self.conflictingOperators):
conflictingGroup=[] # list that holds the operators that have the same recipient # for each of the candidateReceivers
if not self.sorting and self.conflictingOperators: for receiver in [x.candidateEntity.candidateReceiver for x in self.conflictingOperators]:
# for each of the candidateReceivers # find the group of operators that compete for this station
for receiver in [x.candidateEntity.candidateReceiver for x in self.conflictingOperators]: conflictingGroup=[operator for operator in self.conflictingOperators if operator.candidateEntity.candidateReceiver==receiver]
# find the group of operators that compete for this station assert len([station for station in [x.candidateEntity.currentStation for x in conflictingGroup]]),\
conflictingGroup=[operator for operator in self.conflictingOperators if operator.candidateEntity.candidateReceiver==receiver] 'the conflicting entities must reside in the same queue'
assert len([station for station in [x.candidateEntity.currentStation for x in conflictingGroup]]),\ # for each of the competing for the same station operators
'the conflicting entities must reside in the same queue' for operator in conflictingGroup:
# for each of the competing for the same station operators # find the index of entities to be operated by them in the queue that holds them
for operator in conflictingGroup: operator.ind=operator.candidateEntity.currentStation.getActiveObjectQueue().index(operator.candidateEntity)
# find the index of entities to be operated by them in the queue that holds them # the operator that can proceed is the manager of the entity as sorted by the queue that holds them
operator.ind=operator.candidateEntity.currentStation.getActiveObjectQueue().index(operator.candidateEntity) conflictingGroup.sort(key=lambda x: x.ind)
# the operator that can proceed is the manager of the entity as sorted by the queue that holds them # the operators that are not first in the list cannot proceed
conflictingGroup.sort(key=lambda x: x.ind) for operator in conflictingGroup:
# the operators that are not first in the list cannot proceed if conflictingGroup.index(operator)!=0:
for operator in conflictingGroup: self.candidateOperators.remove(operator)
if conflictingGroup.index(operator)!=0:
self.candidateOperators.remove(operator)
self.calledOperators.remove(operator)
if self.managed: if self.managed:
self.printTrace('candidateReceivers for each entity ',[(str(entity.id),\ self.printTrace('candidateReceivers for each entity ',[(str(entity.id),\
......
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