{-# LANGUAGE FlexibleContexts #-}

-- |
-- Module     : Simulation.Aivika.Trans.Queue.Infinite
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- This module defines an infinite queue that can use the specified strategies.
--
module Simulation.Aivika.Trans.Queue.Infinite
       (-- * Queue Types
        FCFSQueue,
        LCFSQueue,
        SIROQueue,
        PriorityQueue,
        Queue,
        -- * Creating Queue
        newFCFSQueue,
        newLCFSQueue,
        newSIROQueue,
        newPriorityQueue,
        newQueue,
        -- * Queue Properties and Activities
        enqueueStoringStrategy,
        dequeueStrategy,
        queueNull,
        queueCount,
        queueCountStats,
        enqueueStoreCount,
        dequeueCount,
        dequeueExtractCount,
        enqueueStoreRate,
        dequeueRate,
        dequeueExtractRate,
        queueWaitTime,
        dequeueWaitTime,
        queueRate,
        -- * Dequeuing and Enqueuing
        dequeue,
        dequeueWithOutputPriority,
        tryDequeue,
        enqueue,
        enqueueWithStoringPriority,
        queueDelete,
        queueDelete_,
        queueDeleteBy,
        queueDeleteBy_,
        queueContains,
        queueContainsBy,
        clearQueue,
        -- * Statistics Reset
        resetQueue,
        -- * Summary
        queueSummary,
        -- * Derived Signals for Properties
        queueNullChanged,
        queueNullChanged_,
        queueCountChanged,
        queueCountChanged_,
        enqueueStoreCountChanged,
        enqueueStoreCountChanged_,
        dequeueCountChanged,
        dequeueCountChanged_,
        dequeueExtractCountChanged,
        dequeueExtractCountChanged_,
        queueWaitTimeChanged,
        queueWaitTimeChanged_,
        dequeueWaitTimeChanged,
        dequeueWaitTimeChanged_,
        queueRateChanged,
        queueRateChanged_,
        -- * Basic Signals
        enqueueStored,
        dequeueRequested,
        dequeueExtracted,
        -- * Overall Signal
        queueChanged_) where

import Data.Monoid
import Data.Maybe

import Control.Monad
import Control.Monad.Trans

import Simulation.Aivika.Trans.Ref.Base
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Parameter
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Dynamics
import Simulation.Aivika.Trans.Internal.Event
import Simulation.Aivika.Trans.Internal.Process
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.Resource.Base
import Simulation.Aivika.Trans.QueueStrategy
import Simulation.Aivika.Trans.Statistics

-- | A type synonym for the ordinary FIFO queue also known as the FCFS
-- (First Come - First Serviced) queue.
type FCFSQueue m a = Queue m FCFS FCFS a

-- | A type synonym for the ordinary LIFO queue also known as the LCFS
-- (Last Come - First Serviced) queue.
type LCFSQueue m a = Queue m LCFS FCFS a

-- | A type synonym for the SIRO (Serviced in Random Order) queue.
type SIROQueue m a = Queue m SIRO FCFS a

-- | A type synonym for the queue with static priorities applied when
-- storing the elements in the queue.
type PriorityQueue m a = Queue m StaticPriorities FCFS a

-- | Represents an infinite queue using the specified strategies for
-- internal storing (in memory), @sm@, and dequeueing (output), @so@, where @a@ denotes
-- the type of items stored in the queue. As usual, type @m@ denotes
-- the underlying computation within which the simulation executes.
data Queue m sm so a =
  Queue { forall (m :: * -> *) sm so a. Queue m sm so a -> sm
enqueueStoringStrategy :: sm,
          -- ^ The strategy applied when storing (in memory) items in the queue.
          forall (m :: * -> *) sm so a. Queue m sm so a -> so
dequeueStrategy :: so,
          -- ^ The strategy applied to the dequeueing (output) processes.
          forall (m :: * -> *) sm so a.
Queue m sm so a -> StrategyQueue m sm (QueueItem a)
queueStore :: StrategyQueue m sm (QueueItem a),
          forall (m :: * -> *) sm so a. Queue m sm so a -> Resource m so
dequeueRes :: Resource m so,
          forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef :: Ref m Int,
          forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (TimingStats Int)
queueCountStatsRef :: Ref m (TimingStats Int),
          forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
enqueueStoreCountRef :: Ref m Int,
          forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueCountRef :: Ref m Int,
          forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueExtractCountRef :: Ref m Int,
          forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
queueWaitTimeRef :: Ref m (SamplingStats Double),
          forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
dequeueWaitTimeRef :: Ref m (SamplingStats Double),
          forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m a
enqueueStoredSource :: SignalSource m a,
          forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m ()
dequeueRequestedSource :: SignalSource m (),
          forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m a
dequeueExtractedSource :: SignalSource m a }

-- | Stores the item and a time of its enqueuing. 
data QueueItem a =
  QueueItem { forall a. QueueItem a -> a
itemValue :: a,
              -- ^ Return the item value.
              forall a. QueueItem a -> Double
itemStoringTime :: Double
              -- ^ Return the time of storing in the queue.
            }
  
-- | Create a new infinite FCFS queue.  
newFCFSQueue :: MonadDES m => Event m (FCFSQueue m a)
{-# INLINABLE newFCFSQueue #-}
newFCFSQueue :: forall (m :: * -> *) a. MonadDES m => Event m (FCFSQueue m a)
newFCFSQueue = forall (m :: * -> *) sm so a.
(MonadDES m, QueueStrategy m sm, QueueStrategy m so) =>
sm -> so -> Event m (Queue m sm so a)
newQueue FCFS
FCFS FCFS
FCFS
  
-- | Create a new infinite LCFS queue.  
newLCFSQueue :: MonadDES m => Event m (LCFSQueue m a)  
{-# INLINABLE newLCFSQueue #-}
newLCFSQueue :: forall (m :: * -> *) a. MonadDES m => Event m (LCFSQueue m a)
newLCFSQueue = forall (m :: * -> *) sm so a.
(MonadDES m, QueueStrategy m sm, QueueStrategy m so) =>
sm -> so -> Event m (Queue m sm so a)
newQueue LCFS
LCFS FCFS
FCFS
  
-- | Create a new infinite SIRO queue.  
newSIROQueue :: (MonadDES m, QueueStrategy m SIRO) => Event m (SIROQueue m a)  
{-# INLINABLE newSIROQueue #-}
newSIROQueue :: forall (m :: * -> *) a.
(MonadDES m, QueueStrategy m SIRO) =>
Event m (SIROQueue m a)
newSIROQueue = forall (m :: * -> *) sm so a.
(MonadDES m, QueueStrategy m sm, QueueStrategy m so) =>
sm -> so -> Event m (Queue m sm so a)
newQueue SIRO
SIRO FCFS
FCFS
  
-- | Create a new infinite priority queue.
newPriorityQueue :: (MonadDES m, QueueStrategy m StaticPriorities) => Event m (PriorityQueue m a)  
{-# INLINABLE newPriorityQueue #-}
newPriorityQueue :: forall (m :: * -> *) a.
(MonadDES m, QueueStrategy m StaticPriorities) =>
Event m (PriorityQueue m a)
newPriorityQueue = forall (m :: * -> *) sm so a.
(MonadDES m, QueueStrategy m sm, QueueStrategy m so) =>
sm -> so -> Event m (Queue m sm so a)
newQueue StaticPriorities
StaticPriorities FCFS
FCFS
  
-- | Create a new infinite queue with the specified strategies.  
newQueue :: (MonadDES m,
             QueueStrategy m sm,
             QueueStrategy m so) =>
            sm
            -- ^ the strategy applied when storing items in the queue
            -> so
            -- ^ the strategy applied to the dequeueing (output) processes when the queue is empty
            -> Event m (Queue m sm so a)  
{-# INLINABLE newQueue #-}
newQueue :: forall (m :: * -> *) sm so a.
(MonadDES m, QueueStrategy m sm, QueueStrategy m so) =>
sm -> so -> Event m (Queue m sm so a)
newQueue sm
sm so
so =
  do Double
t  <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
     Ref m Int
i  <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m (TimingStats Int)
is <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a b. (a -> b) -> a -> b
$ forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
     Ref m Int
cm <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m Int
cr <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m Int
co <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Int
0
     StrategyQueue m sm (QueueItem a)
qm <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) s a.
QueueStrategy m s =>
s -> Simulation m (StrategyQueue m s a)
newStrategyQueue sm
sm
     Resource m so
ro <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) s.
(MonadDES m, QueueStrategy m s) =>
s -> Int -> Maybe Int -> Simulation m (Resource m s)
newResourceWithMaxCount so
so Int
0 forall a. Maybe a
Nothing
     Ref m (SamplingStats Double)
w  <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a. Monoid a => a
mempty
     Ref m (SamplingStats Double)
wo <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a. Monoid a => a
mempty 
     SignalSource m a
s3 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m ()
s4 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m a
s5 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     forall (m :: * -> *) a. Monad m => a -> m a
return Queue { enqueueStoringStrategy :: sm
enqueueStoringStrategy = sm
sm,
                    dequeueStrategy :: so
dequeueStrategy = so
so,
                    queueStore :: StrategyQueue m sm (QueueItem a)
queueStore = StrategyQueue m sm (QueueItem a)
qm,
                    dequeueRes :: Resource m so
dequeueRes = Resource m so
ro,
                    queueCountRef :: Ref m Int
queueCountRef = Ref m Int
i,
                    queueCountStatsRef :: Ref m (TimingStats Int)
queueCountStatsRef = Ref m (TimingStats Int)
is,
                    enqueueStoreCountRef :: Ref m Int
enqueueStoreCountRef = Ref m Int
cm,
                    dequeueCountRef :: Ref m Int
dequeueCountRef = Ref m Int
cr,
                    dequeueExtractCountRef :: Ref m Int
dequeueExtractCountRef = Ref m Int
co,
                    queueWaitTimeRef :: Ref m (SamplingStats Double)
queueWaitTimeRef = Ref m (SamplingStats Double)
w,
                    dequeueWaitTimeRef :: Ref m (SamplingStats Double)
dequeueWaitTimeRef = Ref m (SamplingStats Double)
wo,
                    enqueueStoredSource :: SignalSource m a
enqueueStoredSource = SignalSource m a
s3,
                    dequeueRequestedSource :: SignalSource m ()
dequeueRequestedSource = SignalSource m ()
s4,
                    dequeueExtractedSource :: SignalSource m a
dequeueExtractedSource = SignalSource m a
s5 }

-- | Test whether the queue is empty.
--
-- See also 'queueNullChanged' and 'queueNullChanged_'.
queueNull :: MonadDES m => Queue m sm so a -> Event m Bool
{-# INLINABLE queueNull #-}
queueNull :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Bool
queueNull Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
n <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q)
     forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n forall a. Eq a => a -> a -> Bool
== Int
0)
  
-- | Signal when the 'queueNull' property value has changed.
queueNullChanged :: MonadDES m => Queue m sm so a -> Signal m Bool
{-# INLINABLE queueNullChanged #-}
queueNullChanged :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m Bool
queueNullChanged Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Bool
queueNull Queue m sm so a
q) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueNullChanged_ Queue m sm so a
q)
  
-- | Signal when the 'queueNull' property value has changed.
queueNullChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE queueNullChanged_ #-}
queueNullChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueNullChanged_ = forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueCountChanged_

-- | Return the current queue size.
--
-- See also 'queueCountStats', 'queueCountChanged' and 'queueCountChanged_'.
queueCount :: MonadDES m => Queue m sm so a -> Event m Int
{-# INLINABLE queueCount #-}
queueCount :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
queueCount Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q)

-- | Return the queue size statistics.
queueCountStats :: MonadDES m => Queue m sm so a -> Event m (TimingStats Int)
{-# INLINABLE queueCountStats #-}
queueCountStats :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m (TimingStats Int)
queueCountStats Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (TimingStats Int)
queueCountStatsRef Queue m sm so a
q)
  
-- | Signal when the 'queueCount' property value has changed.
queueCountChanged :: MonadDES m => Queue m sm so a -> Signal m Int
{-# INLINABLE queueCountChanged #-}
queueCountChanged :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m Int
queueCountChanged Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
queueCount Queue m sm so a
q) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueCountChanged_ Queue m sm so a
q)
  
-- | Signal when the 'queueCount' property value has changed.
queueCountChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE queueCountChanged_ #-}
queueCountChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueCountChanged_ Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
enqueueStored Queue m sm so a
q) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
dequeueExtracted Queue m sm so a
q)
      
