-- |
-- Module     : Simulation.Aivika.Trans.Activity
-- 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
--
-- It models an activity that can be utilised. The activity is similar to a 'Server'
-- but destined for simulation within 'Net' computation.
module Simulation.Aivika.Trans.Activity
       (-- * Activity
        Activity,
        newActivity,
        newStateActivity,
        newPreemptibleActivity,
        newPreemptibleStateActivity,
        -- * Processing
        activityNet,
        -- * Activity Properties
        activityInitState,
        activityState,
        activityTotalUtilisationTime,
        activityTotalIdleTime,
        activityTotalPreemptionTime,
        activityUtilisationTime,
        activityIdleTime,
        activityPreemptionTime,
        activityUtilisationFactor,
        activityIdleFactor,
        activityPreemptionFactor,
        -- * Statistics Reset
        resetActivity,
        -- * Summary
        activitySummary,
        -- * Derived Signals for Properties
        activityStateChanged,
        activityStateChanged_,
        activityTotalUtilisationTimeChanged,
        activityTotalUtilisationTimeChanged_,
        activityTotalIdleTimeChanged,
        activityTotalIdleTimeChanged_,
        activityTotalPreemptionTimeChanged,
        activityTotalPreemptionTimeChanged_,
        activityUtilisationTimeChanged,
        activityUtilisationTimeChanged_,
        activityIdleTimeChanged,
        activityIdleTimeChanged_,
        activityPreemptionTimeChanged,
        activityPreemptionTimeChanged_,
        activityUtilisationFactorChanged,
        activityUtilisationFactorChanged_,
        activityIdleFactorChanged,
        activityIdleFactorChanged_,
        activityPreemptionFactorChanged,
        activityPreemptionFactorChanged_,
        -- * Basic Signals
        activityUtilising,
        activityUtilised,
        activityPreemptionBeginning,
        activityPreemptionEnding,
        -- * Overall Signal
        activityChanged_) where

import Data.Monoid

import Control.Monad
import Control.Monad.Trans
import Control.Arrow

import Simulation.Aivika.Trans.Ref.Base
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Parameter
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Dynamics
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Event
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.Cont
import Simulation.Aivika.Trans.Process
import Simulation.Aivika.Trans.Net
import Simulation.Aivika.Trans.Server
import Simulation.Aivika.Trans.Statistics

-- | Like 'Server' it models an activity that takes @a@ and provides @b@ having state @s@.
-- But unlike the former the activity is destined for simulation within 'Net' computation.
data Activity m s a b =
  Activity { Activity m s a b -> s
activityInitState :: s,
             -- ^ The initial state of the activity.
             Activity m s a b -> Ref m s
activityStateRef :: Ref m s,
             -- ^ The current state of the activity.
             Activity m s a b -> s -> a -> Process m (s, b)
activityProcess :: s -> a -> Process m (s, b),
             -- ^ Provide @b@ by specified @a@.
             Activity m s a b -> Bool
activityProcessPreemptible :: Bool,
             -- ^ Whether the process is preemptible.
             Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef :: Ref m Double,
             -- ^ The counted total time of utilising the activity.
             Activity m s a b -> Ref m Double
activityTotalIdleTimeRef :: Ref m Double,
             -- ^ The counted total time when the activity was idle.
             Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef :: Ref m Double,
             -- ^ The counted total time when the activity was preempted. 
             Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef :: Ref m (SamplingStats Double),
             -- ^ The statistics for the utilisation time.
             Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef :: Ref m (SamplingStats Double),
             -- ^ The statistics for the time when the activity was idle.
             Activity m s a b -> Ref m (SamplingStats Double)
activityPreemptionTimeRef :: Ref m (SamplingStats Double),
             -- ^ The statistics for the time when the activity was preempted.
             Activity m s a b -> SignalSource m a
activityUtilisingSource :: SignalSource m a,
             -- ^ A signal raised when starting to utilise the activity.
             Activity m s a b -> SignalSource m (a, b)
activityUtilisedSource :: SignalSource m (a, b),
             -- ^ A signal raised when the activity has been utilised.
             Activity m s a b -> SignalSource m a
activityPreemptionBeginningSource :: SignalSource m a,
             -- ^ A signal raised when the utilisation was preempted.
             Activity m s a b -> SignalSource m a
activityPreemptionEndingSource :: SignalSource m a
             -- ^ A signal raised when the utilisation was proceeded after it had been preempted earlier.
           }

