-- |
-- Module     : Simulation.Aivika.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.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.IORef
import Data.Monoid
import Data.Maybe

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

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

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

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

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

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

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

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

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

-- | The facility preemption mode.
data FacilityPreemptMode a =
  FacilityPreemptMode { forall a. FacilityPreemptMode a -> Bool
facilityPriorityMode :: Bool,
                        -- ^ the Priority mode; otherwise, the Interrupt mode
                        forall a.
FacilityPreemptMode a -> Maybe (FacilityPreemptTransfer a)
facilityTransfer :: Maybe (FacilityPreemptTransfer a),
                        -- ^ where to transfer the preempted transact,
                        -- passing in the remaining time from the process holding
                        -- computation such as the  ADVANCE block
                        forall a. FacilityPreemptMode 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 a = Transact a -> Maybe Double -> Process ()

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

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

-- | Return the current available count of the facility.
facilityCount :: Facility a -> Event Int
facilityCount :: forall a. Facility a -> Event Int
facilityCount Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Int
facilityCountRef Facility a
r)

-- | Return the statistics for the available count of the facility.
facilityCountStats :: Facility a -> Event (TimingStats Int)
facilityCountStats :: forall a. Facility a -> Event (TimingStats Int)
facilityCountStats Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef (TimingStats Int)
facilityCountStatsRef Facility a
r)

-- | Signal triggered when the 'facilityCount' property changes.
facilityCountChanged :: Facility a -> Signal Int
facilityCountChanged :: forall a. Facility a -> Signal Int
facilityCountChanged Facility a
r =
  forall a. SignalSource a -> Signal a
publishSignal forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> SignalSource Int
facilityCountSource Facility a
r

-- | Signal triggered when the 'facilityCount' property changes.
facilityCountChanged_ :: Facility a -> Signal ()
facilityCountChanged_ :: forall a. Facility a -> Signal ()
facilityCountChanged_ Facility a
r =
  forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Signal Int
facilityCountChanged Facility a
r

-- | Return the current capture count of the facility.
facilityCaptureCount :: Facility a -> Event Int
facilityCaptureCount :: forall a. Facility a -> Event Int
facilityCaptureCount Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Int
facilityCaptureCountRef Facility a
r)

-- | Signal triggered when the 'facilityCaptureCount' property changes.
facilityCaptureCountChanged :: Facility a -> Signal Int
facilityCaptureCountChanged :: forall a. Facility a -> Signal Int
facilityCaptureCountChanged Facility a
r =
  forall a. SignalSource a -> Signal a
publishSignal forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> SignalSource Int
facilityCaptureCountSource Facility a
r

-- | Signal triggered when the 'facilityCaptureCount' property changes.
facilityCaptureCountChanged_ :: Facility a -> Signal ()
facilityCaptureCountChanged_ :: forall a. Facility a -> Signal ()
facilityCaptureCountChanged_ Facility a
r =
  forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Signal Int
facilityCaptureCountChanged Facility a
r

-- | Return the current utilisation count of the facility.
facilityUtilisationCount :: Facility a -> Event Int
facilityUtilisationCount :: forall a. Facility a -> Event Int
facilityUtilisationCount Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Int
facilityUtilisationCountRef Facility a
r)

-- | Return the statistics for the utilisation count of the facility.
facilityUtilisationCountStats :: Facility a -> Event (TimingStats Int)
facilityUtilisationCountStats :: forall a. Facility a -> Event (TimingStats Int)
facilityUtilisationCountStats Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef (TimingStats Int)
facilityUtilisationCountStatsRef Facility a
r)

-- | Signal triggered when the 'facilityUtilisationCount' property changes.
facilityUtilisationCountChanged :: Facility a -> Signal Int
facilityUtilisationCountChanged :: forall a. Facility a -> Signal Int
facilityUtilisationCountChanged Facility a
r =
  forall a. SignalSource a -> Signal a
