SEScenarioExec.h
1/* Distributed under the Apache License, Version 2.0.
2 See accompanying NOTICE file for details.*/
3
4#pragma once
5#include "engine/SEEngineInitialization.h"
6#include "cdm/utils/Logger.h"
7#include "cdm/utils/FileUtils.h"
8
9class SEAction;
10class SEScenario;
11class SEScenarioLog;
15
16enum class eRelativeSerialization { ToWorkingDir = 0, ToOutputDir, ToScenarioDir };
17extern CDM_DECL const std::string& eRelativeSerialization_Name(eRelativeSerialization rt);
18
19class CDM_DECL SEScenarioExec : public Loggable
20{
21 friend class PBScenario;//friend the serialization class
22public:
23 SEScenarioExec(Logger* logger);
24 virtual ~SEScenarioExec();
25
26 virtual void Clear();
27 void Copy(const SEScenarioExec& src);
28
29 eSwitch LogToConsole() const { return m_LogToConsole; }
30 void LogToConsole(eSwitch s) { m_LogToConsole = s; }
31
32 virtual eRelativeSerialization GetRelativeSerialization() const { return m_RelativeSerialization; }
33 virtual void SetRelativeSerialization(eRelativeSerialization rt) { m_RelativeSerialization = rt; }
34
35 std::string GetDataRootDirectory() const { return m_DataRootDirectory; }
36 void SetDataRootDirectory(const std::string& fn) { m_DataRootDirectory = fn; }
37
38 std::string GetOutputRootDirectory() const { return m_OutputRootDirectory; }
39 void SetOutputRootDirectory(const std::string& d);
40
41 eSwitch OrganizeOutputDirectory() const { return m_OrganizeOutputDirectory; }
42 void OrganizeOutputDirectory(eSwitch s) { m_OrganizeOutputDirectory = s; }
43
44 std::string GetScenarioContent() const { return m_ScenarioContent; }
45 void SetScenarioContent(const std::string& sc)
46 {
47 m_ScenarioContent = sc;
48 m_ScenarioFilename = "";
49 m_ScenarioDirectory = "";
50 m_ScenarioExecListFilename = "";
51 m_ScenarioLogFilename = "";
52 m_ScenarioLogDirectory = "";
53 }
54 std::string GetScenarioFilename() const { return m_ScenarioFilename; }
55 void SetScenarioFilename(const std::string& fn)
56 {
57 m_ScenarioContent = "";
58 m_ScenarioFilename = fn;
59 m_ScenarioDirectory = "";
60 m_ScenarioExecListFilename = "";
61 m_ScenarioLogFilename = "";
62 m_ScenarioLogDirectory = "";
63 }
64 std::string GetScenarioDirectory() const { return m_ScenarioDirectory; }
65 void SetScenarioDirectory(const std::string& dir)
66 {
67 m_ScenarioContent = "";
68 m_ScenarioFilename = "";
69 m_ScenarioDirectory = dir;
70 m_ScenarioExecListFilename = "";
71 m_ScenarioLogFilename = "";
72 m_ScenarioLogDirectory = "";
73 }
74
75 std::string GetScenarioExecListFilename() const { return m_ScenarioExecListFilename; }
76 void SetScenarioExecListFilename(const std::string& fn)
77 {
78 m_ScenarioContent = "";
79 m_ScenarioFilename = "";
80 m_ScenarioDirectory = "";
81 m_ScenarioExecListFilename = fn;
82 m_ScenarioLogFilename = "";
83 m_ScenarioLogDirectory = "";
84 }
85
86 std::string GetScenarioLogFilename() const { return m_ScenarioLogFilename; }
87 void SetScenarioLogFilename(const std::string& fn)
88 {
89 m_ScenarioLogFilename = fn;
90 m_ScenarioLogDirectory = "";
91 m_ScenarioContent = "";
92 m_ScenarioFilename = "";
93 m_ScenarioDirectory = "";
94 m_ScenarioExecListFilename = "";
95 }
96 std::string GetScenarioLogDirectory() const { return m_ScenarioLogDirectory; }
97 void SetScenarioLogDirectory(const std::string& dir)
98 {
99 m_ScenarioLogFilename = "";
100 m_ScenarioLogDirectory = dir;
101 m_ScenarioContent = "";
102 m_ScenarioFilename = "";
103 m_ScenarioDirectory = "";
104 m_ScenarioExecListFilename = "";
105 }
106
107 std::set<std::string>& GetDataRequestFilesSearch() { return m_DataRequestFilesSearch; }
108 const std::set<std::string>& GetDataRequestFilesSearch() const { return m_DataRequestFilesSearch; }
109
110 eSerializationFormat GetContentFormat() const { return m_ContentFormat; }
111 void SetContentFormat(eSerializationFormat s) { m_ContentFormat = s; }
112
113 int GetThreadCount() const { return m_ThreadCount; }
114 // if > 0, we will create min of (that many threads) or (number of cores available on the host system)
115 // if 0, we will create the number of cores available on the host system many threads
116 // if <0 we will the number of cores available on the host system plus that value many threads
117 void SetThreadCount(int c) { m_ThreadCount = c; }
118
119 std::string GetEngineConfigurationContent() const { return m_EngineConfigurationContent; }
120 void SetEngineConfigurationContent(const std::string& ecc)
121 {
122 m_EngineConfigurationContent = ecc;
123 m_EngineConfigurationFilename = "";
124 }
125 std::string GetEngineConfigurationFilename() const { return m_EngineConfigurationFilename; }
126 void SetEngineConfigurationFilename(const std::string& fn)
127 {
128 m_EngineConfigurationContent = "";
129 m_EngineConfigurationFilename = fn;
130 }
131
132
133 // These parameters will help debug any serialization issues.
134 // A serialization issue is where get different results from running the same scenario where we
135 // 1. run we run the scenario as is
136 // 2. run the scenario but serialized out and in (after actions, or any time really)
137 // The two generated CSV files should match as well as match the baseline
138 // If they do not, then we are not maintaining state properly to the file and back into the engine
139 // To find serialization issues, uncomment out the AutoSerialization line in the ScenarioVerification.config
140 // Then run the `run ScenarioVerification` command to run all scenarios, have then save out and in after each action
141 // This test will also compare the resulting CSV file to the baseline CSV that did not save in and out
142 // If any scenario fails this comparison... run it through here with this block turned on.
143 // To debug the issue, this block will:
144 // 1. Run one run where we save out states after we apply an action and after the subsequent time step
145 // 2. Run another run where we save out the same times, but we reload the state after the first save
146 // This gives us two states at the same point in time, per action, that we can compare to see what is different
147 // Both times can have differences depending on the bug...good luck!
148
149 eSwitch AutoSerializeAfterActions() const { return m_AutoSerializeAfterActions; }
150 void AutoSerializeAfterActions(eSwitch s) { m_AutoSerializeAfterActions = s; }
151
152 double GetAutoSerializePeriod_s() const { return m_AutoSerializePeriod_s; }
153 void SetAutoSerializePeriod_s(double s) { m_AutoSerializePeriod_s = s; }
154
155 eSwitch TimeStampSerializedStates() const { return m_TimeStampSerializedStates; }
156 void TimeStampSerializedStates(eSwitch s) { m_TimeStampSerializedStates = s; }
157
158 bool SerializeToString(std::string& output, eSerializationFormat m) const;
159 bool SerializeFromString(const std::string& src, eSerializationFormat m);
160
161protected:
162 bool ConvertLog();
163 bool Execute(PhysiologyEngine& pe, SEScenario& sce, SEScenarioExecStatus* status=nullptr);
164 bool Process(PhysiologyEngine& pe, SEScenario& sce, SEScenarioExecStatus* status=nullptr);
165 bool ProcessActions(PhysiologyEngine& pe, SEScenario& sce, SEScenarioExecStatus* status=nullptr);
169 bool ProcessAction(PhysiologyEngine& pe, SEAction& action);
170 void AdvanceEngine(PhysiologyEngine& pe);
171
176
177 // These are computed based on the scenario name
178 std::string m_BaseFilename;
179 std::string m_LogFilename;
181
182 // Note, its up to the deriving exec's to merge configs
183 // The basic config is empty
186
187 std::string m_ScenarioContent;
189 std::string m_ScenarioDirectory; // TheadedExec
190 std::string m_ScenarioExecListFilename;// TheadedExec
191
193 std::string m_ScenarioLogDirectory; // TheadedExec
194
195 std::set<std::string> m_DataRequestFilesSearch;
196
197 // For both the EC and Scenrio Content
198 eSerializationFormat m_ContentFormat;
200
201 // Settings for serialization
203 eRelativeSerialization m_RelativeSerialization;
212 std::stringstream m_SerializationOutput;
213 std::stringstream m_SerializationActions;
214};
215
216enum class eScenarioExecutionState { Waiting = 0, Executing, Complete };
217extern CDM_DECL const std::string& eScenarioExecutionState_Name(eScenarioExecutionState s);
218
220{
221 friend class PBScenario;//friend the serialization class
222public:
224 virtual ~SEScenarioExecStatus();
225
226 void ForwardError(std::string const& /*msg*/) override { m_RuntimeError = true; }
227 void ForwardFatal(std::string const& /*msg*/) override { m_FatalRuntimeError = true; }
228
229 void Clear() override;
230 void ClearStatus(); // Clear everything but the scenario filename
231 void Copy(const SEScenarioExecStatus& src);
232
233 bool SerializeToString(std::string& output, eSerializationFormat m, Logger* logger) const override;
234 bool SerializeFromString(const std::string& src, eSerializationFormat m, Logger* logger) override;
235 static bool SerializeToFile(const std::vector<SEScenarioExecStatus>& src, const std::string& filename, Logger* logger);
236 static bool SerializeFromFile(const std::string& filename, std::vector<SEScenarioExecStatus>& dst, Logger* logger);
237
238 bool HasScenarioFilename() const { return !m_ScenarioFilename.empty(); }
239 std::string GetScenarioFilename() const { return m_ScenarioFilename; }
240 void SetScenarioFilename(const std::string& fn) { m_ScenarioFilename = fn; }
241
242 eScenarioExecutionState GetScenarioExecutionState() const { return m_ScenarioExecutionState; }
243 void SetScenarioExecutionState(eScenarioExecutionState s) { m_ScenarioExecutionState = s; }
244
245 bool HasRuntimeError() const { return m_RuntimeError; }
246 void SetRuntimeError(bool e) { m_RuntimeError = e; }
247
248 bool HasFatalRuntimeError() const { return m_FatalRuntimeError; }
249 void SetFatalRuntimeError(bool e) { m_FatalRuntimeError = e; }
250
251 double GetFinalSimulationTime_s() const { return m_FinalSimulationTime_s; }
252 void SetFinalSimulationTime_s(double t) { m_FinalSimulationTime_s = t; }
253
254protected:
256 eScenarioExecutionState m_ScenarioExecutionState;
260};
Definition: Logger.h:23
Definition: Logger.h:71
Definition: PBScenario.h:10
static bool SerializeFromFile(const std::string &filename, SEScenario &dst)
Definition: PBScenario.cpp:102
static bool SerializeToFile(const SEScenario &src, const std::string &filename)
Definition: PBScenario.cpp:88
static bool SerializeFromString(const std::string &src, SEScenario &dst, eSerializationFormat m)
Definition: PBScenario.cpp:94
static void Copy(const SEScenario &src, SEScenario &dst)
Definition: PBScenario.cpp:74
static bool SerializeToString(const SEScenario &src, std::string &output, eSerializationFormat m)
Definition: PBScenario.cpp:82
Definition: PhysiologyEngine.h:68
Definition: SEAction.h:14
Definition: SEEngineConfiguration.h:8
Definition: SEEngineInitialization.h:70
Definition: SEScenarioExec.h:20
void SetScenarioDirectory(const std::string &dir)
Definition: SEScenarioExec.h:65
void AutoSerializeAfterActions(eSwitch s)
Definition: SEScenarioExec.h:150
double m_AutoSerializePeriod_s
Definition: SEScenarioExec.h:208
double GetAutoSerializePeriod_s() const
Definition: SEScenarioExec.h:152
void SetThreadCount(int c)
Definition: SEScenarioExec.h:117
std::string GetScenarioLogFilename() const
Definition: SEScenarioExec.h:86
std::string m_AutoSerializeBaseFilename
Definition: SEScenarioExec.h:205
void SetScenarioLogDirectory(const std::string &dir)
Definition: SEScenarioExec.h:97
int GetThreadCount() const
Definition: SEScenarioExec.h:113
eSwitch m_OrganizeOutputDirectory
Definition: SEScenarioExec.h:175
int m_ThreadCount
Definition: SEScenarioExec.h:199
std::string m_EngineConfigurationFilename
Definition: SEScenarioExec.h:185
void SetEngineConfigurationFilename(const std::string &fn)
Definition: SEScenarioExec.h:126
eSerializationFormat m_ContentFormat
Definition: SEScenarioExec.h:198
std::set< std::string > & GetDataRequestFilesSearch()
Definition: SEScenarioExec.h:107
eSerializationFormat GetContentFormat() const
Definition: SEScenarioExec.h:110
void LogToConsole(eSwitch s)
Definition: SEScenarioExec.h:30
std::string m_BaseFilename
Definition: SEScenarioExec.h:178
void SetScenarioFilename(const std::string &fn)
Definition: SEScenarioExec.h:55
std::string GetDataRootDirectory() const
Definition: SEScenarioExec.h:35
std::string GetScenarioExecListFilename() const
Definition: SEScenarioExec.h:75
std::string m_DataRootDirectory
Definition: SEScenarioExec.h:173
void SetContentFormat(eSerializationFormat s)
Definition: SEScenarioExec.h:111
std::string m_EngineConfigurationContent
Definition: SEScenarioExec.h:184
std::string m_ScenarioFilename
Definition: SEScenarioExec.h:188
std::string m_AutoSerializeFilenameExt
Definition: SEScenarioExec.h:206
std::stringstream m_SerializationOutput
Definition: SEScenarioExec.h:212
std::string GetEngineConfigurationContent() const
Definition: SEScenarioExec.h:119
eRelativeSerialization m_RelativeSerialization
Definition: SEScenarioExec.h:203
std::string GetScenarioDirectory() const
Definition: SEScenarioExec.h:64
eSwitch LogToConsole() const
Definition: SEScenarioExec.h:29
eSwitch TimeStampSerializedStates() const
Definition: SEScenarioExec.h:155
eSwitch OrganizeOutputDirectory() const
Definition: SEScenarioExec.h:41
eSwitch m_LogToConsole
Definition: SEScenarioExec.h:172
void TimeStampSerializedStates(eSwitch s)
Definition: SEScenarioExec.h:156
std::string m_DataRequestCSVFilename
Definition: SEScenarioExec.h:180
eSwitch m_ReloadSerializedState
Definition: SEScenarioExec.h:211
std::string GetScenarioLogDirectory() const
Definition: SEScenarioExec.h:96
std::string m_AutoSerializeFilename
Definition: SEScenarioExec.h:204
eSwitch m_AutoSerializeAfterActions
Definition: SEScenarioExec.h:207
std::string GetScenarioContent() const
Definition: SEScenarioExec.h:44
std::string m_ScenarioLogDirectory
Definition: SEScenarioExec.h:193
void SetScenarioExecListFilename(const std::string &fn)
Definition: SEScenarioExec.h:76
const std::set< std::string > & GetDataRequestFilesSearch() const
Definition: SEScenarioExec.h:108
std::string GetScenarioFilename() const
Definition: SEScenarioExec.h:54
std::string m_ScenarioExecListFilename
Definition: SEScenarioExec.h:190
eSwitch m_TimeStampSerializedStates
Definition: SEScenarioExec.h:210
std::string m_ScenarioDirectory
Definition: SEScenarioExec.h:189
void SetScenarioContent(const std::string &sc)
Definition: SEScenarioExec.h:45
double m_AutoSerializeTime_s
Definition: SEScenarioExec.h:209
void SetScenarioLogFilename(const std::string &fn)
Definition: SEScenarioExec.h:87
std::set< std::string > m_DataRequestFilesSearch
Definition: SEScenarioExec.h:195
void SetEngineConfigurationContent(const std::string &ecc)
Definition: SEScenarioExec.h:120
std::string GetOutputRootDirectory() const
Definition: SEScenarioExec.h:38
std::string GetEngineConfigurationFilename() const
Definition: SEScenarioExec.h:125
void OrganizeOutputDirectory(eSwitch s)
Definition: SEScenarioExec.h:42
virtual eRelativeSerialization GetRelativeSerialization() const
Definition: SEScenarioExec.h:32
std::string m_LogFilename
Definition: SEScenarioExec.h:179
eSwitch AutoSerializeAfterActions() const
Definition: SEScenarioExec.h:149
void SetAutoSerializePeriod_s(double s)
Definition: SEScenarioExec.h:153
std::string m_ScenarioContent
Definition: SEScenarioExec.h:187
virtual void SetRelativeSerialization(eRelativeSerialization rt)
Definition: SEScenarioExec.h:33
bool m_SaveNextStep
Definition: SEScenarioExec.h:202
void SetDataRootDirectory(const std::string &fn)
Definition: SEScenarioExec.h:36
std::string m_OutputRootDirectory
Definition: SEScenarioExec.h:174
std::stringstream m_SerializationActions
Definition: SEScenarioExec.h:213
std::string m_ScenarioLogFilename
Definition: SEScenarioExec.h:192
Definition: SEScenarioExec.h:220
double GetFinalSimulationTime_s() const
Definition: SEScenarioExec.h:251
void ForwardError(std::string const &) override
Definition: SEScenarioExec.h:226
std::string m_ScenarioFilename
Definition: SEScenarioExec.h:255
eScenarioExecutionState m_ScenarioExecutionState
Definition: SEScenarioExec.h:256
bool m_FatalRuntimeError
Definition: SEScenarioExec.h:258
void SetScenarioFilename(const std::string &fn)
Definition: SEScenarioExec.h:240
std::string GetScenarioFilename() const
Definition: SEScenarioExec.h:239
double m_FinalSimulationTime_s
Definition: SEScenarioExec.h:259
bool HasRuntimeError() const
Definition: SEScenarioExec.h:245
bool m_RuntimeError
Definition: SEScenarioExec.h:257
void ForwardFatal(std::string const &) override
Definition: SEScenarioExec.h:227
void SetRuntimeError(bool e)
Definition: SEScenarioExec.h:246
bool HasFatalRuntimeError() const
Definition: SEScenarioExec.h:248
bool HasScenarioFilename() const
Definition: SEScenarioExec.h:238
eScenarioExecutionState GetScenarioExecutionState() const
Definition: SEScenarioExec.h:242
void SetFinalSimulationTime_s(double t)
Definition: SEScenarioExec.h:252
void SetScenarioExecutionState(eScenarioExecutionState s)
Definition: SEScenarioExec.h:243
void SetFatalRuntimeError(bool e)
Definition: SEScenarioExec.h:249
Definition: SEScenario.h:12
Definition: SEScenarioLog.h:14

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.