-- | Create a new activity that can provide output @b@ by input @a@.
--
-- By default, it is assumed that the activity utilisation cannot be preempted,
-- because the handling of possible task preemption is rather costly
-- operation.
newActivity :: MonadDES m
               => (a -> Process m b)
               -- ^ provide an output by the specified input
               -> Simulation m (Activity m () a b)
{-# INLINABLE newActivity #-}
newActivity :: (a -> Process m b) -> Simulation m (Activity m () a b)
newActivity = Bool -> (a -> Process m b) -> Simulation m (Activity m () a b)
forall (m :: * -> *) a b.
MonadDES m =>
Bool -> (a -> Process m b) -> Simulation m (Activity m () a b)
newPreemptibleActivity Bool
False

-- | Create a new activity that can provide output @b@ by input @a@
-- starting from state @s@.
--
-- By default, it is assumed that the activity utilisation cannot be preempted,
-- because the handling of possible task preemption is rather costly
-- operation.
newStateActivity :: MonadDES m
                    => (s -> a -> Process m (s, b))
                    -- ^ provide a new state and output by the specified 
                    -- old state and input
                    -> s
                    -- ^ the initial state
                    -> Simulation m (Activity m s a b)
{-# INLINABLE newStateActivity #-}
newStateActivity :: (s -> a -> Process m (s, b))
-> s -> Simulation m (Activity m s a b)
newStateActivity = Bool
-> (s -> a -> Process m (s, b))
-> s
-> Simulation m (Activity m s a b)
forall (m :: * -> *) s a b.
MonadDES m =>
Bool
-> (s -> a -> Process m (s, b))
-> s
-> Simulation m (Activity m s a b)
newPreemptibleStateActivity Bool
False

-- | Create a new interruptible activity that can provide output @b@ by input @a@.
newPreemptibleActivity :: MonadDES m
                          => Bool
                          -- ^ whether the activity can be preempted
                          -> (a -> Process m b)
                          -- ^ provide an output by the specified input
                          -> Simulation m (Activity m () a b)
{-# INLINABLE newPreemptibleActivity #-}
newPreemptibleActivity :: Bool -> (a -> Process m b) -> Simulation m (Activity m () a b)
newPreemptibleActivity Bool
preemptible a -> Process m b
provide =
  ((() -> a -> Process m ((), b))
 -> () -> Simulation m (Activity m () a b))
-> ()
-> (() -> a -> Process m ((), b))
-> Simulation m (Activity m () a b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Bool
-> (() -> a -> Process m ((), b))
-> ()
-> Simulation m (Activity m () a b)
forall (m :: * -> *) s a b.
MonadDES m =>
Bool
-> (s -> a -> Process m (s, b))
-> s
-> Simulation m (Activity m s a b)
newPreemptibleStateActivity Bool
preemptible) () ((() -> a -> Process m ((), b))
 -> Simulation m (Activity m () a b))
-> (() -> a -> Process m ((), b))
-> Simulation m (Activity m () a b)
forall a b. (a -> b) -> a -> b
$ \()
s a
a ->
  do b
b <- a -> Process m b
provide a
a
     ((), b) -> Process m ((), b)
forall (m :: * -> *) a. Monad m => a -> m a
return (()
s, b
b)

-- | Create a new activity that can provide output @b@ by input @a@
-- starting from state @s@.
newPreemptibleStateActivity :: MonadDES m
                               => Bool
                               -- ^ whether the activity can be preempted
                               -> (s -> a -> Process m (s, b))
                               -- ^ provide a new state and output by the specified 
                               -- old state and input
                               -> s
                               -- ^ the initial state
                               -> Simulation m (Activity m s a b)
{-# INLINABLE newPreemptibleStateActivity #-}
newPreemptibleStateActivity :: Bool
-> (s -> a -> Process m (s, b))
-> s
-> Simulation m (Activity m s a b)
newPreemptibleStateActivity Bool
preemptible s -> a -> Process m (s, b)
provide s
state =
  do Ref m s
r0 <- s -> Simulation m (Ref m s)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef s
state
     Ref m Double
r1 <- Double -> Simulation m (Ref m Double)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m Double
r2 <- Double -> Simulation m (Ref m Double)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m Double
r3 <- Double -> Simulation m (Ref m Double)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m (SamplingStats Double)
r4 <- SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
     Ref m (SamplingStats Double)
r5 <- SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
     Ref m (SamplingStats Double)
r6 <- SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
     SignalSource m a
s1 <- Simulation m (SignalSource m a)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m (a, b)
s2 <- Simulation m (SignalSource m (a, b))
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m a
s3 <- Simulation m (SignalSource m a)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m a
s4 <- Simulation m (SignalSource m a)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Activity m s a b -> Simulation m (Activity m s a b)
forall (m :: * -> *) a. Monad m => a -> m a
return Activity :: forall (m :: * -> *) s a b.
s
-> Ref m s
-> (s -> a -> Process m (s, b))
-> Bool
-> Ref m Double
-> Ref m Double
-> Ref m Double
-> Ref m (SamplingStats Double)
-> Ref m (SamplingStats Double)
-> Ref m (SamplingStats Double)
-> SignalSource m a
-> SignalSource m (a, b)
-> SignalSource m a
-> SignalSource m a
-> Activity m s a b
Activity { activityInitState :: s
activityInitState = s
state,
                       activityStateRef :: Ref m s
activityStateRef = Ref m s
r0,
                       activityProcess :: s -> a -> Process m (s, b)
activityProcess = s -> a -> Process m (s, b)
provide,
                       activityProcessPreemptible :: Bool
activityProcessPreemptible = Bool
preemptible,
                       activityTotalUtilisationTimeRef :: Ref m Double
activityTotalUtilisationTimeRef = Ref m Double
r1,
                       activityTotalIdleTimeRef :: Ref m Double
activityTotalIdleTimeRef = Ref m Double
r2,
                       activityTotalPreemptionTimeRef :: Ref m Double
activityTotalPreemptionTimeRef = Ref m Double
r3,
                       activityUtilisationTimeRef :: Ref m (SamplingStats Double)
activityUtilisationTimeRef = Ref m (SamplingStats Double)
r4,
                       activityIdleTimeRef :: Ref m (SamplingStats Double)
activityIdleTimeRef = Ref m (SamplingStats Double)
r5,
                       activityPreemptionTimeRef :: Ref m (SamplingStats Double)
activityPreemptionTimeRef = Ref m (SamplingStats Double)
r6,
                       activityUtilisingSource :: SignalSource m a
activityUtilisingSource = SignalSource m a
s1,
                       activityUtilisedSource :: SignalSource m (a, b)
activityUtilisedSource = SignalSource m (a, b)
s2,
                       activityPreemptionBeginningSource :: SignalSource m a
activityPreemptionBeginningSource = SignalSource m a
s3,
                       activityPreemptionEndingSource :: SignalSource m a
activityPreemptionEndingSource = SignalSource m a
s4 }

-- | Return a network computation for the specified activity.
--
-- The computation updates the internal state of the activity. The usual case is when 
-- the computation is applied only once in a chain of data processing. Otherwise; 
-- every time the computation is used, the state of the activity changes. Sometimes 
-- it can be indeed useful if you want to aggregate the statistics for different 
-- activities simultaneously, but it would be more preferable to avoid this.
--
-- If you connect different activity computations returned by this function in a chain 
-- with help of '>>>' or other category combinator then this chain will act as one 
-- whole, where the first activity will take a new task only after the last activity 
-- finishes its current task and requests for the next one from the previous activity 
-- in the chain. This is not always that thing you might need.
activityNet :: MonadDES m => Activity m s a b -> Net m a b
{-# INLINABLE activityNet #-}
activityNet :: Activity m s a b -> Net m a b
activityNet Activity m s a b
act = (a -> Process m (b, Net m a b)) -> Net m a b
forall (m :: * -> *) a b.
(a -> Process m (b, Net m a b)) -> Net m a b
Net ((a -> Process m (b, Net m a b)) -> Net m a b)
-> (a -> Process m (b, Net m a b)) -> Net m a b
forall a b. (a -> b) -> a -> b
$ s -> Maybe Double -> a -> Process m (b, Net m a b)
loop (Activity m s a b -> s
forall (m :: * -> *) s a b. Activity m s a b -> s
activityInitState Activity m s a b
act) Maybe Double
forall a. Maybe a
Nothing
  where
    loop :: s -> Maybe Double -> a -> Process m (b, Net m a b)
loop s
s Maybe Double
r a
a =
      do Double
t0 <- Dynamics m Double -> Process m Double
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics Dynamics m Double
forall (m :: * -> *). Monad m => Dynamics m Double
time
         Event m () -> Process m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent (Event m () -> Process m ()) -> Event m () -> Process m ()
forall a b. (a -> b) -> a -> b
$
           do case Maybe Double
r of
                Maybe Double
Nothing -> () -> Event m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                Just Double
t' ->
                  do Ref m Double -> (Double -> Double) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act) (Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
t0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t'))
                     Ref m (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef Activity m s a b
act) ((SamplingStats Double -> SamplingStats Double) -> Event m ())
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall a b. (a -> b) -> a -> b
$
                       Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t')
              SignalSource m a -> a -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Activity m s a b -> SignalSource m a
forall (m :: * -> *) s a b. Activity m s a b -> SignalSource m a
activityUtilisingSource Activity m s a b
act) a
a
         -- utilise the activity
         (s
s', b
b, Double
dt) <- if Activity m s a b -> Bool
forall (m :: * -> *) s a b. Activity m s a b -> Bool
activityProcessPreemptible Activity m s a b
act
                        then Activity m s a b -> s -> a -> Process m (s, b, Double)
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> s -> a -> Process m (s, b, Double)
activityProcessPreempting Activity m s a b
act s
s a
a
                        else do (s
s', b
b) <- Activity m s a b -> s -> a -> Process m (s, b)
forall (m :: * -> *) s a b.
Activity m s a b -> s -> a -> Process m (s, b)
activityProcess Activity m s a b
act s
s a
a
                                (s, b, Double) -> Process m (s, b, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s', b
b, Double
0)
         Double
t1 <- Dynamics m Double -> Process m Double
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics Dynamics m Double
forall (m :: * -> *). Monad m => Dynamics m Double
time
         Event m () -> Process m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent (Event m () -> Process m ()) -> Event m () -> Process m ()
forall a b. (a -> b) -> a -> b
$
           do Ref m s -> s -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Activity m s a b -> Ref m s
forall (m :: * -> *) s a b. Activity m s a b -> Ref m s
activityStateRef Activity m s a b
act) (s -> Event m ()) -> s -> Event m ()
forall a b. (a -> b) -> a -> b
$! s
s'
              Ref m Double -> (Double -> Double) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act) (Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
dt))
              Ref m (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef Activity m s a b
act) ((SamplingStats Double -> SamplingStats Double) -> Event m ())
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall a b. (a -> b) -> a -> b
$
                Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
dt)
              SignalSource m (a, b) -> (a, b) -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Activity m s a b -> SignalSource m (a, b)
