{-# LANGUAGE FlexibleContexts #-}

-- |
-- Module     : Simulation.Aivika.Trans.GPSS.Queue
-- Copyright  : Copyright (c) 2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.2
--
-- This module defines a GPSS queue entity.
--
module Simulation.Aivika.Trans.GPSS.Queue
       (-- * Queue Types
        Queue,
        QueueEntry(..),
        -- * Creating Queue
        newQueue,
        -- * Queue Properties and Activities
        queueNull,
        queueContent,
        queueContentStats,
        enqueueCount,
        enqueueZeroEntryCount,
        queueWaitTime,
        queueNonZeroEntryWaitTime,
        queueRate,
        -- * Dequeuing and Enqueuing
        enqueue,
        dequeue,
        -- * Statistics Reset
        resetQueue,
        -- * Derived Signals for Properties
        queueNullChanged,
        queueNullChanged_,
        queueContentChanged,
        queueContentChanged_,
        enqueueCountChanged,
        enqueueCountChanged_,
        enqueueZeroEntryCountChanged,
        enqueueZeroEntryCountChanged_,
        queueWaitTimeChanged,
        queueWaitTimeChanged_,
        queueNonZeroEntryWaitTimeChanged,
        queueNonZeroEntryWaitTimeChanged_,
        queueRateChanged,
        queueRateChanged_,
        -- * Basic Signals
        enqueued,
        dequeued,
        -- * Overall Signal
        queueChanged_) where

import Data.Monoid
import Data.Maybe
import Data.Hashable

import Control.Monad
import Control.Monad.Trans

import Simulation.Aivika.Trans
import Simulation.Aivika.Trans.Internal.Specs
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.Statistics

import Simulation.Aivika.Trans.GPSS.Transact

-- | Represents the queue entity.
data Queue m =
  Queue { Queue m -> Int
queueSequenceNo :: Int,
          Queue m -> Ref m Int
queueContentRef :: Ref m Int,
          Queue m -> Ref m (TimingStats Int)
queueContentStatsRef :: Ref m (TimingStats Int),
          Queue m -> Ref m Int
enqueueCountRef :: Ref m Int,
          Queue m -> Ref m Int
enqueueZeroEntryCountRef :: Ref m Int,
          Queue m -> Ref m (SamplingStats Double)
queueWaitTimeRef :: Ref m (SamplingStats Double),
          Queue m -> Ref m (SamplingStats Double)
queueNonZeroEntryWaitTimeRef :: Ref m (SamplingStats Double),
          Queue m -> SignalSource m ()
enqueuedSource :: SignalSource m (),
          Queue m -> SignalSource m ()
dequeuedSource :: SignalSource m ()
        }

-- | The information about queue entry.
data QueueEntry m =
  QueueEntry { QueueEntry m -> Queue m
entryQueue :: Queue m,
               -- ^ the entry queue
               QueueEntry m -> Double
entryEnqueueTime :: Double
               -- ^ the time of registering the queue entry
             } deriving QueueEntry m -> QueueEntry m -> Bool
(QueueEntry m -> QueueEntry m -> Bool)
-> (QueueEntry m -> QueueEntry m -> Bool) -> Eq (QueueEntry m)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (m :: * -> *).
MonadDES m =>
QueueEntry m -> QueueEntry m -> Bool
/= :: QueueEntry m -> QueueEntry m -> Bool
$c/= :: forall (m :: * -> *).
MonadDES m =>
QueueEntry m -> QueueEntry m -> Bool
== :: QueueEntry m -> QueueEntry m -> Bool
$c== :: forall (m :: * -> *).
MonadDES m =>
QueueEntry m -> QueueEntry m -> Bool
Eq

instance MonadDES m => Eq (Queue m) where
  Queue m
x == :: Queue m -> Queue m -> Bool
== Queue m
y = (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
x) Ref m Int -> Ref m Int -> Bool
forall a. Eq a => a -> a -> Bool
== (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
y)

instance Hashable (Queue m) where
  hashWithSalt :: Int -> Queue m -> Int
hashWithSalt Int
salt Queue m
x = Int -> Int -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Queue m -> Int
forall (m :: * -> *). Queue m -> Int
queueSequenceNo Queue m
x)

