-- |
-- Module     : Simulation.Aivika.Trans.GPSS.Facility
-- Copyright  : Copyright (c) 2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- This module defines the GPSS Facility entity.
--
module Simulation.Aivika.Trans.GPSS.Facility
       (-- * Facility Type
        Facility,
        FacilityPreemptMode(..),
        FacilityPreemptTransfer,
        -- * Creating Facility
        newFacility,
        -- * Facility Properties
        facilityCount,
        facilityCountStats,
        facilityCaptureCount,
        facilityUtilisationCount,
        facilityUtilisationCountStats,
        facilityQueueCount,
        facilityQueueCountStats,
        facilityTotalWaitTime,
        facilityWaitTime,
        facilityTotalHoldingTime,
        facilityHoldingTime,
        facilityInterrupted,
        -- * Seizing-Releasing and Preempting-Returning Facility
        seizeFacility,
        releaseFacility,
        preemptFacility,
        returnFacility,
        -- * Statistics Reset
        resetFacility,
        -- * Signals
        facilityCountChanged,
        facilityCountChanged_,
        facilityCaptureCountChanged,
        facilityCaptureCountChanged_,
        facilityUtilisationCountChanged,
        facilityUtilisationCountChanged_,
        facilityQueueCountChanged,
        facilityQueueCountChanged_,
        facilityWaitTimeChanged,
        facilityWaitTimeChanged_,
        facilityHoldingTimeChanged,
        facilityHoldingTimeChanged_,
        facilityChanged_) where

import Data.Monoid
import Data.Maybe

import Control.Monad
import Control.Monad.Trans
import Control.Exception

import Simulation.Aivika.Trans
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Event
import Simulation.Aivika.Trans.Internal.Cont
import Simulation.Aivika.Trans.Internal.Process
import Simulation.Aivika.Trans.QueueStrategy
import Simulation.Aivika.Trans.Statistics
import Simulation.Aivika.Trans.Signal

import Simulation.Aivika.Trans.GPSS.Transact
import Simulation.Aivika.Trans.GPSS.TransactQueueStrategy

-- | Represents a GPSS Facility entity.
data Facility m a = 
  Facility { Facility m a -> Ref m Int
facilityCountRef :: Ref m Int,
             Facility m a -> Ref m (TimingStats Int)
facilityCountStatsRef :: Ref m (TimingStats Int),
             Facility m a -> SignalSource m Int
facilityCountSource :: SignalSource m Int,
             Facility m a -> Ref m Int
facilityCaptureCountRef :: Ref m Int,
             Facility m a -> SignalSource m Int
facilityCaptureCountSource :: SignalSource m Int,
             Facility m a -> Ref m Int
facilityUtilisationCountRef :: Ref m Int,
             Facility m a -> Ref m (TimingStats Int)
facilityUtilisationCountStatsRef :: Ref m (TimingStats Int),
             Facility m a -> SignalSource m Int
facilityUtilisationCountSource :: SignalSource m Int,
             Facility m a -> Ref m Int
facilityQueueCountRef :: Ref m Int,
             Facility m a -> Ref m (TimingStats Int)
facilityQueueCountStatsRef :: Ref m (TimingStats Int),
             Facility m a -> SignalSource m Int
facilityQueueCountSource :: SignalSource m Int,
             Facility m a -> Ref m Double
facilityTotalWaitTimeRef :: Ref m Double,
             Facility m a -> Ref m (SamplingStats Double)
facilityWaitTimeRef :: Ref m (SamplingStats Double),
             Facility m a -> SignalSource m ()
facilityWaitTimeSource :: SignalSource m (),
             Facility m a -> Ref m Double
facilityTotalHoldingTimeRef :: Ref m Double,
             Facility m a -> Ref m (SamplingStats Double)
facilityHoldingTimeRef :: Ref m (SamplingStats Double),
             Facility m a -> SignalSource m ()
facilityHoldingTimeSource :: SignalSource m (),
             Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef :: Ref m (Maybe (FacilityOwnerItem m a)),
             Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
facilityDelayChain :: StrategyQueue m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a),
             Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
facilityInterruptChain :: StrategyQueue m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a),
             Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
facilityPendingChain :: StrategyQueue m (TransactQueueStrategy FCFS) (FacilityPendingItem m a) }

-- | Identifies a transact item that owns the facility.
data FacilityOwnerItem m a =
  FacilityOwnerItem { FacilityOwnerItem m a -> Transact m a
ownerItemTransact :: Transact m a,
                      FacilityOwnerItem m a -> Double
ownerItemTime :: Double,
                      FacilityOwnerItem m a -> Bool
ownerItemPreempting :: Bool,
                      FacilityOwnerItem m a -> Bool
ownerItemInterrupting :: Bool,
                      FacilityOwnerItem m a -> Double
ownerItemAccHoldingTime :: Double }

-- | Idenitifies a transact item that was delayed.
data FacilityDelayedItem m a =
  FacilityDelayedItem { FacilityDelayedItem m a -> Transact m a
delayedItemTransact :: Transact m a,
                        FacilityDelayedItem m a -> Double
delayedItemTime :: Double,
                        FacilityDelayedItem m a -> Bool
delayedItemPreempting :: Bool,
                        FacilityDelayedItem m a -> Bool
delayedItemInterrupting :: Bool,
                        FacilityDelayedItem m a -> FrozenCont m ()
delayedItemCont :: FrozenCont m () }

-- | Idenitifies a transact item that was interrupted.
data FacilityInterruptedItem m a =
  FacilityInterruptedItem { FacilityInterruptedItem m a -> Transact m a
interruptedItemTransact :: Transact m a,
                            FacilityInterruptedItem m a -> Double
interruptedItemTime :: Double,
                            FacilityInterruptedItem m a -> Bool
interruptedItemPreempting :: Bool,
                            FacilityInterruptedItem m a -> Bool
interruptedItemInterrupting :: Bool,
                            FacilityInterruptedItem m a -> Maybe Double
interruptedItemRemainingTime :: Maybe Double,
                            FacilityInterruptedItem m a -> Maybe (FacilityPreemptTransfer m a)
interruptedItemTransfer :: Maybe (FacilityPreemptTransfer m a),
                            FacilityInterruptedItem m a -> Double
interruptedItemAccHoldingTime :: Double }

-- | Idenitifies a transact item which is pending.
data FacilityPendingItem m a =
  FacilityPendingItem { FacilityPendingItem m a -> Transact m a
pendingItemTransact :: Transact m a,
                        FacilityPendingItem m a -> Double
pendingItemTime :: Double,
                        FacilityPendingItem m a -> Bool
pendingItemPreempting :: Bool,
                        FacilityPendingItem m a -> Bool
pendingItemInterrupting :: Bool,
                        FacilityPendingItem m a -> FrozenCont m ()
pendingItemCont :: FrozenCont m () }

instance MonadDES m => Eq (Facility m a) where
  Facility m a
x == :: Facility m a -> Facility m a -> Bool
== Facility m a
y = Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCountRef Facility m a
x Ref m Int -> Ref m Int -> Bool
forall a. Eq a => a -> a -> Bool
== Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCountRef Facility m a
y  -- unique references

-- | The facility preemption mode.
data FacilityPreemptMode m a =
  FacilityPreemptMode { FacilityPreemptMode m a -> Bool
facilityPriorityMode :: Bool,
                        -- ^ the Priority mode; otherwise, the Interrupt mode
                        FacilityPreemptMode m a -> Maybe (FacilityPreemptTransfer m a)
facilityTransfer :: Maybe (FacilityPreemptTransfer m a),
                        -- ^ where to transfer the preempted transact,
                        -- passing in the remaining time from the process holding
                        -- computation such as the ADVANCE block
                        FacilityPreemptMode m a -> Bool
facilityRemoveMode :: Bool
                        -- ^ the Remove mode
                      }

-- | Proceed with the computation by the specified preempted transact
-- and remaining time from the process holding computation such as the ADVANCE block.
type FacilityPreemptTransfer m a = Transact m a -> Maybe Double -> Process m ()

-- | The default facility preemption mode.
defaultFacilityPreemptMode :: FacilityPreemptMode m a
defaultFacilityPreemptMode :: FacilityPreemptMode m a
defaultFacilityPreemptMode =
  FacilityPreemptMode :: forall (m :: * -> *) a.
Bool
-> Maybe (FacilityPreemptTransfer m a)
-> Bool
-> FacilityPreemptMode m a
FacilityPreemptMode { facilityPriorityMode :: Bool
facilityPriorityMode = Bool
False,
                        facilityTransfer :: Maybe (FacilityPreemptTransfer m a)
facilityTransfer = Maybe (FacilityPreemptTransfer m a)
forall a. Maybe a
Nothing,
                        facilityRemoveMode :: Bool
facilityRemoveMode = Bool
False
                      }

-- | Create a new facility.
newFacility :: MonadDES m => Event m (Facility m a)
{-# INLINABLE newFacility #-}
newFacility :: Event m (Facility m a)
newFacility =
  (Point m -> m (Facility m a)) -> Event m (Facility m a)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (Facility m a)) -> Event m (Facility m a))
-> (Point m -> m (Facility m a)) -> Event m (Facility m a)
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let r :: Run m
r = Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
         t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Ref m Int
countRef <- Run m -> Simulation m (Ref m Int) -> m (Ref m Int)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m Int) -> m (Ref m Int))
-> Simulation m (Ref m Int) -> m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
1
     Ref m (TimingStats Int)
countStatsRef <- Run m
-> Simulation m (Ref m (TimingStats Int))
-> m (Ref m (TimingStats Int))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m (TimingStats Int))
 -> m (Ref m (TimingStats Int)))