forall (m :: * -> *) s a b.
Activity m s a b -> SignalSource m (a, b)
activityUtilisedSource Activity m s a b
act) (a
a, b
b)
         (b, Net m a b) -> Process m (b, Net m a b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, (a -> Process m (b, Net m a b)) -> Net m a b
forall (m :: * -> *) a b.
(a -> Process m (b, Net m a b)) -> Net m a b
Net ((a -> Process m (b, Net m a b)) -> Net m a b)
-> (a -> Process m (b, Net m a b)) -> Net m a b
forall a b. (a -> b) -> a -> b
$ s -> Maybe Double -> a -> Process m (b, Net m a b)
loop s
s' (Double -> Maybe Double
forall a. a -> Maybe a
Just Double
t1))

-- | Process the input with ability to handle a possible preemption.
activityProcessPreempting :: MonadDES m => Activity m s a b -> s -> a -> Process m (s, b, Double)
{-# INLINABLE activityProcessPreempting #-}
activityProcessPreempting :: Activity m s a b -> s -> a -> Process m (s, b, Double)
activityProcessPreempting Activity m s a b
act s
s a
a =
  do ProcessId m
pid <- Process m (ProcessId m)
forall (m :: * -> *). MonadDES m => Process m (ProcessId m)
processId
     Double
t0  <- Dynamics m Double -> Process m Double
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics Dynamics m Double
forall (m :: * -> *). Monad m => Dynamics m Double
time
     Ref m Double
rs  <- Simulation m (Ref m Double) -> Process m (Ref m Double)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Double) -> Process m (Ref m Double))
-> Simulation m (Ref m Double) -> Process m (Ref m Double)
forall a b. (a -> b) -> a -> b
$ Double -> Simulation m (Ref m Double)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m Double
r0  <- Simulation m (Ref m Double) -> Process m (Ref m Double)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Double) -> Process m (Ref m Double))
-> Simulation m (Ref m Double) -> Process m (Ref m Double)
forall a b. (a -> b) -> a -> b
$ Double -> Simulation m (Ref m Double)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
t0
     DisposableEvent m
