Esempio n. 1
0
    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
Esempio n. 2
0
    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 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
Esempio n. 5
0
    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
Esempio n. 6
0
    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
Esempio n. 7
0
    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
Esempio n. 8
0
    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
Esempio n. 9
0
    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
Esempio n. 10
0
    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)
Esempio n. 11
0
    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()
Esempio n. 12
0
    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()
Esempio n. 13
0
    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
Esempio n. 14
0
    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)
Esempio n. 15
0
    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
Esempio n. 16
0
 def close(self):
     self.closed = True
     while self.queue:
         heapq.heappop(self.queue)[1].insert()
Esempio n. 17
0
    def PurgeTriggers(self):
        now = blue.os.GetSimTime()
        while self.triggers and self.triggers[0].stamp <= now:
            heapq.heappop(self.triggers)

        return self.UpdateRepeatTime()
Esempio n. 18
0
    def PurgeTriggers(self):
        now = blue.os.GetSimTime()
        while self.triggers and self.triggers[0].stamp <= now:
            heapq.heappop(self.triggers)

        return self.UpdateRepeatTime()
Esempio n. 19
0
 def close(self):
     self.closed = True
     while self.queue:
         heapq.heappop(self.queue)[1].insert()