-> Simulation m (Ref m (TimingStats Int))
-> m (Ref m (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ TimingStats Int -> Simulation m (Ref m (TimingStats Int))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef (TimingStats Int -> Simulation m (Ref m (TimingStats Int)))
-> TimingStats Int -> Simulation m (Ref m (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
1
     SignalSource m Int
countSource <- Run m
-> Simulation m (SignalSource m Int) -> m (SignalSource m Int)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r Simulation m (SignalSource m Int)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Int
captureCountRef <- Run m -> Simulation m (Ref m Int) -> m (Ref m Int)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m Int) -> m (Ref m Int))
-> Simulation m (Ref m Int) -> m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     SignalSource m Int
captureCountSource <- Run m
-> Simulation m (SignalSource m Int) -> m (SignalSource m Int)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r Simulation m (SignalSource m Int)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Int
utilCountRef <- Run m -> Simulation m (Ref m Int) -> m (Ref m Int)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m Int) -> m (Ref m Int))
-> Simulation m (Ref m Int) -> m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m (TimingStats Int)
utilCountStatsRef <- Run m
-> Simulation m (Ref m (TimingStats Int))
-> m (Ref m (TimingStats Int))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m (TimingStats Int))
 -> m (Ref m (TimingStats Int)))
-> Simulation m (Ref m (TimingStats Int))
-> m (Ref m (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ TimingStats Int -> Simulation m (Ref m (TimingStats Int))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef (TimingStats Int -> Simulation m (Ref m (TimingStats Int)))
-> TimingStats Int -> Simulation m (Ref m (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
     SignalSource m Int
utilCountSource <- Run m
-> Simulation m (SignalSource m Int) -> m (SignalSource m Int)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r Simulation m (SignalSource m Int)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Int
queueCountRef <- Run m -> Simulation m (Ref m Int) -> m (Ref m Int)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m Int) -> m (Ref m Int))
-> Simulation m (Ref m Int) -> m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Int
0
     Ref m (TimingStats Int)
queueCountStatsRef <- Run m
-> Simulation m (Ref m (TimingStats Int))
-> m (Ref m (TimingStats Int))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m (TimingStats Int))
 -> m (Ref m (TimingStats Int)))
-> Simulation m (Ref m (TimingStats Int))
-> m (Ref m (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ TimingStats Int -> Simulation m (Ref m (TimingStats Int))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef (TimingStats Int -> Simulation m (Ref m (TimingStats Int)))
-> TimingStats Int -> Simulation m (Ref m (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
     SignalSource m Int
queueCountSource <- Run m
-> Simulation m (SignalSource m Int) -> m (SignalSource m Int)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r Simulation m (SignalSource m Int)
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Double
totalWaitTimeRef <- Run m -> Simulation m (Ref m Double) -> m (Ref m Double)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m Double) -> m (Ref m Double))
-> Simulation m (Ref m Double) -> m (Ref m Double)
forall a b. (a -> b) -> a -> b
$ Double -> Simulation m (Ref m Double)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m (SamplingStats Double)
waitTimeRef <- Run m
-> Simulation m (Ref m (SamplingStats Double))
-> m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m (SamplingStats Double))
 -> m (Ref m (SamplingStats Double)))
-> Simulation m (Ref m (SamplingStats Double))
-> m (Ref m (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
     SignalSource m ()
waitTimeSource <- Run m -> Simulation m (SignalSource m ()) -> m (SignalSource m ())
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r Simulation m (SignalSource m ())
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m Double
totalHoldingTimeRef <- Run m -> Simulation m (Ref m Double) -> m (Ref m Double)
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m Double) -> m (Ref m Double))
-> Simulation m (Ref m Double) -> m (Ref m Double)
forall a b. (a -> b) -> a -> b
$ Double -> Simulation m (Ref m Double)
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Double
0
     Ref m (SamplingStats Double)
holdingTimeRef <- Run m
-> Simulation m (Ref m (SamplingStats Double))
-> m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m (SamplingStats Double))
 -> m (Ref m (SamplingStats Double)))
-> Simulation m (Ref m (SamplingStats Double))
-> m (Ref m (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> Simulation m (Ref m (SamplingStats Double))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
     SignalSource m ()
holdingTimeSource <- Run m -> Simulation m (SignalSource m ()) -> m (SignalSource m ())
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r Simulation m (SignalSource m ())
forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     Ref m (Maybe (FacilityOwnerItem m a))
ownerRef <- Run m
-> Simulation m (Ref m (Maybe (FacilityOwnerItem m a)))
-> m (Ref m (Maybe (FacilityOwnerItem m a)))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation m (Ref m (Maybe (FacilityOwnerItem m a)))
 -> m (Ref m (Maybe (FacilityOwnerItem m a))))
-> Simulation m (Ref m (Maybe (FacilityOwnerItem m a)))
-> m (Ref m (Maybe (FacilityOwnerItem m a)))
forall a b. (a -> b) -> a -> b
$ Maybe (FacilityOwnerItem m a)
-> Simulation m (Ref m (Maybe (FacilityOwnerItem m a)))
forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Maybe (FacilityOwnerItem m a)
forall a. Maybe a
Nothing
     StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
delayChain <- Run m
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a))
-> m (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation
   m
   (StrategyQueue
      m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a))
 -> m (StrategyQueue
         m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)))
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a))
-> m (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a))
forall a b. (a -> b) -> a -> b
$ TransactQueueStrategy FCFS
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a))
forall (m :: * -> *) s a.
QueueStrategy m s =>
s -> Simulation m (StrategyQueue m s a)
newStrategyQueue (FCFS -> TransactQueueStrategy FCFS
forall s. s -> TransactQueueStrategy s
TransactQueueStrategy FCFS
FCFS)
     StrategyQueue
  m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
interruptChain <- Run m
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a))
-> m (StrategyQueue
        m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation
   m
   (StrategyQueue
      m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a))
 -> m (StrategyQueue
         m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)))
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a))
-> m (StrategyQueue
        m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a))
forall a b. (a -> b) -> a -> b
$ TransactQueueStrategy LCFS
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a))
forall (m :: * -> *) s a.
QueueStrategy m s =>
s -> Simulation m (StrategyQueue m s a)
newStrategyQueue (LCFS -> TransactQueueStrategy LCFS
forall s. s -> TransactQueueStrategy s
TransactQueueStrategy LCFS
LCFS)
     StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
pendingChain <- Run m
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityPendingItem m a))
-> m (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityPendingItem m a))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Simulation
   m
   (StrategyQueue
      m (TransactQueueStrategy FCFS) (FacilityPendingItem m a))
 -> m (StrategyQueue
         m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)))
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityPendingItem m a))
-> m (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityPendingItem m a))
forall a b. (a -> b) -> a -> b
$ TransactQueueStrategy FCFS
-> Simulation
     m
     (StrategyQueue
        m (TransactQueueStrategy FCFS) (FacilityPendingItem m a))
forall (m :: * -> *) s a.
QueueStrategy m s =>
s -> Simulation m (StrategyQueue m s a)
newStrategyQueue (FCFS -> TransactQueueStrategy FCFS
forall s. s -> TransactQueueStrategy s
TransactQueueStrategy FCFS
FCFS)
     Facility m a -> m (Facility m a)
forall (m :: * -> *) a. Monad m => a -> m a
return Facility :: forall (m :: * -> *) a.
Ref m Int
-> Ref m (TimingStats Int)
-> SignalSource m Int
-> Ref m Int
-> SignalSource m Int
-> Ref m Int
-> Ref m (TimingStats Int)
-> SignalSource m Int
-> Ref m Int
-> Ref m (TimingStats Int)
-> SignalSource m Int
-> Ref m Double
-> Ref m (SamplingStats Double)
-> SignalSource m ()
-> Ref m Double
-> Ref m (SamplingStats Double)
-> SignalSource m ()
-> Ref m (Maybe (FacilityOwnerItem m a))
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
-> Facility m a
Facility { facilityCountRef :: Ref m Int
facilityCountRef = Ref m Int
countRef,
                       facilityCountStatsRef :: Ref m (TimingStats Int)
facilityCountStatsRef = Ref m (TimingStats Int)
countStatsRef,
                       facilityCountSource :: SignalSource m Int
facilityCountSource = SignalSource m Int
countSource,
                       facilityCaptureCountRef :: Ref m Int
facilityCaptureCountRef = Ref m Int
captureCountRef,
                       facilityCaptureCountSource :: SignalSource m Int
facilityCaptureCountSource = SignalSource m Int
captureCountSource,
                       facilityUtilisationCountRef :: Ref m Int
facilityUtilisationCountRef = Ref m Int
utilCountRef,
                       facilityUtilisationCountStatsRef :: Ref m (TimingStats Int)
facilityUtilisationCountStatsRef = Ref m (TimingStats Int)
utilCountStatsRef,
                       facilityUtilisationCountSource :: SignalSource m Int
facilityUtilisationCountSource = SignalSource m Int
utilCountSource,
                       facilityQueueCountRef :: Ref m Int
facilityQueueCountRef = Ref m Int
queueCountRef,
                       facilityQueueCountStatsRef :: Ref m (TimingStats Int)
facilityQueueCountStatsRef = Ref m (TimingStats Int)
queueCountStatsRef,
                       facilityQueueCountSource :: SignalSource m Int
facilityQueueCountSource = SignalSource m Int
queueCountSource,
                       facilityTotalWaitTimeRef :: Ref m Double
facilityTotalWaitTimeRef = Ref m Double
totalWaitTimeRef,
                       facilityWaitTimeRef :: Ref m (SamplingStats Double)
facilityWaitTimeRef = Ref m (SamplingStats Double)
waitTimeRef,
                       facilityWaitTimeSource :: SignalSource m ()
facilityWaitTimeSource = SignalSource m ()
waitTimeSource,
                       facilityTotalHoldingTimeRef :: Ref m Double
facilityTotalHoldingTimeRef = Ref m Double
totalHoldingTimeRef,
                       facilityHoldingTimeRef :: Ref m (SamplingStats Double)
facilityHoldingTimeRef = Ref m (SamplingStats Double)
holdingTimeRef,
                       facilityHoldingTimeSource :: SignalSource m ()
facilityHoldingTimeSource = SignalSource m ()
holdingTimeSource,
                       facilityOwnerRef :: Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef = Ref m (Maybe (FacilityOwnerItem m a))
ownerRef,
                       facilityDelayChain :: StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
facilityDelayChain = StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
delayChain,
                       facilityInterruptChain :: StrategyQueue
  m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
facilityInterruptChain = StrategyQueue
  m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
interruptChain,
                       facilityPendingChain :: StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
facilityPendingChain = StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
pendingChain }

