module Simulation.Aivika.Arrival
(Arrival(..),
ArrivalTimer,
newArrivalTimer,
arrivalTimerProcessor,
arrivalProcessingTime) where
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Simulation
import Simulation.Aivika.Dynamics
import Simulation.Aivika.Event
import Simulation.Aivika.Processor
import Simulation.Aivika.Stream
import Simulation.Aivika.Statistics
import Simulation.Aivika.Ref
import Simulation.Aivika.Internal.Arrival
data ArrivalTimer =
ArrivalTimer { arrivalProcessingTimeRef :: Ref (SamplingStats Double) }
newArrivalTimer :: Simulation ArrivalTimer
newArrivalTimer =
do r <- newRef emptySamplingStats
return ArrivalTimer { arrivalProcessingTimeRef = r }
arrivalProcessingTime :: ArrivalTimer -> Event (SamplingStats Double)
arrivalProcessingTime = readRef . arrivalProcessingTimeRef
arrivalTimerProcessor :: ArrivalTimer -> Processor (Arrival a) (Arrival a)
arrivalTimerProcessor timer =
Processor $ \xs -> Cons $ loop xs where
loop xs =
do (a, xs) <- runStream xs
liftEvent $
do t <- liftDynamics time
modifyRef (arrivalProcessingTimeRef timer) $
addSamplingStats (t arrivalTime a)
return (a, Cons $ loop xs)