publishSignal forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> SignalSource Int
facilityUtilisationCountSource Facility a
r

-- | Signal triggered when the 'facilityUtilisationCount' property changes.
facilityUtilisationCountChanged_ :: Facility a -> Signal ()
facilityUtilisationCountChanged_ :: forall a. Facility a -> Signal ()
facilityUtilisationCountChanged_ Facility a
r =
  forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Signal Int
facilityUtilisationCountChanged Facility a
r

-- | Return the current queue length of the facility.
facilityQueueCount :: Facility a -> Event Int
facilityQueueCount :: forall a. Facility a -> Event Int
facilityQueueCount Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Int
facilityQueueCountRef Facility a
r)

-- | Return the statistics for the queue length of the facility.
facilityQueueCountStats :: Facility a -> Event (TimingStats Int)
facilityQueueCountStats :: forall a. Facility a -> Event (TimingStats Int)
facilityQueueCountStats Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef (TimingStats Int)
facilityQueueCountStatsRef Facility a
r)

-- | Signal triggered when the 'facilityQueueCount' property changes.
facilityQueueCountChanged :: Facility a -> Signal Int
facilityQueueCountChanged :: forall a. Facility a -> Signal Int
facilityQueueCountChanged Facility a
r =
  forall a. SignalSource a -> Signal a
publishSignal forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> SignalSource Int
facilityQueueCountSource Facility a
r

-- | Signal triggered when the 'facilityQueueCount' property changes.
facilityQueueCountChanged_ :: Facility a -> Signal ()
facilityQueueCountChanged_ :: forall a. Facility a -> Signal ()
facilityQueueCountChanged_ Facility a
r =
  forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Signal Int
facilityQueueCountChanged Facility a
r

-- | Return the total wait time of the facility.
facilityTotalWaitTime :: Facility a -> Event Double
facilityTotalWaitTime :: forall a. Facility a -> Event Double
facilityTotalWaitTime Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Double
facilityTotalWaitTimeRef Facility a
r)

-- | Return the statistics for the wait time of the facility.
facilityWaitTime :: Facility a -> Event (SamplingStats Double)
facilityWaitTime :: forall a. Facility a -> Event (SamplingStats Double)
facilityWaitTime Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef (SamplingStats Double)
facilityWaitTimeRef Facility a
r)

-- | Signal triggered when the 'facilityTotalWaitTime' and 'facilityWaitTime' properties change.
facilityWaitTimeChanged :: Facility a -> Signal (SamplingStats Double)
facilityWaitTimeChanged :: forall a. Facility a -> Signal (SamplingStats Double)
facilityWaitTimeChanged Facility a
r =
  forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (\() -> forall a. Facility a -> Event (SamplingStats Double)
facilityWaitTime Facility a
r) forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Signal ()
facilityWaitTimeChanged_ Facility a
r

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

-- | Return the total holding time of the facility.
facilityTotalHoldingTime :: Facility a -> Event Double
facilityTotalHoldingTime :: forall a. Facility a -> Event Double
facilityTotalHoldingTime Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Double
facilityTotalHoldingTimeRef Facility a
r)

-- | Return the statistics for the holding time of the facility.
facilityHoldingTime :: Facility a -> Event (SamplingStats Double)
facilityHoldingTime :: forall a. Facility a -> Event (SamplingStats Double)
facilityHoldingTime Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p -> forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef (SamplingStats Double)
facilityHoldingTimeRef Facility a
r)

-- | Signal triggered when the 'facilityTotalHoldingTime' and 'facilityHoldingTime' properties change.
facilityHoldingTimeChanged :: Facility a -> Signal (SamplingStats Double)
facilityHoldingTimeChanged :: forall a. Facility a -> Signal (SamplingStats Double)
facilityHoldingTimeChanged Facility a
r =
  forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (\() -> forall a. Facility a -> Event (SamplingStats Double)
facilityHoldingTime Facility a
r) forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Signal ()
facilityHoldingTimeChanged_ Facility a
r

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

