module Simulation.Aivika.Trans.Results.IO
(
ResultSourcePrint,
ResultSourceShowS,
printResultsWithTime,
printResultsInStartTime,
printResultsInStopTime,
printResultsInIntegTimes,
printResultsInTime,
printResultsInTimes,
printSimulationResultsInStartTime,
printSimulationResultsInStopTime,
printSimulationResultsInIntegTimes,
printSimulationResultsInTime,
printSimulationResultsInTimes,
showResultsWithTime,
showResultsInStartTime,
showResultsInStopTime,
showResultsInIntegTimes,
showResultsInTime,
showResultsInTimes,
showSimulationResultsInStartTime,
showSimulationResultsInStopTime,
showSimulationResultsInIntegTimes,
showSimulationResultsInTime,
showSimulationResultsInTimes,
hPrintResultSourceIndented,
hPrintResultSource,
hPrintResultSourceInRussian,
hPrintResultSourceInEnglish,
printResultSourceIndented,
printResultSource,
printResultSourceInRussian,
printResultSourceInEnglish,
showResultSourceIndented,
showResultSource,
showResultSourceInRussian,
showResultSourceInEnglish) where
import Control.Monad
import Control.Monad.Trans
import qualified Data.Map as M
import qualified Data.Array as A
import System.IO
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Specs
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Dynamics
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Results
import Simulation.Aivika.Trans.Results.Locale
type ResultSourceShowS m = ResultSource m -> Event m ShowS
type ResultSourcePrint m = ResultSource m -> Event m ()
hPrintResultSourceIndented :: (MonadComp m, MonadIO m)
=> Handle
-> Int
-> ResultLocalisation
-> ResultSourcePrint m
hPrintResultSourceIndented h indent loc source@(ResultItemSource (ResultItem x)) =
hPrintResultSourceIndentedLabelled h indent (resultItemName x) loc source
hPrintResultSourceIndented h indent loc source@(ResultVectorSource x) =
hPrintResultSourceIndentedLabelled h indent (resultVectorName x) loc source
hPrintResultSourceIndented h indent loc source@(ResultObjectSource x) =
hPrintResultSourceIndentedLabelled h indent (resultObjectName x) loc source
hPrintResultSourceIndented h indent loc source@(ResultSeparatorSource x) =
hPrintResultSourceIndentedLabelled h indent (resultSeparatorText x) loc source
hPrintResultSourceIndentedLabelled :: (MonadComp m, MonadIO m)
=> Handle
-> Int
-> ResultName
-> ResultLocalisation
-> ResultSourcePrint m
hPrintResultSourceIndentedLabelled h indent label loc (ResultItemSource (ResultItem x)) =
case resultValueData (resultItemToStringValue x) of
Just m ->
do a <- m
let tab = replicate indent ' '
liftIO $
do hPutStr h tab
hPutStr h "-- "
hPutStr h (loc $ resultItemId x)
hPutStrLn h ""
hPutStr h tab
hPutStr h label
hPutStr h " = "
hPutStrLn h a
hPutStrLn h ""
_ ->
error $
"Expected to see a string value for variable " ++
(resultItemName x) ++ ": hPrintResultSourceIndentedLabelled"
hPrintResultSourceIndentedLabelled h indent label loc (ResultVectorSource x) =
do let tab = replicate indent ' '
liftIO $
do hPutStr h tab
hPutStr h "-- "
hPutStr h (loc $ resultVectorId x)
hPutStrLn h ""
hPutStr h tab
hPutStr h label
hPutStrLn h ":"
hPutStrLn h ""
let items = A.elems (resultVectorItems x)
subscript = A.elems (resultVectorSubscript x)
forM_ (zip items subscript) $ \(i, s) ->
hPrintResultSourceIndentedLabelled h (indent + 2) (label ++ s) loc i
hPrintResultSourceIndentedLabelled h indent label loc (ResultObjectSource x) =
do let tab = replicate indent ' '
liftIO $
do hPutStr h tab
hPutStr h "-- "
hPutStr h (loc $ resultObjectId x)
hPutStrLn h ""
hPutStr h tab
hPutStr h label
hPutStrLn h ":"
hPutStrLn h ""
forM_ (resultObjectProperties x) $ \p ->
do let indent' = 2 + indent
tab' = " " ++ tab
label' = resultPropertyLabel p
source' = resultPropertySource p
hPrintResultSourceIndentedLabelled h indent' label' loc source'
hPrintResultSourceIndentedLabelled h indent label loc (ResultSeparatorSource x) =
do let tab = replicate indent ' '
liftIO $
do hPutStr h tab
hPutStr h label
hPutStrLn h ""
hPutStrLn h ""
printResultSourceIndented :: (MonadComp m, MonadIO m)
=> Int
-> ResultLocalisation
-> ResultSourcePrint m
printResultSourceIndented = hPrintResultSourceIndented stdout
hPrintResultSource :: (MonadComp m, MonadIO m)
=> Handle
-> ResultLocalisation
-> ResultSourcePrint m
hPrintResultSource h = hPrintResultSourceIndented h 0
printResultSource :: (MonadComp m, MonadIO m)
=> ResultLocalisation
-> ResultSourcePrint m
printResultSource = hPrintResultSource stdout
hPrintResultSourceInRussian :: (MonadComp m, MonadIO m) => Handle -> ResultSourcePrint m
hPrintResultSourceInRussian h = hPrintResultSource h russianResultLocalisation
hPrintResultSourceInEnglish :: (MonadComp m, MonadIO m) => Handle -> ResultSourcePrint m
hPrintResultSourceInEnglish h = hPrintResultSource h englishResultLocalisation
printResultSourceInRussian :: (MonadComp m, MonadIO m) => ResultSourcePrint m
printResultSourceInRussian = hPrintResultSourceInRussian stdout
printResultSourceInEnglish :: (MonadComp m, MonadIO m) => ResultSourcePrint m
printResultSourceInEnglish = hPrintResultSourceInEnglish stdout
showResultSourceIndented :: MonadComp m
=> Int
-> ResultLocalisation
-> ResultSourceShowS m
showResultSourceIndented indent loc source@(ResultItemSource (ResultItem x)) =
showResultSourceIndentedLabelled indent (resultItemName x) loc source
showResultSourceIndented indent loc source@(ResultVectorSource x) =
showResultSourceIndentedLabelled indent (resultVectorName x) loc source
showResultSourceIndented indent loc source@(ResultObjectSource x) =
showResultSourceIndentedLabelled indent (resultObjectName x) loc source
showResultSourceIndented indent loc source@(ResultSeparatorSource x) =
showResultSourceIndentedLabelled indent (resultSeparatorText x) loc source
showResultSourceIndentedLabelled :: MonadComp m
=> Int
-> String
-> ResultLocalisation
-> ResultSourceShowS m
showResultSourceIndentedLabelled indent label loc (ResultItemSource (ResultItem x)) =
case resultValueData (resultItemToStringValue x) of
Just m ->
do a <- m
let tab = replicate indent ' '
return $
showString tab .
showString "-- " .
showString (loc $ resultItemId x) .
showString "\n" .
showString tab .
showString label .
showString " = " .
showString a .
showString "\n\n"
_ ->
error $
"Expected to see a string value for variable " ++
(resultItemName x) ++ ": showResultSourceIndentedLabelled"
showResultSourceIndentedLabelled indent label loc (ResultVectorSource x) =
do let tab = replicate indent ' '
items = A.elems (resultVectorItems x)
subscript = A.elems (resultVectorSubscript x)
contents <-
forM (zip items subscript) $ \(i, s) ->
showResultSourceIndentedLabelled (indent + 2) (label ++ s) loc i
let showContents = foldr (.) id contents
return $
showString tab .
showString "-- " .
showString (loc $ resultVectorId x) .
showString "\n" .
showString tab .
showString label .
showString ":\n\n" .
showContents
showResultSourceIndentedLabelled indent label loc (ResultObjectSource x) =
do let tab = replicate indent ' '
contents <-
forM (resultObjectProperties x) $ \p ->
do let indent' = 2 + indent
tab' = " " ++ tab
label' = resultPropertyLabel p
output' = resultPropertySource p
showResultSourceIndentedLabelled indent' label' loc output'
let showContents = foldr (.) id contents
return $
showString tab .
showString "-- " .
showString (loc $ resultObjectId x) .
showString "\n" .
showString tab .
showString label .
showString ":\n\n" .
showContents
showResultSourceIndentedLabelled indent label loc (ResultSeparatorSource x) =
do let tab = replicate indent ' '
return $
showString tab .
showString label .
showString "\n\n"
showResultSource :: MonadComp m
=> ResultLocalisation
-> ResultSourceShowS m
showResultSource = showResultSourceIndented 0
showResultSourceInRussian :: MonadComp m => ResultSourceShowS m
showResultSourceInRussian = showResultSource russianResultLocalisation
showResultSourceInEnglish :: MonadComp m => ResultSourceShowS m
showResultSourceInEnglish = showResultSource englishResultLocalisation
printResultsWithTime :: (MonadComp m, MonadIO m) => ResultSourcePrint m -> Results m -> Event m ()
printResultsWithTime print results =
do let x1 = textResultSource "----------"
x2 = timeResultSource
x3 = textResultSource ""
xs = resultSourceList results
print x1
print x2
mapM_ print xs
printResultsInStartTime :: (MonadComp m, MonadIO m) => ResultSourcePrint m -> Results m -> Simulation m ()
printResultsInStartTime print results =
runEventInStartTime $ printResultsWithTime print results
printResultsInStopTime :: (MonadComp m, MonadIO m) => ResultSourcePrint m -> Results m -> Simulation m ()
printResultsInStopTime print results =
runEventInStopTime $ printResultsWithTime print results
printResultsInIntegTimes :: (MonadComp m, MonadIO m) => ResultSourcePrint m -> Results m -> Simulation m ()
printResultsInIntegTimes print results =
do let loop (m : ms) = m >> loop ms
loop [] = return ()
ms <- runDynamicsInIntegTimes $ runEvent $
printResultsWithTime print results
liftComp $ loop ms
printResultsInTime :: (MonadComp m, MonadIO m) => Double -> ResultSourcePrint m -> Results m -> Simulation m ()
printResultsInTime t print results =
runDynamicsInTime t $ runEvent $
printResultsWithTime print results
printResultsInTimes :: (MonadComp m, MonadIO m) => [Double] -> ResultSourcePrint m -> Results m -> Simulation m ()
printResultsInTimes ts print results =
do let loop (m : ms) = m >> loop ms
loop [] = return ()
ms <- runDynamicsInTimes ts $ runEvent $
printResultsWithTime print results
liftComp $ loop ms
showResultsWithTime :: MonadComp m => ResultSourceShowS m -> Results m -> Event m ShowS
showResultsWithTime f results =
do let x1 = textResultSource "----------"
x2 = timeResultSource
x3 = textResultSource ""
xs = resultSourceList results
y1 <- f x1
y2 <- f x2
y3 <- f x3
ys <- forM xs f
return $
y1 .
y2 .
foldr (.) id ys
showResultsInStartTime :: MonadComp m => ResultSourceShowS m -> Results m -> Simulation m ShowS
showResultsInStartTime f results =
runEventInStartTime $ showResultsWithTime f results
showResultsInStopTime :: MonadComp m => ResultSourceShowS m -> Results m -> Simulation m ShowS
showResultsInStopTime f results =
runEventInStopTime $ showResultsWithTime f results
showResultsInIntegTimes :: MonadComp m => ResultSourceShowS m -> Results m -> Simulation m ShowS
showResultsInIntegTimes f results =
do let loop (m : ms) = return (.) `ap` m `ap` loop ms
loop [] = return id
ms <- runDynamicsInIntegTimes $ runEvent $
showResultsWithTime f results
liftComp $ loop ms
showResultsInTime :: MonadComp m => Double -> ResultSourceShowS m -> Results m -> Simulation m ShowS
showResultsInTime t f results =
runDynamicsInTime t $ runEvent $
showResultsWithTime f results
showResultsInTimes :: MonadComp m => [Double] -> ResultSourceShowS m -> Results m -> Simulation m ShowS
showResultsInTimes ts f results =
do let loop (m : ms) = return (.) `ap` m `ap` loop ms
loop [] = return id
ms <- runDynamicsInTimes ts $ runEvent $
showResultsWithTime f results
liftComp $ loop ms
printSimulationResultsInStartTime :: (MonadComp m, MonadIO m) => ResultSourcePrint m -> Simulation m (Results m) -> Specs m -> m ()
printSimulationResultsInStartTime print model specs =
flip runSimulation specs $
model >>= printResultsInStartTime print
printSimulationResultsInStopTime :: (MonadComp m, MonadIO m) => ResultSourcePrint m -> Simulation m (Results m) -> Specs m -> m ()
printSimulationResultsInStopTime print model specs =
flip runSimulation specs $
model >>= printResultsInStopTime print
printSimulationResultsInIntegTimes :: (MonadComp m, MonadIO m) => ResultSourcePrint m -> Simulation m (Results m) -> Specs m -> m ()
printSimulationResultsInIntegTimes print model specs =
flip runSimulation specs $
model >>= printResultsInIntegTimes print
printSimulationResultsInTime :: (MonadComp m, MonadIO m) => Double -> ResultSourcePrint m -> Simulation m (Results m) -> Specs m -> m ()
printSimulationResultsInTime t print model specs =
flip runSimulation specs $
model >>= printResultsInTime t print
printSimulationResultsInTimes :: (MonadComp m, MonadIO m) => [Double] -> ResultSourcePrint m -> Simulation m (Results m) -> Specs m -> m ()
printSimulationResultsInTimes ts print model specs =
flip runSimulation specs $
model >>= printResultsInTimes ts print
showSimulationResultsInStartTime :: MonadComp m => ResultSourceShowS m -> Simulation m (Results m) -> Specs m -> m ShowS
showSimulationResultsInStartTime f model specs =
flip runSimulation specs $
model >>= showResultsInStartTime f
showSimulationResultsInStopTime :: MonadComp m => ResultSourceShowS m -> Simulation m (Results m) -> Specs m -> m ShowS
showSimulationResultsInStopTime f model specs =
flip runSimulation specs $
model >>= showResultsInStopTime f
showSimulationResultsInIntegTimes :: MonadComp m => ResultSourceShowS m -> Simulation m (Results m) -> Specs m -> m ShowS
showSimulationResultsInIntegTimes f model specs =
flip runSimulation specs $
model >>= showResultsInIntegTimes f
showSimulationResultsInTime :: MonadComp m => Double -> ResultSourceShowS m -> Simulation m (Results m) -> Specs m -> m ShowS
showSimulationResultsInTime t f model specs =
flip runSimulation specs $
model >>= showResultsInTime t f
showSimulationResultsInTimes :: MonadComp m => [Double] -> ResultSourceShowS m -> Simulation m (Results m) -> Specs m -> m ShowS
showSimulationResultsInTimes ts f model specs =
flip runSimulation specs $
model >>= showResultsInTimes ts f