-- |
-- Module     : Simulation.Aivika.Trans.GPSS.Storage
-- 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.1
--
-- This module defines the GPSS Storage entity.
--
module Simulation.Aivika.Trans.GPSS.Storage
       (-- * Storage Type
        Storage,
        -- * Creating Storage
        newStorage,
        -- * Storage Properties
        storageCapacity,
        storageEmpty,
        storageFull,
        storageContent,
        storageContentStats,
        storageUseCount,
        storageUsedContent,
        storageUtilisationCount,
        storageUtilisationCountStats,
        storageQueueCount,
        storageQueueCountStats,
        storageTotalWaitTime,
        storageWaitTime,
        storageAverageHoldingTime,
        -- * Entering-Leaving Storage
        enterStorage,
        leaveStorage,
        leaveStorageWithinEvent,
        -- * Statistics Reset
        resetStorage,
        -- * Signals
        storageContentChanged,
        storageContentChanged_,
        storageUseCountChanged,
        storageUseCountChanged_,
        storageUsedContentChanged,
        storageUsedContentChanged_,
        storageUtilisationCountChanged,
        storageUtilisationCountChanged_,
        storageQueueCountChanged,
        storageQueueCountChanged_,
        storageWaitTimeChanged,
        storageWaitTimeChanged_,
        storageChanged_) where

import Data.Monoid
import Data.Maybe

import Control.Monad
import Control.Monad.Trans
import Control.Exception

import Simulation.Aivika.Trans
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Event
import Simulation.Aivika.Trans.Internal.Cont
import Simulation.Aivika.Trans.Internal.Process
import Simulation.Aivika.Trans.QueueStrategy
import Simulation.Aivika.Trans.Statistics
import Simulation.Aivika.Trans.Signal

import Simulation.Aivika.Trans.GPSS.Transact
import Simulation.Aivika.Trans.GPSS.TransactQueueStrategy

-- | Represents a GPSS Storage entity.
data Storage m = 
  Storage { forall (m :: * -> *). Storage m -> Int
storageCapacity :: Int,
            -- ^ Return the storage capacity.
            forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef :: Ref m Int,
            forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageContentStatsRef :: Ref m (TimingStats Int),
            forall (m :: * -> *). Storage m -> SignalSource m Int
storageContentSource :: SignalSource m Int,
            forall (m :: * -> *). Storage m -> Ref m Int
storageUseCountRef :: Ref m Int,
            forall (m :: * -> *). Storage m -> SignalSource m Int
storageUseCountSource :: SignalSource m Int,
            forall (m :: * -> *). Storage m -> Ref m Int
storageUsedContentRef :: Ref m Int,
            forall (m :: * -> *). Storage m -> SignalSource m Int
storageUsedContentSource :: SignalSource m Int,
            forall (m :: * -> *). Storage m -> Ref m Int
storageUtilisationCountRef :: Ref m Int,
            forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageUtilisationCountStatsRef :: Ref m (TimingStats Int),
            forall (m :: * -> *). Storage m -> SignalSource m Int
storageUtilisationCountSource :: SignalSource m Int,
            forall (m :: * -> *). Storage m -> Ref m Int
storageQueueCountRef :: Ref m Int,
            forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageQueueCountStatsRef :: Ref m (TimingStats Int),
            forall (m :: * -> *). Storage m -> SignalSource m Int
storageQueueCountSource :: SignalSource m Int,
            forall (m :: * -> *). Storage m -> Ref m Double
storageTotalWaitTimeRef :: Ref m Double,
            forall (m :: * -> *). Storage m -> Ref m (SamplingStats Double)
storageWaitTimeRef :: Ref m (SamplingStats Double),
            forall (m :: * -> *). Storage m -> SignalSource m ()
storageWaitTimeSource :: SignalSource m (),
            forall (m :: * -> *).
Storage m
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (StorageDelayedItem m)
storageDelayChain :: StrategyQueue m (TransactQueueStrategy FCFS) (StorageDelayedItem m) }

-- | Identifies an item that was delayed.
data StorageDelayedItem m =
  StorageDelayedItem { forall (m :: * -> *). StorageDelayedItem m -> Double
delayedItemTime :: Double,
                       forall (m :: * -> *). StorageDelayedItem m -> Int
delayedItemDecrement :: Int,
                       forall (m :: * -> *). StorageDelayedItem m -> FrozenCont m ()
delayedItemCont :: FrozenCont m () }

