def smallest(self): heap = self._heap v, k = heap[0] while k not in self or self[k] != v: heapq.heappop(heap) v, k = heap[0] return k
def pop_smallest(self): heap = self._heap v, k = heapq.heappop(heap) while k not in self or self[k] != v: v, k = heapq.heappop(heap) del self[k] return k
def pop_smallest(self): """Return the item with the lowest priority and remove it. Raises IndexError if the object is empty. """ heap = self._heap v, k = heapq.heappop(heap) while k not in self or self[k] != v: v, k = heapq.heappop(heap) del self[k] return k
def smallest(self): """Return the item with the lowest priority. Raises IndexError if the object is empty. """ heap = self._heap v, k = heap[0] while k not in self or self[k] != v: heapq.heappop(heap) v, k = heap[0] return k
def Pass(self, sequenceNo): if self.closed: return if self.expected is None: self.expected = sequenceNo if sequenceNo < self.expected: return while sequenceNo > self.expected: me = (sequenceNo, stackless.getcurrent()) heapq.heappush(self.queue, me) self.OnSleep(sequenceNo) stackless.schedule_remove() self.OnWakeUp(sequenceNo) if self.closed: return self.Assert(self.expected == sequenceNo) self.expected += 1 expected = self.expected while self.queue and self.queue[0][0] == expected: self.OnWakingUp(sequenceNo, expected) expected += 1 other = heapq.heappop(self.queue) other[1].insert() if self.lastThrough is not None: self.Assert(self.lastThrough + 1 == sequenceNo) self.lastThrough = sequenceNo
def Pass(self, sequenceNo): """SequenceNo must be a monotonously incrementing integer""" if self.closed: return if self.expected is None: self.expected = sequenceNo if sequenceNo < self.expected: return while sequenceNo > self.expected: me = (sequenceNo, stackless.getcurrent()) heapq.heappush(self.queue, me) self.OnSleep(sequenceNo) stackless.schedule_remove() self.OnWakeUp(sequenceNo) if self.closed: return self.Assert(self.expected == sequenceNo) self.expected += 1 expected = self.expected while self.queue and self.queue[0][0] == expected: self.OnWakingUp(sequenceNo, expected) expected += 1 other = heapq.heappop(self.queue) other[1].insert() if self.lastThrough is not None: self.Assert(self.lastThrough + 1 == sequenceNo) self.lastThrough = sequenceNo
def RunSimulation(self, runSimUntil = None, beNice = True): if self.colonyData is None: raise RuntimeError('Attempting to run simulation on a colony without attached colonyData') with self.planetBroker.LockedService(self.ownerID): if self.currentSimTime is None: self.RecalculateCurrentSimTime() if self.currentSimTime is None: raise RuntimeError('CurrentSimTime is none for character', self.ownerID, '. This is a fatal error that can cause infinite looping.') simEndTime = runSimUntil if runSimUntil is not None else blue.os.GetWallclockTime() with bluepy.Timer('BaseColony::PrimeSimulation'): self.PrimeSimulation(simEndTime) with bluepy.Timer('BaseColony::RunSimulation'): while len(self.simQueue) > 0: simTime, simPinID = heapq.heappop(self.simQueue) if simTime > simEndTime: break self.currentSimTime = simTime simPin = self.GetPin(simPinID) if simPin is None: log.LogTraceback('Unable to find scheduled pin! This should never happen!') continue with bluepy.TimerPush('EvaluatePin::' + simPin.__guid__): if not simPin.CanRun(self.currentSimTime): continue self.EvaluatePin(simPin) if beNice: blue.pyos.BeNice() self.currentSimTime = simEndTime return simEndTime
def RouteCommodityOutput(self, sourcePin, commodities): if self.colonyData is None: raise RuntimeError('No colony data attached - cannot route commodity output') pinsReceivingCommodities = {} done = False for isStorageRoutes, listOfRoutes in enumerate(self.colonyData.GetSortedRoutesForPin(sourcePin.id, commodities)): if done: break while listOfRoutes: dummy, (destID, commodityTypeID, qty) = heapq.heappop(listOfRoutes) maxAmount = None if isStorageRoutes: maxAmount = math.ceil(float(commodities.get(commodityTypeID, 0)) / (len(listOfRoutes) + 1)) typeID, qty = self.TransferCommodities(sourcePin.id, destID, commodityTypeID, qty, commodities, maxAmount=maxAmount) if typeID in commodities: commodities[typeID] -= qty if commodities[typeID] <= 0: del commodities[typeID] if qty > 0: if destID not in pinsReceivingCommodities: pinsReceivingCommodities[destID] = {} if typeID not in pinsReceivingCommodities[destID]: pinsReceivingCommodities[destID][typeID] = 0 pinsReceivingCommodities[destID][typeID] += qty if len(commodities) <= 0: done = True break for receivingPinID, commodsAdded in pinsReceivingCommodities.iteritems(): receivingPin = self.GetPin(receivingPinID) if receivingPin.IsConsumer(): self.SchedulePin(receivingPin) if not sourcePin.IsStorage() and receivingPin.IsStorage(): self.LogInfo('RouteCommodityOutput :: Redistributing added commods', commodsAdded, 'from', sourcePin.id, 'via', receivingPin.id) self.RouteCommodityOutput(receivingPin, commodsAdded)
def CheckForStopCandidates(self): now = blue.os.GetSimTime() while self.stopCandidates and self.stopCandidates[0].isZombie: heapq.heappop(self.stopCandidates) while self.stopCandidates and self.stopCandidates[0].stamp < now: activation = heapq.heappop(self.stopCandidates) state = activation.Repeat() if state == -1: self.LogInfo('FxSequencer::CheckForStopCandidates: activation expired') self.RemoveActivation(activation) else: heapq.heappush(self.stopCandidates, activation) while self.stopCandidates and self.stopCandidates[0].isZombie: heapq.heappop(self.stopCandidates) now = blue.os.GetSimTime()
def RunSimulation(self, runSimUntil = None, beNice = True): """ This runs the core simulation for this colony object. It returns the time at which the simulation terminated. USAGE NOTES: While this method is running, the owner's network MUST NOT be modified. This method yields frequently. You must make sure to acquire a service lock on this colony's ownerID whenever you wish to modify the network. See the below with-statement for an example. Optional Parameters: runSimUntil - A blue time. This is the time at which the simulation run will terminate. If not supplied, the simulation will run to the current time. This is primarily of interest to the client, which needs to run until a given timestamp to match the server, then perform an action, then continue running. beNice - Boolean. If true, a BeNice call will be made after each pin evaluation, so a running simulation does not freeze up the process if it's running for a long time. """ if self.colonyData is None: raise RuntimeError('Attempting to run simulation on a colony without attached colonyData') with self.planetBroker.LockedService(self.ownerID): if self.currentSimTime is None: self.RecalculateCurrentSimTime() if self.currentSimTime is None: raise RuntimeError('CurrentSimTime is none for character', self.ownerID, '. This is a fatal error that can cause infinite looping.') simEndTime = runSimUntil if runSimUntil is not None else blue.os.GetWallclockTime() with bluepy.Timer('BaseColony::PrimeSimulation'): self.PrimeSimulation(simEndTime) with bluepy.Timer('BaseColony::RunSimulation'): while len(self.simQueue) > 0: simTime, simPinID = heapq.heappop(self.simQueue) if simTime > simEndTime: break self.currentSimTime = simTime simPin = self.GetPin(simPinID) if simPin is None: log.LogTraceback('Unable to find scheduled pin! This should never happen!') continue with bluepy.TimerPush('EvaluatePin::' + simPin.__guid__): if not simPin.CanRun(self.currentSimTime): continue self.EvaluatePin(simPin) if beNice: blue.pyos.BeNice() self.currentSimTime = simEndTime return simEndTime
def RouteCommodityOutput(self, sourcePin, commodities): if self.colonyData is None: raise RuntimeError( 'No colony data attached - cannot route commodity output') pinsReceivingCommodities = {} done = False for isStorageRoutes, listOfRoutes in enumerate( self.colonyData.GetSortedRoutesForPin(sourcePin.id, commodities)): if done: break while listOfRoutes: dummy, (destID, commodityTypeID, qty) = heapq.heappop(listOfRoutes) maxAmount = None if isStorageRoutes: maxAmount = math.ceil( float(commodities.get(commodityTypeID, 0)) / (len(listOfRoutes) + 1)) typeID, qty = self.TransferCommodities(sourcePin.id, destID, commodityTypeID, qty, commodities, maxAmount=maxAmount) if typeID in commodities: commodities[typeID] -= qty if commodities[typeID] <= 0: del commodities[typeID] if qty > 0: if destID not in pinsReceivingCommodities: pinsReceivingCommodities[destID] = {} if typeID not in pinsReceivingCommodities[destID]: pinsReceivingCommodities[destID][typeID] = 0 pinsReceivingCommodities[destID][typeID] += qty if len(commodities) <= 0: done = True break for receivingPinID, commodsAdded in pinsReceivingCommodities.iteritems( ): receivingPin = self.GetPin(receivingPinID) if receivingPin.IsConsumer(): self.SchedulePin(receivingPin) if not sourcePin.IsStorage() and receivingPin.IsStorage(): self.LogInfo( 'RouteCommodityOutput :: Redistributing added commods', commodsAdded, 'from', sourcePin.id, 'via', receivingPin.id) self.RouteCommodityOutput(receivingPin, commodsAdded)
def RunSimulation(self, runSimUntil=None, beNice=True): if self.colonyData is None: raise RuntimeError( 'Attempting to run simulation on a colony without attached colonyData' ) with self.planetBroker.LockedService(self.ownerID): if self.currentSimTime is None: self.RecalculateCurrentSimTime() if self.currentSimTime is None: raise RuntimeError( 'CurrentSimTime is none for character', self.ownerID, '. This is a fatal error that can cause infinite looping.') simEndTime = runSimUntil if runSimUntil is not None else blue.os.GetWallclockTime( ) with bluepy.Timer('BaseColony::PrimeSimulation'): self.PrimeSimulation(simEndTime) with bluepy.Timer('BaseColony::RunSimulation'): while len(self.simQueue) > 0: simTime, simPinID = heapq.heappop(self.simQueue) if simTime > simEndTime: break self.currentSimTime = simTime simPin = self.GetPin(simPinID) if simPin is None: log.LogTraceback( 'Unable to find scheduled pin! This should never happen!' ) continue with bluepy.TimerPush('EvaluatePin::' + simPin.__guid__): if not simPin.CanRun(self.currentSimTime): continue self.EvaluatePin(simPin) if beNice: blue.pyos.BeNice() self.currentSimTime = simEndTime return simEndTime
def close(self): self.closed = True while self.queue: heapq.heappop(self.queue)[1].insert()
def PurgeTriggers(self): now = blue.os.GetSimTime() while self.triggers and self.triggers[0].stamp <= now: heapq.heappop(self.triggers) return self.UpdateRepeatTime()