-- |
-- Module     : Simulation.Aivika.Trans.Operation
-- 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 defines a stateless activity, some simplification of 'Server' and 'Activity'.
module Simulation.Aivika.Trans.Operation
       (-- * Operation
        Operation,
        newOperation,
        newPreemptibleOperation,
        -- * Processing
        operationProcess,
        -- * Operation Properties
        operationTotalUtilisationTime,
        operationTotalPreemptionTime,
        operationUtilisationTime,
        operationPreemptionTime,
        operationUtilisationFactor,
        operationPreemptionFactor,
        -- * Statistics Reset
        resetOperation,
        -- * Summary
        operationSummary,
        -- * Derived Signals for Properties
        operationTotalUtilisationTimeChanged,
        operationTotalUtilisationTimeChanged_,
        operationTotalPreemptionTimeChanged,
        operationTotalPreemptionTimeChanged_,
        operationUtilisationTimeChanged,
        operationUtilisationTimeChanged_,
        operationPreemptionTimeChanged,
        operationPreemptionTimeChanged_,
        operationUtilisationFactorChanged,
        operationUtilisationFactorChanged_,
        operationPreemptionFactorChanged,
        operationPreemptionFactorChanged_,
        -- * Basic Signals
        operationUtilising,
        operationUtilised,
        operationPreemptionBeginning,
        operationPreemptionEnding,
        -- * Overall Signal
        operationChanged_) where

import Data.Monoid

import Control.Monad
import Control.Monad.Trans

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.Activity
import Simulation.Aivika.Trans.Server
import Simulation.Aivika.Trans.Statistics

-- | Like 'Server' it models an activity that takes @a@ and provides @b@.
-- But unlike the former this kind of activity has no state. Also it is destined
-- to be used within 'Process' computations.
data Operation m a b =
  Operation { forall (m :: * -> *) a b. Operation m a b -> a -> Process m b
operationInitProcess :: a -> Process m b,
              -- ^ Provide @b@ by specified @a@.
              forall (m :: * -> *) a b. Operation m a b -> Bool
operationProcessPreemptible :: Bool,
              -- ^ Whether the process is preemptible.
              forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationStartTimeRef :: Ref m Double,
              -- ^ The start time of creating the operation.
              forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationLastTimeRef :: Ref m Double,
              -- ^ The last time of utilising the operation activity.
              forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalUtilisationTimeRef :: Ref m Double,
              -- ^ The counted total time of utilising the activity.
              forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalPreemptionTimeRef :: Ref m Double,
              -- ^ The counted total time when the activity was preempted. 
              forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationUtilisationTimeRef :: Ref m (SamplingStats Double),
              -- ^ The statistics for the utilisation time.
              forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationPreemptionTimeRef :: Ref m (SamplingStats Double),
              -- ^ The statistics for the time when the activity was preempted.
              forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationUtilisingSource :: SignalSource m a,
              -- ^ A signal raised when starting to utilise the activity.
              forall (m :: * -> *) a b. Operation m a b -> SignalSource m (a, b)
operationUtilisedSource :: SignalSource m (a, b),
              -- ^ A signal raised when the activity has been utilised.
              forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationPreemptionBeginningSource :: SignalSource m a,
              -- ^ A signal raised when the utilisation was preempted.
              forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationPreemptionEndingSource :: SignalSource m a
              -- ^ A signal raised when the utilisation was proceeded after it had been preempted earlier.
           }