instance MonadDES m => Eq (Storage m) where

  {-# INLINABLE (==) #-}
  Storage m
x == :: Storage m -> Storage m -> Bool
== Storage m
y = forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
x forall a. Eq a => a -> a -> Bool
== forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
y  -- unique references

-- | Create a new storage by the specified capacity.
newStorage :: MonadDES m => Int -> Event m (Storage m)
{-# INLINABLE newStorage #-}
newStorage :: forall (m :: * -> *). MonadDES m => Int -> Event m (Storage m)
newStorage Int
capacity =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let r :: Run m
r = forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
         t :: Double
t = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Ref m Int
contentRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
capacity
     Ref m (TimingStats Int)
contentStatsRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES 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
capacity
     SignalSource m Int
contentSource <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Int
useCountRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     SignalSource m Int
useCountSource <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Int
usedContentRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     SignalSource m Int
usedContentSource <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Int
utilCountRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m (TimingStats Int)
utilCountStatsRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES 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
     SignalSource m Int
utilCountSource <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Int
queueCountRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m (TimingStats Int)
queueCountStatsRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES 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
     SignalSource m Int
queueCountSource <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Double
totalWaitTimeRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m (SamplingStats Double)
waitTimeRef <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef forall a. SamplingData a => SamplingStats a
emptySamplingStats
     SignalSource m ()
waitTimeSource <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     StrategyQueue m (TransactQueueStrategy FCFS) (StorageDelayedItem m)
delayChain <- forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) s a.
QueueStrategy m s =>
s -> Simulation m (StrategyQueue m s a)
newStrategyQueue (forall s. s -> TransactQueueStrategy s
TransactQueueStrategy FCFS
FCFS)
     forall (m :: * -> *) a. Monad m => a -> m a
return Storage { storageCapacity :: Int
storageCapacity = Int
capacity,
                      storageContentRef :: Ref m Int
storageContentRef = Ref m Int
contentRef,
                      storageContentStatsRef :: Ref m (TimingStats Int)
storageContentStatsRef = Ref m (TimingStats Int)
contentStatsRef,
                      storageContentSource :: SignalSource m Int
storageContentSource = SignalSource m Int
contentSource,
                      storageUseCountRef :: Ref m Int
storageUseCountRef = Ref m Int
useCountRef,
                      storageUseCountSource :: SignalSource m Int
storageUseCountSource = SignalSource m Int
useCountSource,
                      storageUsedContentRef :: Ref m Int
storageUsedContentRef = Ref m Int
usedContentRef,
                      storageUsedContentSource :: SignalSource m Int
storageUsedContentSource = SignalSource m Int
usedContentSource,
                      storageUtilisationCountRef :: Ref m Int
storageUtilisationCountRef = Ref m Int
utilCountRef,
                      storageUtilisationCountStatsRef :: Ref m (TimingStats Int)
storageUtilisationCountStatsRef = Ref m (TimingStats Int)
utilCountStatsRef,
                      storageUtilisationCountSource :: SignalSource m Int
storageUtilisationCountSource = SignalSource m Int
utilCountSource,
                      storageQueueCountRef :: Ref m Int
storageQueueCountRef = Ref m Int
queueCountRef,
                      storageQueueCountStatsRef :: Ref m (TimingStats Int)
storageQueueCountStatsRef = Ref m (TimingStats Int)
queueCountStatsRef,
                      storageQueueCountSource :: SignalSource m Int
storageQueueCountSource = SignalSource m Int
queueCountSource,
                      storageTotalWaitTimeRef :: Ref m Double
storageTotalWaitTimeRef = Ref m Double
totalWaitTimeRef,
                      storageWaitTimeRef :: Ref m (SamplingStats Double)
storageWaitTimeRef = Ref m (SamplingStats Double)
waitTimeRef,
                      storageWaitTimeSource :: SignalSource m ()
storageWaitTimeSource = SignalSource m ()
waitTimeSource,
                      storageDelayChain :: StrategyQueue m (TransactQueueStrategy FCFS) (StorageDelayedItem m)
storageDelayChain = StrategyQueue m (TransactQueueStrategy FCFS) (StorageDelayedItem m)
delayChain }

-- | Whether the storage is empty, i.e. completely unused.
storageEmpty :: MonadDES m => Storage m -> Event m Bool
{-# INLINABLE storageEmpty #-}
storageEmpty :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Bool
storageEmpty Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r)
     forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n forall a. Eq a => a -> a -> Bool
== forall (m :: * -> *). Storage m -> Int
storageCapacity Storage m
r)