h1  <- Event m (DisposableEvent m) -> Process m (DisposableEvent m)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent (Event m (DisposableEvent m) -> Process m (DisposableEvent m))
-> Event m (DisposableEvent m) -> Process m (DisposableEvent m)
forall a b. (a -> b) -> a -> b
$
            Signal m () -> (() -> Event m ()) -> Event m (DisposableEvent m)
forall (m :: * -> *) a.
Signal m a -> (a -> Event m ()) -> Event m (DisposableEvent m)
handleSignal (ProcessId m -> Signal m ()
forall (m :: * -> *). MonadDES m => ProcessId m -> Signal m ()
processPreemptionBeginning ProcessId m
pid) ((() -> Event m ()) -> Event m (DisposableEvent m))
-> (() -> Event m ()) -> Event m (DisposableEvent m)
forall a b. (a -> b) -> a -> b
$ \() ->
            do Double
t0 <- Dynamics m Double -> Event m Double
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics Dynamics m Double
forall (m :: * -> *). Monad m => Dynamics m Double
time
               Ref m Double -> Double -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef Ref m Double
r0 Double
t0
               SignalSource m a -> a -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Activity m s a b -> SignalSource m a
forall (m :: * -> *) s a b. Activity m s a b -> SignalSource m a
activityPreemptionBeginningSource Activity m s a b
act) a
a
     DisposableEvent m
h2  <- Event m (DisposableEvent m) -> Process m (DisposableEvent m)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent (Event m (DisposableEvent m) -> Process m (DisposableEvent m))
-> Event m (DisposableEvent m) -> Process m (DisposableEvent m)
forall a b. (a -> b) -> a -> b
$
            Signal m () -> (() -> Event m ()) -> Event m (DisposableEvent m)
forall (m :: * -> *) a.
Signal m a -> (a -> Event m ()) -> Event m (DisposableEvent m)
handleSignal (ProcessId m -> Signal m ()
forall (m :: * -> *). MonadDES m => ProcessId m -> Signal m ()
processPreemptionEnding ProcessId m
pid) ((() -> Event m ()) -> Event m (DisposableEvent m))
-> (() -> Event m ()) -> Event m (DisposableEvent m)
forall a b. (a -> b) -> a -> b
$ \() ->
            do Double
t0 <- Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef Ref m Double
r0
               Double
t1 <- Dynamics m Double -> Event m Double
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics Dynamics m Double
forall (m :: * -> *). Monad m => Dynamics m Double
time
               let dt :: Double
dt = Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0
               Ref m Double -> (Double -> Double) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef Ref m Double
rs (Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
dt)
               Ref m Double -> (Double -> Double) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act) (Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
dt)
               Ref m (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityPreemptionTimeRef Activity m s a b
act) ((SamplingStats Double -> SamplingStats Double) -> Event m ())
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall a b. (a -> b) -> a -> b
$
                 Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
dt
               SignalSource m a -> a -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Activity m s a b -> SignalSource m a
forall (m :: * -> *) s a b. Activity m s a b -> SignalSource m a
activityPreemptionEndingSource Activity m s a b
act) a
a 
     let m1 :: Process m (s, b, Double)
m1 =
           do (s
s', b
b) <- Activity m s a b -> s -> a -> Process m (s, b)
forall (m :: * -> *) s a b.
Activity m s a b -> s -> a -> Process m (s, b)
activityProcess Activity m s a b
act s
s a
a
              Double
dt <- Event m Double -> Process m Double
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent (Event m Double -> Process m Double)
-> Event m Double -> Process m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef Ref m Double
rs
              (s, b, Double) -> Process m (s, b, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s', b
b, Double
dt)
         m2 :: Process m ()
m2 =
           Event m () -> Process m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent (Event m () -> Process m ()) -> Event m () -> Process m ()
forall a b. (a -> b) -> a -> b
$
           do DisposableEvent m -> Event m ()
forall (m :: * -> *). DisposableEvent m -> Event m ()
disposeEvent DisposableEvent m
h1
              DisposableEvent m -> Event m ()
forall (m :: * -> *). DisposableEvent m -> Event m ()
disposeEvent DisposableEvent m
h2
     Process m (s, b, Double)
-> Process m () -> Process m (s, b, Double)
forall (m :: * -> *) a b.
MonadDES m =>
Process m a -> Process m b -> Process m a
finallyProcess Process m (s, b, Double)
m1 Process m ()
m2

-- | Return the current state of the activity.
--
-- See also 'activityStateChanged' and 'activityStateChanged_'.
activityState :: MonadDES m => Activity m s a b -> Event m s
{-# INLINABLE activityState #-}
activityState :: Activity m s a b -> Event m s
activityState Activity m s a b
act =
  (Point m -> m s) -> Event m s
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m s) -> Event m s) -> (Point m -> m s) -> Event m s
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m s -> m s
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m s -> m s) -> Event m s -> m s
forall a b. (a -> b) -> a -> b
$ Ref m s -> Event m s
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m s
forall (m :: * -> *) s a b. Activity m s a b -> Ref m s
activityStateRef Activity m s a b
act)
  
