-- |
-- 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 { forall (m :: * -> *) s a b. Activity m s a b -> s
activityInitState :: s,
             -- ^ The initial state of the activity.
             forall (m :: * -> *) s a b. Activity m s a b -> Ref m s
activityStateRef :: Ref m s,
             -- ^ The current state of the activity.
             forall (m :: * -> *) s a b.
Activity m s a b -> s -> a -> Process m (s, b)
activityProcess :: s -> a -> Process m (s, b),
             -- ^ Provide @b@ by specified @a@.
             forall (m :: * -> *) s a b. Activity m s a b -> Bool
activityProcessPreemptible :: Bool,
             -- ^ Whether the process is preemptible.
             forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef :: Ref m Double,
             -- ^ The counted total time of utilising the activity.
             forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef :: Ref m Double,
             -- ^ The counted total time when the activity was idle.
             forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef :: Ref m Double,
             -- ^ The counted total time when the activity was preempted. 
             forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef :: Ref m (SamplingStats Double),
             -- ^ The statistics for the utilisation time.
             forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef :: Ref m (SamplingStats Double),
             -- ^ The statistics for the time when the activity was idle.
             forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityPreemptionTimeRef :: Ref m (SamplingStats Double),
             -- ^ The statistics for the time when the activity was preempted.
             forall (m :: * -> *) s a b. Activity m s a b -> SignalSource m a
activityUtilisingSource :: SignalSource m a,
             -- ^ A signal raised when starting to utilise the activity.
             forall (m :: * -> *) s a b.
Activity m s a b -> SignalSource m (a, b)
activityUtilisedSource :: SignalSource m (a, b),
             -- ^ A signal raised when the activity has been utilised.
             forall (m :: * -> *) s a b. Activity m s a b -> SignalSource m a
activityPreemptionBeginningSource :: SignalSource m a,
             -- ^ A signal raised when the utilisation was preempted.
             forall (m :: * -> *) s a b. 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 :: forall (m :: * -> *) a b.
MonadDES m =>
(a -> Process m b) -> Simulation m (Activity m () a b)
newActivity = 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 :: forall (m :: * -> *) s a b.
MonadDES m =>
(s -> a -> Process m (s, b))
-> s -> Simulation m (Activity m s a b)
newStateActivity = 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 :: forall (m :: * -> *) a b.
MonadDES m =>
Bool -> (a -> Process m b) -> Simulation m (Activity m () a b)
newPreemptibleActivity Bool
preemptible a -> Process m b
provide =
  forall a b c. (a -> b -> c) -> b -> a -> c
flip (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) () forall a b. (a -> b) -> a -> b
$ \()
s a
a ->
  do b
b <- a -> Process m b
provide a
a
     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 :: 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 s -> a -> Process m (s, b)
provide s
state =
  do Ref m s
r0 <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef s
state
     Ref m Double
r1 <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m Double
r2 <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m Double
r3 <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m (SamplingStats Double)
r4 <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a. SamplingData a => SamplingStats a
emptySamplingStats
     Ref m (SamplingStats Double)
r5 <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a. SamplingData a => SamplingStats a
emptySamplingStats
     Ref m (SamplingStats Double)
r6 <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a. SamplingData a => SamplingStats a
emptySamplingStats
     SignalSource m a
s1 <- forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m (a, b)
s2 <- forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m a
s3 <- forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     SignalSource m a
s4 <- forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     forall (m :: * -> *) a. Monad m => a -> m a
return 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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Net m a b
activityNet Activity m s a b
act = forall (m :: * -> *) a b.
(a -> Process m (b, Net m a b)) -> Net m a b
Net forall a b. (a -> b) -> a -> b
$ s -> Maybe Double -> a -> Process m (b, Net m a b)
loop (forall (m :: * -> *) s a b. Activity m s a b -> s
activityInitState Activity m s a b
act) 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 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
         forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
           do case Maybe Double
r of
                Maybe Double
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
                Just Double
t' ->
                  do forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act) (forall a. Num a => a -> a -> a
+ (Double
t0 forall a. Num a => a -> a -> a
- Double
t'))
                     forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef Activity m s a b
act) forall a b. (a -> b) -> a -> b
$
                       forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t0 forall a. Num a => a -> a -> a
- Double
t')
              forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (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 forall (m :: * -> *) s a b. Activity m s a b -> Bool
activityProcessPreemptible Activity m s a b
act
                        then 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) <- 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
                                forall (m :: * -> *) a. Monad m => a -> m a
return (s
s', b
b, Double
0)
         Double
t1 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
         forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
           do forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m s
activityStateRef Activity m s a b
act) forall a b. (a -> b) -> a -> b
$! s
s'
              forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act) (forall a. Num a => a -> a -> a
+ (Double
t1 forall a. Num a => a -> a -> a
- Double
t0 forall a. Num a => a -> a -> a
- Double
dt))
              forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef Activity m s a b