-- | Whether the storage is full, i.e. completely used.
storageFull :: MonadDES m => Storage m -> Event m Bool
{-# INLINABLE storageFull #-}
storageFull :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Bool
storageFull Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r)
     forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n forall a. Eq a => a -> a -> Bool
== Int
0)

-- | Return the current storage content available for use.
storageContent :: MonadDES m => Storage m -> Event m Int
{-# INLINABLE storageContent #-}
storageContent :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Int
storageContent Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r)

-- | Return the statistics of the storage content available for use.
storageContentStats :: MonadDES m => Storage m -> Event m (TimingStats Int)
{-# INLINABLE storageContentStats #-}
storageContentStats :: forall (m :: * -> *).
MonadDES m =>
Storage m -> Event m (TimingStats Int)
storageContentStats Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageContentStatsRef Storage m
r)

-- | Signal triggered when the 'storageContent' property changes.
storageContentChanged :: MonadDES m => Storage m -> Signal m Int
{-# INLINABLE storageContentChanged #-}
storageContentChanged :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageContentChanged Storage m
r =
  forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Storage m -> SignalSource m Int
storageContentSource Storage m
r

-- | Signal triggered when the 'storageContent' property changes.
storageContentChanged_ :: MonadDES m => Storage m -> Signal m ()
{-# INLINABLE storageContentChanged_ #-}
storageContentChanged_ :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageContentChanged_ Storage m
r =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageContentChanged Storage m
r

-- | Return the total use count of the storage.
storageUseCount :: MonadDES m => Storage m -> Event m Int
{-# INLINABLE storageUseCount #-}
storageUseCount :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Int
storageUseCount Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUseCountRef Storage m
r)

-- | Signal triggered when the 'storageUseCount' property changes.
storageUseCountChanged :: MonadDES m => Storage m -> Signal m Int
{-# INLINABLE storageUseCountChanged #-}
storageUseCountChanged :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageUseCountChanged Storage m
r =
  forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Storage m -> SignalSource m Int
storageUseCountSource Storage m
r

-- | Signal triggered when the 'storageUseCount' property changes.
storageUseCountChanged_ :: MonadDES m => Storage m -> Signal m ()
{-# INLINABLE storageUseCountChanged_ #-}
storageUseCountChanged_ :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageUseCountChanged_ Storage m
r =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageUseCountChanged Storage m
r

-- | Return the total used content of the storage.
storageUsedContent :: MonadDES m => Storage m -> Event m Int
{-# INLINABLE storageUsedContent #-}
storageUsedContent :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Int
storageUsedContent Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUsedContentRef Storage m
r)

-- | Signal triggered when the 'storageUsedContent' property changes.
storageUsedContentChanged :: MonadDES m => Storage m -> Signal m Int
{-# INLINABLE storageUsedContentChanged #-}
storageUsedContentChanged :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageUsedContentChanged Storage m
r =
  forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Storage m -> SignalSource m Int
storageUsedContentSource Storage m
r

-- | Signal triggered when the 'storageUsedContent' property changes.
storageUsedContentChanged_ :: MonadDES m => Storage m -> Signal m ()
{-# INLINABLE storageUsedContentChanged_ #-}
storageUsedContentChanged_ :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageUsedContentChanged_ Storage m
r =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageUsedContentChanged Storage m
r

-- | Return the current utilisation count of the storage.
storageUtilisationCount :: MonadDES m => Storage m -> Event m Int
{-# INLINABLE storageUtilisationCount #-}
storageUtilisationCount :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Int
storageUtilisationCount Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUtilisationCountRef Storage m
r)

-- | Return the statistics for the utilisation count of the storage.
storageUtilisationCountStats :: MonadDES m => Storage m -> Event m (TimingStats Int)
{-# INLINABLE storageUtilisationCountStats #-}
storageUtilisationCountStats :: forall (m :: * -> *).
MonadDES m =>
Storage m -> Event m (TimingStats Int)
storageUtilisationCountStats Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageUtilisationCountStatsRef Storage m
r)

-- | Signal triggered when the 'storageUtilisationCount' property changes.
storageUtilisationCountChanged :: MonadDES m => Storage m -> Signal m Int
{-# INLINABLE storageUtilisationCountChanged #-}
storageUtilisationCountChanged :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageUtilisationCountChanged Storage m
r =
  forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Storage m -> SignalSource m Int
storageUtilisationCountSource Storage m
r

-- | Signal triggered when the 'storageUtilisationCount' property changes.
storageUtilisationCountChanged_ :: MonadDES m => Storage m -> Signal m ()
{-# INLINABLE storageUtilisationCountChanged_ #-}
storageUtilisationCountChanged_ :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageUtilisationCountChanged_ Storage m
r =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageUtilisationCountChanged Storage m
r

-- | Return the current queue length of the storage.
storageQueueCount :: MonadDES m => Storage m -> Event m Int
{-# INLINABLE storageQueueCount #-}
storageQueueCount :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Int
storageQueueCount Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageQueueCountRef Storage m
r)

-- | Return the statistics for the queue length of the storage.
storageQueueCountStats :: MonadDES m => Storage m -> Event m (TimingStats Int)
{-# INLINABLE storageQueueCountStats #-}
storageQueueCountStats :: forall (m :: * -> *).
MonadDES m =>
Storage m -> Event m (TimingStats Int)
storageQueueCountStats Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageQueueCountStatsRef Storage m
r)

-- | Signal triggered when the 'storageQueueCount' property changes.
storageQueueCountChanged :: MonadDES m => Storage m -> Signal m Int
{-# INLINABLE storageQueueCountChanged #-}
storageQueueCountChanged :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageQueueCountChanged Storage m
r =
  forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Storage m -> SignalSource m Int
storageQueueCountSource Storage m
r

-- | Signal triggered when the 'storageQueueCount' property changes.
storageQueueCountChanged_ :: MonadDES m => Storage m -> Signal m ()
{-# INLINABLE storageQueueCountChanged_ #-}
storageQueueCountChanged_ :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageQueueCountChanged_ Storage m
r =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Signal m Int
storageQueueCountChanged Storage m
r

-- | Return the total wait time of the storage.
storageTotalWaitTime :: MonadDES m => Storage m -> Event m Double
{-# INLINABLE storageTotalWaitTime #-}
storageTotalWaitTime :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Double
storageTotalWaitTime Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Double
storageTotalWaitTimeRef Storage m
r)

-- | Return the statistics for the wait time of the storage.
storageWaitTime :: MonadDES m => Storage m -> Event m (SamplingStats Double)
{-# INLINABLE storageWaitTime #-}
storageWaitTime :: forall (m :: * -> *).
MonadDES m =>
Storage m -> Event m (SamplingStats Double)
storageWaitTime Storage m
r =
  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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m (SamplingStats Double)
storageWaitTimeRef Storage m
r)

-- | Signal triggered when the 'storageTotalWaitTime' and 'storageWaitTime' properties change.
storageWaitTimeChanged :: MonadDES m => Storage m -> Signal m (SamplingStats Double)
{-# INLINABLE storageWaitTimeChanged #-}
storageWaitTimeChanged :: forall (m :: * -> *).
MonadDES m =>
Storage m -> Signal m (SamplingStats Double)
storageWaitTimeChanged Storage m
r =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (\() -> forall (m :: * -> *).
MonadDES m =>
Storage m -> Event m (SamplingStats Double)
storageWaitTime Storage m
r) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageWaitTimeChanged_ Storage m
r

-- | Signal triggered when the 'storageTotalWaitTime' and 'storageWaitTime' properties change.
storageWaitTimeChanged_ :: MonadDES m => Storage m -> Signal m ()
{-# INLINABLE storageWaitTimeChanged_ #-}
storageWaitTimeChanged_ :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageWaitTimeChanged_ Storage m
r =
  forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Storage m -> SignalSource m ()
storageWaitTimeSource Storage m
r

-- | Return the average holding time per unit.
storageAverageHoldingTime :: MonadDES m => Storage m -> Event m Double
{-# INLINABLE storageAverageHoldingTime #-}
storageAverageHoldingTime :: forall (m :: * -> *). MonadDES m => Storage m -> Event m Double
storageAverageHoldingTime Storage m
r =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do TimingStats Int
s <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageUtilisationCountStatsRef Storage m
r)
     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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUtilisationCountRef Storage m
r)
     Int
m <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUsedContentRef Storage m
r)
     let t :: Double
t  = forall (m :: * -> *). Point m -> Double
pointTime Point m
p
         s' :: TimingStats Int
s' = forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
n TimingStats Int
s
         k :: Double
k  = forall a. TimingStats a -> Double
timingStatsSum TimingStats Int
s' forall a. Fractional a => a -> a -> a
/ (forall a. Fractional a => Rational -> a
fromRational forall a b. (a -> b) -> a -> b
$ forall a. Real a => a -> Rational
toRational Int
m)
     forall (m :: * -> *) a. Monad m => a -> m a
return Double
k

-- | Enter the storage.
enterStorage :: MonadDES m
                => Storage m
                -- ^ the requested storage
                -> Transact m a
                -- ^ a transact that makes the request
                -> Int
                -- ^ the content decrement
                -> Process m ()
{-# INLINABLE enterStorage #-}
enterStorage :: forall (m :: * -> *) a.
MonadDES m =>
Storage m -> Transact m a -> Int -> Process m ()
enterStorage Storage m
r Transact m a
transact Int
decrement =
  forall (m :: * -> *) a. (ProcessId m -> Cont m a) -> Process m a
Process forall a b. (a -> b) -> a -> b
$ \ProcessId m
pid ->
  forall (m :: * -> *) a. (ContParams m a -> Event m ()) -> Cont m a
Cont forall a b. (a -> b) -> a -> b
$ \ContParams m ()
c ->
  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
     Bool
f <- 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.
QueueStrategy m s =>
StrategyQueue m s a -> Event m Bool
strategyQueueNull (forall (m :: * -> *).
Storage m
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (StorageDelayedItem m)
storageDelayChain Storage m
r)
     if Bool
f
       then forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
            forall (m :: * -> *) a. ContParams m a -> Cont m a -> Event m ()
invokeCont ContParams m ()
c forall a b. (a -> b) -> a -> b
$
            forall (m :: * -> *) a. ProcessId m -> Process m a -> Cont m a
invokeProcess ProcessId m
pid forall a b. (a -> b) -> a -> b
$
            forall (m :: * -> *) a.
MonadDES m =>
Storage m -> Transact m a -> Int -> Process m ()
enterStorage' Storage m
r Transact m a
transact Int
decrement
       else do FrozenCont m ()
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.
MonadDES m =>
ContParams m a -> a -> Event m () -> Event m (FrozenCont m a)
freezeContReentering ContParams m ()
c () forall a b. (a -> b) -> a -> b
$
                    forall (m :: * -> *) a. ContParams m a -> Cont m a -> Event m ()
invokeCont ContParams m ()
c forall a b. (a -> b) -> a -> b
$
                    forall (m :: * -> *) a. ProcessId m -> Process m a -> Cont m a
invokeProcess ProcessId m
pid forall a b. (a -> b) -> a -> b
$
                    forall (m :: * -> *) a.
MonadDES m =>
Storage m -> Transact m a -> Int -> Process m ()
enterStorage Storage m
r Transact m a
transact Int
decrement
               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 :: * -> *).
Storage m
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (StorageDelayedItem m)
storageDelayChain Storage m
r)
                 (forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact)
                 (forall (m :: * -> *).
Double -> Int -> FrozenCont m () -> StorageDelayedItem m
StorageDelayedItem Double
t Int
decrement FrozenCont m ()
c)
               forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageQueueCount Storage m
r Int
1

-- | Enter the storage.
enterStorage' :: MonadDES m
                 => Storage m
                 -- ^ the requested storage
                 -> Transact m a
                 -- ^ a transact that makes the request
                 -> Int
                 -- ^ the content decrement
                 -> Process m ()
{-# INLINABLE enterStorage' #-}
enterStorage' :: forall (m :: * -> *) a.
MonadDES m =>
Storage m -> Transact m a -> Int -> Process m ()
enterStorage' Storage m
r Transact m a
transact Int
decrement =
  forall (m :: * -> *) a. (ProcessId m -> Cont m a) -> Process m a
Process forall a b. (a -> b) -> a -> b
$ \ProcessId m
pid ->
  forall (m :: * -> *) a. (ContParams m a -> Event m ()) -> Cont m a
Cont forall a b. (a -> b) -> a -> b
$ \ContParams m ()
c ->
  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
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r)
     if Int
a forall a. Ord a => a -> a -> Bool
< Int
decrement
       then do FrozenCont m ()
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.
MonadDES m =>
ContParams m a -> a -> Event m () -> Event m (FrozenCont m a)
freezeContReentering ContParams m ()
c () forall a b. (a -> b) -> a -> b
$
                    forall (m :: * -> *) a. ContParams m a -> Cont m a -> Event m ()
invokeCont ContParams m ()
c forall a b. (a -> b) -> a -> b
$
                    forall (m :: * -> *) a. ProcessId m -> Process m a -> Cont m a
invokeProcess ProcessId m
pid forall a b. (a -> b) -> a -> b
$
                    forall (m :: * -> *) a.
MonadDES m =>
Storage m -> Transact m a -> Int -> Process m ()
enterStorage Storage m
r Transact m a
transact Int
decrement
               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 :: * -> *).
Storage m
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (StorageDelayedItem m)
storageDelayChain Storage m
r)
                 (forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact)
                 (forall (m :: * -> *).
Double -> Int -> FrozenCont m () -> StorageDelayedItem m
StorageDelayedItem Double
t Int
decrement FrozenCont m ()
c)
               forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageQueueCount Storage m
r Int
1
       else do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadDES m =>
Storage m -> Double -> Event m ()
updateStorageWaitTime Storage m
r Double
0
               forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageContent Storage m
r (- Int
decrement)
               forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUseCount Storage m
r 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 :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUsedContent Storage m
r Int
decrement
               forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUtilisationCount Storage m
r Int
decrement
               forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
resumeCont ContParams m ()
c ()

-- | Leave the storage.
leaveStorage :: MonadDES m
                => Storage m
                -- ^ the storage to leave
                -> Int
                -- ^ the content increment
                -> Process m ()
{-# INLINABLE leaveStorage #-}
leaveStorage :: forall (m :: * -> *).
MonadDES m =>
Storage m -> Int -> Process m ()
leaveStorage Storage m
r Int
increment =
  forall (m :: * -> *) a. (ProcessId m -> Cont m a) -> Process m a
Process forall a b. (a -> b) -> a -> b
$ \ProcessId m
_ ->
  forall (m :: * -> *) a. (ContParams m a -> Event m ()) -> Cont m a
Cont forall a b. (a -> b) -> a -> b
$ \ContParams m ()
c ->
  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 :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
leaveStorageWithinEvent Storage m
r Int
increment
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
resumeCont ContParams m ()
c ()

-- | Leave the storage.
leaveStorageWithinEvent :: MonadDES m
                           => Storage m
                           -- ^ the storage to leave
                           -> Int
                           -- ^ the content increment
                           -> Event m ()
{-# INLINABLE leaveStorageWithinEvent #-}
leaveStorageWithinEvent :: forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
leaveStorageWithinEvent Storage m
r Int
increment =
  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
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUtilisationCount Storage m
r (- Int
increment)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageContent Storage m
r Int
increment
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent Double
t forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Event m ()
tryEnterStorage Storage m
r

-- | Try to enter the storage.
tryEnterStorage :: MonadDES m => Storage m -> Event m ()
{-# INLINABLE tryEnterStorage #-}
tryEnterStorage :: forall (m :: * -> *). MonadDES m => Storage m -> Event m ()
tryEnterStorage Storage m
r =
  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
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r)
     if Int
a forall a. Ord a => a -> a -> Bool
> Int
0
       then forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Event m ()
letEnterStorage Storage m
r
       else forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Let enter the storage.
letEnterStorage :: MonadDES m => Storage m -> Event m ()
{-# INLINABLE letEnterStorage #-}
letEnterStorage :: forall (m :: * -> *). MonadDES m => Storage m -> Event m ()
letEnterStorage Storage m
r =
  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
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r)
     forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
a forall a. Ord a => a -> a -> Bool
> forall (m :: * -> *). Storage m -> Int
storageCapacity Storage m
r) forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> m a
throwComp forall a b. (a -> b) -> a -> b
$
       String -> SimulationRetry
SimulationRetry forall a b. (a -> b) -> a -> b
$
       String
"The storage content cannot exceed the limited capacity: leaveStorage'"
     Maybe (StorageDelayedItem m)
x <- 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.
DeletingQueueStrategy m s =>
StrategyQueue m s a -> (a -> Bool) -> Event m (Maybe a)
strategyQueueDeleteBy
          (forall (m :: * -> *).
Storage m
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (StorageDelayedItem m)
storageDelayChain Storage m
r)
          (\StorageDelayedItem m
i -> forall (m :: * -> *). StorageDelayedItem m -> Int
delayedItemDecrement StorageDelayedItem m
i forall a. Ord a => a -> a -> Bool
<= Int
a)
     case Maybe (StorageDelayedItem m)
x of
       Maybe (StorageDelayedItem m)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
       Just (StorageDelayedItem Double
t0 Int
decrement0 FrozenCont m ()
c0) ->
         do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageQueueCount Storage m
r (-Int
1)
            Maybe (ContParams m ())
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.
FrozenCont m a -> Event m (Maybe (ContParams m a))
unfreezeCont FrozenCont m ()
c0
            case Maybe (ContParams m ())
c of
              Maybe (ContParams m ())
Nothing ->
                forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Event m ()
letEnterStorage Storage m
r
              Just ContParams m ()
c ->
                do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageContent Storage m
r (- Int
decrement0)
                   forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadDES m =>
Storage m -> Double -> Event m ()
updateStorageWaitTime Storage m
r (Double
t forall a. Num a => a -> a -> a
- Double
t0)
                   forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUtilisationCount Storage m
r Int
decrement0
                   forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUseCount Storage m
r 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 :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUsedContent Storage m
r Int
decrement0
                   forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent Double
t forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
reenterCont ContParams m ()
c ()

-- | Signal triggered when one of the storage counters changes.
storageChanged_ :: MonadDES m => Storage m -> Signal m ()
{-# INLINABLE storageChanged_ #-}
storageChanged_ :: forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageChanged_ Storage m
r =
  forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageContentChanged_ Storage m
r forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageUsedContentChanged_ Storage m
r forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageUtilisationCountChanged_ Storage m
r forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *). MonadDES m => Storage m -> Signal m ()
storageQueueCountChanged_ Storage m
r

-- | Update the storage content and its statistics.
updateStorageContent :: MonadDES m => Storage m -> Int -> Event m ()
{-# INLINABLE updateStorageContent #-}
updateStorageContent :: forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageContent Storage m
r Int
delta =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r) Int
a'
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageContentStatsRef Storage m
r) forall a b. (a -> b) -> a -> b
$
       forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (forall (m :: * -> *). Point m -> Double
pointTime Point m
p) Int
a'
     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 :: * -> *). Storage m -> SignalSource m Int
storageContentSource Storage m
r) Int
a'

-- | Update the storage use count.
updateStorageUseCount :: MonadDES m => Storage m -> Int -> Event m ()
{-# INLINABLE updateStorageUseCount #-}
updateStorageUseCount :: forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUseCount Storage m
r Int
delta =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUseCountRef Storage m
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUseCountRef Storage m
r) Int
a'
     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 :: * -> *). Storage m -> SignalSource m Int
storageUseCountSource Storage m
r) Int
a'

-- | Update the storage used content.
updateStorageUsedContent :: MonadDES m => Storage m -> Int -> Event m ()
{-# INLINABLE updateStorageUsedContent #-}
updateStorageUsedContent :: forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUsedContent Storage m
r Int
delta =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUsedContentRef Storage m
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUsedContentRef Storage m
r) Int
a'
     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 :: * -> *). Storage m -> SignalSource m Int
storageUsedContentSource Storage m
r) Int
a'

-- | Update the storage queue length and its statistics.
updateStorageQueueCount :: MonadDES m => Storage m -> Int -> Event m ()
{-# INLINABLE updateStorageQueueCount #-}
updateStorageQueueCount :: forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageQueueCount Storage m
r Int
delta =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageQueueCountRef Storage m
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Int
storageQueueCountRef Storage m
r) Int
a'
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageQueueCountStatsRef Storage m
r) forall a b. (a -> b) -> a -> b
$
       forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (forall (m :: * -> *). Point m -> Double
pointTime Point m
p) Int
a'
     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 :: * -> *). Storage m -> SignalSource m Int