-- | Return the current available count of the facility.
facilityCount :: MonadDES m => Facility m a -> Event m Int
{-# INLINABLE facilityCount #-}
facilityCount :: Facility m a -> Event m Int
facilityCount Facility m a
r =
  (Point m -> m Int) -> Event m Int
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Int) -> Event m Int)
-> (Point m -> m Int) -> Event m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCountRef Facility m a
r)

-- | Return the statistics for the available count of the facility.
facilityCountStats :: MonadDES m => Facility m a -> Event m (TimingStats Int)
{-# INLINABLE facilityCountStats #-}
facilityCountStats :: Facility m a -> Event m (TimingStats Int)
facilityCountStats Facility m a
r =
  (Point m -> m (TimingStats Int)) -> Event m (TimingStats Int)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (TimingStats Int)) -> Event m (TimingStats Int))
-> (Point m -> m (TimingStats Int)) -> Event m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m (TimingStats Int) -> m (TimingStats Int)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (TimingStats Int) -> m (TimingStats Int))
-> Event m (TimingStats Int) -> m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ Ref m (TimingStats Int) -> Event m (TimingStats Int)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityCountStatsRef Facility m a
r)

-- | Signal triggered when the 'facilityCount' property changes.
facilityCountChanged :: MonadDES m => Facility m a -> Signal m Int
{-# INLINABLE facilityCountChanged #-}
facilityCountChanged :: Facility m a -> Signal m Int
facilityCountChanged Facility m a
r =
  SignalSource m Int -> Signal m Int
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m Int -> Signal m Int)
-> SignalSource m Int -> Signal m Int
forall a b. (a -> b) -> a -> b
$ Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityCountSource Facility m a
r

-- | Signal triggered when the 'facilityCount' property changes.
facilityCountChanged_ :: MonadDES m => Facility m a -> Signal m ()
{-# INLINABLE facilityCountChanged_ #-}
facilityCountChanged_ :: Facility m a -> Signal m ()
facilityCountChanged_ Facility m a
r =
  (Int -> ()) -> Signal m Int -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> Int -> ()
forall a b. a -> b -> a
const ()) (Signal m Int -> Signal m ()) -> Signal m Int -> Signal m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Signal m Int
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m Int
facilityCountChanged Facility m a
r

-- | Return the current capture count of the facility.
facilityCaptureCount :: MonadDES m => Facility m a -> Event m Int
{-# INLINABLE facilityCaptureCount #-}
facilityCaptureCount :: Facility m a -> Event m Int
facilityCaptureCount Facility m a
r =
  (Point m -> m Int) -> Event m Int
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Int) -> Event m Int)
-> (Point m -> m Int) -> Event m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCaptureCountRef Facility m a
r)

-- | Signal triggered when the 'facilityCaptureCount' property changes.
facilityCaptureCountChanged :: MonadDES m => Facility m a -> Signal m Int
{-# INLINABLE facilityCaptureCountChanged #-}
facilityCaptureCountChanged :: Facility m a -> Signal m Int
facilityCaptureCountChanged Facility m a
r =
  SignalSource m Int -> Signal m Int
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m Int -> Signal m Int)
-> SignalSource m Int -> Signal m Int
forall a b. (a -> b) -> a -> b
$ Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityCaptureCountSource Facility m a
r

-- | Signal triggered when the 'facilityCaptureCount' property changes.
facilityCaptureCountChanged_ :: MonadDES m => Facility m a -> Signal m ()
{-# INLINABLE facilityCaptureCountChanged_ #-}
facilityCaptureCountChanged_ :: Facility m a -> Signal m ()
facilityCaptureCountChanged_ Facility m a
r =
  (Int -> ()) -> Signal m Int -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> Int -> ()
forall a b. a -> b -> a
const ()) (Signal m Int -> Signal m ()) -> Signal m Int -> Signal m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Signal m Int
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m Int
facilityCaptureCountChanged Facility m a
r

-- | Return the current utilisation count of the facility.
facilityUtilisationCount :: MonadDES m => Facility m a -> Event m Int
{-# INLINABLE facilityUtilisationCount #-}
facilityUtilisationCount :: Facility m a -> Event m Int
facilityUtilisationCount Facility m a
r =
  (Point m -> m Int) -> Event m Int
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Int) -> Event m Int)
-> (Point m -> m Int) -> Event m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityUtilisationCountRef Facility m a
r)

-- | Return the statistics for the utilisation count of the facility.
facilityUtilisationCountStats :: MonadDES m => Facility m a -> Event m (TimingStats Int)
{-# INLINABLE facilityUtilisationCountStats #-}
facilityUtilisationCountStats :: Facility m a -> Event m (TimingStats Int)
facilityUtilisationCountStats Facility m a
r =
  (Point m -> m (TimingStats Int)) -> Event m (TimingStats Int)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (TimingStats Int)) -> Event m (TimingStats Int))
-> (Point m -> m (TimingStats Int)) -> Event m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m (TimingStats Int) -> m (TimingStats Int)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (TimingStats Int) -> m (TimingStats Int))
-> Event m (TimingStats Int) -> m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ Ref m (TimingStats Int) -> Event m (TimingStats Int)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityUtilisationCountStatsRef Facility m a
r)

-- | Signal triggered when the 'facilityUtilisationCount' property changes.
facilityUtilisationCountChanged :: MonadDES m => Facility m a -> Signal m Int
{-# INLINABLE facilityUtilisationCountChanged #-}
facilityUtilisationCountChanged :: Facility m a -> Signal m Int
facilityUtilisationCountChanged Facility m a
r =
  SignalSource m Int -> Signal m Int
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m Int -> Signal m Int)
-> SignalSource m Int -> Signal m Int
forall a b. (a -> b) -> a -> b
$ Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityUtilisationCountSource Facility m a
r

-- | Signal triggered when the 'facilityUtilisationCount' property changes.
facilityUtilisationCountChanged_ :: MonadDES m => Facility m a -> Signal m ()
{-# INLINABLE facilityUtilisationCountChanged_ #-}
facilityUtilisationCountChanged_ :: Facility m a -> Signal m ()
facilityUtilisationCountChanged_ Facility m a
r =
  (Int -> ()) -> Signal m Int -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> Int -> ()
forall a b. a -> b -> a
const ()) (Signal m Int -> Signal m ()) -> Signal m Int -> Signal m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Signal m Int
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m Int
facilityUtilisationCountChanged Facility m a
r

-- | Return the current queue length of the facility.
facilityQueueCount :: MonadDES m => Facility m a -> Event m Int
{-# INLINABLE facilityQueueCount #-}
facilityQueueCount :: Facility m a -> Event m Int
facilityQueueCount Facility m a
r =
  (Point m -> m Int) -> Event m Int
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Int) -> Event m Int)
-> (Point m -> m Int) -> Event m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityQueueCountRef Facility m a
r)

-- | Return the statistics for the queue length of the facility.
facilityQueueCountStats :: MonadDES m => Facility m a -> Event m (TimingStats Int)
{-# INLINABLE facilityQueueCountStats #-}
facilityQueueCountStats :: Facility m a -> Event m (TimingStats Int)
facilityQueueCountStats Facility m a
r =
  (Point m -> m (TimingStats Int)) -> Event m (TimingStats Int)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (TimingStats Int)) -> Event m (TimingStats Int))
-> (Point m -> m (TimingStats Int)) -> Event m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m (TimingStats Int) -> m (TimingStats Int)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (TimingStats Int) -> m (TimingStats Int))
-> Event m (TimingStats Int) -> m (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ Ref m (TimingStats Int) -> Event m (TimingStats Int)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityQueueCountStatsRef Facility m a
r)

-- | Signal triggered when the 'facilityQueueCount' property changes.
facilityQueueCountChanged :: MonadDES m => Facility m a -> Signal m Int
{-# INLINABLE facilityQueueCountChanged #-}
facilityQueueCountChanged :: Facility m a -> Signal m Int
facilityQueueCountChanged Facility m a
r =
  SignalSource m Int -> Signal m Int
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m Int -> Signal m Int)
-> SignalSource m Int -> Signal m Int
forall a b. (a -> b) -> a -> b
$ Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityQueueCountSource Facility m a
r

-- | Signal triggered when the 'facilityQueueCount' property changes.
facilityQueueCountChanged_ :: MonadDES m => Facility m a -> Signal m ()
{-# INLINABLE facilityQueueCountChanged_ #-}
facilityQueueCountChanged_ :: Facility m a -> Signal m ()
facilityQueueCountChanged_ Facility m a
r =
  (Int -> ()) -> Signal m Int -> Signal m ()
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (() -> Int -> ()
forall a b. a -> b -> a
const ()) (Signal m Int -> Signal m ()) -> Signal m Int -> Signal m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Signal m Int
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m Int
facilityQueueCountChanged Facility m a
r

-- | Return the total wait time of the facility.
facilityTotalWaitTime :: MonadDES m => Facility m a -> Event m Double
{-# INLINABLE facilityTotalWaitTime #-}
facilityTotalWaitTime :: Facility m a -> Event m Double
facilityTotalWaitTime Facility m a
r =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Double
forall (m :: * -> *) a. Facility m a -> Ref m Double
facilityTotalWaitTimeRef Facility m a
r)

-- | Return the statistics for the wait time of the facility.
facilityWaitTime :: MonadDES m => Facility m a -> Event m (SamplingStats Double)
{-# INLINABLE facilityWaitTime #-}
facilityWaitTime :: Facility m a -> Event m (SamplingStats Double)
facilityWaitTime Facility m a
r =
  (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (SamplingStats Double))
 -> Event m (SamplingStats Double))
-> (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (SamplingStats Double)
forall (m :: * -> *) a.
Facility m a -> Ref m (SamplingStats Double)
facilityWaitTimeRef Facility m a
r)

-- | Signal triggered when the 'facilityTotalWaitTime' and 'facilityWaitTime' properties change.
facilityWaitTimeChanged :: MonadDES m => Facility m a -> Signal m (SamplingStats Double)
{-# INLINABLE facilityWaitTimeChanged #-}
facilityWaitTimeChanged :: Facility m a -> Signal m (SamplingStats Double)
facilityWaitTimeChanged Facility m a
r =
  (() -> 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 (\() -> Facility m a -> Event m (SamplingStats Double)
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Event m (SamplingStats Double)
facilityWaitTime Facility m a
r) (Signal m () -> Signal m (SamplingStats Double))
-> Signal m () -> Signal m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Facility m a -> Signal m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m ()
facilityWaitTimeChanged_ Facility m a
r

-- | Signal triggered when the 'facilityTotalWaitTime' and 'facilityWaitTime' properties change.
facilityWaitTimeChanged_ :: MonadDES m => Facility m a -> Signal m ()
{-# INLINABLE facilityWaitTimeChanged_ #-}
facilityWaitTimeChanged_ :: Facility m a -> Signal m ()
facilityWaitTimeChanged_ Facility m a
r =
  SignalSource m () -> Signal m ()
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m () -> Signal m ())
-> SignalSource m () -> Signal m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> SignalSource m ()
forall (m :: * -> *) a. Facility m a -> SignalSource m ()
facilityWaitTimeSource Facility m a
r

-- | Return the total holding time of the facility.
facilityTotalHoldingTime :: MonadDES m => Facility m a -> Event m Double
{-# INLINABLE facilityTotalHoldingTime #-}
facilityTotalHoldingTime :: Facility m a -> Event m Double
facilityTotalHoldingTime Facility m a
r =
  (Point m -> m Double) -> Event m Double
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Double) -> Event m Double)
-> (Point m -> m Double) -> Event m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Double
forall (m :: * -> *) a. Facility m a -> Ref m Double
facilityTotalHoldingTimeRef Facility m a
r)

-- | Return the statistics for the holding time of the facility.
facilityHoldingTime :: MonadDES m => Facility m a -> Event m (SamplingStats Double)
{-# INLINABLE facilityHoldingTime #-}
facilityHoldingTime :: Facility m a -> Event m (SamplingStats Double)
facilityHoldingTime Facility m a
r =
  (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m (SamplingStats Double))
 -> Event m (SamplingStats Double))
-> (Point m -> m (SamplingStats Double))
-> Event m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point m
p -> Point m
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (SamplingStats Double) -> m (SamplingStats Double))
-> Event m (SamplingStats Double) -> m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> Event m (SamplingStats Double)
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (SamplingStats Double)
forall (m :: * -> *) a.
Facility m a -> Ref m (SamplingStats Double)
facilityHoldingTimeRef Facility m a
r)