-- | Signal when the 'activityState' property value has changed.
activityStateChanged :: MonadDES m => Activity m s a b -> Signal m s
{-# INLINABLE activityStateChanged #-}
activityStateChanged :: Activity m s a b -> Signal m s
activityStateChanged Activity m s a b
act =
  (() -> Event m s) -> Signal m () -> Signal m s
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m s -> () -> Event m s
forall a b. a -> b -> a
const (Event m s -> () -> Event m s) -> Event m s -> () -> Event m s
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m s
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m s
activityState Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityStateChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityState' property value has changed.
activityStateChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityStateChanged_ #-}
activityStateChanged_ :: Activity m s a b -> Signal m ()
activityStateChanged_ Activity m s a b
act =
  ((a, b) -> ()) -> Signal m (a, b) -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> (a, b) -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m (a, b)
forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act)

-- | Return the counted total time when the activity was utilised.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'activityTotalUtilisationTimeChanged' and 'activityTotalUtilisationTimeChanged_'.
activityTotalUtilisationTime :: MonadDES m => Activity m s a b -> Event m Double
{-# INLINABLE activityTotalUtilisationTime #-}
activityTotalUtilisationTime :: Activity m s a b -> Event m Double
activityTotalUtilisationTime Activity m s a b
act =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
  
-- | Signal when the 'activityTotalUtilisationTime' property value has changed.
activityTotalUtilisationTimeChanged :: MonadDES m => Activity m s a b -> Signal m Double
{-# INLINABLE activityTotalUtilisationTimeChanged #-}
activityTotalUtilisationTimeChanged :: Activity m s a b -> Signal m Double
activityTotalUtilisationTimeChanged Activity m s a b
act =
  (() -> Event m Double) -> Signal m () -> Signal m Double
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Double -> () -> Event m Double
forall a b. a -> b -> a
const (Event m Double -> () -> Event m Double)
-> Event m Double -> () -> Event m Double
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m Double
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalUtilisationTime Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityTotalUtilisationTimeChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityTotalUtilisationTime' property value has changed.
activityTotalUtilisationTimeChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityTotalUtilisationTimeChanged_ #-}
activityTotalUtilisationTimeChanged_ :: Activity m s a b -> Signal m ()
activityTotalUtilisationTimeChanged_ Activity m s a b
act =
  ((a, b) -> ()) -> Signal m (a, b) -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> (a, b) -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m (a, b)
forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act)

-- | Return the counted total time when the activity was idle.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'activityTotalIdleTimeChanged' and 'activityTotalIdleTimeChanged_'.
activityTotalIdleTime :: MonadDES m => Activity m s a b -> Event m Double
{-# INLINABLE activityTotalIdleTime #-}
activityTotalIdleTime :: Activity m s a b -> Event m Double
activityTotalIdleTime Activity m s a b
act =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
  
-- | Signal when the 'activityTotalIdleTime' property value has changed.
activityTotalIdleTimeChanged :: MonadDES m => Activity m s a b -> Signal m Double
{-# INLINABLE activityTotalIdleTimeChanged #-}
activityTotalIdleTimeChanged :: Activity m s a b -> Signal m Double
activityTotalIdleTimeChanged Activity m s a b
act =
  (() -> Event m Double) -> Signal m () -> Signal m Double
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Double -> () -> Event m Double
forall a b. a -> b -> a
const (Event m Double -> () -> Event m Double)
-> Event m Double -> () -> Event m Double
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m Double
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalIdleTime Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityTotalIdleTimeChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityTotalIdleTime' property value has changed.
activityTotalIdleTimeChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityTotalIdleTimeChanged_ #-}
activityTotalIdleTimeChanged_ :: Activity m s a b -> Signal m ()
activityTotalIdleTimeChanged_ Activity m s a b
act =
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act)

-- | Return the counted total time when the activity was preemted waiting for
-- the further proceeding.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'activityTotalPreemptionTimeChanged' and 'activityTotalPreemptionTimeChanged_'.
activityTotalPreemptionTime :: MonadDES m => Activity m s a b -> Event m Double
{-# INLINABLE activityTotalPreemptionTime #-}
activityTotalPreemptionTime :: Activity m s a b -> Event m Double
activityTotalPreemptionTime Activity m s a b
act =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act)
  
-- | Signal when the 'activityTotalPreemptionTime' property value has changed.
activityTotalPreemptionTimeChanged :: MonadDES m => Activity m s a b -> Signal m Double
{-# INLINABLE activityTotalPreemptionTimeChanged #-}
activityTotalPreemptionTimeChanged :: Activity m s a b -> Signal m Double
activityTotalPreemptionTimeChanged Activity m s a b
act =
  (() -> Event m Double) -> Signal m () -> Signal m Double
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Double -> () -> Event m Double
forall a b. a -> b -> a
const (Event m Double -> () -> Event m Double)
-> Event m Double -> () -> Event m Double
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m Double
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalPreemptionTime Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityTotalPreemptionTimeChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityTotalPreemptionTime' property value has changed.
activityTotalPreemptionTimeChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityTotalPreemptionTimeChanged_ #-}
activityTotalPreemptionTimeChanged_ :: Activity m s a b -> Signal m ()
activityTotalPreemptionTimeChanged_ Activity m s a b
act =
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityPreemptionEnding Activity m s a b
act)

-- | Return the statistics for the time when the activity was utilised.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'activityUtilisationTimeChanged' and 'activityUtilisationTimeChanged_'.
activityUtilisationTime :: MonadDES m => Activity m s a b -> Event m (SamplingStats Double)
{-# INLINABLE activityUtilisationTime #-}
activityUtilisationTime :: Activity m s a b -> Event m (SamplingStats Double)
activityUtilisationTime Activity m s a b
act =
  (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (SamplingStats Double))
 -> Event m (SamplingStats Double))
-> (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef Activity m s a b
act)
  
-- | Signal when the 'activityUtilisationTime' property value has changed.
activityUtilisationTimeChanged :: MonadDES m => Activity m s a b -> Signal m (SamplingStats Double)
{-# INLINABLE activityUtilisationTimeChanged #-}
activityUtilisationTimeChanged :: Activity m s a b -> Signal m (SamplingStats Double)
activityUtilisationTimeChanged Activity m s a b
act =
  (() -> Event m (SamplingStats Double))
-> Signal m () -> Signal m (SamplingStats Double)
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m (SamplingStats Double)
-> () -> Event m (SamplingStats Double)
forall a b. a -> b -> a
const (Event m (SamplingStats Double)
 -> () -> Event m (SamplingStats Double))
-> Event m (SamplingStats Double)
-> ()
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m (SamplingStats Double)
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityUtilisationTime Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityUtilisationTimeChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityUtilisationTime' property value has changed.
activityUtilisationTimeChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityUtilisationTimeChanged_ #-}
activityUtilisationTimeChanged_ :: Activity m s a b -> Signal m ()
activityUtilisationTimeChanged_ Activity m s a b
act =
  ((a, b) -> ()) -> Signal m (a, b) -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> (a, b) -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m (a, b)
forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act)

-- | Return the statistics for the time when the activity was idle.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'activityIdleTimeChanged' and 'activityIdleTimeChanged_'.
activityIdleTime :: MonadDES m => Activity m s a b -> Event m (SamplingStats Double)
{-# INLINABLE activityIdleTime #-}
activityIdleTime :: Activity m s a b -> Event m (SamplingStats Double)
activityIdleTime Activity m s a b
act =
  (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (SamplingStats Double))
 -> Event m (SamplingStats Double))
-> (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef Activity m s a b
act)
  
-- | Signal when the 'activityIdleTime' property value has changed.
activityIdleTimeChanged :: MonadDES m => Activity m s a b -> Signal m (SamplingStats Double)
{-# INLINABLE activityIdleTimeChanged #-}
activityIdleTimeChanged :: Activity m s a b -> Signal m (SamplingStats Double)
activityIdleTimeChanged Activity m s a b
act =
  (() -> Event m (SamplingStats Double))
-> Signal m () -> Signal m (SamplingStats Double)
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m (SamplingStats Double)
-> () -> Event m (SamplingStats Double)
forall a b. a -> b -> a
const (Event m (SamplingStats Double)
 -> () -> Event m (SamplingStats Double))
-> Event m (SamplingStats Double)
-> ()
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m (SamplingStats Double)
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityIdleTime Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityIdleTimeChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityIdleTime' property value has changed.
activityIdleTimeChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityIdleTimeChanged_ #-}
activityIdleTimeChanged_ :: Activity m s a b -> Signal m ()
activityIdleTimeChanged_ Activity m s a b
act =
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act)

-- | Return the statistics for the time when the activity was preempted
-- waiting for the further proceeding.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'activityPreemptionTimeChanged' and 'activityPreemptionTimeChanged_'.
activityPreemptionTime :: MonadDES m => Activity m s a b -> Event m (SamplingStats Double)
{-# INLINABLE activityPreemptionTime #-}
activityPreemptionTime :: Activity m s a b -> Event m (SamplingStats Double)
activityPreemptionTime Activity m s a b
act =
  (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (SamplingStats Double))
 -> Event m (SamplingStats Double))
-> (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityPreemptionTimeRef Activity m s a b
act)
  
-- | Signal when the 'activityPreemptionTime' property value has changed.
activityPreemptionTimeChanged :: MonadDES m => Activity m s a b -> Signal m (SamplingStats Double)
{-# INLINABLE activityPreemptionTimeChanged #-}
activityPreemptionTimeChanged :: Activity m s a b -> Signal m (SamplingStats Double)
activityPreemptionTimeChanged Activity m s a b
act =
  (() -> Event m (SamplingStats Double))
-> Signal m () -> Signal m (SamplingStats Double)
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m (SamplingStats Double)
-> () -> Event m (SamplingStats Double)
forall a b. a -> b -> a
const (Event m (SamplingStats Double)
 -> () -> Event m (SamplingStats Double))
-> Event m (SamplingStats Double)
-> ()
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m (SamplingStats Double)
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityPreemptionTime Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityPreemptionTimeChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityPreemptionTime' property value has changed.
activityPreemptionTimeChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityPreemptionTimeChanged_ #-}
activityPreemptionTimeChanged_ :: Activity m s a b -> Signal m ()
activityPreemptionTimeChanged_ Activity m s a b
act =
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityPreemptionEnding Activity m s a b
act)
  
-- | It returns the factor changing from 0 to 1, which estimates how often
-- the activity was utilised.
--
-- This factor is calculated as
--
-- @
--   totalUtilisationTime \/ (totalUtilisationTime + totalIdleTime + totalPreemptionTime)
-- @
--
-- As before in this module, the value returned changes discretely and
-- it is usually delayed relative to the current simulation time.
--
-- See also 'activityUtilisationFactorChanged' and 'activityUtilisationFactorChanged_'.
activityUtilisationFactor :: MonadDES m => Activity m s a b -> Event m Double
{-# INLINABLE activityUtilisationFactor #-}
activityUtilisationFactor :: Activity m s a b -> Event m Double
activityUtilisationFactor Activity m s a b
act =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
x1 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
     Double
x2 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
     Double
x3 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act)
     Double -> m Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x3))
  
