module Simulation.Aivika.Trans.Agent
(Agent,
AgentState,
newAgent,
newState,
newSubstate,
selectedState,
selectedStateChanged,
selectedStateChanged_,
selectState,
stateAgent,
stateParent,
addTimeout,
addTimer,
setStateActivation,
setStateDeactivation,
setStateTransition) where
import Control.Monad
import Simulation.Aivika.Trans.Ref.Base
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Event
import Simulation.Aivika.Trans.Signal
data Agent m = Agent { forall (m :: * -> *). Agent m -> Ref m AgentMode
agentModeRef :: Ref m AgentMode,
forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef :: Ref m (Maybe (AgentState m)),
forall (m :: * -> *).
Agent m -> SignalSource m (Maybe (AgentState m))
agentStateChangedSource :: SignalSource m (Maybe (AgentState m)) }
data AgentState m = AgentState { forall (m :: * -> *). AgentState m -> Agent m
stateAgent :: Agent m,
forall (m :: * -> *). AgentState m -> Maybe (AgentState m)
stateParent :: Maybe (AgentState m),
forall (m :: * -> *). AgentState m -> Ref m (Event m ())
stateActivateRef :: Ref m (Event m ()),
forall (m :: * -> *). AgentState m -> Ref m (Event m ())
stateDeactivateRef :: Ref m (Event m ()),
forall (m :: * -> *).
AgentState m -> Ref m (Event m (Maybe (AgentState m)))
stateTransitRef :: Ref m (Event m (Maybe (AgentState m))),
forall (m :: * -> *). AgentState m -> Ref m Int
stateVersionRef :: Ref m Int }
data AgentMode = CreationMode
| TransientMode
| ProcessingMode
instance MonadDES m => Eq (Agent m) where
{-# INLINE (==) #-}
Agent m
x == :: Agent m -> Agent m -> Bool
== Agent m
y = forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef Agent m
x forall a. Eq a => a -> a -> Bool
== forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef Agent m
y
instance MonadDES m => Eq (AgentState m) where
{-# INLINE (==) #-}
AgentState m
x == :: AgentState m -> AgentState m -> Bool
== AgentState m
y = forall (m :: * -> *). AgentState m -> Ref m Int
stateVersionRef AgentState m
x forall a. Eq a => a -> a -> Bool
== forall (m :: * -> *). AgentState m -> Ref m Int
stateVersionRef AgentState m
y
fullPath :: AgentState m -> [AgentState m] -> [AgentState m]
fullPath :: forall (m :: * -> *).
AgentState m -> [AgentState m] -> [AgentState m]
fullPath AgentState m
st [AgentState m]
acc =
case forall (m :: * -> *). AgentState m -> Maybe (AgentState m)
stateParent AgentState m
st of
Maybe (AgentState m)
Nothing -> AgentState m
st forall a. a -> [a] -> [a]
: [AgentState m]
acc
Just AgentState m
st' -> forall (m :: * -> *).
AgentState m -> [AgentState m] -> [AgentState m]
fullPath AgentState m
st' (AgentState m
st forall a. a -> [a] -> [a]
: [AgentState m]
acc)
partitionPath :: MonadDES m => [AgentState m] -> [AgentState m] -> ([AgentState m], [AgentState m])
{-# INLINABLE partitionPath #-}
partitionPath :: forall (m :: * -> *).
MonadDES m =>
[AgentState m]
-> [AgentState m] -> ([AgentState m], [AgentState m])
partitionPath [AgentState m]
path1 [AgentState m]
path2 =
case ([AgentState m]
path1, [AgentState m]
path2) of
(AgentState m
h1 : [AgentState m]
t1, [AgentState m
h2]) | AgentState m
h1 forall a. Eq a => a -> a -> Bool
== AgentState m
h2 ->
(forall a. [a] -> [a]
reverse [AgentState m]
path1, [AgentState m]
path2)
(AgentState m
h1 : [AgentState m]
t1, AgentState m
h2 : [AgentState m]
t2) | AgentState m
h1 forall a. Eq a => a -> a -> Bool
== AgentState m
h2 ->
forall (m :: * -> *).
MonadDES m =>
[AgentState m]
-> [AgentState m] -> ([AgentState m], [AgentState m])
partitionPath [AgentState m]
t1 [AgentState m]
t2
([AgentState m], [AgentState m])
_ ->
(forall a. [a] -> [a]
reverse [AgentState m]
path1, [AgentState m]
path2)
findPath :: MonadDES m => Maybe (AgentState m) -> AgentState m -> ([AgentState m], [AgentState m])
{-# INLINABLE findPath #-}
findPath :: forall (m :: * -> *).
MonadDES m =>
Maybe (AgentState m)
-> AgentState m -> ([AgentState m], [AgentState m])
findPath Maybe (AgentState m)
Nothing AgentState m
target = ([], forall (m :: * -> *).
AgentState m -> [AgentState m] -> [AgentState m]
fullPath AgentState m
target [])
findPath (Just AgentState m
source) AgentState m
target
| forall (m :: * -> *). AgentState m -> Agent m
stateAgent AgentState m
source forall a. Eq a => a -> a -> Bool
/= forall (m :: * -> *). AgentState m -> Agent m
stateAgent AgentState m
target =
forall a. HasCallStack => [Char] -> a
error [Char]
"Different agents: findPath."
| Bool
otherwise =
forall (m :: * -> *).
MonadDES m =>
[AgentState m]
-> [AgentState m] -> ([AgentState m], [AgentState m])
partitionPath [AgentState m]
path1 [AgentState m]
path2
where
path1 :: [AgentState m]
path1 = forall (m :: * -> *).
AgentState m -> [AgentState m] -> [AgentState m]
fullPath AgentState m
source []
path2 :: [AgentState m]
path2 = forall (m :: * -> *).
AgentState m -> [AgentState m] -> [AgentState m]
fullPath AgentState m
target []
traversePath :: MonadDES m => Maybe (AgentState m) -> AgentState m -> Event m ()
{-# INLINABLE traversePath #-}
traversePath :: forall (m :: * -> *).
MonadDES m =>
Maybe (AgentState m) -> AgentState m -> Event m ()
traversePath Maybe (AgentState m)
source AgentState m
target =
let ([AgentState m]
path1, [AgentState m]
path2) = forall (m :: * -> *).
MonadDES m =>
Maybe (AgentState m)
-> AgentState m -> ([AgentState m], [AgentState m])
findPath Maybe (AgentState m)
source AgentState m
target
agent :: Agent m
agent = forall (m :: * -> *). AgentState m -> Agent m
stateAgent AgentState m
target
activate :: AgentState m -> Point m -> m ()
activate AgentState m
st Point m
p = forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). AgentState m -> Ref m (Event m ())
stateActivateRef AgentState m
st))
deactivate :: AgentState m -> Point m -> m ()
deactivate AgentState m
st Point m
p = forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). AgentState m -> Ref m (Event m ())
stateDeactivateRef AgentState m
st))
transit :: AgentState m -> Point m -> m (Maybe (AgentState m))
transit AgentState m
st Point m
p = forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *).
AgentState m -> Ref m (Event m (Maybe (AgentState m)))
stateTransitRef AgentState m
st))
continue :: AgentState m -> Point m -> m ()
continue AgentState m
st Point m
p = forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadDES m =>
Maybe (AgentState m) -> AgentState m -> Event m ()
traversePath (forall a. a -> Maybe a
Just AgentState m
target) AgentState m
st
in forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [AgentState m]
path1 Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => t a -> Bool
null [AgentState m]
path2) forall a b. (a -> b) -> a -> b
$
do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Agent m -> Ref m AgentMode
agentModeRef Agent m
agent) AgentMode
TransientMode
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [AgentState m]
path1 forall a b. (a -> b) -> a -> b
$ \AgentState m
st ->
do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef Agent m
agent) (forall a. a -> Maybe a
Just AgentState m
st)
forall {m :: * -> *}. MonadRef m => AgentState m -> Point m -> m ()
deactivate AgentState m
st Point m
p
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
MonadRef m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *). AgentState m -> Ref m Int
stateVersionRef AgentState m
st) (Int
1 forall a. Num a => a -> a -> a
+)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [AgentState m]
path2 forall a b. (a -> b) -> a -> b
$ \AgentState m
st ->
do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef Agent m
agent) (forall a. a -> Maybe a
Just AgentState m
st)
forall {m :: * -> *}. MonadRef m => AgentState m -> Point m -> m ()
activate AgentState m
st Point m
p
Maybe (AgentState m)
st' <- forall {m :: * -> *}.
MonadRef m =>
AgentState m -> Point m -> m (Maybe (AgentState m))
transit AgentState m
target Point m
p
case Maybe (AgentState m)
st' of
Maybe (AgentState m)
Nothing ->
do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Agent m -> Ref m AgentMode
agentModeRef Agent m
agent) AgentMode
ProcessingMode
forall (m :: * -> *). MonadDES m => Point m -> Agent m -> m ()
triggerAgentStateChanged Point m
p Agent m
agent
Just AgentState m
st' ->
AgentState m -> Point m -> m ()
continue AgentState m
st' Point m
p
addTimeout :: MonadDES m => AgentState m -> Double -> Event m () -> Event m ()
{-# INLINABLE addTimeout #-}
addTimeout :: forall (m :: * -> *).
MonadDES m =>
AgentState m -> Double -> Event m () -> Event m ()
addTimeout AgentState m
st Double
dt Event m ()
action =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Int
v <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). AgentState m -> Ref m Int
stateVersionRef AgentState m
st)
let m1 :: Event m ()
m1 = forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Int
v' <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). AgentState m -> Ref m Int
stateVersionRef AgentState m
st)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
v forall a. Eq a => a -> a -> Bool
== Int
v') forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p Event m ()
action
m2 :: Event m ()
m2 = forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent (forall (m :: * -> *). Point m -> Double
pointTime Point m
p forall a. Num a => a -> a -> a
+ Double
dt) Event m ()
m1
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p Event m ()
m2
addTimer :: MonadDES m => AgentState m -> Event m Double -> Event m () -> Event m ()
{-# INLINABLE addTimer #-}
addTimer :: forall (m :: * -> *).
MonadDES m =>
AgentState m -> Event m Double -> Event m () -> Event m ()
addTimer AgentState m
st Event m Double
dt Event m ()
action =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Int
v <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). AgentState m -> Ref m Int
stateVersionRef AgentState m
st)
let m1 :: Event m ()
m1 = forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Int
v' <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). AgentState m -> Ref m Int
stateVersionRef AgentState m
st)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
v forall a. Eq a => a -> a -> Bool
== Int
v') forall a b. (a -> b) -> a -> b
$
do forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p Event m ()
m2
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p Event m ()
action
m2 :: Event m ()
m2 = forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do Double
dt' <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p Event m Double
dt
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
EventQueueing m =>
Double -> Event m () -> Event m ()
enqueueEvent (forall (m :: * -> *). Point m -> Double
pointTime Point m
p forall a. Num a => a -> a -> a
+ Double
dt') Event m ()
m1
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p Event m ()
m2
newState :: MonadDES m => Agent m -> Simulation m (AgentState m)
{-# INLINABLE newState #-}
newState :: forall (m :: * -> *).
MonadDES m =>
Agent m -> Simulation m (AgentState m)
newState Agent m
agent =
do Ref m (Event m ())
aref <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()
Ref m (Event m ())
dref <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()
Ref m (Event m (Maybe (AgentState m)))
tref <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Ref m Int
vref <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Int
0
forall (m :: * -> *) a. Monad m => a -> m a
return AgentState { stateAgent :: Agent m
stateAgent = Agent m
agent,
stateParent :: Maybe (AgentState m)
stateParent = forall a. Maybe a
Nothing,
stateActivateRef :: Ref m (Event m ())
stateActivateRef = Ref m (Event m ())
aref,
stateDeactivateRef :: Ref m (Event m ())
stateDeactivateRef = Ref m (Event m ())
dref,
stateTransitRef :: Ref m (Event m (Maybe (AgentState m)))
stateTransitRef = Ref m (Event m (Maybe (AgentState m)))
tref,
stateVersionRef :: Ref m Int
stateVersionRef = Ref m Int
vref }
newSubstate :: MonadDES m => AgentState m -> Simulation m (AgentState m)
{-# INLINABLE newSubstate #-}
newSubstate :: forall (m :: * -> *).
MonadDES m =>
AgentState m -> Simulation m (AgentState m)
newSubstate AgentState m
parent =
do let agent :: Agent m
agent = forall (m :: * -> *). AgentState m -> Agent m
stateAgent AgentState m
parent
Ref m (Event m ())
aref <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()
Ref m (Event m ())
dref <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()
Ref m (Event m (Maybe (AgentState m)))
tref <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Ref m Int
vref <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef Int
0
forall (m :: * -> *) a. Monad m => a -> m a
return AgentState { stateAgent :: Agent m
stateAgent = Agent m
agent,
stateParent :: Maybe (AgentState m)
stateParent = forall a. a -> Maybe a
Just AgentState m
parent,
stateActivateRef :: Ref m (Event m ())
stateActivateRef= Ref m (Event m ())
aref,
stateDeactivateRef :: Ref m (Event m ())
stateDeactivateRef = Ref m (Event m ())
dref,
stateTransitRef :: Ref m (Event m (Maybe (AgentState m)))
stateTransitRef = Ref m (Event m (Maybe (AgentState m)))
tref,
stateVersionRef :: Ref m Int
stateVersionRef = Ref m Int
vref }
newAgent :: MonadDES m => Simulation m (Agent m)
{-# INLINABLE newAgent #-}
newAgent :: forall (m :: * -> *). MonadDES m => Simulation m (Agent m)
newAgent =
do Ref m AgentMode
modeRef <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef AgentMode
CreationMode
Ref m (Maybe (AgentState m))
stateRef <- forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef forall a. Maybe a
Nothing
SignalSource m (Maybe (AgentState m))
stateChangedSource <- forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
forall (m :: * -> *) a. Monad m => a -> m a
return Agent { agentModeRef :: Ref m AgentMode
agentModeRef = Ref m AgentMode
modeRef,
agentStateRef :: Ref m (Maybe (AgentState m))
agentStateRef = Ref m (Maybe (AgentState m))
stateRef,
agentStateChangedSource :: SignalSource m (Maybe (AgentState m))
agentStateChangedSource = SignalSource m (Maybe (AgentState m))
stateChangedSource }
selectedState :: MonadDES m => Agent m -> Event m (Maybe (AgentState m))
{-# INLINABLE selectedState #-}
selectedState :: forall (m :: * -> *).
MonadDES m =>
Agent m -> Event m (Maybe (AgentState m))
selectedState Agent m
agent = forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef Agent m
agent)
selectState :: MonadDES m => AgentState m -> Event m ()
{-# INLINABLE selectState #-}
selectState :: forall (m :: * -> *). MonadDES m => AgentState m -> Event m ()
selectState AgentState m
st =
forall (m :: * -> *) a. (Point m -> m a) -> Event m a
Event forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do let agent :: Agent m
agent = forall (m :: * -> *). AgentState m -> Agent m
stateAgent AgentState m
st
AgentMode
mode <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Agent m -> Ref m AgentMode
agentModeRef Agent m
agent)
case AgentMode
mode of
AgentMode
CreationMode ->
do Maybe (AgentState m)
x0 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef Agent m
agent)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadDES m =>
Maybe (AgentState m) -> AgentState m -> Event m ()
traversePath Maybe (AgentState m)
x0 AgentState m
st
AgentMode
TransientMode ->
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"Use the setStateTransition function to define " forall a. [a] -> [a] -> [a]
++
[Char]
"the transition state: activateState."
AgentMode
ProcessingMode ->
do Maybe (AgentState m)
x0 <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef Agent m
agent)
case Maybe (AgentState m)
x0 of
Just AgentState m
st0 -> forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadDES m =>
Maybe (AgentState m) -> AgentState m -> Event m ()
traversePath Maybe (AgentState m)
x0 AgentState m
st
Maybe (AgentState m)
Nothing -> forall a. HasCallStack => [Char] -> a
error [Char]
"Pattern match failed: selectState"
setStateActivation :: MonadDES m => AgentState m -> Event m () -> Event m ()
{-# INLINABLE setStateActivation #-}
setStateActivation :: forall (m :: * -> *).
MonadDES m =>
AgentState m -> Event m () -> Event m ()
setStateActivation AgentState m
st Event m ()
action =
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). AgentState m -> Ref m (Event m ())
stateActivateRef AgentState m
st) Event m ()
action
setStateDeactivation :: MonadDES m => AgentState m -> Event m () -> Event m ()
{-# INLINABLE setStateDeactivation #-}
setStateDeactivation :: forall (m :: * -> *).
MonadDES m =>
AgentState m -> Event m () -> Event m ()
setStateDeactivation AgentState m
st Event m ()
action =
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). AgentState m -> Ref m (Event m ())
stateDeactivateRef AgentState m
st) Event m ()
action
setStateTransition :: MonadDES m => AgentState m -> Event m (Maybe (AgentState m)) -> Event m ()
{-# INLINABLE setStateTransition #-}
setStateTransition :: forall (m :: * -> *).
MonadDES m =>
AgentState m -> Event m (Maybe (AgentState m)) -> Event m ()
setStateTransition AgentState m
st Event m (Maybe (AgentState m))
action =
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *).
AgentState m -> Ref m (Event m (Maybe (AgentState m)))
stateTransitRef AgentState m
st) Event m (Maybe (AgentState m))
action
triggerAgentStateChanged :: MonadDES m => Point m -> Agent m -> m ()
{-# INLINABLE triggerAgentStateChanged #-}
triggerAgentStateChanged :: forall (m :: * -> *). MonadDES m => Point m -> Agent m -> m ()
triggerAgentStateChanged Point m
p Agent m
agent =
do Maybe (AgentState m)
st <- forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Agent m -> Ref m (Maybe (AgentState m))
agentStateRef Agent m
agent)
forall (m :: * -> *) a. Point m -> Event m a -> m a
invokeEvent Point m
p forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal (forall (m :: * -> *).
Agent m -> SignalSource m (Maybe (AgentState m))
agentStateChangedSource Agent m
agent) Maybe (AgentState m)
st
selectedStateChanged :: Agent m -> Signal m (Maybe (AgentState m))
{-# INLINABLE selectedStateChanged #-}
selectedStateChanged :: forall (m :: * -> *). Agent m -> Signal m (Maybe (AgentState m))
selectedStateChanged Agent m
agent =
forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal (forall (m :: * -> *).
Agent m -> SignalSource m (Maybe (AgentState m))
agentStateChangedSource Agent m
agent)
selectedStateChanged_ :: MonadDES m => Agent m -> Signal m ()
{-# INLINABLE selectedStateChanged_ #-}
selectedStateChanged_ :: forall (m :: * -> *). MonadDES m => Agent m -> Signal m ()
selectedStateChanged_ Agent m
agent =
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 :: * -> *). Agent m -> Signal m (Maybe (AgentState m))
selectedStateChanged Agent m
agent