-- | Signal triggered when the 'facilityTotalHoldingTime' and 'facilityHoldingTime' properties change.
facilityHoldingTimeChanged :: MonadDES m => Facility m a -> Signal m (SamplingStats Double)
{-# INLINABLE facilityHoldingTimeChanged #-}
facilityHoldingTimeChanged :: Facility m a -> Signal m (SamplingStats Double)
facilityHoldingTimeChanged Facility m a
r =
  (() -> 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 (\() -> Facility m a -> Event m (SamplingStats Double)
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Event m (SamplingStats Double)
facilityHoldingTime Facility m a
r) (Signal m () -> Signal m (SamplingStats Double))
-> Signal m () -> Signal m (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Facility m a -> Signal m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m ()
facilityHoldingTimeChanged_ Facility m a
r

-- | Signal triggered when the 'facilityTotalHoldingTime' and 'facilityHoldingTime' properties change.
facilityHoldingTimeChanged_ :: MonadDES m => Facility m a -> Signal m ()
{-# INLINABLE facilityHoldingTimeChanged_ #-}
facilityHoldingTimeChanged_ :: Facility m a -> Signal m ()
facilityHoldingTimeChanged_ Facility m a
r =
  SignalSource m () -> Signal m ()
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (SignalSource m () -> Signal m ())
-> SignalSource m () -> Signal m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> SignalSource m ()
forall (m :: * -> *) a. Facility m a -> SignalSource m ()
facilityHoldingTimeSource Facility m a
r

-- | Whether the facility is currently interrupted.
facilityInterrupted :: MonadDES m => Facility m a -> Event m Bool
{-# INLINABLE facilityInterrupted #-}
facilityInterrupted :: Facility m a -> Event m Bool
facilityInterrupted Facility m a
r =
  (Point m -> m Bool) -> Event m Bool
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m Bool) -> Event m Bool)
-> (Point m -> m Bool) -> Event m Bool
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Maybe (FacilityOwnerItem m a)
x <- Point m
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe (FacilityOwnerItem m a))
 -> m (Maybe (FacilityOwnerItem m a)))
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Event m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r)
     case Maybe (FacilityOwnerItem m a)
x of
       Maybe (FacilityOwnerItem m a)
Nothing -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
       Just FacilityOwnerItem m a
a  -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (FacilityOwnerItem m a -> Bool
forall (m :: * -> *) a. FacilityOwnerItem m a -> Bool
ownerItemPreempting FacilityOwnerItem m a
a)

-- | Seize the facility.
seizeFacility :: MonadDES m
                 => Facility m a
                 -- ^ the requested facility
                 -> Transact m a
                 -- ^ the transact that tries to seize the facility
                 -> Process m ()
{-# INLINABLE seizeFacility #-}
seizeFacility :: Facility m a -> Transact m a -> Process m ()
seizeFacility Facility m a
r Transact m a
transact =
  (ProcessId m -> Cont m ()) -> Process m ()
forall (m :: * -> *) a. (ProcessId m -> Cont m a) -> Process m a
Process ((ProcessId m -> Cont m ()) -> Process m ())
-> (ProcessId m -> Cont m ()) -> Process m ()
forall a b. (a -> b) -> a -> b
$ \ProcessId m
pid ->
  (ContParams m () -> Event m ()) -> Cont m ()
forall (m :: * -> *) a. (ContParams m a -> Event m ()) -> Cont m a
Cont ((ContParams m () -> Event m ()) -> Cont m ())
-> (ContParams m () -> Event m ()) -> Cont m ()
forall a b. (a -> b) -> a -> b
$ \ContParams m ()
c ->
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Bool
f <- do Bool
f1 <- Point m -> Event m Bool -> m Bool
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Bool -> m Bool) -> Event m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
-> Event m Bool
forall (m :: * -> *) s a.
QueueStrategy m s =>
StrategyQueue m s a -> Event m Bool
strategyQueueNull (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
facilityDelayChain Facility m a
r)
             if Bool
f1
               then do Bool
f2 <- Point m -> Event m Bool -> m Bool
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Bool -> m Bool) -> Event m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
-> Event m Bool
forall (m :: * -> *) s a.
QueueStrategy m s =>
StrategyQueue m s a -> Event m Bool
strategyQueueNull (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
facilityInterruptChain Facility m a
r)
                       if Bool
f2
                         then Point m -> Event m Bool -> m Bool
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Bool -> m Bool) -> Event m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
-> Event m Bool
forall (m :: * -> *) s a.
QueueStrategy m s =>
StrategyQueue m s a -> Event m Bool
strategyQueueNull (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
facilityPendingChain Facility m a
r)
                         else Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
               else Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
     if Bool
f
       then Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
            ContParams m () -> Cont m () -> Event m ()
forall (m :: * -> *) a. ContParams m a -> Cont m a -> Event m ()
invokeCont ContParams m ()
c (Cont m () -> Event m ()) -> Cont m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
            ProcessId m -> Process m () -> Cont m ()
forall (m :: * -> *) a. ProcessId m -> Process m a -> Cont m a
invokeProcess ProcessId m
pid (Process m () -> Cont m ()) -> Process m () -> Cont m ()
forall a b. (a -> b) -> a -> b
$
            Facility m a -> Transact m a -> Process m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Transact m a -> Process m ()
seizeFacility' Facility m a
r Transact m a
transact
       else do FrozenCont m ()
c <- Point m -> Event m (FrozenCont m ()) -> m (FrozenCont m ())
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (FrozenCont m ()) -> m (FrozenCont m ()))
-> Event m (FrozenCont m ()) -> m (FrozenCont m ())
forall a b. (a -> b) -> a -> b
$
                    ContParams m () -> () -> Event m () -> Event m (FrozenCont m ())
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m () -> Event m (FrozenCont m a)
freezeContReentering ContParams m ()
c () (Event m () -> Event m (FrozenCont m ()))
-> Event m () -> Event m (FrozenCont m ())
forall a b. (a -> b) -> a -> b
$
                    ContParams m () -> Cont m () -> Event m ()
forall (m :: * -> *) a. ContParams m a -> Cont m a -> Event m ()
invokeCont ContParams m ()
c (Cont m () -> Event m ()) -> Cont m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
                    ProcessId m -> Process m () -> Cont m ()
forall (m :: * -> *) a. ProcessId m -> Process m a -> Cont m a
invokeProcess ProcessId m
pid (Process m () -> Cont m ()) -> Process m () -> Cont m ()
forall a b. (a -> b) -> a -> b
$
                    Facility m a -> Transact m a -> Process m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Transact m a -> Process m ()
seizeFacility Facility m a
r Transact m a
transact
               Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
                 StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
-> Int -> FacilityDelayedItem m a -> Event m ()
forall (m :: * -> *) s p a.
PriorityQueueStrategy m s p =>
StrategyQueue m s a -> p -> a -> Event m ()
strategyEnqueueWithPriority
                 (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
facilityDelayChain Facility m a
r)
                 (Transact m a -> Int
forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact)
                 (Transact m a
-> Double
-> Bool
-> Bool
-> FrozenCont m ()
-> FacilityDelayedItem m a
forall (m :: * -> *) a.
Transact m a
-> Double
-> Bool
-> Bool
-> FrozenCont m ()
-> FacilityDelayedItem m a
FacilityDelayedItem Transact m a
transact Double
t Bool
False Bool
False FrozenCont m ()
c)
               Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r Int
1

-- | Seize the facility.
seizeFacility' :: MonadDES m
                  => Facility m a
                  -- ^ the requested facility
                  -> Transact m a
                  -- ^ the transact that tries to seize the facility
                  -> Process m ()
{-# INLINABLE seizeFacility' #-}
seizeFacility' :: Facility m a -> Transact m a -> Process m ()
seizeFacility' Facility m a
r Transact m a
transact =
  (ProcessId m -> Cont m ()) -> Process m ()
forall (m :: * -> *) a. (ProcessId m -> Cont m a) -> Process m a
Process ((ProcessId m -> Cont m ()) -> Process m ())
-> (ProcessId m -> Cont m ()) -> Process m ()
forall a b. (a -> b) -> a -> b
$ \ProcessId m
pid ->
  (ContParams m () -> Event m ()) -> Cont m ()
forall (m :: * -> *) a. (ContParams m a -> Event m ()) -> Cont m a
Cont ((ContParams m () -> Event m ()) -> Cont m ())
-> (ContParams m () -> Event m ()) -> Cont m ()
forall a b. (a -> b) -> a -> b
$ \ContParams m ()
c ->
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Maybe (FacilityOwnerItem m a)
a <- Point m
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe (FacilityOwnerItem m a))
 -> m (Maybe (FacilityOwnerItem m a)))
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Event m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r)
     case Maybe (FacilityOwnerItem m a)