-- | Return the total number of input items that were stored.
--
-- See also 'enqueueStoreCountChanged' and 'enqueueStoreCountChanged_'.
enqueueStoreCount :: MonadDES m => Queue m sm so a -> Event m Int
{-# INLINABLE enqueueStoreCount #-}
enqueueStoreCount :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
enqueueStoreCount Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
enqueueStoreCountRef Queue m sm so a
q)
  
-- | Signal when the 'enqueueStoreCount' property value has changed.
enqueueStoreCountChanged :: MonadDES m => Queue m sm so a -> Signal m Int
{-# INLINABLE enqueueStoreCountChanged #-}
enqueueStoreCountChanged :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m Int
enqueueStoreCountChanged Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
enqueueStoreCount Queue m sm so a
q) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
enqueueStoreCountChanged_ Queue m sm so a
q)
  
-- | Signal when the 'enqueueStoreCount' property value has changed.
enqueueStoreCountChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE enqueueStoreCountChanged_ #-}
enqueueStoreCountChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
enqueueStoreCountChanged_ Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
enqueueStored Queue m sm so a
q)
      
-- | Return the total number of requests for dequeueing the items,
-- not taking into account the failed attempts to dequeue immediately
-- without suspension.
--
-- See also 'dequeueCountChanged' and 'dequeueCountChanged_'.
dequeueCount :: MonadDES m => Queue m sm so a -> Event m Int
{-# INLINABLE dequeueCount #-}
dequeueCount :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
dequeueCount Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueCountRef Queue m sm so a
q)
      
