module Simulation.Aivika.Queue.Infinite
(
FCFSQueue,
LCFSQueue,
SIROQueue,
PriorityQueue,
Queue,
newFCFSQueue,
newLCFSQueue,
newSIROQueue,
newPriorityQueue,
newQueue,
queueStoringStrategy,
queueOutputStrategy,
queueNull,
queueCount,
queueStoreCount,
queueOutputRequestCount,
queueOutputCount,
queueStoreRate,
queueOutputRequestRate,
queueOutputRate,
queueWaitTime,
queueOutputWaitTime,
dequeue,
dequeueWithOutputPriority,
tryDequeue,
enqueue,
enqueueWithStoringPriority,
queueSummary,
queueNullChanged,
queueNullChanged_,
queueCountChanged,
queueCountChanged_,
queueStoreCountChanged,
queueStoreCountChanged_,
queueOutputRequestCountChanged,
queueOutputRequestCountChanged_,
queueOutputCountChanged,
queueOutputCountChanged_,
queueWaitTimeChanged,
queueWaitTimeChanged_,
queueOutputWaitTimeChanged,
queueOutputWaitTimeChanged_,
enqueueStored,
dequeueRequested,
dequeueExtracted,
queueChanged_) where
import Data.IORef
import Data.Monoid
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Internal.Specs
import Simulation.Aivika.Internal.Simulation
import Simulation.Aivika.Internal.Dynamics
import Simulation.Aivika.Internal.Event
import Simulation.Aivika.Internal.Process
import Simulation.Aivika.Internal.Signal
import Simulation.Aivika.Signal
import Simulation.Aivika.Resource
import Simulation.Aivika.QueueStrategy
import Simulation.Aivika.Statistics
import Simulation.Aivika.Stream
import Simulation.Aivika.Processor
import qualified Simulation.Aivika.DoubleLinkedList as DLL
import qualified Simulation.Aivika.Vector as V
import qualified Simulation.Aivika.PriorityQueue as PQ
type FCFSQueue a =
Queue FCFS DLL.DoubleLinkedList FCFS DLL.DoubleLinkedList a
type LCFSQueue a =
Queue LCFS DLL.DoubleLinkedList FCFS DLL.DoubleLinkedList a
type SIROQueue a =
Queue SIRO V.Vector FCFS DLL.DoubleLinkedList a
type PriorityQueue a =
Queue StaticPriorities PQ.PriorityQueue FCFS DLL.DoubleLinkedList a
data Queue sm qm so qo a =
Queue { queueStoringStrategy :: sm,
queueOutputStrategy :: so,
queueStore :: qm (QueueItem a),
queueOutputRes :: Resource so qo,
queueCountRef :: IORef Int,
queueStoreCountRef :: IORef Int,
queueOutputRequestCountRef :: IORef Int,
queueOutputCountRef :: IORef Int,
queueWaitTimeRef :: IORef (SamplingStats Double),
queueOutputWaitTimeRef :: IORef (SamplingStats Double),
enqueueStoredSource :: SignalSource a,
dequeueRequestedSource :: SignalSource (),
dequeueExtractedSource :: SignalSource a }
data QueueItem a =
QueueItem { itemValue :: a,
itemStoringTime :: Double
}
newFCFSQueue :: Simulation (FCFSQueue a)
newFCFSQueue = newQueue FCFS FCFS
newLCFSQueue :: Simulation (LCFSQueue a)
newLCFSQueue = newQueue LCFS FCFS
newSIROQueue :: Simulation (SIROQueue a)
newSIROQueue = newQueue SIRO FCFS
newPriorityQueue :: Simulation (PriorityQueue a)
newPriorityQueue = newQueue StaticPriorities FCFS
newQueue :: (QueueStrategy sm qm,
QueueStrategy so qo) =>
sm
-> so
-> Simulation (Queue sm qm so qo a)
newQueue sm so =
do i <- liftIO $ newIORef 0
cm <- liftIO $ newIORef 0
cr <- liftIO $ newIORef 0
co <- liftIO $ newIORef 0
qm <- newStrategyQueue sm
ro <- newResourceWithMaxCount so 0 Nothing
w <- liftIO $ newIORef mempty
wo <- liftIO $ newIORef mempty
s3 <- newSignalSource
s4 <- newSignalSource
s5 <- newSignalSource
return Queue { queueStoringStrategy = sm,
queueOutputStrategy = so,
queueStore = qm,
queueOutputRes = ro,
queueCountRef = i,
queueStoreCountRef = cm,
queueOutputRequestCountRef = cr,
queueOutputCountRef = co,
queueWaitTimeRef = w,
queueOutputWaitTimeRef = wo,
enqueueStoredSource = s3,
dequeueRequestedSource = s4,
dequeueExtractedSource = s5 }
queueNull :: Queue sm qm so qo a -> Event Bool
queueNull q =
Event $ \p ->
do n <- readIORef (queueCountRef q)
return (n == 0)
queueNullChanged :: Queue sm qm so qo a -> Signal Bool
queueNullChanged q =
mapSignalM (const $ queueNull q) (queueNullChanged_ q)
queueNullChanged_ :: Queue sm qm so qo a -> Signal ()
queueNullChanged_ = queueCountChanged_
queueCount :: Queue sm qm so qo a -> Event Int
queueCount q =
Event $ \p -> readIORef (queueCountRef q)
queueCountChanged :: Queue sm qm so qo a -> Signal Int
queueCountChanged q =
mapSignalM (const $ queueCount q) (queueCountChanged_ q)
queueCountChanged_ :: Queue sm qm so qo a -> Signal ()
queueCountChanged_ q =
mapSignal (const ()) (enqueueStored q) <>
mapSignal (const ()) (dequeueExtracted q)
queueStoreCount :: Queue sm qm so qo a -> Event Int
queueStoreCount q =
Event $ \p -> readIORef (queueStoreCountRef q)
queueStoreCountChanged :: Queue sm qm so qo a -> Signal Int
queueStoreCountChanged q =
mapSignalM (const $ queueStoreCount q) (queueStoreCountChanged_ q)
queueStoreCountChanged_ :: Queue sm qm so qo a -> Signal ()
queueStoreCountChanged_ q =
mapSignal (const ()) (enqueueStored q)
queueOutputRequestCount :: Queue sm qm so qo a -> Event Int
queueOutputRequestCount q =
Event $ \p -> readIORef (queueOutputRequestCountRef q)
queueOutputRequestCountChanged :: Queue sm qm so qo a -> Signal Int
queueOutputRequestCountChanged q =
mapSignalM (const $ queueOutputRequestCount q) (queueOutputRequestCountChanged_ q)
queueOutputRequestCountChanged_ :: Queue sm qm so qo a -> Signal ()
queueOutputRequestCountChanged_ q =
mapSignal (const ()) (dequeueRequested q)
queueOutputCount :: Queue sm qm so qo a -> Event Int
queueOutputCount q =
Event $ \p -> readIORef (queueOutputCountRef q)
queueOutputCountChanged :: Queue sm qm so qo a -> Signal Int
queueOutputCountChanged q =
mapSignalM (const $ queueOutputCount q) (queueOutputCountChanged_ q)
queueOutputCountChanged_ :: Queue sm qm so qo a -> Signal ()
queueOutputCountChanged_ q =
mapSignal (const ()) (dequeueExtracted q)
queueStoreRate :: Queue sm qm so qo a -> Event Double
queueStoreRate q =
Event $ \p ->
do x <- readIORef (queueStoreCountRef q)
let t0 = spcStartTime $ pointSpecs p
t = pointTime p
return (fromIntegral x / (t t0))
queueOutputRequestRate :: Queue sm qm so qo a -> Event Double
queueOutputRequestRate q =
Event $ \p ->
do x <- readIORef (queueOutputRequestCountRef q)
let t0 = spcStartTime $ pointSpecs p
t = pointTime p
return (fromIntegral x / (t t0))
queueOutputRate :: Queue sm qm so qo a -> Event Double
queueOutputRate q =
Event $ \p ->
do x <- readIORef (queueOutputCountRef q)
let t0 = spcStartTime $ pointSpecs p
t = pointTime p
return (fromIntegral x / (t t0))
queueWaitTime :: Queue sm qm so qo a -> Event (SamplingStats Double)
queueWaitTime q =
Event $ \p -> readIORef (queueWaitTimeRef q)
queueWaitTimeChanged :: Queue sm qm so qo a -> Signal (SamplingStats Double)
queueWaitTimeChanged q =
mapSignalM (const $ queueWaitTime q) (queueWaitTimeChanged_ q)
queueWaitTimeChanged_ :: Queue sm qm so qo a -> Signal ()
queueWaitTimeChanged_ q =
mapSignal (const ()) (dequeueExtracted q)
queueOutputWaitTime :: Queue sm qm so qo a -> Event (SamplingStats Double)
queueOutputWaitTime q =
Event $ \p -> readIORef (queueOutputWaitTimeRef q)
queueOutputWaitTimeChanged :: Queue sm qm so qo a -> Signal (SamplingStats Double)
queueOutputWaitTimeChanged q =
mapSignalM (const $ queueOutputWaitTime q) (queueOutputWaitTimeChanged_ q)
queueOutputWaitTimeChanged_ :: Queue sm qm so qo a -> Signal ()
queueOutputWaitTimeChanged_ q =
mapSignal (const ()) (dequeueExtracted q)
dequeue :: (DequeueStrategy sm qm,
EnqueueStrategy so qo)
=> Queue sm qm so qo a
-> Process a
dequeue q =
do t <- liftEvent $ dequeueRequest q
requestResource (queueOutputRes q)
liftEvent $ dequeueExtract q t
dequeueWithOutputPriority :: (DequeueStrategy sm qm,
PriorityQueueStrategy so qo po)
=> Queue sm qm so qo a
-> po
-> Process a
dequeueWithOutputPriority q po =
do t <- liftEvent $ dequeueRequest q
requestResourceWithPriority (queueOutputRes q) po
liftEvent $ dequeueExtract q t
tryDequeue :: DequeueStrategy sm qm
=> Queue sm qm so qo a
-> Event (Maybe a)
tryDequeue q =
do x <- tryRequestResourceWithinEvent (queueOutputRes q)
if x
then do t <- dequeueRequest q
fmap Just $ dequeueExtract q t
else return Nothing
enqueue :: (EnqueueStrategy sm qm,
DequeueStrategy so qo)
=> Queue sm qm so qo a
-> a
-> Event ()
enqueue = enqueueStore
enqueueWithStoringPriority :: (PriorityQueueStrategy sm qm pm,
DequeueStrategy so qo)
=> Queue sm qm so qo a
-> pm
-> a
-> Event ()
enqueueWithStoringPriority = enqueueStoreWithPriority
enqueueStored :: Queue sm qm so qo a -> Signal a
enqueueStored q = publishSignal (enqueueStoredSource q)
dequeueRequested :: Queue sm qm so qo a -> Signal ()
dequeueRequested q = publishSignal (dequeueRequestedSource q)
dequeueExtracted :: Queue sm qm so qo a -> Signal a
dequeueExtracted q = publishSignal (dequeueExtractedSource q)
enqueueStore :: (EnqueueStrategy sm qm,
DequeueStrategy so qo)
=> Queue sm qm so qo a
-> a
-> Event ()
enqueueStore q a =
Event $ \p ->
do let i = QueueItem { itemValue = a,
itemStoringTime = pointTime p }
invokeEvent p $
strategyEnqueue (queueStoringStrategy q) (queueStore q) i
modifyIORef (queueCountRef q) (+ 1)
modifyIORef (queueStoreCountRef q) (+ 1)
invokeEvent p $
releaseResourceWithinEvent (queueOutputRes q)
invokeEvent p $
triggerSignal (enqueueStoredSource q) (itemValue i)
enqueueStoreWithPriority :: (PriorityQueueStrategy sm qm pm,
DequeueStrategy so qo)
=> Queue sm qm so qo a
-> pm
-> a
-> Event ()
enqueueStoreWithPriority q pm a =
Event $ \p ->
do let i = QueueItem { itemValue = a,
itemStoringTime = pointTime p }
invokeEvent p $
strategyEnqueueWithPriority (queueStoringStrategy q) (queueStore q) pm i
modifyIORef (queueCountRef q) (+ 1)
modifyIORef (queueStoreCountRef q) (+ 1)
invokeEvent p $
releaseResourceWithinEvent (queueOutputRes q)
invokeEvent p $
triggerSignal (enqueueStoredSource q) (itemValue i)
dequeueRequest :: Queue sm qm so qo a
-> Event Double
dequeueRequest q =
Event $ \p ->
do modifyIORef (queueOutputRequestCountRef q) (+ 1)
invokeEvent p $
triggerSignal (dequeueRequestedSource q) ()
return $ pointTime p
dequeueExtract :: DequeueStrategy sm qm
=> Queue sm qm so qo a
-> Double
-> Event a
dequeueExtract q t' =
Event $ \p ->
do i <- invokeEvent p $
strategyDequeue (queueStoringStrategy q) (queueStore q)
modifyIORef (queueCountRef q) (+ ( 1))
modifyIORef (queueOutputCountRef q) (+ 1)
invokeEvent p $
dequeueStat q t' i
invokeEvent p $
triggerSignal (dequeueExtractedSource q) (itemValue i)
return $ itemValue i
dequeueStat :: Queue sm qm so qo a
-> Double
-> QueueItem a
-> Event ()
dequeueStat q t' i =
Event $ \p ->
do let t1 = itemStoringTime i
t = pointTime p
modifyIORef (queueOutputWaitTimeRef q) $
addSamplingStats (t t')
modifyIORef (queueWaitTimeRef q) $
addSamplingStats (t t1)
queueChanged_ :: Queue sm qm so qo a -> Signal ()
queueChanged_ q =
mapSignal (const ()) (enqueueStored q) <>
dequeueRequested q <>
mapSignal (const ()) (dequeueExtracted q)
queueSummary :: (Show sm, Show so) => Queue sm qm so qo a -> Int -> Event ShowS
queueSummary q indent =
do let sm = queueStoringStrategy q
so = queueOutputStrategy q
null <- queueNull q
count <- queueCount q
storeCount <- queueStoreCount q
outputRequestCount <- queueOutputRequestCount q
outputCount <- queueOutputCount q
storeRate <- queueStoreRate q
outputRequestRate <- queueOutputRequestRate q
outputRate <- queueOutputRate q
waitTime <- queueWaitTime q
outputWaitTime <- queueOutputWaitTime q
let tab = replicate indent ' '
return $
showString tab .
showString "the storing (memory) strategy = " .
shows sm .
showString "\n" .
showString tab .
showString "the output (dequeueing) strategy = " .
shows so .
showString "\n" .
showString tab .
showString "empty? = " .
shows null .
showString "\n" .
showString tab .
showString "size = " .
shows count .
showString "\n" .
showString tab .
showString "the store count (number of the input items that were stored) = " .
shows storeCount .
showString "\n" .
showString tab .
showString "the output request count (number of requests for dequeueing an item) = " .
shows outputRequestCount .
showString "\n" .
showString tab .
showString "the output count (number of the output items that were dequeued) = " .
shows outputCount .
showString "\n" .
showString tab .
showString "the store rate (how many input items were stored per time) = " .
shows storeRate .
showString "\n" .
showString tab .
showString "the output request rate (how many requests for dequeueing per time) = " .
shows outputRequestRate .
showString "\n" .
showString tab .
showString "the output rate (how many output items were dequeued per time) = " .
shows outputRate .
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 output wait time (when was requested for dequeueing -> when was dequeued) = \n\n" .
samplingStatsSummary outputWaitTime (2 + indent)