module Simulation.Aivika.Trans.GPSS.MatchChain
(MatchChain,
newMatchChain,
matchTransact,
transactMatching,
transactMatchingChanged,
transactMatchingChangedByTransact_,
transactMatchingChangedByAssemblySet_) where
import Control.Monad
import Control.Monad.Trans
import qualified Data.HashMap.Lazy as HM
import Simulation.Aivika.Trans
import Simulation.Aivika.Trans.GPSS.Transact
import Simulation.Aivika.Trans.GPSS.AssemblySet
data MatchChain m =
MatchChain { forall (m :: * -> *).
MatchChain m -> Ref m (HashMap (AssemblySet m) (ProcessId m))
matchChainMap :: Ref m (HM.HashMap (AssemblySet m) (ProcessId m)),
forall (m :: * -> *).
MatchChain m -> SignalSource m (AssemblySet m)
matchChainSource :: SignalSource m (AssemblySet m)
}
newMatchChain :: MonadDES m => Simulation m (MatchChain m)
{-# INLINABLE newMatchChain #-}
newMatchChain :: forall (m :: * -> *). MonadDES m => Simulation m (MatchChain m)
newMatchChain =
do Ref m (HashMap (AssemblySet m) (ProcessId m))
map <- forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef forall k v. HashMap k v
HM.empty
SignalSource m (AssemblySet m)
src <- forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
forall (m :: * -> *) a. Monad m => a -> m a
return MatchChain { matchChainMap :: Ref m (HashMap (AssemblySet m) (ProcessId m))
matchChainMap = Ref m (HashMap (AssemblySet m) (ProcessId m))
map,
matchChainSource :: SignalSource m (AssemblySet m)
matchChainSource = SignalSource m (AssemblySet m)
src
}
matchTransact :: MonadDES m => MatchChain m -> Transact m a -> Process m ()
{-# INLINABLE matchTransact #-}
matchTransact :: forall (m :: * -> *) a.
MonadDES m =>
MatchChain m -> Transact m a -> Process m ()
matchTransact MatchChain m
chain Transact m a
t =
do (HashMap (AssemblySet m) (ProcessId m)
map, AssemblySet m
set) <-
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
do HashMap (AssemblySet m) (ProcessId m)
map <- forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *).
MatchChain m -> Ref m (HashMap (AssemblySet m) (ProcessId m))
matchChainMap MatchChain m
chain)
AssemblySet m
set <- forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Event m (AssemblySet m)
transactAssemblySet Transact m a
t
forall (m :: * -> *) a. Monad m => a -> m a
return (HashMap (AssemblySet m) (ProcessId m)
map, AssemblySet m
set)
case forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup AssemblySet m
set HashMap (AssemblySet m) (ProcessId m)
map of
Just ProcessId m
pid ->
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
do forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *).
MatchChain m -> Ref m (HashMap (AssemblySet m) (ProcessId m))
matchChainMap MatchChain m
chain) forall a b. (a -> b) -> a -> b
$
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
HM.delete AssemblySet m
set
forall (m :: * -> *). MonadDES m => Event m () -> Event m ()
yieldEvent forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *).
MatchChain m -> SignalSource m (AssemblySet m)
matchChainSource MatchChain m
chain) AssemblySet m
set
forall (m :: * -> *). MonadDES m => ProcessId m -> Event m ()
reactivateProcess ProcessId m
pid
Maybe (ProcessId m)
Nothing ->
do forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
do ProcessId m
pid <- forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Event m (ProcessId m)
requireTransactProcessId Transact m a
t
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *).
MatchChain m -> Ref m (HashMap (AssemblySet m) (ProcessId m))
matchChainMap MatchChain m
chain) forall a b. (a -> b) -> a -> b
$
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert AssemblySet m
set ProcessId m
pid
forall (m :: * -> *). MonadDES m => Event m () -> Event m ()
yieldEvent forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *).
MatchChain m -> SignalSource m (AssemblySet m)
matchChainSource MatchChain m
chain) AssemblySet m
set
forall (m :: * -> *). MonadDES m => Process m ()
passivateProcess
transactMatching :: MonadDES m => MatchChain m -> AssemblySet m -> Event m Bool
{-# INLINABLE transactMatching #-}
transactMatching :: forall (m :: * -> *).
MonadDES m =>
MatchChain m -> AssemblySet m -> Event m Bool
transactMatching MatchChain m
chain AssemblySet m
set =
do HashMap (AssemblySet m) (ProcessId m)
map <- forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *).
MatchChain m -> Ref m (HashMap (AssemblySet m) (ProcessId m))
matchChainMap MatchChain m
chain)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall k a. (Eq k, Hashable k) => k -> HashMap k a -> Bool
HM.member AssemblySet m
set HashMap (AssemblySet m) (ProcessId m)
map)
transactMatchingChangedByAssemblySet_ :: MonadDES m => MatchChain m -> AssemblySet m -> Signal m ()
{-# INLINABLE transactMatchingChangedByAssemblySet_ #-}
transactMatchingChangedByAssemblySet_ :: forall (m :: * -> *).
MonadDES m =>
MatchChain m -> AssemblySet m -> Signal m ()
transactMatchingChangedByAssemblySet_ MatchChain m
chain AssemblySet m
set =
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a.
MonadDES m =>
(a -> Bool) -> Signal m a -> Signal m a
filterSignal (forall a. Eq a => a -> a -> Bool
== AssemblySet m
set) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadDES m =>
MatchChain m -> Signal m (AssemblySet m)
transactMatchingChanged MatchChain m
chain
transactMatchingChangedByTransact_ :: MonadDES m => MatchChain m -> Transact m a -> Signal m ()
{-# INLINABLE transactMatchingChangedByTransact_ #-}
transactMatchingChangedByTransact_ :: forall (m :: * -> *) a.
MonadDES m =>
MatchChain m -> Transact m a -> Signal m ()
transactMatchingChangedByTransact_ MatchChain m
chain Transact m a
t =
forall (m :: * -> *) a b.
MonadDES m =>
(a -> b) -> Signal m a -> Signal m b
mapSignal (forall a b. a -> b -> a
const ()) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a.
MonadDES m =>
(a -> Event m Bool) -> Signal m a -> Signal m a
filterSignalM AssemblySet m -> Event m Bool
pred forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadDES m =>
MatchChain m -> Signal m (AssemblySet m)
transactMatchingChanged MatchChain m
chain
where pred :: AssemblySet m -> Event m Bool
pred AssemblySet m
set =
do AssemblySet m
set' <- forall (m :: * -> *) a.
MonadDES m =>
Transact m a -> Event m (AssemblySet m)
transactAssemblySet Transact m a
t
forall (m :: * -> *) a. Monad m => a -> m a
return (AssemblySet m
set forall a. Eq a => a -> a -> Bool
== AssemblySet m
set')
transactMatchingChanged :: MonadDES m => MatchChain m -> Signal m (AssemblySet m)
{-# INLINABLE transactMatchingChanged #-}
transactMatchingChanged :: forall (m :: * -> *).
MonadDES m =>
MatchChain m -> Signal m (AssemblySet m)
transactMatchingChanged MatchChain m
chain =
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (forall (m :: * -> *).
MatchChain m -> SignalSource m (AssemblySet m)
matchChainSource MatchChain m
chain)