storageQueueCountSource Storage m
r) Int
a'

-- | Update the storage utilisation count and its statistics.
updateStorageUtilisationCount :: MonadDES m => Storage m -> Int -> Event m ()
{-# INLINABLE updateStorageUtilisationCount #-}
updateStorageUtilisationCount :: forall (m :: * -> *). MonadDES m => Storage m -> Int -> Event m ()
updateStorageUtilisationCount Storage m
r Int
delta =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUtilisationCountRef Storage m
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUtilisationCountRef Storage m
r) Int
a'
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageUtilisationCountStatsRef Storage m
r) forall a b. (a -> b) -> a -> b
$
       forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (forall (m :: * -> *). Point m -> Double
pointTime Point m
p) Int
a'
     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 :: * -> *). Storage m -> SignalSource m Int
storageUtilisationCountSource Storage m
r) Int
a'

-- | Update the storage wait time and its statistics.
updateStorageWaitTime :: MonadDES m => Storage m -> Double -> Event m ()
{-# INLINABLE updateStorageWaitTime #-}
updateStorageWaitTime :: forall (m :: * -> *).
MonadDES m =>
Storage m -> Double -> Event m ()
updateStorageWaitTime Storage m
r Double
delta =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
a <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Double
storageTotalWaitTimeRef Storage m
r)
     let a' :: Double