-- | Whether the facility is currently interrupted.
facilityInterrupted :: Facility a -> Event Bool
facilityInterrupted :: forall a. Facility a -> Event Bool
facilityInterrupted Facility a
r =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Maybe (FacilityOwnerItem a)
x <- forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef (Maybe (FacilityOwnerItem a))
facilityOwnerRef Facility a
r)
     case Maybe (FacilityOwnerItem a)
x of
       Maybe (FacilityOwnerItem a)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
       Just FacilityOwnerItem a
a  -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. FacilityOwnerItem a -> Bool
ownerItemPreempting FacilityOwnerItem a
a)

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

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

-- | Preempt the facility.
preemptFacility :: Facility a
                   -- ^ the requested facility
                   -> Transact a
                   -- ^ the transact that tries to preempt the facility
                   -> FacilityPreemptMode a
                   -- ^ the Preempt mode
                   -> Process ()
preemptFacility :: forall a.
Facility a -> Transact a -> FacilityPreemptMode a -> Process ()
preemptFacility Facility a
r Transact a
transact FacilityPreemptMode a
mode =
  forall a. (ProcessId -> Cont a) -> Process a
Process forall a b. (a -> b) -> a -> b
$ \ProcessId
pid ->
  forall a. (ContParams a -> Event ()) -> Cont a
Cont forall a b. (a -> b) -> a -> b
$ \ContParams ()
c ->
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let t :: Double
t = Point -> Double
pointTime Point
p
     Maybe (FacilityOwnerItem a)
a <- forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef (Maybe (FacilityOwnerItem a))
facilityOwnerRef Facility a
r)
     case Maybe (FacilityOwnerItem a)
a of
       Maybe (FacilityOwnerItem a)
Nothing ->
         do forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef (Maybe (FacilityOwnerItem a))
facilityOwnerRef Facility a
r) forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (forall a.
Transact a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem a
FacilityOwnerItem Transact a
transact Double
t Bool
True Bool
False Double
0)
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Double -> Event ()
updateFacilityWaitTime Facility a
r Double
0
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Int -> Event ()
updateFacilityCount Facility a
r (-Int
1)
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Int -> Event ()
updateFacilityCaptureCount Facility a
r Int
1
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Int -> Event ()
updateFacilityUtilisationCount Facility a
r Int
1
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. ContParams a -> a -> Event ()
resumeCont ContParams ()
c ()
       Just owner :: FacilityOwnerItem a
owner@(FacilityOwnerItem Transact a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0)
         | (Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall a. FacilityPreemptMode a -> Bool
facilityPriorityMode FacilityPreemptMode a
mode) Bool -> Bool -> Bool
&& Bool
interrupting0 ->
         do FrozenCont ()
c <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
                 forall a. ContParams a -> a -> Event () -> Event (FrozenCont a)
freezeContReentering ContParams ()
c () forall a b. (a -> b) -> a -> b
$
                 forall a. ContParams a -> Cont a -> Event ()
invokeCont ContParams ()
c forall a b. (a -> b) -> a -> b
$
                 forall a. ProcessId -> Process a -> Cont a
invokeProcess ProcessId
pid forall a b. (a -> b) -> a -> b
$
                 forall a.
Facility a -> Transact a -> FacilityPreemptMode a -> Process ()
preemptFacility Facility a
r Transact a
transact FacilityPreemptMode a
mode
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
              forall s p i.
PriorityQueueStrategy s p =>
StrategyQueue s i -> p -> i -> Event ()
strategyEnqueueWithPriority
              (forall a.
Facility a
-> StrategyQueue
     (TransactQueueStrategy FCFS) (FacilityPendingItem a)
facilityPendingChain Facility a
r)
              (forall a. Transact a -> Int
transactPriority Transact a
transact)
              (forall a.
Transact a
-> Double -> Bool -> Bool -> FrozenCont () -> FacilityPendingItem a
FacilityPendingItem Transact a
transact Double
t Bool
True Bool
True FrozenCont ()
c)
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Int -> Event ()
updateFacilityQueueCount Facility a
r Int
1
       Just owner :: FacilityOwnerItem a