-- | Create a new queue.
newQueue :: MonadDES m => Event m (Queue m)
{-# INLINABLE newQueue #-}
newQueue :: Event m (Queue m)
newQueue =
  do Double
t  <- Dynamics m Double -> Event m Double
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics Dynamics m Double
forall (m :: * -> *). Monad m => Dynamics m Double
time
     Generator m
g  <- Parameter m (Generator m) -> Event m (Generator m)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
ParameterLift t m =>
Parameter m a -> t m a
liftParameter Parameter m (Generator m)
forall (m :: * -> *). Monad m => Parameter m (Generator m)
generatorParameter
     Int
no <- m Int -> Event m Int
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
MonadCompTrans t m =>
m a -> t m a
liftComp (m Int -> Event m Int) -> m Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ Generator m -> m Int
forall (m :: * -> *). MonadGenerator m => Generator m -> m Int
generateSequenceNo Generator m
g
     Ref m Int
i  <- Simulation m (Ref m Int) -> Event m (Ref m Int)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Int) -> Event m (Ref m Int))
-> Simulation m (Ref m Int) -> Event m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m (TimingStats Int)
is <- Simulation m (Ref m (TimingStats Int))
-> Event m (Ref m (TimingStats Int))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m (TimingStats Int))
 -> Event m (Ref m (TimingStats Int)))
-> Simulation m (Ref m (TimingStats Int))
-> Event m (Ref m (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ TimingStats Int -> Simulation m (Ref m (TimingStats Int))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef (TimingStats Int -> Simulation m (Ref m (TimingStats Int)))
-> TimingStats Int -> Simulation m (Ref m (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
     Ref m Int
e  <- Simulation m (Ref m Int) -> Event m (Ref m Int)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Int) -> Event m (Ref m Int))
-> Simulation m (Ref m Int) -> Event m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m Int
z  <- Simulation m (Ref m Int) -> Event m (Ref m Int)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Int) -> Event m (Ref m Int))
-> Simulation m (Ref m Int) -> Event m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0 
     Ref m (SamplingStats Double)
w  <- Simulation m (Ref m (SamplingStats Double))
-> Event m (Ref m (SamplingStats Double))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m (SamplingStats Double))
 -> Event m (Ref m (SamplingStats Double)))
-> Simulation m (Ref m (SamplingStats Double))
-> Event m (Ref m (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef SamplingStats Double
forall a. Monoid a => a
mempty
     Ref m (SamplingStats Double)
w2 <- Simulation m (Ref m (SamplingStats Double))
-> Event m (Ref m (SamplingStats Double))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m (SamplingStats Double))
 -> Event m (Ref m (SamplingStats Double)))
-> Simulation m (Ref m (SamplingStats Double))
-> Event m (Ref m (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef SamplingStats Double
forall a. Monoid a => a
mempty
     SignalSource m ()
s1 <- Simulation m (SignalSource m ()) -> Event m (SignalSource m ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (SignalSource m ()) -> Event m (SignalSource m ()))
-> Simulation m (SignalSource m ()) -> Event m (SignalSource m ())
forall a b. (a -> b) -> a -> b
$ Simulation m (SignalSource m ())
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m ()
s2 <- Simulation m (SignalSource m ()) -> Event m (SignalSource m ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (SignalSource m ()) -> Event m (SignalSource m ()))
-> Simulation m (SignalSource m ()) -> Event m (SignalSource m ())
forall a b. (a -> b) -> a -> b
$ Simulation m (SignalSource m ())
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Queue m -> Event m (Queue m)
forall (m :: * -> *) a. Monad m => a -> m a
return Queue :: forall (m :: * -> *).
Int
-> Ref m Int
-> Ref m (TimingStats Int)
-> Ref m Int
-> Ref m Int
-> Ref m (SamplingStats Double)
-> Ref m (SamplingStats Double)
-> SignalSource m ()
-> SignalSource m ()
-> Queue m
Queue { queueSequenceNo :: Int
queueSequenceNo = Int
no,
                    queueContentRef :: Ref m Int
queueContentRef = Ref m Int
i,
                    queueContentStatsRef :: Ref m (TimingStats Int)
queueContentStatsRef = Ref m (TimingStats Int)
is,
                    enqueueCountRef :: Ref m Int
enqueueCountRef = Ref m Int
e,
                    enqueueZeroEntryCountRef :: Ref m Int
enqueueZeroEntryCountRef = Ref m Int
z,
                    queueWaitTimeRef :: Ref m (SamplingStats Double)
queueWaitTimeRef = Ref m (SamplingStats Double)
w,
                    queueNonZeroEntryWaitTimeRef :: Ref m (SamplingStats Double)
queueNonZeroEntryWaitTimeRef = Ref m (SamplingStats Double)
w2,
                    enqueuedSource :: SignalSource m ()
enqueuedSource = SignalSource m ()
s1,
                    dequeuedSource :: SignalSource m ()
dequeuedSource = SignalSource m ()
s2 }
  
-- | Test whether the queue is empty.
--
-- See also 'queueNullChanged' and 'queueNullChanged_'.
queueNull :: MonadDES m => Queue m -> Event m Bool
{-# INLINABLE queueNull #-}
queueNull :: Queue m -> Event m Bool
queueNull Queue m
q =
  (Point m -> m Bool) -> Event m Bool
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Bool) -> Event m Bool)
-> (Point m -> m Bool) -> Event m Bool
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
n <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
q)
     Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)
  
