module Simulation.Aivika.Trans.Statistics.Accumulator
(
TimingStatsAccumulator,
newTimingStatsAccumulator,
timingStatsAccumulated) where
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Dynamics
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Ref
import Simulation.Aivika.Trans.Statistics
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.DES
newtype TimingStatsAccumulator m a =
TimingStatsAccumulator { timingStatsAccumulatedRef :: Ref m (TimingStats a) }
timingStatsAccumulated :: MonadDES m => TimingStatsAccumulator m a -> Event m (TimingStats a)
timingStatsAccumulated = readRef . timingStatsAccumulatedRef
newTimingStatsAccumulator :: (MonadDES m, TimingData a) => Signalable m a -> Event m (TimingStatsAccumulator m a)
newTimingStatsAccumulator x =
do t0 <- liftDynamics time
a0 <- readSignalable x
r <- liftSimulation $ newRef (returnTimingStats t0 a0)
handleSignal_ (signalableChanged x) $ \a ->
do t <- liftDynamics time
modifyRef r $ addTimingStats t a
return TimingStatsAccumulator { timingStatsAccumulatedRef = r }