-- | Signal when the 'dequeueCount' property value has changed.
dequeueCountChanged :: MonadDES m => Queue m sm so a -> Signal m Int
{-# INLINABLE dequeueCountChanged #-}
dequeueCountChanged :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m Int
dequeueCountChanged Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
dequeueCount Queue m sm so a
q) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueCountChanged_ Queue m sm so a
q)
  
-- | Signal when the 'dequeueCount' property value has changed.
dequeueCountChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE dequeueCountChanged_ #-}
dequeueCountChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueCountChanged_ Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueRequested Queue m sm so a
q)
      
-- | Return the total number of output items that were actually dequeued.
--
-- See also 'dequeueExtractCountChanged' and 'dequeueExtractCountChanged_'.
dequeueExtractCount :: MonadDES m => Queue m sm so a -> Event m Int
{-# INLINABLE dequeueExtractCount #-}
dequeueExtractCount :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
dequeueExtractCount Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueExtractCountRef Queue m sm so a
q)
      
-- | Signal when the 'dequeueExtractCount' property value has changed.
dequeueExtractCountChanged :: MonadDES m => Queue m sm so a -> Signal m Int
{-# INLINABLE dequeueExtractCountChanged #-}
dequeueExtractCountChanged :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m Int
dequeueExtractCountChanged Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
dequeueExtractCount Queue m sm so a
q) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueExtractCountChanged_ Queue m sm so a
q)
  
-- | Signal when the 'dequeueExtractCount' property value has changed.
dequeueExtractCountChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE dequeueExtractCountChanged_ #-}
dequeueExtractCountChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueExtractCountChanged_ Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
dequeueExtracted Queue m sm so a
q)

-- | Return the rate of the items that were stored: how many items
-- per time.
enqueueStoreRate :: MonadDES m => Queue m sm so a -> Event m Double
{-# INLINABLE enqueueStoreRate #-}
enqueueStoreRate :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
enqueueStoreRate Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
x <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
enqueueStoreCountRef Queue m sm so a
q)
     let t0 :: Double
t0 = forall (m :: * -> *). Specs m -> Double
spcStartTime forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Point m -> Specs m
pointSpecs Point m
p
         t :: Double
t  = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x forall a. Fractional a => a -> a -> a
/ (Double
t forall a. Num a => a -> a -> a
- Double
t0))
      
-- | Return the rate of the requests for dequeueing the items: how many requests
-- per time. It does not include the failed attempts to dequeue immediately
-- without suspension.
dequeueRate :: MonadDES m => Queue m sm so a -> Event m Double
{-# INLINABLE dequeueRate #-}
dequeueRate :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueRate Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
x <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueCountRef Queue m sm so a
q)
     let t0 :: Double
t0 = forall (m :: * -> *). Specs m -> Double
spcStartTime forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Point m -> Specs m
pointSpecs Point m
p
         t :: Double
t  = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x forall a. Fractional a => a -> a -> a
/ (Double
t forall a. Num a => a -> a -> a
- Double
t0))
      
-- | Return the rate of the output items that were dequeued: how many items
-- per time.
dequeueExtractRate :: MonadDES m => Queue m sm so a -> Event m Double
{-# INLINABLE dequeueExtractRate #-}
dequeueExtractRate :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueExtractRate Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
x <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueExtractCountRef Queue m sm so a
q)
     let t0 :: Double
t0 = forall (m :: * -> *). Specs m -> Double
spcStartTime forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Point m -> Specs m
pointSpecs Point m
p
         t :: Double
t  = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x forall a. Fractional a => a -> a -> a
/ (Double
t forall a. Num a => a -> a -> a
- Double
t0))
      