-- | Signal when the 'queueNull' property value has changed.
queueNullChanged :: MonadDES m => Queue m -> Signal m Bool
{-# INLINABLE queueNullChanged #-}
queueNullChanged :: Queue m -> Signal m Bool
queueNullChanged Queue m
q =
  (() -> Event m Bool) -> Signal m () -> Signal m Bool
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Bool -> () -> Event m Bool
forall a b. a -> b -> a
const (Event m Bool -> () -> Event m Bool)
-> Event m Bool -> () -> Event m Bool
forall a b. (a -> b) -> a -> b
$ Queue m -> Event m Bool
forall (m :: * -> *). MonadDES m => Queue m -> Event m Bool
queueNull Queue m
q) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
queueNullChanged_ Queue m
q)
  
-- | Signal when the 'queueNull' property value has changed.
queueNullChanged_ :: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE queueNullChanged_ #-}
queueNullChanged_ :: Queue m -> Signal m ()
queueNullChanged_ = Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
queueContentChanged_

-- | Return the current queue content.
--
-- See also 'queueContentStats', 'queueContentChanged' and 'queueContentChanged_'.
queueContent :: MonadDES m => Queue m -> Event m Int
{-# INLINABLE queueContent #-}
queueContent :: Queue m -> Event m Int
queueContent Queue m
q =
  (Point m -> m Int) -> Event m Int
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Int) -> Event m Int)
-> (Point m -> m Int) -> Event m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
q)

-- | Return the queue content statistics.
queueContentStats :: MonadDES m => Queue m -> Event m (TimingStats Int)
{-# INLINABLE queueContentStats #-}
queueContentStats :: Queue m -> Event m (TimingStats Int)
queueContentStats Queue m
q =
  (Point m -> m (TimingStats Int)) -> Event m (TimingStats Int)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (TimingStats Int)) -> Event m (TimingStats Int))
-> (Point m -> m (TimingStats Int)) -> Event m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m (TimingStats Int) -> m (TimingStats Int)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (TimingStats Int) -> m (TimingStats Int))
-> Event m (TimingStats Int) -> m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ Ref m (TimingStats Int) -> Event m (TimingStats Int)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m (TimingStats Int)
forall (m :: * -> *). Queue m -> Ref m (TimingStats Int)
queueContentStatsRef Queue m
q)
  
-- | Signal when the 'queueContent' property value has changed.
queueContentChanged :: MonadDES m => Queue m -> Signal m Int
{-# INLINABLE queueContentChanged #-}
queueContentChanged :: Queue m -> Signal m Int
queueContentChanged Queue m
q =
  (() -> Event m Int) -> Signal m () -> Signal m Int
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Int -> () -> Event m Int
forall a b. a -> b -> a
const (Event m Int -> () -> Event m Int)
-> Event m Int -> () -> Event m Int
forall a b. (a -> b) -> a -> b
$ Queue m -> Event m Int
forall (m :: * -> *). MonadDES m => Queue m -> Event m Int
queueContent Queue m
q) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
queueContentChanged_ Queue m
q)
  