-- | Signal when the 'activityUtilisationFactor' property value has changed.
activityUtilisationFactorChanged :: MonadDES m => Activity m s a b -> Signal m Double
{-# INLINABLE activityUtilisationFactorChanged #-}
activityUtilisationFactorChanged :: Activity m s a b -> Signal m Double
activityUtilisationFactorChanged Activity m s a b
act =
  (() -> Event m Double) -> Signal m () -> Signal m Double
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Double -> () -> Event m Double
forall a b. a -> b -> a
const (Event m Double -> () -> Event m Double)
-> Event m Double -> () -> Event m Double
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m Double
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityUtilisationFactor Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityUtilisationFactorChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityUtilisationFactor' property value has changed.
activityUtilisationFactorChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityUtilisationFactorChanged_ #-}
activityUtilisationFactorChanged_ :: Activity m s a b -> Signal m ()
activityUtilisationFactorChanged_ Activity m s a b
act =
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  ((a, b) -> ()) -> Signal m (a, b) -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> (a, b) -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m (a, b)
forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityPreemptionEnding Activity m s a b
act)
  
-- | It returns the factor changing from 0 to 1, which estimates how often
-- the activity was idle.
--
-- This factor is calculated as
--
-- @
--   totalIdleTime \/ (totalUtilisationTime + totalIdleTime + totalPreemptionTime)
-- @
--
-- As before in this module, the value returned changes discretely and
-- it is usually delayed relative to the current simulation time.
--
-- See also 'activityIdleFactorChanged' and 'activityIdleFactorChanged_'.
activityIdleFactor :: MonadDES m => Activity m s a b -> Event m Double
{-# INLINABLE activityIdleFactor #-}
activityIdleFactor :: Activity m s a b -> Event m Double
activityIdleFactor Activity m s a b
act =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
x1 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
     Double
x2 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
     Double
x3 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act)
     Double -> m Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x2 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x3))
  