-- | Return the wait time from the time at which the item was stored in the queue to
-- the time at which it was dequeued.
--
-- See also 'queueWaitTimeChanged' and 'queueWaitTimeChanged_'.
queueWaitTime :: MonadDES m => Queue m sm so a -> Event m (SamplingStats Double)
{-# INLINABLE queueWaitTime #-}
queueWaitTime :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m (SamplingStats Double)
queueWaitTime Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
queueWaitTimeRef Queue m sm so a
q)
      
-- | Signal when the 'queueWaitTime' property value has changed.
queueWaitTimeChanged :: MonadDES m => Queue m sm so a -> Signal m (SamplingStats Double)
{-# INLINABLE queueWaitTimeChanged #-}
queueWaitTimeChanged :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m (SamplingStats Double)
queueWaitTimeChanged Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m (SamplingStats Double)
queueWaitTime Queue m sm so a
q) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueWaitTimeChanged_ Queue m sm so a
q)
  
-- | Signal when the 'queueWaitTime' property value has changed.
queueWaitTimeChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE queueWaitTimeChanged_ #-}
queueWaitTimeChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueWaitTimeChanged_ Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
dequeueExtracted Queue m sm so a
q)
      
-- | Return the dequeue wait time from the time at which the item was requested
-- for dequeueing to the time at which it was actually dequeued.
--
-- See also 'dequeueWaitTimeChanged' and 'dequeueWaitTimeChanged_'.
dequeueWaitTime :: MonadDES m => Queue m sm so a -> Event m (SamplingStats Double)
{-# INLINABLE dequeueWaitTime #-}
dequeueWaitTime :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m (SamplingStats Double)
dequeueWaitTime Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
dequeueWaitTimeRef Queue m sm so a
q)
      
-- | Signal when the 'dequeueWaitTime' property value has changed.
dequeueWaitTimeChanged :: MonadDES m => Queue m sm so a -> Signal m (SamplingStats Double)
{-# INLINABLE dequeueWaitTimeChanged #-}
dequeueWaitTimeChanged :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m (SamplingStats Double)
dequeueWaitTimeChanged Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m (SamplingStats Double)
dequeueWaitTime Queue m sm so a
q) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueWaitTimeChanged_ Queue m sm so a
q)
  
-- | Signal when the 'dequeueWaitTime' property value has changed.
dequeueWaitTimeChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE dequeueWaitTimeChanged_ #-}
dequeueWaitTimeChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueWaitTimeChanged_ Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
dequeueExtracted Queue m sm so a
q)

-- | Return a long-term average queue rate calculated as
-- the average queue size divided by the average wait time.
--
-- It should conform with Little's rule.
--
-- See also 'queueRateChanged' and 'queueRateChanged_'.
queueRate :: MonadDES m => Queue m sm so a -> Event m Double
{-# INLINABLE queueRate #-}
queueRate :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
queueRate Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do TimingStats Int
x <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (TimingStats Int)
queueCountStatsRef Queue m sm so a
q)
     SamplingStats Double
y <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
queueWaitTimeRef Queue m sm so a
q)
     forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. TimingData a => TimingStats a -> Double
timingStatsMean TimingStats Int
x forall a. Fractional a => a -> a -> a
/ forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats Double
y) 

-- | Signal when the 'queueRate' property value has changed.
queueRateChanged :: MonadDES m => Queue m sm so a -> Signal m Double
{-# INLINABLE queueRateChanged #-}
queueRateChanged :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m Double
queueRateChanged Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
queueRate Queue m sm so a
q) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueRateChanged_ Queue m sm so a
q)

-- | Signal when the 'queueRate' property value has changed.
queueRateChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE queueRateChanged_ #-}
queueRateChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueRateChanged_ Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
enqueueStored Queue m sm so a
q) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
dequeueExtracted Queue m sm so a
q)
  
-- | Dequeue suspending the process if the queue is empty.
dequeue :: (MonadDES m,
            DequeueStrategy m sm,
            EnqueueStrategy m so)
           => Queue m sm so a
           -- ^ the queue
           -> Process m a
           -- ^ the dequeued value
{-# INLINABLE dequeue #-}
dequeue :: forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm, EnqueueStrategy m so) =>
Queue m sm so a -> Process m a
dequeue Queue m sm so a
q =
  do Double
t <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueRequest Queue m sm so a
q
     forall (m :: * -> *) s.
(MonadDES m, EnqueueStrategy m s) =>
Resource m s -> Process m ()
requestResource (forall (m :: * -> *) sm so a. Queue m sm so a -> Resource m so
dequeueRes Queue m sm so a
q)
     forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Double -> Event m a
dequeueExtract Queue m sm so a
q Double
t
  