-- | Signal when the 'queueContent' property value has changed.
queueContentChanged_ :: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE queueContentChanged_ #-}
queueContentChanged_ :: Queue m -> Signal m ()
queueContentChanged_ Queue m
q =
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
enqueued Queue m
q) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
dequeued Queue m
q)

-- | Return the total number of input items that were enqueued.
--
-- See also 'enqueueCountChanged' and 'enqueueCountChanged_'.
enqueueCount :: MonadDES m => Queue m -> Event m Int
{-# INLINABLE enqueueCount #-}
enqueueCount :: Queue m -> Event m Int
enqueueCount Queue m
q =
  (Point m -> m Int) -> Event m Int
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Int) -> Event m Int)
-> (Point m -> m Int) -> Event m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
enqueueCountRef Queue m
q)
  
-- | Signal when the 'enqueueCount' property value has changed.
enqueueCountChanged :: MonadDES m => Queue m -> Signal m Int
{-# INLINABLE enqueueCountChanged #-}
enqueueCountChanged :: Queue m -> Signal m Int
enqueueCountChanged Queue m
q =
  (() -> Event m Int) -> Signal m () -> Signal m Int
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Int -> () -> Event m Int
forall a b. a -> b -> a
const (Event m Int -> () -> Event m Int)
-> Event m Int -> () -> Event m Int
forall a b. (a -> b) -> a -> b
$ Queue m -> Event m Int
forall (m :: * -> *). MonadDES m => Queue m -> Event m Int
enqueueCount Queue m
q) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
enqueueCountChanged_ Queue m
q)
  
-- | Signal when the 'enqueueCount' property value has changed.
enqueueCountChanged_ :: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE enqueueCountChanged_ #-}
enqueueCountChanged_ :: Queue m -> Signal m ()
enqueueCountChanged_ Queue m
q =
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
enqueued Queue m
q)

-- | Return the total number of zero entry items.
--
-- See also 'enqueueZeroEntryCountChanged' and 'enqueueZeroEntryCountChanged_'.
enqueueZeroEntryCount :: MonadDES m => Queue m -> Event m Int
{-# INLINABLE enqueueZeroEntryCount #-}
enqueueZeroEntryCount :: Queue m -> Event m Int
enqueueZeroEntryCount Queue m
q =
  (Point m -> m Int) -> Event m Int
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Int) -> Event m Int)
-> (Point m -> m Int) -> Event m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
enqueueZeroEntryCountRef Queue m
q)
  
-- | Signal when the 'enqueueZeroEntryCount' property value has changed.
enqueueZeroEntryCountChanged :: MonadDES m => Queue m -> Signal m Int
{-# INLINABLE enqueueZeroEntryCountChanged #-}
enqueueZeroEntryCountChanged :: Queue m -> Signal m Int
enqueueZeroEntryCountChanged Queue m
q =
  (() -> Event m Int) -> Signal m () -> Signal m Int
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Int -> () -> Event m Int
forall a b. a -> b -> a
const (Event m Int -> () -> Event m Int)
-> Event m Int -> () -> Event m Int
forall a b. (a -> b) -> a -> b
$ Queue m -> Event m Int
forall (m :: * -> *). MonadDES m => Queue m -> Event m Int
enqueueZeroEntryCount Queue m
q) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
enqueueZeroEntryCountChanged_ Queue m
q)
  
-- | Signal when the 'enqueueZeroEntryCount' property value has changed.
enqueueZeroEntryCountChanged_ :: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE enqueueZeroEntryCountChanged_ #-}
enqueueZeroEntryCountChanged_ :: Queue m -> Signal m ()
enqueueZeroEntryCountChanged_ Queue m
q =
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
dequeued Queue m
q)

-- | Return the wait (or residence) time.
--
-- See also 'queueWaitTimeChanged' and 'queueWaitTimeChanged_'.
queueWaitTime :: MonadDES m => Queue m -> Event m (SamplingStats Double)
{-# INLINABLE queueWaitTime #-}
queueWaitTime :: Queue m -> Event m (SamplingStats Double)
queueWaitTime Queue m
q =
  (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (SamplingStats Double))
 -> Event m (SamplingStats Double))