-- | Create a new operation 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.
newOperation :: MonadDES m
                => (a -> Process m b)
                -- ^ provide an output by the specified input
                -> Event m (Operation m a b)
{-# INLINABLE newOperation #-}
newOperation :: forall (m :: * -> *) a b.
MonadDES m =>
(a -> Process m b) -> Event m (Operation m a b)
newOperation = Bool -> (a -> Process m b) -> Event m (Operation m a b)
forall (m :: * -> *) a b.
MonadDES m =>
Bool -> (a -> Process m b) -> Event m (Operation m a b)
newPreemptibleOperation Bool
False

-- | Create a new operation that can provide output @b@ by input @a@.
newPreemptibleOperation :: MonadDES m
                           => Bool
                           -- ^ whether the activity can be preempted
                           -> (a -> Process m b)
                           -- ^ provide an output by the specified input
                           -> Event m (Operation m a b)
{-# INLINABLE newPreemptibleOperation #-}
newPreemptibleOperation :: forall (m :: * -> *) a b.
MonadDES m =>
Bool -> (a -> Process m b) -> Event m (Operation m a b)
newPreemptibleOperation Bool
preemptible a -> Process m b
provide =
  do Double
t0 <- Dynamics m Double -> Event m Double
forall a. Dynamics m a -> Event m a
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
r' <- Simulation m (Ref m Double) -> Event m (Ref m Double)
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Double) -> Event m (Ref m Double))
-> Simulation m (Ref m Double) -> Event m (Ref m Double)
forall a b. (a -> b) -> a -> b
$ Double -> Simulation m (Ref m Double)
forall a. a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
t0
     Ref m Double
r0 <- Simulation m (Ref m Double) -> Event m (Ref m Double)
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Double) -> Event m (Ref m Double))
-> Simulation m (Ref m Double) -> Event m (Ref m Double)
forall a b. (a -> b) -> a -> b
$ Double -> Simulation m (Ref m Double)
forall a. a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
t0
     Ref m Double
r1 <- Simulation m (Ref m Double) -> Event m (Ref m Double)
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Double) -> Event m (Ref m Double))
-> Simulation m (Ref m Double) -> Event m (Ref m Double)
forall a b. (a -> b) -> a -> b
$ Double -> Simulation m (Ref m Double)
forall a. a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m Double
r2 <- Simulation m (Ref m Double) -> Event m (Ref m Double)
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Double) -> Event m (Ref m Double))
-> Simulation m (Ref m Double) -> Event m (Ref m Double)
forall a b. (a -> b) -> a -> b
$ Double -> Simulation m (Ref m Double)
forall a. a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m (SamplingStats Double)
r3 <- Simulation m (Ref m (SamplingStats Double))
-> Event m (Ref m (SamplingStats Double))
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m (SamplingStats Double))
 -> Event m (Ref m (SamplingStats Double)))
-> Simulation m (Ref m (SamplingStats Double))
-> Event m (Ref m (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall a. a -> Simulation m (Ref m a)
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)
r4 <- Simulation m (Ref m (SamplingStats Double))
-> Event m (Ref m (SamplingStats Double))
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m (SamplingStats Double))
 -> Event m (Ref m (SamplingStats Double)))
-> Simulation m (Ref m (SamplingStats Double))
-> Event m (Ref m (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall a. a -> Simulation m (Ref m a)
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) -> Event m (SignalSource m a)
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation 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))
-> Event m (SignalSource m (a, b))
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation 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) -> Event m (SignalSource m a)
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation 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) -> Event m (SignalSource m a)
forall a. Simulation m a -> Event m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation Simulation m (SignalSource m a)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Operation m a b -> Event m (Operation m a b)
forall a. a -> Event m a
forall (m :: * -> *) a. Monad m => a -> m a
return Operation { operationInitProcess :: a -> Process m b
operationInitProcess = a -> Process m b
provide,
                        operationProcessPreemptible :: Bool
operationProcessPreemptible = Bool
preemptible,
                        operationStartTimeRef :: Ref m Double
operationStartTimeRef = Ref m Double
r',
                        operationLastTimeRef :: Ref m Double
operationLastTimeRef = Ref m Double
r0,
                        operationTotalUtilisationTimeRef :: Ref m Double
operationTotalUtilisationTimeRef = Ref m Double
r1,
                        operationTotalPreemptionTimeRef :: Ref m Double
operationTotalPreemptionTimeRef = Ref m Double
r2,
                        operationUtilisationTimeRef :: Ref m (SamplingStats Double)
operationUtilisationTimeRef = Ref m (SamplingStats Double)
r3,
                        operationPreemptionTimeRef :: Ref m (SamplingStats Double)
operationPreemptionTimeRef = Ref m (SamplingStats Double)
r4,
                        operationUtilisingSource :: SignalSource m a
operationUtilisingSource = SignalSource m a
s1,
                        operationUtilisedSource :: SignalSource m (a, b)
operationUtilisedSource = SignalSource m (a, b)
s2,
                        operationPreemptionBeginningSource :: SignalSource m a
operationPreemptionBeginningSource = SignalSource m a
s3,
                        operationPreemptionEndingSource :: SignalSource m a
operationPreemptionEndingSource = SignalSource m a
s4 }