a' = Double
a forall a. Num a => a -> a -> a
+ Double
delta
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Double
storageTotalWaitTimeRef Storage m
r) Double
a'
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *). Storage m -> Ref m (SamplingStats Double)
storageWaitTimeRef Storage m
r) forall a b. (a -> b) -> a -> b
$
       forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
delta
     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 :: * -> *). Storage m -> SignalSource m ()
storageWaitTimeSource Storage m
r) ()

-- | Reset the statistics.
resetStorage :: MonadDES m => Storage m -> Event m ()
{-# INLINABLE resetStorage #-}
resetStorage :: forall (m :: * -> *). MonadDES m => Storage m -> Event m ()
resetStorage Storage m
r =
  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
content <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageContentRef Storage m
r)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageContentStatsRef Storage m
r) forall a b. (a -> b) -> a -> b
$
       forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
content
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUseCountRef Storage m
r) Int
0
     let usedContent :: Int
usedContent = forall (m :: * -> *). Storage m -> Int
storageCapacity Storage m
r forall a. Num a => a -> a -> a
- Int
content
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUsedContentRef Storage m
r) Int
usedContent
     Int
utilCount <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageUtilisationCountRef Storage m
r)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageUtilisationCountStatsRef Storage m
r) forall a b. (a -> b) -> a -> b
$
       forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
utilCount
     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. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Storage m -> Ref m Int
storageQueueCountRef Storage m
r)
     forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m (TimingStats Int)
storageQueueCountStatsRef Storage m
r) 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. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m Double
storageTotalWaitTimeRef Storage m
r) Double
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. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Storage m -> Ref m (SamplingStats Double)
storageWaitTimeRef Storage m
r) forall a. SamplingData a => SamplingStats a
emptySamplingStats
     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 :: * -> *). Storage m -> SignalSource m Int
storageUseCountSource Storage m
r) 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. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *). Storage m -> SignalSource m Int
storageUsedContentSource Storage m
r) Int
usedContent
     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 :: * -> *). Storage m -> SignalSource m Int
storageUtilisationCountSource Storage m
r) Int
utilCount
     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 :: * -> *). Storage m -> SignalSource m ()
storageWaitTimeSource Storage m
r) ()