-- | Signal when the 'activityIdleFactor' property value has changed.
activityIdleFactorChanged :: MonadDES m => Activity m s a b -> Signal m Double
{-# INLINABLE activityIdleFactorChanged #-}
activityIdleFactorChanged :: Activity m s a b -> Signal m Double
activityIdleFactorChanged Activity m s a b
act =
  (() -> Event m Double) -> Signal m () -> Signal m Double
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Double -> () -> Event m Double
forall a b. a -> b -> a
const (Event m Double -> () -> Event m Double)
-> Event m Double -> () -> Event m Double
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m Double
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityIdleFactor Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityIdleFactorChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityIdleFactor' property value has changed.
activityIdleFactorChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityIdleFactorChanged_ #-}
activityIdleFactorChanged_ :: Activity m s a b -> Signal m ()
activityIdleFactorChanged_ Activity m s a b
act =
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  ((a, b) -> ()) -> Signal m (a, b) -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> (a, b) -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m (a, b)
forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityPreemptionEnding Activity m s a b
act)

-- | It returns the factor changing from 0 to 1, which estimates how often
-- the activity was preempted waiting for the further proceeding.
--
-- This factor is calculated as
--
-- @
--   totalUtilisationTime \/ (totalUtilisationTime + totalIdleTime + totalPreemptionTime)
-- @
--
-- As before in this module, the value returned changes discretely and
-- it is usually delayed relative to the current simulation time.
--
-- See also 'activityPreemptionFactorChanged' and 'activityPreemptionFactorChanged_'.
activityPreemptionFactor :: MonadDES m => Activity m s a b -> Event m Double
{-# INLINABLE activityPreemptionFactor #-}
activityPreemptionFactor :: Activity m s a b -> Event m Double
activityPreemptionFactor Activity m s a b
act =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
x1 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
     Double
x2 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
     Double
x3 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act)
     Double -> m Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x3 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x3))
  
-- | Signal when the 'activityPreemptionFactor' property value has changed.
activityPreemptionFactorChanged :: MonadDES m => Activity m s a b -> Signal m Double
{-# INLINABLE activityPreemptionFactorChanged #-}
activityPreemptionFactorChanged :: Activity m s a b -> Signal m Double
activityPreemptionFactorChanged Activity m s a b
act =
  (() -> Event m Double) -> Signal m () -> Signal m Double
forall (m :: * -> *) a b.
MonadDES m =>
(a -> Event m b) -> Signal m a -> Signal m b
mapSignalM (Event m Double -> () -> Event m Double
forall a b. a -> b -> a
const (Event m Double -> () -> Event m Double)
-> Event m Double -> () -> Event m Double
forall a b. (a -> b) -> a -> b
$ Activity m s a b -> Event m Double
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityPreemptionFactor Activity m s a b
act) (Activity m s a b -> Signal m ()
forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityPreemptionFactorChanged_ Activity m s a b
act)
  
-- | Signal when the 'activityPreemptionFactor' property value has changed.
activityPreemptionFactorChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityPreemptionFactorChanged_ #-}
activityPreemptionFactorChanged_ :: Activity m s a b -> Signal m ()
activityPreemptionFactorChanged_ Activity m s a b
act =
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  ((a, b) -> ()) -> Signal m (a, b) -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> (a, b) -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m (a, b)
forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityPreemptionEnding Activity m s a b
act)
  