-- | Return a computation for the specified operation. It updates internal counters.
--
-- The computation can be used only within one process at any time.
operationProcess :: MonadDES m => Operation m a b -> a -> Process m b
{-# INLINABLE operationProcess #-}
operationProcess :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> a -> Process m b
operationProcess Operation m a b
op a
a =
  do Double
t0 <- Dynamics m Double -> Process m Double
forall a. Dynamics m a -> Process m a
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 a. Event m a -> Process m a
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
$
       SignalSource m a -> a -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Operation m a b -> SignalSource m a
forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationUtilisingSource Operation m a b
op) a
a
     -- utilise the activity
     (b
b, Double
dt) <- if Operation m a b -> Bool
forall (m :: * -> *) a b. Operation m a b -> Bool
operationProcessPreemptible Operation m a b
op
                then Operation m a b -> a -> Process m (b, Double)
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> a -> Process m (b, Double)
operationProcessPreempting Operation m a b
op a
a
                else do b
b <- Operation m a b -> a -> Process m b
forall (m :: * -> *) a b. Operation m a b -> a -> Process m b
operationInitProcess Operation m a b
op a
a
                        (b, Double) -> Process m (b, Double)
forall a. a -> Process m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, Double
0)
     Double
t1 <- Dynamics m Double -> Process m Double
forall a. Dynamics m a -> Process m a
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 a. Event m a -> Process m a
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 Double -> (Double -> Double) -> Event m ()
forall a. Ref m a -> (a -> a) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalUtilisationTimeRef Operation m a b
op) (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 a. Ref m a -> (a -> a) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Operation m a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationUtilisationTimeRef Operation m a b
op) ((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)
          Ref m Double -> Double -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationLastTimeRef Operation m a b
op) Double
t1
          SignalSource m (a, b) -> (a, b) -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Operation m a b -> SignalSource m (a, b)
forall (m :: * -> *) a b. Operation m a b -> SignalSource m (a, b)
operationUtilisedSource Operation m a b
op) (a
a, b
b)
     b -> Process m b
forall a. a -> Process m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
b

-- | Process the input with ability to handle a possible preemption.
operationProcessPreempting :: MonadDES m => Operation m a b -> a -> Process m (b, Double)
{-# INLINABLE operationProcessPreempting #-}
operationProcessPreempting :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> a -> Process m (b, Double)
operationProcessPreempting Operation m a b
op 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 a. Dynamics m a -> Process m a
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 a. Simulation m a -> Process m a
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 a. a -> Simulation m (Ref m a)
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 a. Simulation m a -> Process m a
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 a. a -> Simulation m (Ref m a)
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 a. Event m a -> Process m a
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 a. Dynamics m a -> Event m a
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 a. Ref m a -> a -> 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 (Operation m a b -> SignalSource m a
forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationPreemptionBeginningSource Operation m a b
op) a
a
     DisposableEvent m
h2  <- Event m (DisposableEvent m) -> Process m (DisposableEvent m)
forall a. Event m a -> Process m a
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 a. Ref m a -> Event m a
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 a. Dynamics m a -> Event m a
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 a. Ref m a -> (a -> a) -> 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 a. Ref m a -> (a -> a) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalPreemptionTimeRef Operation m a b
op) (Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
dt)
               Ref m (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall a. Ref m a -> (a -> a) -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Operation m a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationPreemptionTimeRef Operation m a b
op) ((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
               Ref m Double -> Double -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationLastTimeRef Operation m a b
op) Double
t1
               SignalSource m a -> a -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Operation m a b -> SignalSource m a
forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationPreemptionEndingSource Operation m a b
op) a
a 
     let m1 :: Process m (b, Double)
