module Simulation.Aivika.GPSS.Block.Terminate
(terminateBlock,
terminateBlockByCount,
terminateBlockByCountM) where
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika
import Simulation.Aivika.GPSS.Block
terminateBlock :: Block a ()
terminateBlock =
Block { blockProcess = \a -> return () }
terminateBlockByCountM :: Ref Int
-> Event Int
-> Block a ()
terminateBlockByCountM counter decrement =
Block { blockProcess = \a -> action }
where
action =
liftEvent $
do i <- decrement
n <- readRef counter
let n' = n i
n' `seq` writeRef counter n'
when (n' <= 0) $
throwEvent $
SimulationAbort "Terminated by exceeding the counter"
terminateBlockByCount :: Ref Int
-> Int
-> Block a ()
terminateBlockByCount counter i =
Block { blockProcess = \a -> action }
where
action =
liftEvent $
do n <- readRef counter
let n' = n i
n' `seq` writeRef counter n'
when (n' <= 0) $
throwEvent $
SimulationAbort "Terminated by exceeding the counter"