-> (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m (SamplingStats Double)
forall (m :: * -> *). Queue m -> Ref m (SamplingStats Double)
queueWaitTimeRef Queue m
q)
      
-- | Signal when the 'queueWaitTime' property value has changed.
queueWaitTimeChanged :: MonadDES m => Queue m -> Signal m (SamplingStats Double)
{-# INLINABLE queueWaitTimeChanged #-}
queueWaitTimeChanged :: Queue m -> Signal m (SamplingStats Double)
queueWaitTimeChanged Queue m
q =
  (() -> Event m (SamplingStats Double))
-> Signal m () -> Signal m (SamplingStats Double)
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m (SamplingStats Double)
-> () -> Event m (SamplingStats Double)
forall a b. a -> b -> a
const (Event m (SamplingStats Double)
 -> () -> Event m (SamplingStats Double))
-> Event m (SamplingStats Double)
-> ()
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Queue m -> Event m (SamplingStats Double)
forall (m :: * -> *).
MonadDES m =>
Queue m -> Event m (SamplingStats Double)
queueWaitTime Queue m
q) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
queueWaitTimeChanged_ Queue m
q)
  
-- | Signal when the 'queueWaitTime' property value has changed.
queueWaitTimeChanged_ :: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE queueWaitTimeChanged_ #-}
queueWaitTimeChanged_ :: Queue m -> Signal m ()
queueWaitTimeChanged_ Queue m
q =
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
dequeued Queue m
q)
      
-- | Return the wait (or residence) time excluding zero entries.
--
-- See also 'queueNonZeroEntryWaitTimeChanged' and 'queueNonZeroEntryWaitTimeChanged_'.
queueNonZeroEntryWaitTime :: MonadDES m => Queue m -> Event m (SamplingStats Double)
{-# INLINABLE queueNonZeroEntryWaitTime #-}
queueNonZeroEntryWaitTime :: Queue m -> Event m (SamplingStats Double)
queueNonZeroEntryWaitTime Queue m
q =
  (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (SamplingStats Double))
 -> Event m (SamplingStats Double))
-> (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m (SamplingStats Double)
forall (m :: * -> *). Queue m -> Ref m (SamplingStats Double)
queueNonZeroEntryWaitTimeRef Queue m
q)
      
-- | Signal when the 'queueNonZeroEntryWaitTime' property value has changed.
queueNonZeroEntryWaitTimeChanged :: MonadDES m => Queue m -> Signal m (SamplingStats Double)
{-# INLINABLE queueNonZeroEntryWaitTimeChanged #-}
queueNonZeroEntryWaitTimeChanged :: Queue m -> Signal m (SamplingStats Double)
queueNonZeroEntryWaitTimeChanged Queue m
q =
  (() -> Event m (SamplingStats Double))
-> Signal m () -> Signal m (SamplingStats Double)
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m (SamplingStats Double)
-> () -> Event m (SamplingStats Double)
forall a b. a -> b -> a
const (Event m (SamplingStats Double)
 -> () -> Event m (SamplingStats Double))
-> Event m (SamplingStats Double)
-> ()
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Queue m -> Event m (SamplingStats Double)
forall (m :: * -> *).
MonadDES m =>
Queue m -> Event m (SamplingStats Double)
queueNonZeroEntryWaitTime Queue m
q) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
queueNonZeroEntryWaitTimeChanged_ Queue m
q)
  
-- | Signal when the 'queueNonZeroEntryWaitTime' property value has changed.
queueNonZeroEntryWaitTimeChanged_ :: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE queueNonZeroEntryWaitTimeChanged_ #-}
queueNonZeroEntryWaitTimeChanged_ :: Queue m -> Signal m ()
queueNonZeroEntryWaitTimeChanged_ Queue m
q =
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
dequeued Queue m
q)