a of
       Maybe (FacilityOwnerItem m a)
Nothing ->
         do Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r) (Maybe (FacilityOwnerItem m a) -> Event m ())
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$ FacilityOwnerItem m a -> Maybe (FacilityOwnerItem m a)
forall a. a -> Maybe a
Just (Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
forall (m :: * -> *) a.
Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
FacilityOwnerItem Transact m a
transact Double
t Bool
False Bool
False Double
0)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityWaitTime Facility m a
r Double
0
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCount Facility m a
r (-Int
1)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCaptureCount Facility m a
r Int
1
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityUtilisationCount Facility m a
r Int
1
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ ContParams m () -> () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
resumeCont ContParams m ()
c ()
       Just FacilityOwnerItem m a
owner ->
         do FrozenCont m ()
c <- Point m -> Event m (FrozenCont m ()) -> m (FrozenCont m ())
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (FrozenCont m ()) -> m (FrozenCont m ()))
-> Event m (FrozenCont m ()) -> m (FrozenCont m ())
forall a b. (a -> b) -> a -> b
$
                 ContParams m () -> () -> Event m () -> Event m (FrozenCont m ())
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m () -> Event m (FrozenCont m a)
freezeContReentering ContParams m ()
c () (Event m () -> Event m (FrozenCont m ()))
-> Event m () -> Event m (FrozenCont m ())
forall a b. (a -> b) -> a -> b
$
                 ContParams m () -> Cont m () -> Event m ()
forall (m :: * -> *) a. ContParams m a -> Cont m a -> Event m ()
invokeCont ContParams m ()
c (Cont m () -> Event m ()) -> Cont m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
                 ProcessId m -> Process m () -> Cont m ()
forall (m :: * -> *) a. ProcessId m -> Process m a -> Cont m a
invokeProcess ProcessId m
pid (Process m () -> Cont m ()) -> Process m () -> Cont m ()
forall a b. (a -> b) -> a -> b
$
                 Facility m a -> Transact m a -> Process m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Transact m a -> Process m ()
seizeFacility Facility m a
r Transact m a
transact
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
              StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
-> Int -> FacilityDelayedItem m a -> Event m ()
forall (m :: * -> *) s p a.
PriorityQueueStrategy m s p =>
StrategyQueue m s a -> p -> a -> Event m ()
strategyEnqueueWithPriority
              (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
facilityDelayChain Facility m a
r)
              (Transact m a -> Int
forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact)
              (Transact m a
-> Double
-> Bool
-> Bool
-> FrozenCont m ()
-> FacilityDelayedItem m a
forall (m :: * -> *) a.
Transact m a
-> Double
-> Bool
-> Bool
-> FrozenCont m ()
-> FacilityDelayedItem m a
FacilityDelayedItem Transact m a
transact Double
t Bool
False Bool
False FrozenCont m ()
c)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r Int
1

-- | Preempt the facility.
preemptFacility :: MonadDES m
                   => Facility m a
                   -- ^ the requested facility
                   -> Transact m a
                   -- ^ the transact that tries to preempt the facility
                   -> FacilityPreemptMode m a
                   -- ^ the Preempt mode
                   -> Process m ()
{-# INLINABLE preemptFacility #-}
preemptFacility :: Facility m a
-> Transact m a -> FacilityPreemptMode m a -> Process m ()
preemptFacility Facility m a
r Transact m a
transact FacilityPreemptMode m a
mode =
  (ProcessId m -> Cont m ()) -> Process m ()
forall (m :: * -> *) a. (ProcessId m -> Cont m a) -> Process m a
Process ((ProcessId m -> Cont m ()) -> Process m ())
-> (ProcessId m -> Cont m ()) -> Process m ()
forall a b. (a -> b) -> a -> b
$ \ProcessId m
pid ->
  (ContParams m () -> Event m ()) -> Cont m ()
forall (m :: * -> *) a. (ContParams m a -> Event m ()) -> Cont m a
Cont ((ContParams m () -> Event m ()) -> Cont m ())
-> (ContParams m () -> Event m ()) -> Cont m ()
forall a b. (a -> b) -> a -> b
$ \ContParams m ()
c ->
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Maybe (FacilityOwnerItem m a)
a <- Point m
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe (FacilityOwnerItem m a))
 -> m (Maybe (FacilityOwnerItem m a)))
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Event m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r)
     case Maybe (FacilityOwnerItem m a)
a of
       Maybe (FacilityOwnerItem m a)
Nothing ->
         do Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r) (Maybe (FacilityOwnerItem m a) -> Event m ())
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$ FacilityOwnerItem m a -> Maybe (FacilityOwnerItem m a)
forall a. a -> Maybe a
Just (Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
forall (m :: * -> *) a.
Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
FacilityOwnerItem Transact m a
transact Double
t Bool
True Bool
False Double
0)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityWaitTime Facility m a
r Double
0
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCount Facility m a
r (-Int
1)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCaptureCount Facility m a
r Int
1
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityUtilisationCount Facility m a
r Int
1
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ ContParams m () -> () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
resumeCont ContParams m ()
c ()
       Just owner :: FacilityOwnerItem m a
owner@(FacilityOwnerItem Transact m a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0)
         | (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ FacilityPreemptMode m a -> Bool
forall (m :: * -> *) a. FacilityPreemptMode m a -> Bool
facilityPriorityMode FacilityPreemptMode m a
mode) Bool -> Bool -> Bool
&& Bool
interrupting0 ->
         do FrozenCont m ()
c <- Point m -> Event m (FrozenCont m ()) -> m (FrozenCont m ())
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (FrozenCont m ()) -> m (FrozenCont m ()))
-> Event m (FrozenCont m ()) -> m (FrozenCont m ())
forall a b. (a -> b) -> a -> b
$
                 ContParams m () -> () -> Event m () -> Event m (FrozenCont m ())
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m () -> Event m (FrozenCont m a)
freezeContReentering ContParams m ()
c () (Event m () -> Event m (FrozenCont m ()))
-> Event m () -> Event m (FrozenCont m ())
forall a b. (a -> b) -> a -> b
$
                 ContParams m () -> Cont m () -> Event m ()
forall (m :: * -> *) a. ContParams m a -> Cont m a -> Event m ()
invokeCont ContParams m ()
c (Cont m () -> Event m ()) -> Cont m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
                 ProcessId m -> Process m () -> Cont m ()
forall (m :: * -> *) a. ProcessId m -> Process m a -> Cont m a
invokeProcess ProcessId m
pid (Process m () -> Cont m ()) -> Process m () -> Cont m ()
forall a b. (a -> b) -> a -> b
$
                 Facility m a
-> Transact m a -> FacilityPreemptMode m a -> Process m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a
-> Transact m a -> FacilityPreemptMode m a -> Process m ()
preemptFacility Facility m a
r Transact m a
transact FacilityPreemptMode m a
mode
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
              StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
-> Int -> FacilityPendingItem m a -> Event m ()
forall (m :: * -> *) s p a.
PriorityQueueStrategy m s p =>
StrategyQueue m s a -> p -> a -> Event m ()
strategyEnqueueWithPriority
              (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
facilityPendingChain Facility m a
r)
              (Transact m a -> Int
forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact)
              (Transact m a
-> Double
-> Bool
-> Bool
-> FrozenCont m ()
-> FacilityPendingItem m a
forall (m :: * -> *) a.
Transact m a
-> Double
-> Bool
-> Bool
-> FrozenCont m ()
-> FacilityPendingItem m a
FacilityPendingItem Transact m a
transact Double
t Bool
True Bool
True FrozenCont m ()
c)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r Int
1
       Just owner :: FacilityOwnerItem m a
owner@(FacilityOwnerItem Transact m a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0)
         | FacilityPreemptMode m a -> Bool
forall (m :: * -> *) a. FacilityPreemptMode m a -> Bool
facilityPriorityMode FacilityPreemptMode m a
mode Bool -> Bool -> Bool
&& (Transact m a -> Int
forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Transact m a -> Int
forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact0) ->
         do FrozenCont m ()
c <- Point m -> Event m (FrozenCont m ()) -> m (FrozenCont m ())
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (FrozenCont m ()) -> m (FrozenCont m ()))
-> Event m (FrozenCont m ()) -> m (FrozenCont m ())
forall a b. (a -> b) -> a -> b
$
                 ContParams m () -> () -> Event m () -> Event m (FrozenCont m ())
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m () -> Event m (FrozenCont m a)
freezeContReentering ContParams m ()
c () (Event m () -> Event m (FrozenCont m ()))
-> Event m () -> Event m (FrozenCont m ())
forall a b. (a -> b) -> a -> b
$
                 ContParams m () -> Cont m () -> Event m ()
forall (m :: * -> *) a. ContParams m a -> Cont m a -> Event m ()
invokeCont ContParams m ()
c (Cont m () -> Event m ()) -> Cont m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
                 ProcessId m -> Process m () -> Cont m ()
forall (m :: * -> *) a. ProcessId m -> Process m a -> Cont m a
invokeProcess ProcessId m
pid (Process m () -> Cont m ()) -> Process m () -> Cont m ()
forall a b. (a -> b) -> a -> b
$
                 Facility m a
-> Transact m a -> FacilityPreemptMode m a -> Process m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a
-> Transact m a -> FacilityPreemptMode m a -> Process m ()
preemptFacility Facility m a
r Transact m a
transact FacilityPreemptMode m a
mode
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
              StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
