-- |
-- Module     : Simulation.Aivika.Trans.Arrival
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- This module defines the types and functions for working with the events
-- that can represent something that arrive from outside the model, or
-- represent other things which computation is delayed and hence is not synchronized.
--
-- Therefore, the additional information is provided about the time and delay of arrival.

module Simulation.Aivika.Trans.Arrival
       (Arrival(..),
        ArrivalTimer,
        newArrivalTimer,
        arrivalTimerProcessor,
        arrivalTimerSignal,
        arrivalTimerChannel,
        arrivalProcessingTime,
        arrivalProcessingTimeChanged,
        arrivalProcessingTimeChanged_,
        resetArrivalTimer) where

import Control.Monad
import Control.Monad.Trans

import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Dynamics
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Composite
import Simulation.Aivika.Trans.Processor
import Simulation.Aivika.Trans.Stream
import Simulation.Aivika.Trans.Statistics
import Simulation.Aivika.Trans.Ref
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.Channel
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Arrival (Arrival(..))

-- | Accumulates the statistics about that how long the arrived events are processed.
data ArrivalTimer m =
  ArrivalTimer { forall (m :: * -> *).
ArrivalTimer m -> Ref m (SamplingStats Double)
arrivalProcessingTimeRef :: Ref m (SamplingStats Double),
                 forall (m :: * -> *). ArrivalTimer m -> SignalSource m ()
arrivalProcessingTimeChangedSource :: SignalSource m () }

-- | Create a new timer that measures how long the arrived events are processed.
newArrivalTimer :: MonadDES m => Simulation m (ArrivalTimer m)
{-# INLINABLE newArrivalTimer #-}
newArrivalTimer :: forall (m :: * -> *). MonadDES m => Simulation m (ArrivalTimer m)
newArrivalTimer =
  do Ref m (SamplingStats Double)
r <- forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef forall a. SamplingData a => SamplingStats a
emptySamplingStats
     SignalSource m ()
s <- forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     forall (m :: * -> *) a. Monad m => a -> m a
return ArrivalTimer { arrivalProcessingTimeRef :: Ref m (SamplingStats Double)
arrivalProcessingTimeRef = Ref m (SamplingStats Double)
r,
                           arrivalProcessingTimeChangedSource :: SignalSource m ()
arrivalProcessingTimeChangedSource = SignalSource m ()
s }

-- | Return the statistics about that how long the arrived events were processed.
arrivalProcessingTime :: MonadDES m => ArrivalTimer m -> Event m (SamplingStats Double)
{-# INLINABLE arrivalProcessingTime #-}
arrivalProcessingTime :: forall (m :: * -> *).
MonadDES m =>
ArrivalTimer m -> Event m (SamplingStats Double)
arrivalProcessingTime = forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
ArrivalTimer m -> Ref m (SamplingStats Double)
arrivalProcessingTimeRef

-- | Return a signal raised when the the processing time statistics changes.
arrivalProcessingTimeChanged :: MonadDES m => ArrivalTimer m -> Signal m (SamplingStats Double)
{-# INLINABLE arrivalProcessingTimeChanged #-}
arrivalProcessingTimeChanged :: forall (m :: * -> *).
MonadDES m =>
ArrivalTimer m -> Signal m (SamplingStats Double)
arrivalProcessingTimeChanged ArrivalTimer m
timer =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadDES m =>
ArrivalTimer m -> Event m (SamplingStats Double)
arrivalProcessingTime ArrivalTimer m
timer) (forall (m :: * -> *). MonadDES m => ArrivalTimer m -> Signal m ()
arrivalProcessingTimeChanged_ ArrivalTimer m
timer)

-- | Return a signal raised when the the processing time statistics changes.
arrivalProcessingTimeChanged_ :: MonadDES m => ArrivalTimer m -> Signal m ()
{-# INLINABLE arrivalProcessingTimeChanged_ #-}
arrivalProcessingTimeChanged_ :: forall (m :: * -> *). MonadDES m => ArrivalTimer m -> Signal m ()
arrivalProcessingTimeChanged_ ArrivalTimer m
timer =
  forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (forall (m :: * -> *). ArrivalTimer m -> SignalSource m ()
arrivalProcessingTimeChangedSource ArrivalTimer m
timer)

-- | Return a processor that actually measures how much time has passed from
-- the time of arriving the events.
arrivalTimerProcessor :: MonadDES m => ArrivalTimer m -> Processor m (Arrival a) (Arrival a)
{-# INLINABLE arrivalTimerProcessor #-}
arrivalTimerProcessor :: forall (m :: * -> *) a.
MonadDES m =>
ArrivalTimer m -> Processor m (Arrival a) (Arrival a)
arrivalTimerProcessor ArrivalTimer m
timer =
  forall (m :: * -> *) a b.
(Stream m a -> Stream m b) -> Processor m a b
Processor forall a b. (a -> b) -> a -> b
$ \Stream m (Arrival a)
xs -> forall (m :: * -> *) a. Process m (a, Stream m a) -> Stream m a
Cons forall a b. (a -> b) -> a -> b
$ forall {a}.
Stream m (Arrival a) -> Process m (Arrival a, Stream m (Arrival a))
loop Stream m (Arrival a)
xs where
    loop :: Stream m (Arrival a) -> Process m (Arrival a, Stream m (Arrival a))
loop Stream m (Arrival a)
xs =
      do (Arrival a
a, Stream m (Arrival a)
xs) <- forall (m :: * -> *) a. Stream m a -> Process m (a, Stream m a)
runStream Stream m (Arrival a)
xs
         forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
           do Double
t <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
              forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *).
ArrivalTimer m -> Ref m (SamplingStats Double)
arrivalProcessingTimeRef ArrivalTimer m
timer) forall a b. (a -> b) -> a -> b
$
                forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t forall a. Num a => a -> a -> a
- forall a. Arrival a -> Double
arrivalTime Arrival a
a)
              forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *). ArrivalTimer m -> SignalSource m ()