-- | Return a long-term average queue rate calculated as
-- the average queue size divided by the average wait time.
--
-- See also 'queueRateChanged' and 'queueRateChanged_'.
queueRate :: MonadDES m => Queue m -> Event m Double
{-# INLINABLE queueRate #-}
queueRate :: Queue m -> Event m Double
queueRate Queue m
q =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do TimingStats Int
x <- Point m -> Event m (TimingStats Int) -> m (TimingStats Int)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (TimingStats Int) -> m (TimingStats Int))
-> Event m (TimingStats Int) -> m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ Ref m (TimingStats Int) -> Event m (TimingStats Int)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m (TimingStats Int)
forall (m :: * -> *). Queue m -> Ref m (TimingStats Int)
queueContentStatsRef Queue m
q)
     SamplingStats Double
y <- Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m (SamplingStats Double)
forall (m :: * -> *). Queue m -> Ref m (SamplingStats Double)
queueWaitTimeRef Queue m
q)
     Double -> m Double
forall (m :: * -> *) a. Monad m => a -> m a
return (TimingStats Int -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsMean TimingStats Int
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ SamplingStats Double -> Double
forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats Double
y) 
      
-- | Signal when the 'queueRate' property value has changed.
queueRateChanged :: MonadDES m => Queue m -> Signal m Double
{-# INLINABLE queueRateChanged #-}
queueRateChanged :: Queue m -> Signal m Double
queueRateChanged Queue m
q =
  (() -> Event m Double) -> Signal m () -> Signal m Double
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Double -> () -> Event m Double
forall a b. a -> b -> a
const (Event m Double -> () -> Event m Double)
-> Event m Double -> () -> Event m Double
forall a b. (a -> b) -> a -> b
$ Queue m -> Event m Double
forall (m :: * -> *). MonadDES m => Queue m -> Event m Double
queueRate Queue m
q) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
queueRateChanged_ Queue m
q)
      
-- | Signal when the 'queueRate' property value has changed.
queueRateChanged_ :: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE queueRateChanged_ #-}
queueRateChanged_ :: Queue m -> Signal m ()
queueRateChanged_ Queue m
q =
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
enqueued Queue m
q) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
dequeued Queue m
q)

-- | Return a signal that notifies when enqueuing an item.
enqueued:: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE enqueued #-}
enqueued :: Queue m -> Signal m ()
enqueued Queue m
q = SignalSource m () -> Signal m ()
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (Queue m -> SignalSource m ()
forall (m :: * -> *). Queue m -> SignalSource m ()
enqueuedSource Queue m
q)

-- | Return a signal that notifies when the dequeuing the item.
dequeued :: MonadDES m => Queue m -> Signal m ()
{-# INLINABLE dequeued #-}
dequeued :: Queue m -> Signal m ()
dequeued Queue m
q = SignalSource m () -> Signal m ()
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (Queue m -> SignalSource m ()
forall (m :: * -> *). Queue m -> SignalSource m ()
dequeuedSource Queue m
q)

-- | Enqueue the item.
enqueue :: MonadDES m
           => Queue m
           -- ^ the queue
           -> Transact m a
           -- ^ the item to be enqueued
           -> Int
           -- ^ the content increment
           -> Event m ()
{-# INLINABLE enqueue #-}
enqueue :: Queue m -> Transact m a -> Int -> Event m ()
enqueue Queue m
q Transact m a
transact Int
increment =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
         e :: QueueEntry m
e = QueueEntry :: forall (m :: * -> *). Queue m -> Double -> QueueEntry m
QueueEntry { entryQueue :: Queue m
entryQueue = Queue m
q,
                          entryEnqueueTime :: Double
entryEnqueueTime = Double
t }
     Int
n <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
enqueueCountRef Queue m
q)
     let n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
enqueueCountRef Queue m
q) Int
n'
     Int
c <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
q)
     let c' :: Int
c' = Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
increment
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
q) Int
c'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Queue m -> Ref m (TimingStats Int)
forall (m :: * -> *). Queue m -> Ref m (TimingStats Int)
queueContentStatsRef Queue m
q) (Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
c')
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Transact m a -> QueueEntry m -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> QueueEntry m -> Event m ()
registerTransactQueueEntry Transact m a
transact QueueEntry m
e
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m () -> () -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Queue m -> SignalSource m ()
forall (m :: * -> *). Queue m -> SignalSource m ()
enqueuedSource Queue m
q) ()