-> Int -> FacilityDelayedItem m a -> Event m ()
forall (m :: * -> *) s p a.
PriorityQueueStrategy m s p =>
StrategyQueue m s a -> p -> a -> Event m ()
strategyEnqueueWithPriority
              (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
facilityDelayChain Facility m a
r)
              (Transact m a -> Int
forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact)
              (Transact m a
-> Double
-> Bool
-> Bool
-> FrozenCont m ()
-> FacilityDelayedItem m a
forall (m :: * -> *) a.
Transact m a
-> Double
-> Bool
-> Bool
-> FrozenCont m ()
-> FacilityDelayedItem m a
FacilityDelayedItem Transact m a
transact Double
t Bool
True Bool
True FrozenCont m ()
c)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r Int
1
       Just owner :: FacilityOwnerItem m a
owner@(FacilityOwnerItem Transact m a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0)
         | (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ FacilityPreemptMode m a -> Bool
forall (m :: * -> *) a. FacilityPreemptMode m a -> Bool
facilityRemoveMode FacilityPreemptMode m a
mode) ->
         do Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r) (Maybe (FacilityOwnerItem m a) -> Event m ())
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$ FacilityOwnerItem m a -> Maybe (FacilityOwnerItem m a)
forall a. a -> Maybe a
Just (Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
forall (m :: * -> *) a.
Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
FacilityOwnerItem Transact m a
transact Double
t Bool
True Bool
True Double
0)
            ProcessId m
pid0 <- Point m -> Event m (ProcessId m) -> m (ProcessId m)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (ProcessId m) -> m (ProcessId m))
-> Event m (ProcessId m) -> m (ProcessId m)
forall a b. (a -> b) -> a -> b
$ Transact m a -> Event m (ProcessId m)
forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Event m (ProcessId m)
requireTransactProcessId Transact m a
transact0
            Maybe Double
t2   <- Point m -> Event m (Maybe Double) -> m (Maybe Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe Double) -> m (Maybe Double))
-> Event m (Maybe Double) -> m (Maybe Double)
forall a b. (a -> b) -> a -> b
$ ProcessId m -> Event m (Maybe Double)
forall (m :: * -> *).
MonadDES m =>
ProcessId m -> Event m (Maybe Double)
processInterruptionTime ProcessId m
pid0
            let dt0 :: Maybe Double
dt0 = (Double -> Double) -> Maybe Double -> Maybe Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Double
x -> Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t) Maybe Double
t2
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
              StrategyQueue
  m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
-> Int -> FacilityInterruptedItem m a -> Event m ()
forall (m :: * -> *) s p a.
PriorityQueueStrategy m s p =>
StrategyQueue m s a -> p -> a -> Event m ()
strategyEnqueueWithPriority
              (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
facilityInterruptChain Facility m a
r)
              (Transact m a -> Int
forall (m :: * -> *) a. Transact m a -> Int
transactPriority Transact m a
transact0)
              (Transact m a
-> Double
-> Bool
-> Bool
-> Maybe Double
-> Maybe (FacilityPreemptTransfer m a)
-> Double
-> FacilityInterruptedItem m a
forall (m :: * -> *) a.
Transact m a
-> Double
-> Bool
-> Bool
-> Maybe Double
-> Maybe (FacilityPreemptTransfer m a)
-> Double
-> FacilityInterruptedItem m a
FacilityInterruptedItem Transact m a
transact0 Double
t Bool
preempting0 Bool
interrupting0 Maybe Double
dt0 (FacilityPreemptMode m a -> Maybe (FacilityPreemptTransfer m a)
forall (m :: * -> *) a.
FacilityPreemptMode m a -> Maybe (FacilityPreemptTransfer m a)
facilityTransfer FacilityPreemptMode m a
mode) (Double
acc0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)))
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r Int
1
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityWaitTime Facility m a
r Double
0
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCaptureCount Facility m a
r Int
1
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Transact m a -> Event m ()
forall (m :: * -> *) a. MonadDES m => Transact m a -> Event m ()
transactPreemptionBegin Transact m a
transact0
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ ContParams m () -> () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
resumeCont ContParams m ()
c ()
       Just owner :: FacilityOwnerItem m a
owner@(FacilityOwnerItem Transact m a
transact0 Double
t0 Bool
preempting0 Bool
interruptin0 Double
acc0)
         | FacilityPreemptMode m a -> Bool
forall (m :: * -> *) a. FacilityPreemptMode m a -> Bool
facilityRemoveMode FacilityPreemptMode m a
mode ->
         do Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r) (Maybe (FacilityOwnerItem m a) -> Event m ())
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$ FacilityOwnerItem m a -> Maybe (FacilityOwnerItem m a)
forall a. a -> Maybe a
Just (Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
forall (m :: * -> *) a.
Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
FacilityOwnerItem Transact m a
transact Double
t Bool
True Bool
True Double
0)
            ProcessId m
pid0 <- Point m -> Event m (ProcessId m) -> m (ProcessId m)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (ProcessId m) -> m (ProcessId m))
-> Event m (ProcessId m) -> m (ProcessId m)
forall a b. (a -> b) -> a -> b
$ Transact m a -> Event m (ProcessId m)
forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Event m (ProcessId m)
requireTransactProcessId Transact m a
transact0
            Maybe Double
t2   <- Point m -> Event m (Maybe Double) -> m (Maybe Double)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe Double) -> m (Maybe Double))
-> Event m (Maybe Double) -> m (Maybe Double)
forall a b. (a -> b) -> a -> b
$ ProcessId m -> Event m (Maybe Double)
forall (m :: * -> *).
MonadDES m =>
ProcessId m -> Event m (Maybe Double)
processInterruptionTime ProcessId m
pid0
            let dt0 :: Maybe Double
dt0 = (Double -> Double) -> Maybe Double -> Maybe Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Double
x -> Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t) Maybe Double
t2
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityWaitTime Facility m a
r Double
0
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCaptureCount Facility m a
r Int
1
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityHoldingTime Facility m a
r (Double
acc0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0))
            case FacilityPreemptMode m a -> Maybe (FacilityPreemptTransfer m a)
forall (m :: * -> *) a.
FacilityPreemptMode m a -> Maybe (FacilityPreemptTransfer m a)
facilityTransfer FacilityPreemptMode m a
mode of
              Maybe (FacilityPreemptTransfer m a)
Nothing ->
                SimulationRetry -> m ()
forall (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> m a
throwComp (SimulationRetry -> m ()) -> SimulationRetry -> m ()
forall a b. (a -> b) -> a -> b
$
                String -> SimulationRetry
SimulationRetry
                String
"The transfer destination is not specified for the removed preempted transact: preemptFacility"
              Just FacilityPreemptTransfer m a
transfer ->
                Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Transact m a -> Process m () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Process m () -> Event m ()
transferTransact Transact m a
transact0 (FacilityPreemptTransfer m a
transfer Transact m a
transact0 Maybe Double
dt0)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ ContParams m () -> () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
resumeCont ContParams m ()
c ()

-- | Return the facility by the active transact.
returnFacility :: MonadDES m
                  => Facility m a
                  -- ^ the facility to return
                  -> Transact m a
                  -- ^ the active transact that tries to return the facility
                  -> Process m ()
{-# INLINABLE returnFacility #-}
returnFacility :: Facility m a -> Transact m a -> Process m ()
returnFacility Facility m a
r Transact m a
transact = Facility m a -> Transact m a -> Bool -> Process m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Transact m a -> Bool -> Process m ()
releaseFacility' Facility m a
r Transact m a
transact Bool
True 

-- | Release the facility by the active transact.
releaseFacility :: MonadDES m
                   => Facility m a
                   -- ^ the facility to release
                   -> Transact m a
                   -- ^ the active transact that tries to release the facility
                   -> Process m ()
{-# INLINABLE releaseFacility #-}
releaseFacility :: Facility m a -> Transact m a -> Process m ()
releaseFacility Facility m a
r Transact m a
transact = Facility m a -> Transact m a -> Bool -> Process m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Transact m a -> Bool -> Process m ()
releaseFacility' Facility m a
r Transact m a
transact Bool
False 

-- | Release the facility by the active transact.
releaseFacility' :: MonadDES m
                    => Facility m a
                    -- ^ the facility to release
                    -> Transact m a
                    -- ^ the active transact that tries to release the facility
                    -> Bool
                    -- ^ whether the transact is preempting
                    -> Process m ()
{-# INLINABLE releaseFacility' #-}
releaseFacility' :: Facility m a -> Transact m a -> Bool -> Process m ()
releaseFacility' Facility m a
r Transact m a
transact Bool
preempting = 
  (ProcessId m -> Cont m ()) -> Process m ()
forall (m :: * -> *) a. (ProcessId m -> Cont m a) -> Process m a
Process ((ProcessId m -> Cont m ()) -> Process m ())
-> (ProcessId m -> Cont m ()) -> Process m ()
forall a b. (a -> b) -> a -> b
$ \ProcessId m
pid ->
  (ContParams m () -> Event m ()) -> Cont m ()
forall (m :: * -> *) a. (ContParams m a -> Event m ()) -> Cont m a
Cont ((ContParams m () -> Event m ()) -> Cont m ())
-> (ContParams m () -> Event m ()) -> Cont m ()
forall a b. (a -> b) -> a -> b
$ \ContParams m ()
c ->
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Maybe (FacilityOwnerItem m a)
a <- Point m
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe (FacilityOwnerItem m a))
 -> m (Maybe (FacilityOwnerItem m a)))
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Event m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r)
     case Maybe (FacilityOwnerItem m a)
a of
       Maybe (FacilityOwnerItem m a)
Nothing ->
         SimulationRetry -> m ()
forall (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> m a
throwComp (SimulationRetry -> m ()) -> SimulationRetry -> m ()
forall a b. (a -> b) -> a -> b
$
         String -> SimulationRetry
SimulationRetry
         String
"There is no owner of the facility: releaseFacility'"
       Just owner :: FacilityOwnerItem m a
owner@(FacilityOwnerItem Transact m a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0) | Transact m a
transact0 Transact m a -> Transact m a -> Bool
forall a. Eq a => a -> a -> Bool
== Transact m a
transact Bool -> Bool -> Bool
&& Bool
preempting0 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
/= Bool
preempting ->
         SimulationRetry -> m ()
forall (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> m a
throwComp (SimulationRetry -> m ()) -> SimulationRetry -> m ()
forall a b. (a -> b) -> a -> b
$
         String -> SimulationRetry
SimulationRetry
         String
"The mismatch use of releaseFacility and returnFacility: releaseFacility'"
       Just owner :: FacilityOwnerItem m a
owner@(FacilityOwnerItem Transact m a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0) | Transact m a
transact0 Transact m a -> Transact m a -> Bool
forall a. Eq a => a -> a -> Bool
== Transact m a
transact ->
         do Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r) Maybe (FacilityOwnerItem m a)
forall a. Maybe a
Nothing
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityUtilisationCount Facility m a
r (-Int
1)
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityHoldingTime Facility m a
r (Double
acc0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0))
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCount Facility m a
r Int
1
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Double -> Event m () -> Event m ()
forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent Double
t (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Event m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Event m ()
tryCaptureFacility Facility m a
r
            Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ ContParams m () -> () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
resumeCont ContParams m ()
c ()
       Just FacilityOwnerItem m a
owner ->
         SimulationRetry -> m ()
forall (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> m a
throwComp (SimulationRetry -> m ()) -> SimulationRetry -> m ()
forall a b. (a -> b) -> a -> b
$
         String -> SimulationRetry
SimulationRetry
         String
"The facility has another owner: releaseFacility'"

-- | Try to capture the facility.
tryCaptureFacility :: MonadDES m => Facility m a -> Event m ()
{-# INLINABLE tryCaptureFacility #-}
tryCaptureFacility :: Facility m a -> Event m ()
tryCaptureFacility Facility m a
r =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Maybe (FacilityOwnerItem m a)
a <- Point m
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe (FacilityOwnerItem m a))
 -> m (Maybe (FacilityOwnerItem m a)))
-> Event m (Maybe (FacilityOwnerItem m a))
-> m (Maybe (FacilityOwnerItem m a))
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Event m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r)
     case Maybe (FacilityOwnerItem m a)
a of
       Maybe (FacilityOwnerItem m a)
Nothing ->
         Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Event m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Event m ()
captureFacility Facility m a
r
       Just FacilityOwnerItem m a
owner -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Find another owner of the facility.
captureFacility :: MonadDES m => Facility m a -> Event m ()
{-# INLINABLE captureFacility #-}
captureFacility :: Facility m a -> Event m ()
captureFacility Facility m a
r =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Bool
f <- Point m -> Event m Bool -> m Bool
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Bool -> m Bool) -> Event m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
-> Event m Bool
forall (m :: * -> *) s a.
QueueStrategy m s =>
StrategyQueue m s a -> Event m Bool
strategyQueueNull (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
facilityPendingChain Facility m a
r)
     if Bool -> Bool
not Bool
f
       then do FacilityPendingItem Transact m a
transact Double
t0 Bool
preempting Bool
interrupting FrozenCont m ()
c0 <- Point m
-> Event m (FacilityPendingItem m a) -> m (FacilityPendingItem m a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (FacilityPendingItem m a) -> m (FacilityPendingItem m a))
-> Event m (FacilityPendingItem m a) -> m (FacilityPendingItem m a)
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
-> Event m (FacilityPendingItem m a)
forall (m :: * -> *) s a.
DequeueStrategy m s =>
StrategyQueue m s a -> Event m a
strategyDequeue (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityPendingItem m a)
facilityPendingChain Facility m a
r)
               Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r (-Int
1)
               Maybe (ContParams m ())