-- | Dequeue with the output priority suspending the process if the queue is empty.
dequeueWithOutputPriority :: (MonadDES m,
                              DequeueStrategy m sm,
                              PriorityQueueStrategy m so po)
                             => Queue m sm so a
                             -- ^ the queue
                             -> po
                             -- ^ the priority for output
                             -> Process m a
                             -- ^ the dequeued value
{-# INLINABLE dequeueWithOutputPriority #-}
dequeueWithOutputPriority :: forall (m :: * -> *) sm so po a.
(MonadDES m, DequeueStrategy m sm,
 PriorityQueueStrategy m so po) =>
Queue m sm so a -> po -> Process m a
dequeueWithOutputPriority Queue m sm so a
q po
po =
  do Double
t <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueRequest Queue m sm so a
q
     forall (m :: * -> *) s p.
(MonadDES m, PriorityQueueStrategy m s p) =>
Resource m s -> p -> Process m ()
requestResourceWithPriority (forall (m :: * -> *) sm so a. Queue m sm so a -> Resource m so
dequeueRes Queue m sm so a
q) po
po
     forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Double -> Event m a
dequeueExtract Queue m sm so a
q Double
t
  
-- | Try to dequeue immediately.
tryDequeue :: (MonadDES m, DequeueStrategy m sm)
              => Queue m sm so a
              -- ^ the queue
              -> Event m (Maybe a)
              -- ^ the dequeued value of 'Nothing'
{-# INLINABLE tryDequeue #-}
tryDequeue :: forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Event m (Maybe a)
tryDequeue Queue m sm so a
q =
  do Bool
x <- forall (m :: * -> *) s. MonadDES m => Resource m s -> Event m Bool
tryRequestResourceWithinEvent (forall (m :: * -> *) sm so a. Queue m sm so a -> Resource m so
dequeueRes Queue m sm so a
q)
     if Bool
x 
       then do Double
t <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueRequest Queue m sm so a
q
               forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Double -> Event m a
dequeueExtract Queue m sm so a
q Double
t
       else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing

-- | Remove the item from the queue and return a flag indicating
-- whether the item was found and actually removed.
queueDelete :: (MonadDES m,
                Eq a,
                DeletingQueueStrategy m sm,
                DequeueStrategy m so)
               => Queue m sm so a
               -- ^ the queue
               -> a
               -- ^ the item to remove from the queue
               -> Event m Bool
               -- ^ whether the item was found and removed
{-# INLINABLE queueDelete #-}
queueDelete :: forall (m :: * -> *) a sm so.
(MonadDES m, Eq a, DeletingQueueStrategy m sm,
 DequeueStrategy m so) =>
Queue m sm so a -> a -> Event m Bool
queueDelete Queue m sm so a
q a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
(MonadDES m, DeletingQueueStrategy m sm, DequeueStrategy m so) =>
Queue m sm so a -> (a -> Bool) -> Event m (Maybe a)
queueDeleteBy Queue m sm so a
q (forall a. Eq a => a -> a -> Bool
== a
a)

-- | Remove the specified item from the queue.
queueDelete_ :: (MonadDES m,
                 Eq a,
                 DeletingQueueStrategy m sm,
                 DequeueStrategy m so)
                => Queue m sm so a
                -- ^ the queue
                -> a
                -- ^ the item to remove from the queue
                -> Event m ()
{-# INLINABLE queueDelete_ #-}
queueDelete_ :: forall (m :: * -> *) a sm so.
(MonadDES m, Eq a, DeletingQueueStrategy m sm,
 DequeueStrategy m so) =>
Queue m sm so a -> a -> Event m ()
queueDelete_ Queue m sm so a
q a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
(MonadDES m, DeletingQueueStrategy m sm, DequeueStrategy m so) =>
Queue m sm so a -> (a -> Bool) -> Event m (Maybe a)
queueDeleteBy Queue m sm so a
q (forall a. Eq a => a -> a -> Bool
== a
a)

-- | Remove an item satisfying the specified predicate and return the item if found.
queueDeleteBy :: (MonadDES m,
                  DeletingQueueStrategy m sm,
                  DequeueStrategy m so)
                 => Queue m sm so a
                 -- ^ the queue
                 -> (a -> Bool)
                 -- ^ the predicate
                 -> Event m (Maybe a)
{-# INLINABLE queueDeleteBy #-}
queueDeleteBy :: forall (m :: * -> *) sm so a.
(MonadDES m, DeletingQueueStrategy m sm, DequeueStrategy m so) =>
Queue m sm so a -> (a -> Bool) -> Event m (Maybe a)
queueDeleteBy Queue m sm so a
q a -> Bool
pred =
  do Bool
x <- forall (m :: * -> *) s. MonadDES m => Resource m s -> Event m Bool
tryRequestResourceWithinEvent (forall (m :: * -> *) sm so a. Queue m sm so a -> Resource m so
dequeueRes Queue m sm so a
q)
     if Bool
x
       then do Maybe (QueueItem a)
i <- forall (m :: * -> *) s a.
DeletingQueueStrategy m s =>
StrategyQueue m s a -> (a -> Bool) -> Event m (Maybe a)
strategyQueueDeleteBy (forall (m :: * -> *) sm so a.
Queue m sm so a -> StrategyQueue m sm (QueueItem a)
queueStore Queue m sm so a
q) (a -> Bool
pred forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. QueueItem a -> a
itemValue)
               case Maybe (QueueItem a)
i of
                 Maybe (QueueItem a)
Nothing ->
                   do forall (m :: * -> *) s.
(MonadDES m, DequeueStrategy m s) =>
Resource m s -> Event m ()
releaseResourceWithinEvent (forall (m :: * -> *) sm so a. Queue m sm so a -> Resource m so
dequeueRes Queue m sm so a
q)
                      forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
                 Just QueueItem a
i ->
                   do Double
t <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueRequest Queue m sm so a
q
                      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Double -> QueueItem a -> Event m a
dequeuePostExtract Queue m sm so a
q Double
t QueueItem a
i
       else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
               
-- | Remove an item satisfying the specified predicate.
queueDeleteBy_ :: (MonadDES m,
                   DeletingQueueStrategy m sm,
                   DequeueStrategy m so)
                  => Queue m sm so a
                  -- ^ the queue
                  -> (a -> Bool)
                  -- ^ the predicate
                  -> Event m ()
{-# INLINABLE queueDeleteBy_ #-}
queueDeleteBy_ :: forall (m :: * -> *) sm so a.
(MonadDES m, DeletingQueueStrategy m sm, DequeueStrategy m so) =>
Queue m sm so a -> (a -> Bool) -> Event m ()
queueDeleteBy_ Queue m sm so a
q a -> Bool
pred = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
(MonadDES m, DeletingQueueStrategy m sm, DequeueStrategy m so) =>
Queue m sm so a -> (a -> Bool) -> Event m (Maybe a)
queueDeleteBy Queue m sm so a
q a -> Bool
pred

-- | Detect whether the item is contained in the queue.
queueContains :: (MonadDES m,
                  Eq a,
                  DeletingQueueStrategy m sm)
                 => Queue m sm so a
                 -- ^ the queue
                 -> a
                 -- ^ the item to search the queue for
                 -> Event m Bool
                 -- ^ whether the item was found
{-# INLINABLE queueContains #-}
queueContains :: forall (m :: * -> *) a sm so.
(MonadDES m, Eq a, DeletingQueueStrategy m sm) =>
Queue m sm so a -> a -> Event m Bool
queueContains Queue m sm so a
q a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) sm so a.
(MonadDES m, DeletingQueueStrategy m sm) =>
Queue m sm so a -> (a -> Bool) -> Event m (Maybe a)
queueContainsBy Queue m sm so a
q (forall a. Eq a => a -> a -> Bool
== a
a)

-- | Detect whether an item satisfying the specified predicate is contained in the queue.
queueContainsBy :: (MonadDES m,
                    DeletingQueueStrategy m sm)
                   => Queue m sm so a
                   -- ^ the queue
                   -> (a -> Bool)
                   -- ^ the predicate
                   -> Event m (Maybe a)
                   -- ^ the item if it was found
{-# INLINABLE queueContainsBy #-}
queueContainsBy :: forall (m :: * -> *) sm so a.
(MonadDES m, DeletingQueueStrategy m sm) =>
Queue m sm so a -> (a -> Bool) -> Event m (Maybe a)
queueContainsBy Queue m sm so a
q a -> Bool
pred =
  do Maybe (QueueItem a)
x <- forall (m :: * -> *) s a.
DeletingQueueStrategy m s =>
StrategyQueue m s a -> (a -> Bool) -> Event m (Maybe a)
strategyQueueContainsBy (forall (m :: * -> *) sm so a.
Queue m sm so a -> StrategyQueue m sm (QueueItem a)
queueStore Queue m sm so a
q) (a -> Bool
pred forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. QueueItem a -> a
itemValue)
     case Maybe (QueueItem a)
x of
       Maybe (QueueItem a)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
       Just QueueItem a
i  -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (forall a. QueueItem a -> a
itemValue QueueItem a
i)

-- | Clear the queue immediately.
clearQueue :: (MonadDES m,
               DequeueStrategy m sm)
              => Queue m sm so a
              -- ^ the queue
              -> Event m ()
{-# INLINABLE clearQueue #-}
clearQueue :: forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Event m ()
clearQueue Queue m sm so a
q =
  do Maybe a
x <- forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Event m (Maybe a)
tryDequeue Queue m sm so a
q
     case Maybe a
x of
       Maybe a
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
       Just a
a  -> forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Event m ()
clearQueue Queue m sm so a
q

-- | Enqueue the item.  
enqueue :: (MonadDES m,
            EnqueueStrategy m sm,
            DequeueStrategy m so)
           => Queue m sm so a
           -- ^ the queue
           -> a
           -- ^ the item to enqueue
           -> Event m ()
{-# INLINABLE enqueue #-}
enqueue :: forall (m :: * -> *) sm so a.
(MonadDES m, EnqueueStrategy m sm, DequeueStrategy m so) =>
Queue m sm so a -> a -> Event m ()
enqueue = forall (m :: * -> *) sm so a.
(MonadDES m, EnqueueStrategy m sm, DequeueStrategy m so) =>
Queue m sm so a -> a -> Event m ()
enqueueStore
     
-- | Enqueue with the storing priority the item.  
enqueueWithStoringPriority :: (MonadDES m,
                               PriorityQueueStrategy m sm pm,
                               DequeueStrategy m so)
                              => Queue m sm so a
                              -- ^ the queue
                              -> pm
                              -- ^ the priority for storing
                              -> a
                              -- ^ the item to enqueue
                              -> Event m ()
{-# INLINABLE enqueueWithStoringPriority #-}
enqueueWithStoringPriority :: forall (m :: * -> *) sm pm so a.
(MonadDES m, PriorityQueueStrategy m sm pm,
 DequeueStrategy m so) =>
Queue m sm so a -> pm -> a -> Event m ()
enqueueWithStoringPriority = forall (m :: * -> *) sm pm so a.
(MonadDES m, PriorityQueueStrategy m sm pm,
 DequeueStrategy m so) =>
Queue m sm so a -> pm -> a -> Event m ()
enqueueStoreWithPriority

-- | Return a signal that notifies when the enqueued item
-- is stored in the internal memory of the queue.
enqueueStored :: MonadDES m => Queue m sm so a -> Signal m a
{-# INLINABLE enqueueStored #-}
enqueueStored :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
enqueueStored Queue m sm so a
q = forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m a
enqueueStoredSource Queue m sm so a
q)

-- | Return a signal that notifies when the dequeuing operation was requested.
dequeueRequested :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE dequeueRequested #-}
dequeueRequested :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueRequested Queue m sm so a
q = forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m ()
dequeueRequestedSource Queue m sm so a
q)

-- | Return a signal that notifies when the item was extracted from the internal
-- storage of the queue and prepared for immediate receiving by the dequeuing process.
dequeueExtracted :: MonadDES m => Queue m sm so a -> Signal m a
{-# INLINABLE dequeueExtracted #-}
dequeueExtracted :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
dequeueExtracted Queue m sm so a
q = forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m a
dequeueExtractedSource Queue m sm so a
q)

-- | Store the item.
enqueueStore :: (MonadDES m,
                 EnqueueStrategy m sm,
                 DequeueStrategy m so)
                => Queue m sm so a
                -- ^ the queue
                -> a
                -- ^ the item to be stored
                -> Event m ()
{-# INLINE enqueueStore #-}
enqueueStore :: forall (m :: * -> *) sm so a.
(MonadDES m, EnqueueStrategy m sm, DequeueStrategy m so) =>
Queue m sm so a -> a -> Event m ()
enqueueStore Queue m sm so a
q a
a =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let i :: QueueItem a
i = QueueItem { itemValue :: a
itemValue = a
a,
                         itemStoringTime :: Double
itemStoringTime = forall (m :: * -> *). Point m -> Double
pointTime Point m
p }
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) s a.
EnqueueStrategy m s =>
StrategyQueue m s a -> a -> Event m ()
strategyEnqueue (forall (m :: * -> *) sm so a.
Queue m sm so a -> StrategyQueue m sm (QueueItem a)
queueStore Queue m sm so a
q) QueueItem a
i
     Int
c <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
          forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q)
     let c' :: Int
c' = Int
c forall a. Num a => a -> a -> a
+ Int
1
         t :: Double
t  = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Int
c' seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q) Int
c'
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (TimingStats Int)
queueCountStatsRef Queue m sm so a
q) (forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
c')
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
enqueueStoreCountRef Queue m sm so a
q) (forall a. Num a => a -> a -> a
+ Int
1)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) s.
(MonadDES m, DequeueStrategy m s) =>
Resource m s -> Event m ()
releaseResourceWithinEvent (forall (m :: * -> *) sm so a. Queue m sm so a -> Resource m so
dequeueRes Queue m sm so a
q)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m a
enqueueStoredSource Queue m sm so a
q) (forall a. QueueItem a -> a
itemValue QueueItem a
i)

-- | Store with the priority the item.
enqueueStoreWithPriority :: (MonadDES m,
                             PriorityQueueStrategy m sm pm,
                             DequeueStrategy m so)
                            => Queue m sm so a
                            -- ^ the queue
                            -> pm
                            -- ^ the priority for storing
                            -> a
                            -- ^ the item to be enqueued
                            -> Event m ()
{-# INLINE enqueueStoreWithPriority #-}
enqueueStoreWithPriority :: forall (m :: * -> *) sm pm so a.
(MonadDES m, PriorityQueueStrategy m sm pm,
 DequeueStrategy m so) =>
Queue m sm so a -> pm -> a -> Event m ()
enqueueStoreWithPriority Queue m sm so a
q pm
pm a
a =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let i :: QueueItem a
i = QueueItem { itemValue :: a
itemValue = a
a,
                         itemStoringTime :: Double
itemStoringTime = forall (m :: * -> *). Point m -> Double
pointTime Point m
p }
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) s p a.
PriorityQueueStrategy m s p =>
StrategyQueue m s a -> p -> a -> Event m ()
strategyEnqueueWithPriority (forall (m :: * -> *) sm so a.
Queue m sm so a -> StrategyQueue m sm (QueueItem a)
queueStore Queue m sm so a
q) pm
pm QueueItem a
i
     Int
c <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
          forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q)
     let c' :: Int
c' = Int
c forall a. Num a => a -> a -> a
+ Int
1
         t :: Double
t  = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Int
c' seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q) Int
c'
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (TimingStats Int)
queueCountStatsRef Queue m sm so a
q) (forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
c')
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
enqueueStoreCountRef Queue m sm so a
q) (forall a. Num a => a -> a -> a
+ Int
1)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) s.
(MonadDES m, DequeueStrategy m s) =>
Resource m s -> Event m ()
releaseResourceWithinEvent (forall (m :: * -> *) sm so a. Queue m sm so a -> Resource m so
dequeueRes Queue m sm so a
q)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m a
enqueueStoredSource Queue m sm so a
q) (forall a. QueueItem a -> a
itemValue QueueItem a
i)

-- | Accept the dequeuing request and return the current simulation time.
dequeueRequest :: MonadDES m
                  => Queue m sm so a
                  -- ^ the queue
                  -> Event m Double
                  -- ^ the current time
{-# INLINE dequeueRequest #-}
dequeueRequest :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueRequest Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueCountRef Queue m sm so a
q) (forall a. Num a => a -> a -> a
+ Int
1)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m ()
dequeueRequestedSource Queue m sm so a
q) ()
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Point m -> Double
pointTime Point m
p 