arrivalProcessingTimeChangedSource ArrivalTimer m
timer) ()
         forall (m :: * -> *) a. Monad m => a -> m a
return (Arrival a
a, forall (m :: * -> *) a. Process m (a, Stream m a) -> Stream m a
Cons forall a b. (a -> b) -> a -> b
$ Stream m (Arrival a) -> Process m (Arrival a, Stream m (Arrival a))
loop Stream m (Arrival a)
xs)

-- | Return a signal that actually measures how much time has passed from
-- the time of arriving the events.
--
-- Note that the statistics is counted each time you subscribe to the output signal.
-- For example, if you subscribe twice then the statistics counting is duplicated.
-- Ideally, you should subscribe to the output signal only once.
arrivalTimerSignal :: MonadDES m => ArrivalTimer m -> Signal m (Arrival a) -> Signal m (Arrival a)
{-# INLINABLE arrivalTimerSignal #-}
arrivalTimerSignal :: forall (m :: * -> *) a.
MonadDES m =>
ArrivalTimer m -> Signal m (Arrival a) -> Signal m (Arrival a)
arrivalTimerSignal ArrivalTimer m
timer Signal m (Arrival a)
sa =
  Signal { handleSignal :: (Arrival a -> Event m ()) -> Event m (DisposableEvent m)
handleSignal = \Arrival a -> Event m ()
h ->
            forall (m :: * -> *) a.
Signal m a -> (a -> Event m ()) -> Event m (DisposableEvent m)
handleSignal Signal m (Arrival a)
sa forall a b. (a -> b) -> a -> b
$ \Arrival a
a ->
            do Double
t <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
               forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *).
ArrivalTimer m -> Ref m (SamplingStats Double)
arrivalProcessingTimeRef ArrivalTimer m
timer) forall a b. (a -> b) -> a -> b
$
                 forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t forall a. Num a => a -> a -> a
- forall a. Arrival a -> Double
arrivalTime Arrival a
a)
               Arrival a -> Event m ()
h Arrival a
a
         }

-- | Like 'arrivalTimerSignal' but measures how much time has passed from
-- the time of arriving the events in the channel.
arrivalTimerChannel :: MonadDES m => ArrivalTimer m -> Channel m (Arrival a) (Arrival a)
{-# INLINABLE arrivalTimerChannel #-}
arrivalTimerChannel :: forall (m :: * -> *) a.
MonadDES m =>
ArrivalTimer m -> Channel m (Arrival a) (Arrival a)
arrivalTimerChannel ArrivalTimer m
timer =
  forall (m :: * -> *) a b.
(Signal m a -> Composite m (Signal m b)) -> Channel m a b
Channel forall a b. (a -> b) -> a -> b
$ \Signal m (Arrival a)
sa ->
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
MonadDES m =>
ArrivalTimer m -> Signal m (Arrival a) -> Signal m (Arrival a)
arrivalTimerSignal ArrivalTimer m
timer Signal m (Arrival a)
sa

-- | Reset the statistics.
resetArrivalTimer :: MonadDES m => ArrivalTimer m -> Event m ()
{-# INLINABLE resetArrivalTimer #-}
resetArrivalTimer :: forall (m :: * -> *). MonadDES m => ArrivalTimer m -> Event m ()
resetArrivalTimer ArrivalTimer m
timer =
  do forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *).
ArrivalTimer m -> Ref m (SamplingStats Double)
arrivalProcessingTimeRef ArrivalTimer m
timer) forall a. SamplingData a => SamplingStats a
emptySamplingStats
     forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *). ArrivalTimer m -> SignalSource m ()
arrivalProcessingTimeChangedSource ArrivalTimer m
timer) ()