act) forall a b. (a -> b) -> a -> b
$
                forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t1 forall a. Num a => a -> a -> a
- Double
t0 forall a. Num a => a -> a -> a
- Double
dt)
              forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (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)
         forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a b.
(a -> Process m (b, Net m a b)) -> Net m a b
Net forall a b. (a -> b) -> a -> b
$ s -> Maybe Double -> a -> Process m (b, Net m a b)
loop s
s' (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 :: 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 =
  do ProcessId m
pid <- forall (m :: * -> *). MonadDES m => Process m (ProcessId m)
processId
     Double
t0  <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
     Ref m Double
rs  <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m Double
r0  <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
t0
     DisposableEvent m
h1  <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
            forall (m :: * -> *) a.
Signal m a -> (a -> Event m ()) -> Event m (DisposableEvent m)
handleSignal (forall (m :: * -> *). MonadDES m => ProcessId m -> Signal m ()
processPreemptionBeginning ProcessId m
pid) forall a b. (a -> b) -> a -> b
$ \() ->
            do Double
t0 <- 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. MonadRef m => Ref m a -> a -> Event m ()
writeRef Ref m Double
r0 Double
t0
               forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (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  <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
            forall (m :: * -> *) a.
Signal m a -> (a -> Event m ()) -> Event m (DisposableEvent m)
handleSignal (forall (m :: * -> *). MonadDES m => ProcessId m -> Signal m ()
processPreemptionEnding ProcessId m
pid) forall a b. (a -> b) -> a -> b
$ \() ->
            do Double
t0 <- forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef Ref m Double
r0
               Double
t1 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
               let dt :: Double
dt = Double
t1 forall a. Num a => a -> a -> a
- Double
t0
               forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef Ref m Double
rs (forall a. Num a => a -> a -> a
+ Double
dt)
               forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act) (forall a. Num a => a -> a -> a
+ Double
dt)
               forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityPreemptionTimeRef Activity m s a b
act) forall a b. (a -> b) -> a -> b
$
                 forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
dt
               forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (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) <- 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 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef Ref m Double
rs
              forall (m :: * -> *) a. Monad m => a -> m a
return (s
s', b
b, Double
dt)
         m2 :: Process m ()
m2 =
           forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
           do forall (m :: * -> *). DisposableEvent m -> Event m ()
disposeEvent DisposableEvent m
h1
              forall (m :: * -> *). DisposableEvent m -> Event m ()
disposeEvent DisposableEvent m
h2
     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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m s
activityState Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m s
activityStateChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m s
activityState Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityStateChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalUtilisationTime Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m Double
activityTotalUtilisationTimeChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalUtilisationTime Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityTotalUtilisationTimeChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalIdleTime Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m Double
activityTotalIdleTimeChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalIdleTime Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityTotalIdleTimeChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalPreemptionTime Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m Double
activityTotalPreemptionTimeChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityTotalPreemptionTime Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityTotalPreemptionTimeChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityUtilisationTime Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m (SamplingStats Double)
activityUtilisationTimeChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityUtilisationTime Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityUtilisationTimeChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityIdleTime Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m (SamplingStats Double)
activityIdleTimeChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityIdleTime Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityIdleTimeChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityPreemptionTime Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m (SamplingStats Double)
activityPreemptionTimeChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m (SamplingStats Double)
activityPreemptionTime Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityPreemptionTimeChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityUtilisationFactor Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
x1 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
     Double