-- | Extract an item for the dequeuing request.  
dequeueExtract :: (MonadDES m, DequeueStrategy m sm)
                  => Queue m sm so a
                  -- ^ the queue
                  -> Double
                  -- ^ the time of the dequeuing request
                  -> Event m a
                  -- ^ the dequeued value
{-# INLINE dequeueExtract #-}
dequeueExtract :: forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Double -> Event m a
dequeueExtract Queue m sm so a
q Double
t' =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do QueueItem a
i <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
          forall (m :: * -> *) s a.
DequeueStrategy m s =>
StrategyQueue m s a -> Event m a
strategyDequeue (forall (m :: * -> *) sm so a.
Queue m sm so a -> StrategyQueue m sm (QueueItem a)
queueStore Queue m sm so a
q)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Double -> QueueItem a -> Event m a
dequeuePostExtract Queue m sm so a
q Double
t' QueueItem a
i

-- | A post action after extracting the item by the dequeuing request.  
dequeuePostExtract :: (MonadDES m,
                       DequeueStrategy m sm)
                      => Queue m sm so a
                      -- ^ the queue
                      -> Double
                      -- ^ the time of the dequeuing request
                      -> QueueItem a
                      -- ^ the item to dequeue
                      -> Event m a
                      -- ^ the dequeued value
{-# INLINE dequeuePostExtract #-}
dequeuePostExtract :: forall (m :: * -> *) sm so a.
(MonadDES m, DequeueStrategy m sm) =>
Queue m sm so a -> Double -> QueueItem a -> Event m a
dequeuePostExtract Queue m sm so a
q Double
t' QueueItem a
i =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
c <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
          forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q)
     let c' :: Int
c' = Int
c forall a. Num a => a -> a -> a
- Int
1
         t :: Double
t  = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Int
c' seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q) Int
c'
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (TimingStats Int)
queueCountStatsRef Queue m sm so a
q) (forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
c')
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueExtractCountRef Queue m sm so a
q) (forall a. Num a => a -> a -> a
+ Int
1)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Double -> QueueItem a -> Event m ()
dequeueStat Queue m sm so a
q Double
t' QueueItem a
i
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *) sm so a. Queue m sm so a -> SignalSource m a
dequeueExtractedSource Queue m sm so a
q) (forall a. QueueItem a -> a
itemValue QueueItem a
i)
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. QueueItem a -> a
itemValue QueueItem a
i

