module Simulation.Aivika.Statistics.Accumulator
(
TimingStatsAccumulator,
newTimingStatsAccumulator,
timingStatsAccumulated) where
import Simulation.Aivika.Simulation
import Simulation.Aivika.Dynamics
import Simulation.Aivika.Event
import Simulation.Aivika.Ref
import Simulation.Aivika.Statistics
import Simulation.Aivika.Signal
newtype TimingStatsAccumulator a =
TimingStatsAccumulator { timingStatsAccumulatedRef :: Ref (TimingStats a) }
timingStatsAccumulated :: TimingStatsAccumulator a -> Event (TimingStats a)
timingStatsAccumulated = readRef . timingStatsAccumulatedRef
newTimingStatsAccumulator :: TimingData a => Signalable a -> Event (TimingStatsAccumulator 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 }