c <- Point m
-> Event m (Maybe (ContParams m ())) -> m (Maybe (ContParams m ()))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe (ContParams m ())) -> m (Maybe (ContParams m ())))
-> Event m (Maybe (ContParams m ())) -> m (Maybe (ContParams m ()))
forall a b. (a -> b) -> a -> b
$ FrozenCont m () -> Event m (Maybe (ContParams m ()))
forall (m :: * -> *) a.
FrozenCont m a -> Event m (Maybe (ContParams m a))
unfreezeCont FrozenCont m ()
c0
               case Maybe (ContParams m ())
c of
                 Maybe (ContParams m ())
Nothing ->
                   Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Event m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Event m ()
captureFacility Facility m a
r
                 Just ContParams m ()
c ->
                   do Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r) (Maybe (FacilityOwnerItem m a) -> Event m ())
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$ FacilityOwnerItem m a -> Maybe (FacilityOwnerItem m a)
forall a. a -> Maybe a
Just (Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
forall (m :: * -> *) a.
Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
FacilityOwnerItem Transact m a
transact Double
t Bool
preempting Bool
interrupting Double
0)
                      Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityWaitTime Facility m a
r (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)
                      Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityUtilisationCount Facility m a
r Int
1
                      Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCaptureCount Facility m a
r Int
1
                      Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCount Facility m a
r (-Int
1)
                      Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Double -> Event m () -> Event m ()
forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent Double
t (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$ ContParams m () -> () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
reenterCont ContParams m ()
c ()
       else do Bool
f <- Point m -> Event m Bool -> m Bool
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Bool -> m Bool) -> Event m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
-> Event m Bool
forall (m :: * -> *) s a.
QueueStrategy m s =>
StrategyQueue m s a -> Event m Bool
strategyQueueNull (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
facilityInterruptChain Facility m a
r)
               if Bool -> Bool
not Bool
f
                  then do FacilityInterruptedItem Transact m a
transact Double
t0 Bool
preempting Bool
interrupting Maybe Double
dt0 Maybe (FacilityPreemptTransfer m a)
transfer0 Double
acc0 <- Point m
-> Event m (FacilityInterruptedItem m a)
-> m (FacilityInterruptedItem m a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (FacilityInterruptedItem m a)
 -> m (FacilityInterruptedItem m a))
-> Event m (FacilityInterruptedItem m a)
-> m (FacilityInterruptedItem m a)
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
-> Event m (FacilityInterruptedItem m a)
forall (m :: * -> *) s a.
DequeueStrategy m s =>
StrategyQueue m s a -> Event m a
strategyDequeue (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy LCFS) (FacilityInterruptedItem m a)
facilityInterruptChain Facility m a
r)
                          ProcessId m
pid <- Point m -> Event m (ProcessId m) -> m (ProcessId m)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (ProcessId m) -> m (ProcessId m))
-> Event m (ProcessId m) -> m (ProcessId m)
forall a b. (a -> b) -> a -> b
$ Transact m a -> Event m (ProcessId m)
forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Event m (ProcessId m)
requireTransactProcessId Transact m a
transact
                          Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r (-Int
1)
                          Bool
f <- Point m -> Event m Bool -> m Bool
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Bool -> m Bool) -> Event m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ ProcessId m -> Event m Bool
forall (m :: * -> *). MonadDES m => ProcessId m -> Event m Bool
processCancelled ProcessId m
pid
                          case Bool
f of
                            Bool
True ->
                              Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Event m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Event m ()
captureFacility Facility m a
r
                            Bool
False ->
                              do Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r) (Maybe (FacilityOwnerItem m a) -> Event m ())
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$ FacilityOwnerItem m a -> Maybe (FacilityOwnerItem m a)
forall a. a -> Maybe a
Just (Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
forall (m :: * -> *) a.
Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
FacilityOwnerItem Transact m a
transact Double
t Bool
preempting Bool
interrupting Double
acc0)
                                 Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityWaitTime Facility m a
r (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)
                                 Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityUtilisationCount Facility m a
r Int
1
                                 Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCount Facility m a
r (-Int
1)
                                 case Maybe (FacilityPreemptTransfer m a)
transfer0 of
                                   Maybe (FacilityPreemptTransfer m a)
Nothing -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                                   Just FacilityPreemptTransfer m a
transfer ->
                                     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Transact m a -> Process m () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Process m () -> Event m ()
transferTransact Transact m a
transact (FacilityPreemptTransfer m a
transfer Transact m a
transact Maybe Double
dt0)
                                 Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Transact m a -> Event m ()
forall (m :: * -> *) a. MonadDES m => Transact m a -> Event m ()
transactPreemptionEnd Transact m a
transact
                 else do Bool
f <- Point m -> Event m Bool -> m Bool
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Bool -> m Bool) -> Event m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
-> Event m Bool
forall (m :: * -> *) s a.
QueueStrategy m s =>
StrategyQueue m s a -> Event m Bool
strategyQueueNull (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
facilityDelayChain Facility m a
r)
                         if Bool -> Bool
not Bool
f
                           then do FacilityDelayedItem Transact m a
transact Double
t0 Bool
preempting Bool
interrupting FrozenCont m ()
c0 <- Point m
-> Event m (FacilityDelayedItem m a) -> m (FacilityDelayedItem m a)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (FacilityDelayedItem m a) -> m (FacilityDelayedItem m a))
-> Event m (FacilityDelayedItem m a) -> m (FacilityDelayedItem m a)
forall a b. (a -> b) -> a -> b
$ StrategyQueue
  m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
-> Event m (FacilityDelayedItem m a)
forall (m :: * -> *) s a.
DequeueStrategy m s =>
StrategyQueue m s a -> Event m a
strategyDequeue (Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
forall (m :: * -> *) a.
Facility m a
-> StrategyQueue
     m (TransactQueueStrategy FCFS) (FacilityDelayedItem m a)
facilityDelayChain Facility m a
r)
                                   Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r (-Int
1)
                                   Maybe (ContParams m ())
c <- Point m
-> Event m (Maybe (ContParams m ())) -> m (Maybe (ContParams m ()))
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m (Maybe (ContParams m ())) -> m (Maybe (ContParams m ())))
-> Event m (Maybe (ContParams m ())) -> m (Maybe (ContParams m ()))
forall a b. (a -> b) -> a -> b
$ FrozenCont m () -> Event m (Maybe (ContParams m ()))
forall (m :: * -> *) a.
FrozenCont m a -> Event m (Maybe (ContParams m a))
unfreezeCont FrozenCont m ()
c0
                                   case Maybe (ContParams m ())
c of
                                     Maybe (ContParams m ())
Nothing ->
                                       Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Event m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Event m ()
captureFacility Facility m a
r
                                     Just ContParams m ()