owner@(FacilityOwnerItem Transact a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0)
         | forall a. FacilityPreemptMode a -> Bool
facilityPriorityMode FacilityPreemptMode a
mode Bool -> Bool -> Bool
&& (forall a. Transact a -> Int
transactPriority Transact a
transact forall a. Ord a => a -> a -> Bool
<= forall a. Transact a -> Int
transactPriority Transact a
transact0) ->
         do FrozenCont ()
c <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
                 forall a. ContParams a -> a -> Event () -> Event (FrozenCont a)
freezeContReentering ContParams ()
c () forall a b. (a -> b) -> a -> b
$
                 forall a. ContParams a -> Cont a -> Event ()
invokeCont ContParams ()
c forall a b. (a -> b) -> a -> b
$
                 forall a. ProcessId -> Process a -> Cont a
invokeProcess ProcessId
pid forall a b. (a -> b) -> a -> b
$
                 forall a.
Facility a -> Transact a -> FacilityPreemptMode a -> Process ()
preemptFacility Facility a
r Transact a
transact FacilityPreemptMode a
mode
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
              forall s p i.
PriorityQueueStrategy s p =>
StrategyQueue s i -> p -> i -> Event ()
strategyEnqueueWithPriority
              (forall a.
Facility a
-> StrategyQueue
     (TransactQueueStrategy FCFS) (FacilityDelayedItem a)
facilityDelayChain Facility a
r)
              (forall a. Transact a -> Int
transactPriority Transact a
transact)
              (forall a.
Transact a
-> Double -> Bool -> Bool -> FrozenCont () -> FacilityDelayedItem a
FacilityDelayedItem Transact a
transact Double
t Bool
True Bool
True FrozenCont ()
c)
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Int -> Event ()
updateFacilityQueueCount Facility a
r Int
1
       Just owner :: FacilityOwnerItem a
owner@(FacilityOwnerItem Transact a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0)
         | (Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall a. FacilityPreemptMode a -> Bool
facilityRemoveMode FacilityPreemptMode a
mode) ->
         do forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef (Maybe (FacilityOwnerItem a))
facilityOwnerRef Facility a
r) forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (forall a.
Transact a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem a
FacilityOwnerItem Transact a
transact Double
t Bool
True Bool
True Double
0)
            ProcessId
pid0 <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Transact a -> Event ProcessId
requireTransactProcessId Transact a
transact0
            Maybe Double
t2   <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ ProcessId -> Event (Maybe Double)
processInterruptionTime ProcessId
pid0
            let dt0 :: Maybe Double
dt0 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Double
x -> Double
x forall a. Num a => a -> a -> a
- Double
t) Maybe Double
t2
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
              forall s p i.
PriorityQueueStrategy s p =>
StrategyQueue s i -> p -> i -> Event ()
strategyEnqueueWithPriority
              (forall a.
Facility a
-> StrategyQueue
     (TransactQueueStrategy LCFS) (FacilityInterruptedItem a)
facilityInterruptChain Facility a
r)
              (forall a. Transact a -> Int
transactPriority Transact a
transact0)
              (forall a.
Transact a
-> Double
-> Bool
-> Bool
-> Maybe Double
-> Maybe (FacilityPreemptTransfer a)
-> Double
-> FacilityInterruptedItem a
FacilityInterruptedItem Transact a
transact0 Double
t Bool
preempting0 Bool
interrupting0 Maybe Double
dt0 (forall a.
FacilityPreemptMode a -> Maybe (FacilityPreemptTransfer a)
facilityTransfer FacilityPreemptMode a
mode) (Double
acc0 forall a. Num a => a -> a -> a
+ (Double
t forall a. Num a => a -> a -> a
- Double
t0)))
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Int -> Event ()
updateFacilityQueueCount Facility a
r Int
1
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Double -> Event ()
updateFacilityWaitTime Facility a
r Double
0
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Int -> Event ()
updateFacilityCaptureCount Facility a
r Int
1
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Transact a -> Event ()
transactPreemptionBegin Transact a
transact0
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. ContParams a -> a -> Event ()
resumeCont ContParams ()
c ()
       Just owner :: FacilityOwnerItem a