m1 =
           do b
b <- Operation m a b -> a -> Process m b
forall (m :: * -> *) a b. Operation m a b -> a -> Process m b
operationInitProcess Operation m a b
op a
a
              Double
dt <- Event m Double -> Process m Double
forall a. Event m a -> Process m a
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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef Ref m Double
rs
              (b, Double) -> Process m (b, Double)
forall a. a -> Process m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, Double
dt)
         m2 :: Process m ()
m2 =
           Event m () -> Process m ()
forall a. Event m a -> Process m a
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 (b, Double) -> Process m () -> Process m (b, Double)
forall (m :: * -> *) a b.
MonadDES m =>
Process m a -> Process m b -> Process m a
finallyProcess Process m (b, Double)
m1 Process m ()
m2

-- | Return the counted total time when the operation activity was utilised.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'operationTotalUtilisationTimeChanged' and 'operationTotalUtilisationTimeChanged_'.
operationTotalUtilisationTime :: MonadDES m => Operation m a b -> Event m Double
{-# INLINABLE operationTotalUtilisationTime #-}
operationTotalUtilisationTime :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m Double
operationTotalUtilisationTime Operation m a b
op =
  (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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalUtilisationTimeRef Operation m a b
op)
  
-- | Signal when the 'operationTotalUtilisationTime' property value has changed.
operationTotalUtilisationTimeChanged :: MonadDES m => Operation m a b -> Signal m Double
{-# INLINABLE operationTotalUtilisationTimeChanged #-}
operationTotalUtilisationTimeChanged :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m Double
operationTotalUtilisationTimeChanged Operation m a b
op =
  (() -> 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
$ Operation m a b -> Event m Double
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m Double
operationTotalUtilisationTime Operation m a b
op) (Operation m a b -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationTotalUtilisationTimeChanged_ Operation m a b
op)
  
-- | Signal when the 'operationTotalUtilisationTime' property value has changed.
operationTotalUtilisationTimeChanged_ :: MonadDES m => Operation m a b -> Signal m ()
{-# INLINABLE operationTotalUtilisationTimeChanged_ #-}
operationTotalUtilisationTimeChanged_ :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationTotalUtilisationTimeChanged_ Operation m a b
op =
  ((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 ()) (Operation m a b -> Signal m (a, b)
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m (a, b)
operationUtilised Operation m a b
op)

-- | Return the counted total time when the operation 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 'operationTotalPreemptionTimeChanged' and 'operationTotalPreemptionTimeChanged_'.
operationTotalPreemptionTime :: MonadDES m => Operation m a b -> Event m Double
{-# INLINABLE operationTotalPreemptionTime #-}
operationTotalPreemptionTime :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m Double
operationTotalPreemptionTime Operation m a b
op =
  (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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalPreemptionTimeRef Operation m a b
op)
  
-- | Signal when the 'operationTotalPreemptionTime' property value has changed.
operationTotalPreemptionTimeChanged :: MonadDES m => Operation m a b -> Signal m Double
{-# INLINABLE operationTotalPreemptionTimeChanged #-}
operationTotalPreemptionTimeChanged :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m Double
operationTotalPreemptionTimeChanged Operation m a b
op =
  (() -> 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
$ Operation m a b -> Event m Double
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m Double
operationTotalPreemptionTime Operation m a b
op) (Operation m a b -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationTotalPreemptionTimeChanged_ Operation m a b
op)
  
-- | Signal when the 'operationTotalPreemptionTime' property value has changed.
operationTotalPreemptionTimeChanged_ :: MonadDES m => Operation m a b -> Signal m ()
{-# INLINABLE operationTotalPreemptionTimeChanged_ #-}
operationTotalPreemptionTimeChanged_ :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationTotalPreemptionTimeChanged_ Operation m a b
op =
  (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 ()) (Operation m a b -> Signal m a
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationPreemptionEnding Operation m a b
op)

-- | Return the statistics for the time when the operation activity was utilised.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'operationUtilisationTimeChanged' and 'operationUtilisationTimeChanged_'.
operationUtilisationTime :: MonadDES m => Operation m a b -> Event m (SamplingStats Double)
{-# INLINABLE operationUtilisationTime #-}
operationUtilisationTime :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m (SamplingStats Double)
operationUtilisationTime Operation m a b
op =
  (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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationUtilisationTimeRef Operation m a b
op)
  
-- | Signal when the 'operationUtilisationTime' property value has changed.
operationUtilisationTimeChanged :: MonadDES m => Operation m a b -> Signal m (SamplingStats Double)
{-# INLINABLE operationUtilisationTimeChanged #-}
operationUtilisationTimeChanged :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m (SamplingStats Double)
operationUtilisationTimeChanged Operation m a b
op =
  (() -> 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
$ Operation m a b -> Event m (SamplingStats Double)
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m (SamplingStats Double)
operationUtilisationTime Operation m a b
op) (Operation m a b -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationUtilisationTimeChanged_ Operation m a b
op)
  
-- | Signal when the 'operationUtilisationTime' property value has changed.
operationUtilisationTimeChanged_ :: MonadDES m => Operation m a b -> Signal m ()
{-# INLINABLE operationUtilisationTimeChanged_ #-}
operationUtilisationTimeChanged_ :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationUtilisationTimeChanged_ Operation m a b
op =
  ((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 ()) (Operation m a b -> Signal m (a, b)
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m (a, b)
operationUtilised Operation m a b
op)

-- | Return the statistics for the time when the operation 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 'operationPreemptionTimeChanged' and 'operationPreemptionTimeChanged_'.
operationPreemptionTime :: MonadDES m => Operation m a b -> Event m (SamplingStats Double)
{-# INLINABLE operationPreemptionTime #-}
operationPreemptionTime :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m (SamplingStats Double)
operationPreemptionTime Operation m a b
op =
  (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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationPreemptionTimeRef Operation m a b
op)
  
-- | Signal when the 'operationPreemptionTime' property value has changed.
operationPreemptionTimeChanged :: MonadDES m => Operation m a b -> Signal m (SamplingStats Double)
{-# INLINABLE operationPreemptionTimeChanged #-}
operationPreemptionTimeChanged :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m (SamplingStats Double)
operationPreemptionTimeChanged Operation m a b
op =
  (() -> 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
$ Operation m a b -> Event m (SamplingStats Double)
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m (SamplingStats Double)
operationPreemptionTime Operation m a b
op) (Operation m a b -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationPreemptionTimeChanged_ Operation m a b
op)
  
-- | Signal when the 'operationPreemptionTime' property value has changed.
operationPreemptionTimeChanged_ :: MonadDES m => Operation m a b -> Signal m ()
{-# INLINABLE operationPreemptionTimeChanged_ #-}
operationPreemptionTimeChanged_ :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationPreemptionTimeChanged_ Operation m a b
op =
  (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 ()) (Operation m a b -> Signal m a
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationPreemptionEnding Operation m a b
op)
  
-- | It returns the factor changing from 0 to 1, which estimates how often
-- the operation activity was utilised since the time of creating the operation.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'operationUtilisationFactorChanged' and 'operationUtilisationFactorChanged_'.
operationUtilisationFactor :: MonadDES m => Operation m a b -> Event m Double
{-# INLINABLE operationUtilisationFactor #-}
operationUtilisationFactor :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m Double
operationUtilisationFactor Operation m a b
op =
  (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
t0 <- 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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationStartTimeRef Operation m a b
op)
     Double
t1 <- 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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationLastTimeRef Operation m a b
op)
     Double
x  <- 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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalUtilisationTimeRef Operation m a b
op)
     Double -> m Double
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0))
  
-- | Signal when the 'operationUtilisationFactor' property value has changed.
operationUtilisationFactorChanged :: MonadDES m => Operation m a b -> Signal m Double
{-# INLINABLE operationUtilisationFactorChanged #-}
operationUtilisationFactorChanged :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m Double
operationUtilisationFactorChanged Operation m a b
op =
  (() -> 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
$ Operation m a b -> Event m Double
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m Double
operationUtilisationFactor Operation m a b
op) (Operation m a b -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationUtilisationFactorChanged_ Operation m a b
op)
  
-- | Signal when the 'operationUtilisationFactor' property value has changed.
operationUtilisationFactorChanged_ :: MonadDES m => Operation m a b -> Signal m ()
{-# INLINABLE operationUtilisationFactorChanged_ #-}
operationUtilisationFactorChanged_ :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationUtilisationFactorChanged_ Operation m a b
op =
  ((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 ()) (Operation m a b -> Signal m (a, b)
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m (a, b)
operationUtilised Operation m a b
op) 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 ()) (Operation m a b -> Signal m a
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationPreemptionEnding Operation m a b
op)
  
-- | It returns the factor changing from 0 to 1, which estimates how often
-- the operation activity was preempted waiting for the further proceeding
-- since the time of creating the operation.
--
-- The value returned changes discretely and it is usually delayed relative
-- to the current simulation time.
--
-- See also 'operationPreemptionFactorChanged' and 'operationPreemptionFactorChanged_'.
operationPreemptionFactor :: MonadDES m => Operation m a b -> Event m Double
{-# INLINABLE operationPreemptionFactor #-}
operationPreemptionFactor :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m Double
operationPreemptionFactor Operation m a b
op =
  (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
t0 <- 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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationStartTimeRef Operation m a b
op)
     Double
t1 <- 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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationLastTimeRef Operation m a b
op)
     Double
x  <- 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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalPreemptionTimeRef Operation m a b
op)
     Double -> m Double
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0))
  
-- | Signal when the 'operationPreemptionFactor' property value has changed.
operationPreemptionFactorChanged :: MonadDES m => Operation m a b -> Signal m Double
{-# INLINABLE operationPreemptionFactorChanged #-}
operationPreemptionFactorChanged :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m Double
operationPreemptionFactorChanged Operation m a b
op =
  (() -> 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
$ Operation m a b -> Event m Double
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m Double
operationPreemptionFactor Operation m a b
op) (Operation m a b -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationPreemptionFactorChanged_ Operation m a b
op)
  
-- | Signal when the 'operationPreemptionFactor' property value has changed.
operationPreemptionFactorChanged_ :: MonadDES m => Operation m a b -> Signal m ()
{-# INLINABLE operationPreemptionFactorChanged_ #-}
operationPreemptionFactorChanged_ :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationPreemptionFactorChanged_ Operation m a b
op =
  ((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 ()) (Operation m a b -> Signal m (a, b)
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m (a, b)
operationUtilised Operation m a b
op) 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 ()) (Operation m a b -> Signal m a
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationPreemptionEnding Operation m a b
op)
  
-- | Raised when starting to utilise the operation activity after a new input task is received.
operationUtilising :: MonadDES m => Operation m a b -> Signal m a
{-# INLINABLE operationUtilising #-}
operationUtilising :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationUtilising = SignalSource m a -> Signal m a
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m a -> Signal m a)
-> (Operation m a b -> SignalSource m a)
-> Operation m a b
-> Signal m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Operation m a b -> SignalSource m a
forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationUtilisingSource

-- | Raised when the operation activity has been utilised after the current task is processed.
operationUtilised :: MonadDES m => Operation m a b -> Signal m (a, b)
{-# INLINABLE operationUtilised #-}
operationUtilised :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m (a, b)
operationUtilised = 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))
-> (Operation m a b -> SignalSource m (a, b))
-> Operation m a b
-> Signal m (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Operation m a b -> SignalSource m (a, b)
forall (m :: * -> *) a b. Operation m a b -> SignalSource m (a, b)
operationUtilisedSource

-- | Raised when the operation activity utilisation was preempted.
operationPreemptionBeginning :: MonadDES m => Operation m a b -> Signal m a
{-# INLINABLE operationPreemptionBeginning #-}
operationPreemptionBeginning :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationPreemptionBeginning = SignalSource m a -> Signal m a
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m a -> Signal m a)
-> (Operation m a b -> SignalSource m a)
-> Operation m a b
-> Signal m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Operation m a b -> SignalSource m a
forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationPreemptionBeginningSource

-- | Raised when the operation activity utilisation was proceeded after it had been preempted earlier.
operationPreemptionEnding :: MonadDES m => Operation m a b -> Signal m a
{-# INLINABLE operationPreemptionEnding #-}
operationPreemptionEnding :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationPreemptionEnding = SignalSource m a -> Signal m a
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m a -> Signal m a)
-> (Operation m a b -> SignalSource m a)
-> Operation m a b
-> Signal m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Operation m a b -> SignalSource m a
forall (m :: * -> *) a b. Operation m a b -> SignalSource m a
operationPreemptionEndingSource

-- | Signal whenever any property of the operation changes.
operationChanged_ :: MonadDES m => Operation m a b -> Signal m ()
{-# INLINABLE operationChanged_ #-}
operationChanged_ :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m ()
operationChanged_ Operation m a b
op =
  (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 ()) (Operation m a b -> Signal m a
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationUtilising Operation m a b
op) 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 ()) (Operation m a b -> Signal m (a, b)
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m (a, b)
operationUtilised Operation m a b
op) 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 ()) (Operation m a b -> Signal m a
forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Signal m a
operationPreemptionEnding Operation m a b
op)

-- | Return the summary for the operation with desciption of its
-- properties using the specified indent.
operationSummary :: MonadDES m => Operation m a b -> Int -> Event m ShowS
{-# INLINABLE operationSummary #-}
operationSummary :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Int -> Event m ShowS
operationSummary Operation m a b
op 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
t0  <- 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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationStartTimeRef Operation m a b
op)
     Double
t1  <- 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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationLastTimeRef Operation m a b
op)
     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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalUtilisationTimeRef Operation m a b
op)
     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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalPreemptionTimeRef Operation m a b
op)
     let xf1 :: Double
xf1 = Double
tx1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)
         xf2 :: Double
xf2 = Double
tx2 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)
     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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationUtilisationTimeRef Operation m a b
op)
     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 a. Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Operation m a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationPreemptionTimeRef Operation m a b
op)
     let tab :: [Char]
tab = Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate Int
indent Char
' '
     ShowS -> m ShowS
forall a. a -> m a
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 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
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]
"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]
"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
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]
"utilisation 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
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]
"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
xs2 (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent)

-- | Reset the statistics.
resetOperation :: MonadDES m => Operation m a b -> Event m ()
{-# INLINABLE resetOperation #-}
resetOperation :: forall (m :: * -> *) a b.
MonadDES m =>
Operation m a b -> Event m ()
resetOperation Operation m a b
op =
  do Double
t0 <- Dynamics m Double -> Event m Double
forall a. Dynamics m a -> Event m a
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 a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationStartTimeRef Operation m a b
op) Double
t0
     Ref m Double -> Double -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationLastTimeRef Operation m a b
op) Double
t0
     Ref m Double -> Double -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalUtilisationTimeRef Operation m a b
op) Double
0
     Ref m Double -> Double -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Operation m a b -> Ref m Double
forall (m :: * -> *) a b. Operation m a b -> Ref m Double
operationTotalPreemptionTimeRef Operation m a b
op) Double
0
     Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Operation m a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationUtilisationTimeRef Operation m a b
op) SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
     Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall a. Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Operation m a b -> Ref m (SamplingStats Double)
forall (m :: * -> *) a b.
Operation m a b -> Ref m (SamplingStats Double)
operationPreemptionTimeRef Operation m a b
op) SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats