{-# LANGUAGE TypeFamilies #-}
module Simulation.Aivika.Experiment.Base.FileRenderer where
import Control.Monad
import Control.Monad.Trans
import System.Directory
import System.FilePath
import Simulation.Aivika.Trans.Exception
import Simulation.Aivika.Experiment.Types
import Simulation.Aivika.Experiment.Base.ExperimentWriter
data FileRenderer a = FileRenderer a ExperimentFilePath
type FileGenerator a = ExperimentGenerator (FileRenderer a)
instance ExperimentRendering (FileRenderer a) where
data ExperimentContext (FileRenderer a) = FileContext
type ExperimentEnvironment (FileRenderer a) = FilePath
type ExperimentMonad (FileRenderer a) = ExperimentWriter
liftExperiment :: forall a.
FileRenderer a -> ExperimentMonad (FileRenderer a) a -> IO a
liftExperiment FileRenderer a
r = forall a. ExperimentWriter a -> IO a
runExperimentWriter
prepareExperiment :: Experiment
-> FileRenderer a
-> ExperimentMonad
(FileRenderer a) (ExperimentEnvironment (FileRenderer a))
prepareExperiment Experiment
e (FileRenderer a
_ ExperimentFilePath
path0) =
do FilePath
path <- FilePath -> ExperimentFilePath -> ExperimentWriter FilePath
resolveFilePath FilePath
"" ExperimentFilePath
path0
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Experiment -> Bool
experimentVerbose Experiment
e) forall a b. (a -> b) -> a -> b
$
do FilePath -> IO ()
putStr FilePath
"Updating directory "
FilePath -> IO ()
putStrLn FilePath
path
Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True FilePath
path
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
path
renderExperiment :: Experiment
-> FileRenderer a
-> [ExperimentReporter (FileRenderer a)]
-> ExperimentEnvironment (FileRenderer a)
-> ExperimentMonad (FileRenderer a) ()
renderExperiment Experiment
e FileRenderer a
r [ExperimentReporter (FileRenderer a)]
reporters ExperimentEnvironment (FileRenderer a)
path = forall (m :: * -> *) a. Monad m => a -> m a
return ()
onExperimentCompleted :: Experiment
-> FileRenderer a
-> ExperimentEnvironment (FileRenderer a)
-> ExperimentMonad (FileRenderer a) ()
onExperimentCompleted Experiment
e FileRenderer a
r ExperimentEnvironment (FileRenderer a)
path = forall (m :: * -> *) a. Monad m => a -> m a
return ()
onExperimentFailed :: forall e.
Exception e =>
Experiment
-> FileRenderer a
-> ExperimentEnvironment (FileRenderer a)
-> e
-> ExperimentMonad (FileRenderer a) ()
onExperimentFailed Experiment
e FileRenderer a
r ExperimentEnvironment (FileRenderer a)
path e
e' = forall (m :: * -> *) e a.
(MonadException m, Exception e) =>
e -> m a
throwComp e
e'