Skip to content

Commit e6f5f78

Browse files
committed
simulation: force and print sim events in lockstep
1 parent 9c65fcd commit e6f5f78

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

simulation/src/Sample.hs

+17-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
module Sample where
66

7-
import Control.Monad
87
import Data.Aeson
98
import Data.Aeson.Encoding
109
import qualified Data.ByteString.Lazy as BSL
@@ -48,24 +47,25 @@ runSampleModel' traceFile logEvent (SampleModel s0 accum render) stop =
4847
process m
4948
| Just f <- traceFile =
5049
withFile f WriteMode $ (`go` m) . writeEvents
51-
| otherwise = go (const $ pure ()) m
52-
go :: ([(Time, event)] -> IO ()) -> SimVizModel event state -> IO ()
53-
go w m = case stepSimViz 10000 m of
54-
(before, m'@(SimVizModel ((now, _) : _) _)) -> do
55-
w before
56-
putStrLn $ "time reached: " ++ show now
57-
hFlush stdout
58-
go w m'
59-
(before, SimVizModel [] s) -> do
60-
w before
61-
putStrLn "done."
62-
hFlush stdout
63-
render s
64-
stepSimViz n (SimVizModel es s) = case splitAt n es of
65-
(before, after) -> (,) before $ SimVizModel after (foldl' (\x (t, e) -> accum t e x) s before)
66-
writeEvents h es = forM_ es $ \(Time t, e) ->
50+
| otherwise = go (\(SimVizModel es st) -> return $ foldl' (\x (t, e) -> accum t e x) st es) m
51+
go :: (SimVizModel event state -> IO state) -> SimVizModel event state -> IO ()
52+
go w (SimVizModel es st) = case splitAt 10000 es of
53+
(before, after) -> do
54+
st' <- w (SimVizModel before st)
55+
case after of
56+
((now, _) : _) -> do
57+
putStrLn $ "time reached: " ++ show now
58+
hFlush stdout
59+
go w (SimVizModel after st')
60+
[] -> do
61+
putStrLn "done."
62+
hFlush stdout
63+
render st'
64+
writeEvents _h (SimVizModel [] st) = return st
65+
writeEvents h (SimVizModel ((t'@(Time t), e) : es) st) = do
6766
case logEvent t e of
6867
Nothing -> return ()
6968
Just x -> do
7069
BSL.hPutStr h (encodingToLazyByteString x)
7170
BSL.hPutStr h "\n"
71+
writeEvents h (SimVizModel es (accum t' e st))

0 commit comments

Comments
 (0)