-- | Raised when starting to utilise the activity after a new input task is received.
activityUtilising :: Activity m s a b -> Signal m a
{-# INLINABLE activityUtilising #-}
activityUtilising :: Activity m s a b -> Signal m a
activityUtilising = SignalSource m a -> Signal m a
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m a -> Signal m a)
-> (Activity m s a b -> SignalSource m a)
-> Activity m s a b
-> Signal m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Activity m s a b -> SignalSource m a
forall (m :: * -> *) s a b. Activity m s a b -> SignalSource m a
activityUtilisingSource

-- | Raised when the activity has been utilised after the current task is processed.
activityUtilised :: Activity m s a b -> Signal m (a, b)
{-# INLINABLE activityUtilised #-}
activityUtilised :: Activity m s a b -> Signal m (a, b)
activityUtilised = SignalSource m (a, b) -> Signal m (a, b)
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m (a, b) -> Signal m (a, b))
-> (Activity m s a b -> SignalSource m (a, b))
-> Activity m s a b
-> Signal m (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Activity m s a b -> SignalSource m (a, b)
forall (m :: * -> *) s a b.
Activity m s a b -> SignalSource m (a, b)
activityUtilisedSource

-- | Raised when the activity utilisation was preempted.
activityPreemptionBeginning :: Activity m s a b -> Signal m a
{-# INLINABLE activityPreemptionBeginning #-}
activityPreemptionBeginning :: Activity m s a b -> Signal m a
activityPreemptionBeginning = SignalSource m a -> Signal m a
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m a -> Signal m a)
-> (Activity m s a b -> SignalSource m a)
-> Activity m s a b
-> Signal m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Activity m s a b -> SignalSource m a
forall (m :: * -> *) s a b. Activity m s a b -> SignalSource m a
activityPreemptionBeginningSource

-- | Raised when the activity utilisation was proceeded after it had been preempted earlier.
activityPreemptionEnding :: Activity m s a b -> Signal m a
{-# INLINABLE activityPreemptionEnding #-}
activityPreemptionEnding :: Activity m s a b -> Signal m a
activityPreemptionEnding = SignalSource m a -> Signal m a
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m a -> Signal m a)
-> (Activity m s a b -> SignalSource m a)
-> Activity m s a b
-> Signal m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Activity m s a b -> SignalSource m a
forall (m :: * -> *) s a b. Activity m s a b -> SignalSource m a
activityPreemptionEndingSource

-- | Signal whenever any property of the activity changes.
activityChanged_ :: MonadDES m => Activity m s a b -> Signal m ()
{-# INLINABLE activityChanged_ #-}
activityChanged_ :: Activity m s a b -> Signal m ()
activityChanged_ Activity m s a b
act =
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  ((a, b) -> ()) -> Signal m (a, b) -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> (a, b) -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m (a, b)
forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act) Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal m a -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Activity m s a b -> Signal m a
forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityPreemptionEnding Activity m s a b
act)

-- | Return the summary for the activity with desciption of its
-- properties using the specified indent.
activitySummary :: MonadDES m => Activity m s a b -> Int -> Event m ShowS
{-# INLINABLE activitySummary #-}
activitySummary :: Activity m s a b -> Int -> Event m ShowS
activitySummary Activity m s a b
act Int
indent =
  (Point m -> m ShowS) -> Event m ShowS
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ShowS) -> Event m ShowS)
-> (Point m -> m ShowS) -> Event m ShowS
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
tx1 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
     Double
tx2 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
     Double
tx3 <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act)
     let xf1 :: Double
xf1 = Double
tx1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
tx1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
tx2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
tx3)
         xf2 :: Double
xf2 = Double
tx2 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
tx1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
tx2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
tx3)
         xf3 :: Double
xf3 = Double
tx3 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
tx1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
tx2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
tx3)
     SamplingStats Double
xs1 <- Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef Activity m s a b
act)
     SamplingStats Double
xs2 <- Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef Activity m s a b
act)
     SamplingStats Double
xs3 <- Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityPreemptionTimeRef Activity m s a b
act)
     let tab :: [Char]
tab = Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate Int
indent Char
' '
     ShowS -> m ShowS
forall (m :: * -> *) a. Monad m => a -> m a
return (ShowS -> m ShowS) -> ShowS -> m ShowS
forall a b. (a -> b) -> a -> b
$
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"total utilisation time = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
tx1 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"total idle time = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
tx2 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"total preemption time = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
tx3 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"utilisation factor (from 0 to 1) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
xf1 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"idle factor (from 0 to 1) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
xf2 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"preemption factor (from 0 to 1) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
xf3 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"utilisation time (locked while awaiting the input):\n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       SamplingStats Double -> Int -> ShowS
forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
xs1 (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"idle time:\n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       SamplingStats Double -> Int -> ShowS
forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
xs2 (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"preemption time:\n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       SamplingStats Double -> Int -> ShowS
forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
xs3 (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent)

-- | Reset the statistics.
resetActivity :: MonadDES m => Activity m s a b -> Event m ()
{-# INLINABLE resetActivity #-}
resetActivity :: Activity m s a b -> Event m ()
resetActivity Activity m s a b
act =
  do Ref m Double -> Double -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act) Double
0
     Ref m Double -> Double -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act) Double
0
     Ref m Double -> Double -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Activity m s a b -> Ref m Double
forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act) Double
0
     Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef Activity m s a b
act) SamplingStats Double
forall a. Monoid a => a
mempty
     Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef Activity m s a b
act) SamplingStats Double
forall a. Monoid a => a
mempty
     Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Activity m s a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityPreemptionTimeRef Activity m s a b
act) SamplingStats Double
forall a. Monoid a => a
mempty