@@ -20,19 +20,22 @@ import Control.Distributed.Process
2020 , handleMessage
2121 , handleMessageIf
2222 , wrapMessage
23+ , die
2324 )
2425import qualified Control.Distributed.Process as P
2526 ( liftIO
2627 , Message
2728 )
28- import Control.Distributed.Process.Extras (ExitReason )
29+ import Control.Distributed.Process.Extras (ExitReason ( .. ) )
2930import Control.Distributed.Process.ManagedProcess
3031 ( Action
3132 , GenProcess
3233 , continue
3334 , stopWith
3435 , setProcessState
36+ , processState
3537 )
38+ import qualified Control.Distributed.Process.ManagedProcess.Internal.Types as MP (lift , liftIO )
3639import Control.Distributed.Process.ManagedProcess.Server.Priority
3740 ( push
3841 , act
@@ -62,16 +65,23 @@ import Data.Typeable (Typeable, typeOf)
6265import Data.Tuple (swap , uncurry )
6366-- import GHC.Generics
6467
65- data FsmState s d = FsmState { fsmName :: s
66- , fsmData :: d
67- , fsmProg :: Step s d
68- }
68+ data State s d = State { stName :: s
69+ , stData :: d
70+ , stProg :: Step s d
71+ , stInput :: Maybe P. Message
72+ , stReply :: (P. Message -> Process () )
73+ , stTrans :: Seq (Transition s d )
74+ }
75+
76+ instance forall s d . (Show s ) => Show (State s d ) where
77+ show State {.. } = " State{stName=" ++ (show stName)
78+ ++ " , stTrans=" ++ (show stTrans) ++ " }"
6979
7080data Transition s d = Remain
7181 | PutBack
7282 | Enter s
7383 | Stop ExitReason
74- | Eval (GenProcess (FsmState s d ) () )
84+ | Eval (GenProcess (State s d ) () )
7585
7686instance forall s d . (Show s ) => Show (Transition s d ) where
7787 show Remain = " Remain"
@@ -89,6 +99,7 @@ instance forall m . (Typeable m) => Show (Event m) where
8999 show ev = show $ typeOf ev
90100
91101data Step s d where
102+ Init :: Step s d -> Step s d -> Step s d
92103 Yield :: s -> d -> Step s d
93104 Await :: (Serializable m ) => Event m -> Step s d -> Step s d
94105 Always :: (Serializable m ) => (m -> FSM s d (Transition s d )) -> Step s d
@@ -100,28 +111,16 @@ data Step s d where
100111
101112instance forall s d . (Show s ) => Show (Step s d ) where
102113 show st
114+ | Init _ _ <- st = " Init"
103115 | Yield _ _ <- st = " Yield"
104- | Await _ s <- st = " Await (_" ++ (show s) ++ " )"
116+ | Await _ s <- st = " Await (_ " ++ (show s) ++ " )"
105117 | Always _ <- st = " Always _"
106118 | Perhaps s _ <- st = " Perhaps (" ++ (show s) ++ " )"
107119 | Matching _ _ <- st = " Matching _ _"
108- | Sequence a b <- st = " Sequence [" ++ (show a) ++ " |> " ++ (show b) ++ " ) "
109- | Alternate a b <- st = " Alternate [" ++ (show a) ++ " .| " ++ (show b) ++ " ) "
120+ | Sequence a b <- st = " Sequence [" ++ (show a) ++ " |> " ++ (show b) ++ " ] "
121+ | Alternate a b <- st = " Alternate [" ++ (show a) ++ " .| " ++ (show b) ++ " ] "
110122 | Reply _ <- st = " Reply"
111123
112- -- instance forall s d (Show s) => Show (Step s d)
113-
114- data State s d = State { stName :: s
115- , stData :: d
116- , stInput :: P. Message
117- , stReply :: (P. Message -> Process () )
118- , stTrans :: Seq (Transition s d )
119- }
120-
121- instance forall s d . (Show s ) => Show (State s d ) where
122- show State {.. } = " State{stName=" ++ (show stName)
123- ++ " stTrans" ++ (show stTrans)
124-
125124newtype FSM s d o = FSM {
126125 unFSM :: ST. StateT (State s d ) Process o
127126 }
@@ -180,11 +179,18 @@ getR s =
180179
181180enqueue :: State s d -> Maybe (Transition s d ) -> Maybe (State s d )
182181enqueue st@ State {.. } trans
183- | isJust trans = Just $ st { stTrans = seqEnqueue stTrans (fromJust trans) }
182+ | isJust trans = Just $ st { stTrans = seqPush stTrans (fromJust trans) }
184183 | otherwise = Nothing
185184
186- apply :: State s d -> P. Message -> Step s d -> Process (Maybe (State s d ))
187- apply st@ State {.. } msg step
185+ apply :: (Show s ) => State s d -> P. Message -> Step s d -> Process (Maybe (State s d ))
186+ apply st msg step
187+ | Init is ns <- step = do
188+ -- ensure we only `init` successfully once
189+ P. liftIO $ putStrLn " Init _ _"
190+ st' <- apply st msg is
191+ case st' of
192+ Just s -> apply (s { stProg = ns }) msg ns
193+ Nothing -> die $ ExitOther $ baseErr ++ " :InitFailed"
188194 | Yield sn sd <- step = do
189195 P. liftIO $ putStrLn " Yield s d"
190196 return $ Just $ st { stName = sn, stData = sd }
@@ -197,9 +203,9 @@ apply st@State{..} msg step
197203 P. liftIO $ putStrLn " Always..."
198204 runFSM st (handleMessage msg fsm) >>= mstash
199205 | Perhaps eqn act <- step = do
200- P. liftIO $ putStrLn $ " Perhaps"
201- if eqn == stName then runFSM st act >>= stash
202- else (P. liftIO $ putStrLn " Perhaps Not..." ) >> return Nothing
206+ P. liftIO $ putStrLn $ " Perhaps " ++ ( show eqn) ++ " in " ++ ( show $ stName st)
207+ if eqn == ( stName st) then runFSM st act >>= stash
208+ else (P. liftIO $ putStrLn " Perhaps Not..." ) >> return Nothing
203209 | Matching chk fsm <- step = do
204210 P. liftIO $ putStrLn " Matching..."
205211 runFSM st (handleMessageIf msg chk fsm) >>= mstash
@@ -211,40 +217,45 @@ apply st@State{..} msg step
211217 P. liftIO $ putStrLn $ " Alt LHS valid: " ++ (show $ isJust s)
212218 if isJust s then return s
213219 else (P. liftIO $ putStrLn " try br 2" ) >> apply st msg al2
214- | Reply rply <- step = do (r, s) <- runFSM st rply
215- stReply $ wrapMessage r
216- return $ Just s
220+ | Reply rply <- step = do
221+ let ev = Eval $ do fSt <- processState
222+ MP. lift $ do
223+ P. liftIO $ putStrLn $ " Replying from " ++ (show fSt)
224+ (r, s) <- runFSM fSt rply
225+ (stReply fSt) $ wrapMessage r
226+ -- (_, st') <- runFSM st (addTransition ev)
227+ return $ enqueue st (Just ev)
217228 | otherwise = error $ baseErr ++ " .Internal.Types.apply:InvalidStep"
218229 where
219230 mstash = return . uncurry enqueue . swap
220231 stash (o, s) = return $ enqueue s (Just o)
221232
222- applyTransitions :: forall s d . FsmState s d
223- -> State s d
224- -> [GenProcess (FsmState s d ) () ]
225- -> Action (FsmState s d )
226- applyTransitions fsmSt st@ State {.. } evals
227- | Q. null stTrans, [] <- evals = continue $ copyState fsmSt stName stData
228- | Q. null stTrans = act $ do setProcessState $ copyState fsmSt stName stData
233+ applyTransitions :: forall s d . (Show s )
234+ => State s d
235+ -> [GenProcess (State s d ) () ]
236+ -> Action (State s d )
237+ applyTransitions st@ State {.. } evals
238+ | Q. null stTrans, [] <- evals = continue $ st
239+ | Q. null stTrans = act $ do setProcessState st
240+ MP. liftIO $ putStrLn $ " ProcessState: " ++ (show stName)
229241 mapM_ id evals
230242 | (tr, st2) <- next
231243 , Enter s <- tr = let st' = st2 { stName = s }
232- in applyTransitions (copyState fsmSt s stData) st' evals
244+ in do P. liftIO $ putStrLn $ " NEWSTATE: " ++ (show st')
245+ applyTransitions st' evals
233246 | (tr, st2) <- next
234- , PutBack <- tr = applyTransitions fsmSt st2 ((push stInput) : evals)
247+ , PutBack <- tr = applyTransitions st2 ((push $ fromJust stInput) : evals)
235248 {- let act' = setProcessState $ fsmSt { fsmName = stName, fsmData = stData }
236249 push stInput -}
237250 | (tr, st2) <- next
238- , Eval proc <- tr = applyTransitions fsmSt st2 (proc : evals)
251+ , Eval proc <- tr = applyTransitions st2 (proc : evals)
239252 | (tr, st2) <- next
240- , Remain <- tr = applyTransitions fsmSt st2 evals
253+ , Remain <- tr = applyTransitions st2 evals
241254 | (tr, _) <- next
242- , Stop er <- tr = stopWith (copyState fsmSt stName stData) er
255+ , Stop er <- tr = stopWith st er
243256 | otherwise = error $ baseErr ++ " .Internal.Process.applyTransitions:InvalidState"
244257 where
245- copyState f sn sd = f { fsmName = sn, fsmData = sd }
246-
247- -- don't call splatQ if Q.null
258+ -- don't call if Q.null!
248259 next = let (t, q) = fromJust $ seqDequeue stTrans
249260 in (t, st { stTrans = q })
250261
0 commit comments