x2 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
     Double
x3 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act)
     forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x1 forall a. Fractional a => a -> a -> a
/ (Double
x1 forall a. Num a => a -> a -> a
+ Double
x2 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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m Double
activityUtilisationFactorChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityUtilisationFactor Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityUtilisationFactorChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityIdleFactor Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
x1 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
     Double
x2 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
     Double
x3 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act)
     forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x2 forall a. Fractional a => a -> a -> a
/ (Double
x1 forall a. Num a => a -> a -> a
+ Double
x2 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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m Double
activityIdleFactorChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityIdleFactor Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityIdleFactorChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityPreemptionFactor Activity m s a b
act =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
x1 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
     Double
x2 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
     Double
x3 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act)
     forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x3 forall a. Fractional a => a -> a -> a
/ (Double
x1 forall a. Num a => a -> a -> a
+ Double
x2 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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m Double
activityPreemptionFactorChanged Activity m s a b
act =
  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 :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m Double
activityPreemptionFactor Activity m s a b
act) (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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityPreemptionFactorChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising = forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised = forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityPreemptionBeginning = forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityPreemptionEnding = forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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_ :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Signal m ()
activityChanged_ Activity m s a b
act =
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) s a b. Activity m s a b -> Signal m a
activityUtilising Activity m s a b
act) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (forall (m :: * -> *) s a b. Activity m s a b -> Signal m (a, b)
activityUtilised Activity m s a b
act) forall a. Semigroup a => a -> a -> a
<>
  forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) (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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Int -> Event m ShowS
activitySummary Activity m s a b
act Int
indent =
  forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
tx1 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act)
     Double
tx2 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act)
     Double
tx3 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 forall a. Fractional a => a -> a -> a
/ (Double
tx1 forall a. Num a => a -> a -> a
+ Double
tx2 forall a. Num a => a -> a -> a
+ Double
tx3)
         xf2 :: Double
xf2 = Double
tx2 forall a. Fractional a => a -> a -> a
/ (Double
tx1 forall a. Num a => a -> a -> a
+ Double
tx2 forall a. Num a => a -> a -> a
+ Double
tx3)
         xf3 :: Double
xf3 = Double
tx3 forall a. Fractional a => a -> a -> a
/ (Double
tx1 forall a. Num a => a -> a -> a
+ Double
tx2 forall a. Num a => a -> a -> a
+ Double
tx3)
     SamplingStats Double
xs1 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef Activity m s a b
act)
     SamplingStats Double
xs2 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef Activity m s a b
act)
     SamplingStats Double
xs3 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (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 = forall a. Int -> a -> [a]
replicate Int
indent Char
' '
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"total utilisation time = " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows Double
tx1 forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"total idle time = " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows Double
tx2 forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"total preemption time = " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows Double
tx3 forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"utilisation factor (from 0 to 1) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows Double
xf1 forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"idle factor (from 0 to 1) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows Double
xf2 forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"preemption factor (from 0 to 1) = " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows Double
xf3 forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"utilisation time (locked while awaiting the input):\n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
xs1 (Int
2 forall a. Num a => a -> a -> a
+ Int
indent) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"idle time:\n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
xs2 (Int
2 forall a. Num a => a -> a -> a
+ Int
indent) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"preemption time:\n\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
xs3 (Int
2 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 :: forall (m :: * -> *) s a b.
MonadDES m =>
Activity m s a b -> Event m ()
resetActivity Activity m s a b
act =
  do forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalUtilisationTimeRef Activity m s a b
act) Double
0
     forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalIdleTimeRef Activity m s a b
act) Double
0
     forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) s a b. Activity m s a b -> Ref m Double
activityTotalPreemptionTimeRef Activity m s a b
act) Double
0
     forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityUtilisationTimeRef Activity m s a b
act) forall a. Monoid a => a
mempty
     forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityIdleTimeRef Activity m s a b
act) forall a. Monoid a => a
mempty
     forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *) s a b.
Activity m s a b -> Ref m (SamplingStats Double)
activityPreemptionTimeRef Activity m s a b
act) forall a. Monoid a => a
mempty