-- | Update the statistics for the output wait time of the dequeuing operation
-- and the wait time of storing in the queue.
dequeueStat :: MonadDES m
               => Queue m sm so a
               -- ^ the queue
               -> Double
               -- ^ the time of the dequeuing request
               -> QueueItem a
               -- ^ the item and its input time
               -> Event m ()
               -- ^ the action of updating the statistics
{-# INLINE dequeueStat #-}
dequeueStat :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Double -> QueueItem a -> Event m ()
dequeueStat Queue m sm so a
q Double
t' QueueItem a
i =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t1 :: Double
t1 = forall a. QueueItem a -> Double
itemStoringTime QueueItem a
i
         t :: Double
t  = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
dequeueWaitTimeRef Queue m sm so a
q) forall a b. (a -> b) -> a -> b
$
       forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t forall a. Num a => a -> a -> a
- Double
t')
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
queueWaitTimeRef Queue m sm so a
q) forall a b. (a -> b) -> a -> b
$
       forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t forall a. Num a => a -> a -> a
- Double
t1)

-- | Signal whenever any property of the queue changes.
--
-- The property must have the corresponded signal. There are also characteristics
-- similar to the properties but that have no signals. As a rule, such characteristics
-- already depend on the simulation time and therefore they may change at any
-- time point.
queueChanged_ :: MonadDES m => Queue m sm so a -> Signal m ()
{-# INLINABLE queueChanged_ #-}
queueChanged_ :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
queueChanged_ Queue m sm so a
q =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
enqueueStored Queue m sm so a
q) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m ()
dequeueRequested Queue m sm so a
q forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Signal m a
dequeueExtracted Queue m sm so a
q)