c ->
                                       do Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (Maybe (FacilityOwnerItem m a))
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
forall (m :: * -> *) a.
Facility m a -> Ref m (Maybe (FacilityOwnerItem m a))
facilityOwnerRef Facility m a
r) (Maybe (FacilityOwnerItem m a) -> Event m ())
-> Maybe (FacilityOwnerItem m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$ FacilityOwnerItem m a -> Maybe (FacilityOwnerItem m a)
forall a. a -> Maybe a
Just (Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
forall (m :: * -> *) a.
Transact m a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem m a
FacilityOwnerItem Transact m a
transact Double
t Bool
preempting Bool
interrupting Double
0)
                                          Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Double -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Double -> Event m ()
updateFacilityWaitTime Facility m a
r (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)
                                          Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityUtilisationCount Facility m a
r Int
1
                                          Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCaptureCount Facility m a
r Int
1
                                          Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Facility m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Facility m a -> Int -> Event m ()
updateFacilityCount Facility m a
r (-Int
1)
                                          Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Double -> Event m () -> Event m ()
forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent Double
t (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$ ContParams m () -> () -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
ContParams m a -> a -> Event m ()
reenterCont ContParams m ()
c ()
                           else () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Signal triggered when one of the facility counters changes.
facilityChanged_ :: MonadDES m => Facility m a -> Signal m ()
{-# INLINABLE facilityChanged_ #-}
facilityChanged_ :: Facility m a -> Signal m ()
facilityChanged_ Facility m a
r =
  Facility m a -> Signal m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m ()
facilityCountChanged_ Facility m a
r Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  Facility m a -> Signal m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m ()
facilityCaptureCountChanged_ Facility m a
r Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  Facility m a -> Signal m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m ()
facilityUtilisationCountChanged_ Facility m a
r Signal m () -> Signal m () -> Signal m ()
forall a. Semigroup a => a -> a -> a
<>
  Facility m a -> Signal m ()
forall (m :: * -> *) a. MonadDES m => Facility m a -> Signal m ()
facilityQueueCountChanged_ Facility m a
r

-- | Update the facility count and its statistics.
updateFacilityCount :: MonadDES m => Facility m a -> Int -> Event m ()
{-# INLINABLE updateFacilityCount #-}
updateFacilityCount :: Facility m a -> Int -> Event m ()
updateFacilityCount Facility m a
r Int
delta =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCountRef Facility m a
r)
     let a' :: Int
a' = Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
delta
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCountRef Facility m a
r) Int
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityCountStatsRef Facility m a
r) ((TimingStats Int -> TimingStats Int) -> Event m ())
-> (TimingStats Int -> TimingStats Int) -> Event m ()
forall a b. (a -> b) -> a -> b
$
       Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p) Int
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m Int -> Int -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityCountSource Facility m a
r) Int
a'

-- | Update the facility capture count.
updateFacilityCaptureCount :: MonadDES m => Facility m a -> Int -> Event m ()
{-# INLINABLE updateFacilityCaptureCount #-}
updateFacilityCaptureCount :: Facility m a -> Int -> Event m ()
updateFacilityCaptureCount Facility m a
r Int
delta =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCaptureCountRef Facility m a
r)
     let a' :: Int
a' = Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
delta
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCaptureCountRef Facility m a
r) Int
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m Int -> Int -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityCaptureCountSource Facility m a
r) Int
a'

-- | Update the facility queue length and its statistics.
updateFacilityQueueCount :: MonadDES m => Facility m a -> Int -> Event m ()
{-# INLINABLE updateFacilityQueueCount #-}
updateFacilityQueueCount :: Facility m a -> Int -> Event m ()
updateFacilityQueueCount Facility m a
r Int
delta =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityQueueCountRef Facility m a
r)
     let a' :: Int
a' = Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
delta
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityQueueCountRef Facility m a
r) Int
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityQueueCountStatsRef Facility m a
r) ((TimingStats Int -> TimingStats Int) -> Event m ())
-> (TimingStats Int -> TimingStats Int) -> Event m ()
forall a b. (a -> b) -> a -> b
$
       Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p) Int
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m Int -> Int -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityQueueCountSource Facility m a
r) Int
a'

-- | Update the facility utilisation count and its statistics.
updateFacilityUtilisationCount :: MonadDES m => Facility m a -> Int -> Event m ()
{-# INLINABLE updateFacilityUtilisationCount #-}
updateFacilityUtilisationCount :: Facility m a -> Int -> Event m ()
updateFacilityUtilisationCount Facility m a
r Int
delta =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Int
a <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityUtilisationCountRef Facility m a
r)
     let a' :: Int
a' = Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
delta
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityUtilisationCountRef Facility m a
r) Int
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityUtilisationCountStatsRef Facility m a
r) ((TimingStats Int -> TimingStats Int) -> Event m ())
-> (TimingStats Int -> TimingStats Int) -> Event m ()
forall a b. (a -> b) -> a -> b
$
       Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p) Int
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m Int -> Int -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityUtilisationCountSource Facility m a
r) Int
a'

-- | Update the facility wait time and its statistics.
updateFacilityWaitTime :: MonadDES m => Facility m a -> Double -> Event m ()
{-# INLINABLE updateFacilityWaitTime #-}
updateFacilityWaitTime :: Facility m a -> Double -> Event m ()
updateFacilityWaitTime Facility m a
r Double
delta =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
a <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Double
forall (m :: * -> *) a. Facility m a -> Ref m Double
facilityTotalWaitTimeRef Facility m a
r)
     let a' :: Double
a' = Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
delta
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Double -> Double -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Double
forall (m :: * -> *) a. Facility m a -> Ref m Double
facilityTotalWaitTimeRef Facility m a
r) Double
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Facility m a -> Ref m (SamplingStats Double)
forall (m :: * -> *) a.
Facility m a -> Ref m (SamplingStats Double)
facilityWaitTimeRef Facility m a
r) ((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
delta
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m () -> () -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m ()
forall (m :: * -> *) a. Facility m a -> SignalSource m ()
facilityWaitTimeSource Facility m a
r) ()

-- | Update the facility holding time and its statistics.
updateFacilityHoldingTime :: MonadDES m => Facility m a -> Double -> Event m ()
{-# INLINABLE updateFacilityHoldingTime #-}
updateFacilityHoldingTime :: Facility m a -> Double -> Event m ()
updateFacilityHoldingTime Facility m a
r Double
delta =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do Double
a <- Point m -> Event m Double -> m Double
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Double -> m Double) -> Event m Double -> m Double
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Event m Double
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Double
forall (m :: * -> *) a. Facility m a -> Ref m Double
facilityTotalHoldingTimeRef Facility m a
r)
     let a' :: Double
a' = Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
delta
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m Double -> Double -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Double
forall (m :: * -> *) a. Facility m a -> Ref m Double
facilityTotalHoldingTimeRef Facility m a
r) Double
a'
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       Ref m (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> Event m ()
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (Facility m a -> Ref m (SamplingStats Double)
forall (m :: * -> *) a.
Facility m a -> Ref m (SamplingStats Double)
facilityHoldingTimeRef Facility m a
r) ((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
delta
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m () -> () -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m ()
forall (m :: * -> *) a. Facility m a -> SignalSource m ()
facilityHoldingTimeSource Facility m a
r) ()

-- | Reset the statistics.
resetFacility :: MonadDES m => Facility m a -> Event m ()
{-# INLINABLE resetFacility #-}
resetFacility :: Facility m a -> Event m ()
resetFacility Facility m a
r =
  (Point m -> m ()) -> Event m ()
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event ((Point m -> m ()) -> Event m ())
-> (Point m -> m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let t :: Double
t = Point m -> Double
forall (m :: * -> *). Point m -> Double
pointTime Point m
p
     Int
count <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCountRef Facility m a
r)
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (TimingStats Int) -> TimingStats Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityCountStatsRef Facility m a
r) (TimingStats Int -> Event m ()) -> TimingStats Int -> Event m ()
forall a b. (a -> b) -> a -> b
$
       Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
count
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityCaptureCountRef Facility m a
r) Int
0
     Int
utilCount <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityUtilisationCountRef Facility m a
r)
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (TimingStats Int) -> TimingStats Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityUtilisationCountStatsRef Facility m a
r) (TimingStats Int -> Event m ()) -> TimingStats Int -> Event m ()
forall a b. (a -> b) -> a -> b
$
       Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
utilCount
     Int
queueCount <- Point m -> Event m Int -> m Int
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m Int -> m Int) -> Event m Int -> m Int
forall a b. (a -> b) -> a -> b
$ Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (Facility m a -> Ref m Int
forall (m :: * -> *) a. Facility m a -> Ref m Int
facilityQueueCountRef Facility m a
r)
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (TimingStats Int) -> TimingStats Int -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (TimingStats Int)
forall (m :: * -> *) a. Facility m a -> Ref m (TimingStats Int)
facilityQueueCountStatsRef Facility m a
r) (TimingStats Int -> Event m ()) -> TimingStats Int -> Event m ()
forall a b. (a -> b) -> a -> b
$
       Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
queueCount
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Double -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Double
forall (m :: * -> *) a. Facility m a -> Ref m Double
facilityTotalWaitTimeRef Facility m a
r) Double
0
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (SamplingStats Double)
forall (m :: * -> *) a.
Facility m a -> Ref m (SamplingStats Double)
facilityWaitTimeRef Facility m a
r) SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m Double -> Double -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m Double
forall (m :: * -> *) a. Facility m a -> Ref m Double
facilityTotalHoldingTimeRef Facility m a
r) Double
0
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$ Ref m (SamplingStats Double) -> SamplingStats Double -> Event m ()
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (Facility m a -> Ref m (SamplingStats Double)
forall (m :: * -> *) a.
Facility m a -> Ref m (SamplingStats Double)
facilityHoldingTimeRef Facility m a
r) SamplingStats Double
forall a. SamplingData a => SamplingStats a
emptySamplingStats
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m Int -> Int -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m Int
forall (m :: * -> *) a. Facility m a -> SignalSource m Int
facilityCaptureCountSource Facility m a
r) Int
0
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m () -> () -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m ()
forall (m :: * -> *) a. Facility m a -> SignalSource m ()
facilityWaitTimeSource Facility m a
r) ()
     Point m -> Event m () -> m ()
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p (Event m () -> m ()) -> Event m () -> m ()
forall a b. (a -> b) -> a -> b
$
       SignalSource m () -> () -> Event m ()
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (Facility m a -> SignalSource m ()
forall (m :: * -> *) a. Facility m a -> SignalSource m ()
facilityHoldingTimeSource Facility m a
r) ()