owner@(FacilityOwnerItem Transact a
transact0 Double
t0 Bool
preempting0 Bool
interrupting0 Double
acc0)
         | forall a. FacilityPreemptMode a -> Bool
facilityRemoveMode FacilityPreemptMode a
mode ->
         do forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef (Maybe (FacilityOwnerItem a))
facilityOwnerRef Facility a
r) forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (forall a.
Transact a
-> Double -> Bool -> Bool -> Double -> FacilityOwnerItem a
FacilityOwnerItem Transact a
transact Double
t Bool
True Bool
True Double
0)
            ProcessId
pid0 <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Transact a -> Event ProcessId
requireTransactProcessId Transact a
transact0
            Maybe Double
t2   <- forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ ProcessId -> Event (Maybe Double)
processInterruptionTime ProcessId
pid0
            let dt0 :: Maybe Double
dt0 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Double
x -> Double
x forall a. Num a => a -> a -> a
- Double
t) Maybe Double
t2
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Double -> Event ()
updateFacilityWaitTime Facility a
r Double
0
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Int -> Event ()
updateFacilityCaptureCount Facility a
r Int
1
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Facility a -> Double -> Event ()
updateFacilityHoldingTime Facility a
r (Double
acc0 forall a. Num a => a -> a -> a
+ (Double
t forall a. Num a => a -> a -> a
- Double
t0))
            case forall a.
FacilityPreemptMode a -> Maybe (FacilityPreemptTransfer a)
facilityTransfer FacilityPreemptMode a
mode of
              Maybe (FacilityPreemptTransfer a)
Nothing ->
                forall e a. Exception e => e -> IO a
throwIO 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 a
transfer ->
                forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. Transact a -> Process () -> Event ()
transferTransact Transact a
transact0 (FacilityPreemptTransfer a
transfer Transact a
transact0 Maybe Double
dt0)
            forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$ forall a. ContParams a -> a -> Event ()
resumeCont ContParams ()
c ()

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

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

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

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

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

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

-- | Update the facility count and its statistics.
updateFacilityCount :: Facility a -> Int -> Event ()
updateFacilityCount :: forall a. Facility a -> Int -> Event ()
updateFacilityCount Facility a
r Int
delta =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
a <- forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Int
facilityCountRef Facility a
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     Int
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef Int
facilityCountRef Facility a
r) Int
a'
     forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall a. Facility a -> IORef (TimingStats Int)
facilityCountStatsRef Facility a
r) forall a b. (a -> b) -> a -> b
$
       forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
     forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
       forall a. SignalSource a -> a -> Event ()
triggerSignal (forall a. Facility a -> SignalSource Int
facilityCountSource Facility a
r) Int
a'

-- | Update the facility capture count.
updateFacilityCaptureCount :: Facility a -> Int -> Event ()
updateFacilityCaptureCount :: forall a. Facility a -> Int -> Event ()
updateFacilityCaptureCount Facility a
r Int
delta =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
a <- forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Int
facilityCaptureCountRef Facility a
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     Int
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef Int
facilityCaptureCountRef Facility a
r) Int
a'
     forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
       forall a. SignalSource a -> a -> Event ()
triggerSignal (forall a. Facility a -> SignalSource Int
facilityCaptureCountSource Facility a
r) Int
a'