-- | Return the summary for the queue with desciption of its
-- properties and activities using the specified indent.
queueSummary :: (MonadDES m, Show sm, Show so) => Queue m sm so a -> Int -> Event m ShowS
{-# INLINABLE queueSummary #-}
queueSummary :: forall (m :: * -> *) sm so a.
(MonadDES m, Show sm, Show so) =>
Queue m sm so a -> Int -> Event m ShowS
queueSummary Queue m sm so a
q Int
indent =
  do let sm :: sm
sm = forall (m :: * -> *) sm so a. Queue m sm so a -> sm
enqueueStoringStrategy Queue m sm so a
q
         so :: so
so = forall (m :: * -> *) sm so a. Queue m sm so a -> so
dequeueStrategy Queue m sm so a
q
     Bool
null <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Bool
queueNull Queue m sm so a
q
     Int
count <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
queueCount Queue m sm so a
q
     TimingStats Int
countStats <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m (TimingStats Int)
queueCountStats Queue m sm so a
q
     Int
enqueueStoreCount <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
enqueueStoreCount Queue m sm so a
q
     Int
dequeueCount <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
dequeueCount Queue m sm so a
q
     Int
dequeueExtractCount <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Int
dequeueExtractCount Queue m sm so a
q
     Double
enqueueStoreRate <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
enqueueStoreRate Queue m sm so a
q
     Double
dequeueRate <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueRate Queue m sm so a
q
     Double
dequeueExtractRate <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m Double
dequeueExtractRate Queue m sm so a
q
     SamplingStats Double
waitTime <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m (SamplingStats Double)
queueWaitTime Queue m sm so a
q
     SamplingStats Double
dequeueWaitTime <- forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m (SamplingStats Double)
dequeueWaitTime Queue m sm so a
q
     let tab :: [Char]
tab = forall a. Int -> a -> [a]
replicate Int
indent Char
' '
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the storing (memory) strategy = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows sm
sm forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeueing (output) strategy = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows so
so forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"empty? = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the current size = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows Int
count forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the size statistics = \n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. (Show a, TimingData a) => TimingStats a -> Int -> ShowS
timingStatsSummary TimingStats Int
countStats (Int
2 forall a. Num a => a -> a -> a
+ Int
indent) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueue store count (number of the input items that were stored) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows Int
enqueueStoreCount forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue count (number of requests for dequeueing an item) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows Int
dequeueCount forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue extract count (number of the output items that were dequeued) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows Int
dequeueExtractCount forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueue store rate (how many input items were stored per time) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows Double
enqueueStoreRate forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue rate (how many requests for dequeueing per time) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows Double
dequeueRate forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue extract rate (how many output items were dequeued per time) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => a -> ShowS
shows Double
dequeueExtractRate forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the wait time (when was stored -> when was dequeued) = \n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
waitTime (Int
2 forall a. Num a => a -> a -> a
+ Int
indent) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue wait time (when was requested for dequeueing -> when was dequeued) = \n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
dequeueWaitTime (Int
2 forall a. Num a => a -> a -> a
+ Int
indent)

-- | Reset the statistics.
resetQueue :: MonadDES m => Queue m sm so a -> Event m ()
{-# INLINABLE resetQueue #-}
resetQueue :: forall (m :: * -> *) sm so a.
MonadDES m =>
Queue m sm so a -> Event m ()
resetQueue Queue m sm so a
q =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Int
queueCount <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
queueCountRef Queue m sm so a
q)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (TimingStats Int)
queueCountStatsRef Queue m sm so a
q) forall a b. (a -> b) -> a -> b
$
       forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
queueCount
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
enqueueStoreCountRef Queue m sm so a
q) Int
0
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueCountRef Queue m sm so a
q) Int
0
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a. Queue m sm so a -> Ref m Int
dequeueExtractCountRef Queue m sm so a
q) Int
0
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
queueWaitTimeRef Queue m sm so a
q) forall a. Monoid a => a
mempty
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) sm so a.
Queue m sm so a -> Ref m (SamplingStats Double)
dequeueWaitTimeRef Queue m sm so a
q) forall a. Monoid a => a
mempty