module Simulation.Aivika.Trans.Queue.Infinite
(
FCFSQueue,
LCFSQueue,
SIROQueue,
PriorityQueue,
Queue,
newFCFSQueue,
newLCFSQueue,
newSIROQueue,
newPriorityQueue,
newQueue,
enqueueStoringStrategy,
dequeueStrategy,
queueNull,
queueCount,
queueCountStats,
enqueueStoreCount,
dequeueCount,
dequeueExtractCount,
enqueueStoreRate,
dequeueRate,
dequeueExtractRate,
queueWaitTime,
dequeueWaitTime,
queueRate,
dequeue,
dequeueWithOutputPriority,
tryDequeue,
enqueue,
enqueueWithStoringPriority,
queueSummary,
queueNullChanged,
queueNullChanged_,
queueCountChanged,
queueCountChanged_,
enqueueStoreCountChanged,
enqueueStoreCountChanged_,
dequeueCountChanged,
dequeueCountChanged_,
dequeueExtractCountChanged,
dequeueExtractCountChanged_,
queueWaitTimeChanged,
queueWaitTimeChanged_,
dequeueWaitTimeChanged,
dequeueWaitTimeChanged_,
queueRateChanged,
queueRateChanged_,
enqueueStored,
dequeueRequested,
dequeueExtracted,
queueChanged_) where
import Data.Monoid
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Trans.Session
import Simulation.Aivika.Trans.ProtoRef
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Parameter
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Dynamics
import Simulation.Aivika.Trans.Internal.Event
import Simulation.Aivika.Trans.Internal.Process
import Simulation.Aivika.Trans.Internal.Signal
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.Resource
import Simulation.Aivika.Trans.QueueStrategy
import Simulation.Aivika.Trans.Statistics
type FCFSQueue m a = Queue m FCFS FCFS a
type LCFSQueue m a = Queue m LCFS FCFS a
type SIROQueue m a = Queue m SIRO FCFS a
type PriorityQueue m a = Queue m StaticPriorities FCFS a
data Queue m sm so a =
Queue { enqueueStoringStrategy :: sm,
dequeueStrategy :: so,
queueStore :: StrategyQueue m sm (QueueItem a),
dequeueRes :: Resource m so,
queueCountRef :: ProtoRef m Int,
queueCountStatsRef :: ProtoRef m (TimingStats Int),
enqueueStoreCountRef :: ProtoRef m Int,
dequeueCountRef :: ProtoRef m Int,
dequeueExtractCountRef :: ProtoRef m Int,
queueWaitTimeRef :: ProtoRef m (SamplingStats Double),
dequeueWaitTimeRef :: ProtoRef m (SamplingStats Double),
enqueueStoredSource :: SignalSource m a,
dequeueRequestedSource :: SignalSource m (),
dequeueExtractedSource :: SignalSource m a }
data QueueItem a =
QueueItem { itemValue :: a,
itemStoringTime :: Double
}
newFCFSQueue :: MonadComp m => Event m (FCFSQueue m a)
newFCFSQueue = newQueue FCFS FCFS
newLCFSQueue :: MonadComp m => Event m (LCFSQueue m a)
newLCFSQueue = newQueue LCFS FCFS
newSIROQueue :: MonadComp m => Event m (SIROQueue m a)
newSIROQueue = newQueue SIRO FCFS
newPriorityQueue :: MonadComp m => Event m (PriorityQueue m a)
newPriorityQueue = newQueue StaticPriorities FCFS
newQueue :: (MonadComp m,
QueueStrategy m sm,
QueueStrategy m so) =>
sm
-> so
-> Event m (Queue m sm so a)
newQueue sm so =
do t <- liftDynamics time
sn <- liftParameter simulationSession
i <- liftComp $ newProtoRef sn 0
is <- liftComp $ newProtoRef sn $ returnTimingStats t 0
cm <- liftComp $ newProtoRef sn 0
cr <- liftComp $ newProtoRef sn 0
co <- liftComp $ newProtoRef sn 0
qm <- liftSimulation $ newStrategyQueue sm
ro <- liftSimulation $ newResourceWithMaxCount so 0 Nothing
w <- liftComp $ newProtoRef sn mempty
wo <- liftComp $ newProtoRef sn mempty
s3 <- liftSimulation newSignalSource
s4 <- liftSimulation newSignalSource
s5 <- liftSimulation newSignalSource
return Queue { enqueueStoringStrategy = sm,
dequeueStrategy = so,
queueStore = qm,
dequeueRes = ro,
queueCountRef = i,
queueCountStatsRef = is,
enqueueStoreCountRef = cm,
dequeueCountRef = cr,
dequeueExtractCountRef = co,
queueWaitTimeRef = w,
dequeueWaitTimeRef = wo,
enqueueStoredSource = s3,
dequeueRequestedSource = s4,
dequeueExtractedSource = s5 }
queueNull :: MonadComp m => Queue m sm so a -> Event m Bool
queueNull q =
Event $ \p ->
do n <- readProtoRef (queueCountRef q)
return (n == 0)
queueNullChanged :: MonadComp m => Queue m sm so a -> Signal m Bool
queueNullChanged q =
mapSignalM (const $ queueNull q) (queueNullChanged_ q)
queueNullChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
queueNullChanged_ = queueCountChanged_
queueCount :: MonadComp m => Queue m sm so a -> Event m Int
queueCount q =
Event $ \p -> readProtoRef (queueCountRef q)
queueCountStats :: MonadComp m => Queue m sm so a -> Event m (TimingStats Int)
queueCountStats q =
Event $ \p -> readProtoRef (queueCountStatsRef q)
queueCountChanged :: MonadComp m => Queue m sm so a -> Signal m Int
queueCountChanged q =
mapSignalM (const $ queueCount q) (queueCountChanged_ q)
queueCountChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
queueCountChanged_ q =
mapSignal (const ()) (enqueueStored q) <>
mapSignal (const ()) (dequeueExtracted q)
enqueueStoreCount :: MonadComp m => Queue m sm so a -> Event m Int
enqueueStoreCount q =
Event $ \p -> readProtoRef (enqueueStoreCountRef q)
enqueueStoreCountChanged :: MonadComp m => Queue m sm so a -> Signal m Int
enqueueStoreCountChanged q =
mapSignalM (const $ enqueueStoreCount q) (enqueueStoreCountChanged_ q)
enqueueStoreCountChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
enqueueStoreCountChanged_ q =
mapSignal (const ()) (enqueueStored q)
dequeueCount :: MonadComp m => Queue m sm so a -> Event m Int
dequeueCount q =
Event $ \p -> readProtoRef (dequeueCountRef q)
dequeueCountChanged :: MonadComp m => Queue m sm so a -> Signal m Int
dequeueCountChanged q =
mapSignalM (const $ dequeueCount q) (dequeueCountChanged_ q)
dequeueCountChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
dequeueCountChanged_ q =
mapSignal (const ()) (dequeueRequested q)
dequeueExtractCount :: MonadComp m => Queue m sm so a -> Event m Int
dequeueExtractCount q =
Event $ \p -> readProtoRef (dequeueExtractCountRef q)
dequeueExtractCountChanged :: MonadComp m => Queue m sm so a -> Signal m Int
dequeueExtractCountChanged q =
mapSignalM (const $ dequeueExtractCount q) (dequeueExtractCountChanged_ q)
dequeueExtractCountChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
dequeueExtractCountChanged_ q =
mapSignal (const ()) (dequeueExtracted q)
enqueueStoreRate :: MonadComp m => Queue m sm so a -> Event m Double
enqueueStoreRate q =
Event $ \p ->
do x <- readProtoRef (enqueueStoreCountRef q)
let t0 = spcStartTime $ pointSpecs p
t = pointTime p
return (fromIntegral x / (t t0))
dequeueRate :: MonadComp m => Queue m sm so a -> Event m Double
dequeueRate q =
Event $ \p ->
do x <- readProtoRef (dequeueCountRef q)
let t0 = spcStartTime $ pointSpecs p
t = pointTime p
return (fromIntegral x / (t t0))
dequeueExtractRate :: MonadComp m => Queue m sm so a -> Event m Double
dequeueExtractRate q =
Event $ \p ->
do x <- readProtoRef (dequeueExtractCountRef q)
let t0 = spcStartTime $ pointSpecs p
t = pointTime p
return (fromIntegral x / (t t0))
queueWaitTime :: MonadComp m => Queue m sm so a -> Event m (SamplingStats Double)
queueWaitTime q =
Event $ \p -> readProtoRef (queueWaitTimeRef q)
queueWaitTimeChanged :: MonadComp m => Queue m sm so a -> Signal m (SamplingStats Double)
queueWaitTimeChanged q =
mapSignalM (const $ queueWaitTime q) (queueWaitTimeChanged_ q)
queueWaitTimeChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
queueWaitTimeChanged_ q =
mapSignal (const ()) (dequeueExtracted q)
dequeueWaitTime :: MonadComp m => Queue m sm so a -> Event m (SamplingStats Double)
dequeueWaitTime q =
Event $ \p -> readProtoRef (dequeueWaitTimeRef q)
dequeueWaitTimeChanged :: MonadComp m => Queue m sm so a -> Signal m (SamplingStats Double)
dequeueWaitTimeChanged q =
mapSignalM (const $ dequeueWaitTime q) (dequeueWaitTimeChanged_ q)
dequeueWaitTimeChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
dequeueWaitTimeChanged_ q =
mapSignal (const ()) (dequeueExtracted q)
queueRate :: MonadComp m => Queue m sm so a -> Event m Double
queueRate q =
Event $ \p ->
do x <- readProtoRef (queueCountStatsRef q)
y <- readProtoRef (queueWaitTimeRef q)
return (timingStatsMean x / samplingStatsMean y)
queueRateChanged :: MonadComp m => Queue m sm so a -> Signal m Double
queueRateChanged q =
mapSignalM (const $ queueRate q) (queueRateChanged_ q)
queueRateChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
queueRateChanged_ q =
mapSignal (const ()) (enqueueStored q) <>
mapSignal (const ()) (dequeueExtracted q)
dequeue :: (MonadComp m,
DequeueStrategy m sm,
EnqueueStrategy m so)
=> Queue m sm so a
-> Process m a
dequeue q =
do t <- liftEvent $ dequeueRequest q
requestResource (dequeueRes q)
liftEvent $ dequeueExtract q t
dequeueWithOutputPriority :: (MonadComp m,
DequeueStrategy m sm,
PriorityQueueStrategy m so po)
=> Queue m sm so a
-> po
-> Process m a
dequeueWithOutputPriority q po =
do t <- liftEvent $ dequeueRequest q
requestResourceWithPriority (dequeueRes q) po
liftEvent $ dequeueExtract q t
tryDequeue :: (MonadComp m, DequeueStrategy m sm)
=> Queue m sm so a
-> Event m (Maybe a)
tryDequeue q =
do x <- tryRequestResourceWithinEvent (dequeueRes q)
if x
then do t <- dequeueRequest q
fmap Just $ dequeueExtract q t
else return Nothing
enqueue :: (MonadComp m,
EnqueueStrategy m sm,
DequeueStrategy m so)
=> Queue m sm so a
-> a
-> Event m ()
enqueue = enqueueStore
enqueueWithStoringPriority :: (MonadComp m,
PriorityQueueStrategy m sm pm,
DequeueStrategy m so)
=> Queue m sm so a
-> pm
-> a
-> Event m ()
enqueueWithStoringPriority = enqueueStoreWithPriority
enqueueStored :: MonadComp m => Queue m sm so a -> Signal m a
enqueueStored q = publishSignal (enqueueStoredSource q)
dequeueRequested :: MonadComp m => Queue m sm so a -> Signal m ()
dequeueRequested q = publishSignal (dequeueRequestedSource q)
dequeueExtracted :: MonadComp m => Queue m sm so a -> Signal m a
dequeueExtracted q = publishSignal (dequeueExtractedSource q)
enqueueStore :: (MonadComp m,
EnqueueStrategy m sm,
DequeueStrategy m so)
=> Queue m sm so a
-> a
-> Event m ()
enqueueStore q a =
Event $ \p ->
do let i = QueueItem { itemValue = a,
itemStoringTime = pointTime p }
invokeEvent p $
strategyEnqueue (queueStore q) i
c <- readProtoRef (queueCountRef q)
let c' = c + 1
t = pointTime p
c' `seq` writeProtoRef (queueCountRef q) c'
modifyProtoRef' (queueCountStatsRef q) (addTimingStats t c')
modifyProtoRef' (enqueueStoreCountRef q) (+ 1)
invokeEvent p $
releaseResourceWithinEvent (dequeueRes q)
invokeEvent p $
triggerSignal (enqueueStoredSource q) (itemValue i)
enqueueStoreWithPriority :: (MonadComp m,
PriorityQueueStrategy m sm pm,
DequeueStrategy m so)
=> Queue m sm so a
-> pm
-> a
-> Event m ()
enqueueStoreWithPriority q pm a =
Event $ \p ->
do let i = QueueItem { itemValue = a,
itemStoringTime = pointTime p }
invokeEvent p $
strategyEnqueueWithPriority (queueStore q) pm i
c <- readProtoRef (queueCountRef q)
let c' = c + 1
t = pointTime p
c' `seq` writeProtoRef (queueCountRef q) c'
modifyProtoRef' (queueCountStatsRef q) (addTimingStats t c')
modifyProtoRef' (enqueueStoreCountRef q) (+ 1)
invokeEvent p $
releaseResourceWithinEvent (dequeueRes q)
invokeEvent p $
triggerSignal (enqueueStoredSource q) (itemValue i)
dequeueRequest :: MonadComp m
=> Queue m sm so a
-> Event m Double
dequeueRequest q =
Event $ \p ->
do modifyProtoRef' (dequeueCountRef q) (+ 1)
invokeEvent p $
triggerSignal (dequeueRequestedSource q) ()
return $ pointTime p
dequeueExtract :: (MonadComp m, DequeueStrategy m sm)
=> Queue m sm so a
-> Double
-> Event m a
dequeueExtract q t' =
Event $ \p ->
do i <- invokeEvent p $
strategyDequeue (queueStore q)
c <- readProtoRef (queueCountRef q)
let c' = c 1
t = pointTime p
c' `seq` writeProtoRef (queueCountRef q) c'
modifyProtoRef' (queueCountStatsRef q) (addTimingStats t c')
modifyProtoRef' (dequeueExtractCountRef q) (+ 1)
invokeEvent p $
dequeueStat q t' i
invokeEvent p $
triggerSignal (dequeueExtractedSource q) (itemValue i)
return $ itemValue i
dequeueStat :: MonadComp m
=> Queue m sm so a
-> Double
-> QueueItem a
-> Event m ()
dequeueStat q t' i =
Event $ \p ->
do let t1 = itemStoringTime i
t = pointTime p
modifyProtoRef' (dequeueWaitTimeRef q) $
addSamplingStats (t t')
modifyProtoRef' (queueWaitTimeRef q) $
addSamplingStats (t t1)
queueChanged_ :: MonadComp m => Queue m sm so a -> Signal m ()
queueChanged_ q =
mapSignal (const ()) (enqueueStored q) <>
dequeueRequested q <>
mapSignal (const ()) (dequeueExtracted q)
queueSummary :: (MonadComp m, Show sm, Show so) => Queue m sm so a -> Int -> Event m ShowS
queueSummary q indent =
do let sm = enqueueStoringStrategy q
so = dequeueStrategy q
null <- queueNull q
count <- queueCount q
countStats <- queueCountStats q
enqueueStoreCount <- enqueueStoreCount q
dequeueCount <- dequeueCount q
dequeueExtractCount <- dequeueExtractCount q
enqueueStoreRate <- enqueueStoreRate q
dequeueRate <- dequeueRate q
dequeueExtractRate <- dequeueExtractRate q
waitTime <- queueWaitTime q
dequeueWaitTime <- dequeueWaitTime q
let tab = replicate indent ' '
return $
showString tab .
showString "the storing (memory) strategy = " .
shows sm .
showString "\n" .
showString tab .
showString "the dequeueing (output) strategy = " .
shows so .
showString "\n" .
showString tab .
showString "empty? = " .
shows null .
showString "\n" .
showString tab .
showString "the current size = " .
shows count .
showString "\n" .
showString tab .
showString "the size statistics = \n\n" .
timingStatsSummary countStats (2 + indent) .
showString "\n\n" .
showString tab .
showString "the enqueue store count (number of the input items that were stored) = " .
shows enqueueStoreCount .
showString "\n" .
showString tab .
showString "the dequeue count (number of requests for dequeueing an item) = " .
shows dequeueCount .
showString "\n" .
showString tab .
showString "the dequeue extract count (number of the output items that were dequeued) = " .
shows dequeueExtractCount .
showString "\n" .
showString tab .
showString "the enqueue store rate (how many input items were stored per time) = " .
shows enqueueStoreRate .
showString "\n" .
showString tab .
showString "the dequeue rate (how many requests for dequeueing per time) = " .
shows dequeueRate .
showString "\n" .
showString tab .
showString "the dequeue extract rate (how many output items were dequeued per time) = " .
shows dequeueExtractRate .
showString "\n" .
showString tab .
showString "the wait time (when was stored -> when was dequeued) = \n\n" .
samplingStatsSummary waitTime (2 + indent) .
showString "\n\n" .
showString tab .
showString "the dequeue wait time (when was requested for dequeueing -> when was dequeued) = \n\n" .
samplingStatsSummary dequeueWaitTime (2 + indent)