-- | Update the facility queue length and its statistics.
updateFacilityQueueCount :: Facility a -> Int -> Event ()
updateFacilityQueueCount :: forall a. Facility a -> Int -> Event ()
updateFacilityQueueCount Facility a
r Int
delta =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
a <- forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Int
facilityQueueCountRef Facility a
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     Int
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef Int
facilityQueueCountRef Facility a
r) Int
a'
     forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall a. Facility a -> IORef (TimingStats Int)
facilityQueueCountStatsRef Facility a
r) forall a b. (a -> b) -> a -> b
$
       forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
     forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
       forall a. SignalSource a -> a -> Event ()
triggerSignal (forall a. Facility a -> SignalSource Int
facilityQueueCountSource Facility a
r) Int
a'

-- | Update the facility utilisation count and its statistics.
updateFacilityUtilisationCount :: Facility a -> Int -> Event ()
updateFacilityUtilisationCount :: forall a. Facility a -> Int -> Event ()
updateFacilityUtilisationCount Facility a
r Int
delta =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
a <- forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Int
facilityUtilisationCountRef Facility a
r)
     let a' :: Int
a' = Int
a forall a. Num a => a -> a -> a
+ Int
delta
     Int
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef Int
facilityUtilisationCountRef Facility a
r) Int
a'
     forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall a. Facility a -> IORef (TimingStats Int)
facilityUtilisationCountStatsRef Facility a
r) forall a b. (a -> b) -> a -> b
$
       forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats (Point -> Double
pointTime Point
p) Int
a'
     forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
       forall a. SignalSource a -> a -> Event ()
triggerSignal (forall a. Facility a -> SignalSource Int
facilityUtilisationCountSource Facility a
r) Int
a'

-- | Update the facility wait time and its statistics.
updateFacilityWaitTime :: Facility a -> Double -> Event ()
updateFacilityWaitTime :: forall a. Facility a -> Double -> Event ()
updateFacilityWaitTime Facility a
r Double
delta =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Double
a <- forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Double
facilityTotalWaitTimeRef Facility a
r)
     let a' :: Double
a' = Double
a forall a. Num a => a -> a -> a
+ Double
delta
     Double
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef Double
facilityTotalWaitTimeRef Facility a
r) Double
a'
     forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall a. Facility a -> IORef (SamplingStats Double)
facilityWaitTimeRef Facility a
r) forall a b. (a -> b) -> a -> b
$
       forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
delta
     forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
       forall a. SignalSource a -> a -> Event ()
triggerSignal (forall a. Facility a -> SignalSource ()
facilityWaitTimeSource Facility a
r) ()

-- | Update the facility holding time and its statistics.
updateFacilityHoldingTime :: Facility a -> Double -> Event ()
updateFacilityHoldingTime :: forall a. Facility a -> Double -> Event ()
updateFacilityHoldingTime Facility a
r Double
delta =
  forall a. (Point -> IO a) -> Event a
Event forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Double
a <- forall a. IORef a -> IO a
readIORef (forall a. Facility a -> IORef Double
facilityTotalHoldingTimeRef Facility a
r)
     let a' :: Double
a' = Double
a forall a. Num a => a -> a -> a
+ Double
delta
     Double
a' seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef (forall a. Facility a -> IORef Double
facilityTotalHoldingTimeRef Facility a
r) Double
a'
     forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (forall a. Facility a -> IORef (SamplingStats Double)
facilityHoldingTimeRef Facility a
r) forall a b. (a -> b) -> a -> b
$
       forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats Double
delta
     forall a. Point -> Event a -> IO a
invokeEvent Point
p forall a b. (a -> b) -> a -> b
$
       forall a. SignalSource a -> a -> Event ()
triggerSignal (forall a. Facility a -> SignalSource ()
facilityHoldingTimeSource Facility a
r) ()

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