-- | Dequeue the item.
dequeue :: MonadDES m
           => Queue m
           -- ^ the queue
           -> Transact m a
           -- ^ the item to be dequeued
           -> Int
           -- ^ the content decrement
           -> Event m ()
{-# INLINABLE dequeue #-}
dequeue :: Queue m -> Transact m a -> Int -> Event m ()
dequeue Queue m
q Transact m a
transact Int
decrement =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do QueueEntry m
e <- Point m -> Event m (QueueEntry m) -> m (QueueEntry m)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (QueueEntry m) -> m (QueueEntry m))
-> Event m (QueueEntry m) -> m (QueueEntry m)
forall a b. (a -> b) -> a -> b
$
          Transact m a -> Queue m -> Event m (QueueEntry m)
forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Queue m -> Event m (QueueEntry m)
unregisterTransactQueueEntry Transact m a
transact Queue m
q
     let t :: Double
t  = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
         t0 :: Double
t0 = QueueEntry m -> Double
forall (m :: * -> *). QueueEntry m -> Double
entryEnqueueTime QueueEntry m
e
         dt :: Double
dt = Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0
     Int
c <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
q)
     let c' :: Int
c' = Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
decrement
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
q) Int
c'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Queue m -> Ref m (TimingStats Int)
forall (m :: * -> *). Queue m -> Ref m (TimingStats Int)
queueContentStatsRef Queue m
q) (Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
c')
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Queue m -> Ref m (SamplingStats Double)
forall (m :: * -> *). Queue m -> Ref m (SamplingStats Double)
queueWaitTimeRef Queue m
q) ((SamplingStats Double -> SamplingStats Double) -> Event m ())
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall a b. (a -> b) -> a -> b
$
       Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
dt
     if Double
t Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
t0
       then Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
            Ref m Int -> (Int -> Int) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
enqueueZeroEntryCountRef Queue m
q) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
       else Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
            Ref m (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Queue m -> Ref m (SamplingStats Double)
forall (m :: * -> *). Queue m -> Ref m (SamplingStats Double)
queueNonZeroEntryWaitTimeRef Queue m
q) ((SamplingStats Double -> SamplingStats Double) -> Event m ())
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall a b. (a -> b) -> a -> b
$
            Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
dt
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m () -> () -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Queue m -> SignalSource m ()
forall (m :: * -> *). Queue m -> SignalSource m ()
dequeuedSource Queue m
q) ()

-- | 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 -> Signal m ()
{-# INLINABLE queueChanged_ #-}
queueChanged_ :: Queue m -> Signal m ()
queueChanged_ Queue m
q =
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
enqueued Queue m
q) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  (() -> ()) -> Signal m () -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue m -> Signal m ()
forall (m :: * -> *). MonadDES m => Queue m -> Signal m ()
dequeued Queue m
q)

-- | Reset the statistics.
resetQueue :: MonadDES m => Queue m -> Event m ()
{-# INLINABLE resetQueue #-}
resetQueue :: Queue m -> Event m ()
resetQueue Queue m
q =
  do Double
t  <- Dynamics m Double -> Event m Double
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics Dynamics m Double
forall (m :: * -> *). Monad m => Dynamics m Double
time
     Int
content <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
queueContentRef Queue m
q)
     Ref m (TimingStats Int) -> TimingStats Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Queue m -> Ref m (TimingStats Int)
forall (m :: * -> *). Queue m -> Ref m (TimingStats Int)
queueContentStatsRef Queue m
q) (TimingStats Int -> Event m ()) -> TimingStats Int -> Event m ()
forall a b. (a -> b) -> a -> b
$
       Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
content
     Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
enqueueCountRef Queue m
q) Int
0
     Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Queue m -> Ref m Int
forall (m :: * -> *). Queue m -> Ref m Int
enqueueZeroEntryCountRef Queue m
q) Int
0
     Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Queue m -> Ref m (SamplingStats Double)
forall (m :: * -> *). Queue m -> Ref m (SamplingStats Double)
queueWaitTimeRef Queue m
q) SamplingStats Double
forall a. Monoid a => a
mempty
     Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Queue m -> Ref m (SamplingStats Double)
forall (m :: * -> *). Queue m -> Ref m (SamplingStats Double)
queueNonZeroEntryWaitTimeRef Queue m
q) SamplingStats Double
forall a. Monoid a => a
mempty