SimulationDrivenWare.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file SimulationDrivenWare.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_WARE_SIMULATIONDRIVENWARE_HPP__
41 #define __OPENFLUID_WARE_SIMULATIONDRIVENWARE_HPP__
42 
43 
44 #include <openfluid/dllexport.hpp>
50 
51 
52 /**
53  @internal
54 */
55 #define REQUIRE_SIMULATION_STAGE(stage,msg) \
56  if (OPENFLUID_GetCurrentStage() != (stage)) \
57  { \
58  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
59  throw openfluid::base::FrameworkException(Context,msg); \
60  }
61 
62 /**
63  @internal
64 */
65 #define REQUIRE_SIMULATION_STAGE_GE(stage,msg) \
66  if (OPENFLUID_GetCurrentStage() < (stage)) \
67  { \
68  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
69  throw openfluid::base::FrameworkException(Context,msg); \
70  }
71 
72 /**
73  @internal
74 */
75 #define REQUIRE_SIMULATION_STAGE_LE(stage,msg) \
76  if (OPENFLUID_GetCurrentStage() > (stage)) \
77  { \
78  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
79  throw openfluid::base::FrameworkException(Context,msg); \
80  }
81 
82 
83 // =====================================================================
84 // =====================================================================
85 
86 
87 /**
88  @internal
89 */
90 #define _STREAMTOSTRING(_stream) ((static_cast<std::ostringstream&>(std::ostringstream().flush() << _stream)).str())
91 
92 
93 // Log macros for warnings
94 
95 /**
96  Adds a warning message to simulation log file
97 
98  Exemple:
99  @code{.cpp}
100  OPENFLUID_LogWarning("This is a logged warning message for " << "TestUnits#" << TU->getID());
101  @endcode
102 */
103 #define OPENFLUID_LogWarning(_stream) \
104  appendToLog(openfluid::tools::FileLogger::LOG_WARNING,_STREAMTOSTRING(_stream))
105 
106 /**
107  Displays a warning message to stdout (on screen by default)
108 
109  Exemple:
110  @code{.cpp}
111  OPENFLUID_DisplayWarning("This is a displayed warning message for " << "TestUnits#" << TU->getID());
112  @endcode
113 */
114 #define OPENFLUID_DisplayWarning(_stream) \
115  displayToConsole(openfluid::tools::FileLogger::LOG_WARNING,_STREAMTOSTRING(_stream))
116 
117 /**
118  Adds a warning message to simulation log file and displays it to stdout (on screen by default)
119 
120  Exemple:
121  @code{.cpp}
122  OPENFLUID_LogAndDisplayWarning("This is a logged and displayed warning message for " <<
123  "TestUnits#" << TU->getID());
124  @endcode
125 */
126 #define OPENFLUID_LogAndDisplayWarning(_stream) \
127  OPENFLUID_LogWarning(_stream); \
128  OPENFLUID_DisplayWarning(_stream)
129 
130 
131 // Log macros for infos
132 
133 /**
134  Adds an information message to simulation log file
135 
136  Exemple:
137  @code{.cpp}
138  OPENFLUID_LogInfo("This is a logged information message for " << "TestUnits#" << TU->getID());
139  @endcode
140 */
141 #define OPENFLUID_LogInfo(_stream) \
142  appendToLog(openfluid::tools::FileLogger::LOG_INFO,_STREAMTOSTRING(_stream))
143 
144 /**
145  Displays an information message to stdout (on screen by default)
146 
147  Exemple:
148  @code{.cpp}
149  OPENFLUID_DisplayInfo("This is a displayed information message for " << "TestUnits#" << TU->getID());
150  @endcode
151 */
152 #define OPENFLUID_DisplayInfo(_stream) \
153  displayToConsole(openfluid::tools::FileLogger::LOG_INFO,_STREAMTOSTRING(_stream))
154 
155 /**
156  Adds an information message to simulation log file and displays it to stdout (on screen by default)
157 
158  Exemple:
159  @code{.cpp}
160  OPENFLUID_LogAndDisplayInfo("This is a logged and displayed information message for " <<
161  "TestUnits#" << TU->getID());
162  @endcode
163 */
164 #define OPENFLUID_LogAndDisplayInfo(_stream) \
165  OPENFLUID_LogInfo(_stream); \
166  OPENFLUID_DisplayInfo(_stream)
167 
168 
169 // Log macros for debug
170 
171 /**
172  @def OPENFLUID_LogDebug
173  Adds a debug message to simulation log file
174 
175  Exemple:
176  @code{.cpp}
177  OPENFLUID_LogDebug("This is a logged debug message for " << "TestUnits#" << TU->getID());
178  @endcode
179 */
180 
181 
182 /**
183  @def OPENFLUID_DisplayDebug
184  Displays a debug message to stdout (on screen by default)
185 
186  Exemple:
187  @code{.cpp}
188  OPENFLUID_DisplayDebug("This is a displayed debug message for " << "TestUnits#" << TU->getID());
189  @endcode
190 */
191 
192 /**
193  @def OPENFLUID_LogAndDisplayDebug
194  Adds a debug message to simulation log file and displays it to stdout (on screen by default)
195 
196  Exemple:
197  @code{.cpp}
198  OPENFLUID_LogAndDisplayDebug("This is a logged and displayed debug message for " << "TestUnits#" << TU->getID());
199  @endcode
200 */
201 
202 #ifndef NDEBUG
203 
204 #define OPENFLUID_LogDebug(_stream) \
205  appendToLog(openfluid::tools::FileLogger::LOG_DEBUG,_STREAMTOSTRING(_stream))
206 
207 #define OPENFLUID_DisplayDebug(_stream) \
208  displayToConsole(openfluid::tools::FileLogger::LOG_DEBUG,_STREAMTOSTRING(_stream))
209 
210 #define OPENFLUID_LogAndDisplayDebug(_stream) \
211  OPENFLUID_LogDebug(_stream); \
212  OPENFLUID_DisplayDebug(_stream)
213 
214 #else
215 
216 #define OPENFLUID_LogDebug(_stream)
217 
218 #define OPENFLUID_DisplayDebug(_stream)
219 
220 #define OPENFLUID_LogAndDisplayDebug(_stream)
221 
222 #endif
223 
224 
225 // =====================================================================
226 // =====================================================================
227 
228 
229 namespace openfluid { namespace ware {
230 
231 
233 {
234  private:
235 
236  const openfluid::base::SimulationStatus* mp_SimStatus;
237 
238  openfluid::base::SimulationLogger* mp_SimLogger;
239 
240  openfluid::core::TimeIndex_t m_PreviousTimeIndex;
241 
242 
243  protected:
244 
245 
246  virtual bool isLinked() const
247  { return (PluggableWare::isLinked() && mp_SimLogger != nullptr && mp_SimStatus != nullptr); }
248 
249  void appendToLog(openfluid::tools::FileLogger::LogType LType, const std::string& Msg) const;
250 
251  void displayToConsole(openfluid::tools::FileLogger::LogType LType, const std::string& Msg) const;
252 
253 
254  openfluid::base::ExceptionContext computeWareContext(const std::string& CodeLoc = "") const;
255 
256  openfluid::base::ExceptionContext computeFrameworkContext(const std::string& CodeLoc = "") const;
257 
258 
259  /**
260  Returns the real beginning date of the simulated period
261  @return the date
262  */
263  openfluid::core::DateTime OPENFLUID_GetBeginDate() const;
264 
265  /**
266  Returns the real ending date of the simulated period
267  @return the date
268  */
269  openfluid::core::DateTime OPENFLUID_GetEndDate() const;
270 
271  /**
272  Returns the current real date corresponding to the current time index
273  @return the date
274  */
275  openfluid::core::DateTime OPENFLUID_GetCurrentDate() const;
276 
277  /**
278  Returns the simulation duration in seconds
279  @return the duration in seconds
280  */
281  openfluid::core::Duration_t OPENFLUID_GetSimulationDuration() const;
282 
283  /**
284  Returns the default DeltaT used by the scheduler
285  @return the deltaT in seconds
286  */
287  openfluid::core::Duration_t OPENFLUID_GetDefaultDeltaT() const;
288 
289  /**
290  Returns the current time index of the simulation, in seconds since the simulation started.
291  When the simulation starts, the time index is equal to zero.
292  @return the current time index in seconds
293  */
294  openfluid::core::TimeIndex_t OPENFLUID_GetCurrentTimeIndex() const;
295 
296  /**
297  Returns the time index of the simulation when the plugged ware was previously run
298  @return the time index in seconds
299  */
300  openfluid::core::TimeIndex_t OPENFLUID_GetPreviousRunTimeIndex() const;
301 
302  /**
303  Returns the current stage of the simulation
304  @return the stage
305  */
306  openfluid::base::SimulationStatus::SimulationStage OPENFLUID_GetCurrentStage() const;
307 
308  std::string OPENFLUID_GetCurrentStageAsString() const;
309 
310  /**
311  Returns the scheduling constraint applied to the simulation (may be NONE)
312  @return the constraint type
313  */
314  openfluid::base::SimulationStatus::SchedulingConstraint OPENFLUID_GetSchedulingConstraint() const;
315 
316 
317  /**
318  Raises a time-marked warning message to the kernel. This does not stops the simulation
319  @param[in] Msg the content of the message
320  */
321  virtual void OPENFLUID_RaiseWarning(const std::string& Msg);
322 
323  /**
324  Raises a time-marked warning message to the kernel. This does not stops the simulation
325  @param[in] Source the source of the message
326  @param[in] Msg the content of the message
327  @deprecated Since version 2.1.0.
328  Use openfluid::ware::SimulationDrivenWare::OPENFLUID_RaiseWarning(const std::string&)
329  or #OPENFLUID_LogWarning instead
330  */
331  [[deprecated]] virtual void OPENFLUID_RaiseWarning(const std::string& Source, const std::string& Msg);
332 
333  /**
334  Raises an error message to the kernel. This stops the simulation the next time the kernel has the control
335  @param[in] Msg the content of the message
336  */
337  virtual void OPENFLUID_RaiseError(const std::string& Msg);
338 
339  /**
340  Raises an error message to the kernel. This stops the simulation the next time the kernel has the control
341  @param[in] Source the source of the message
342  @param[in] Msg the content of the message
343  @deprecated Since version 2.1.0.
344  Use openfluid::ware::SimulationDrivenWare::OPENFLUID_RaiseError(const std::string&) instead
345  */
346  [[deprecated]] virtual void OPENFLUID_RaiseError(const std::string& Source, const std::string& Msg);
347 
349  mp_SimStatus(nullptr), mp_SimLogger(nullptr), m_PreviousTimeIndex(0)
350  { }
351 
352 
353  public:
354 
356  { }
357 
358  void linkToSimulation(const openfluid::base::SimulationStatus* SimStatus);
359 
361  { mp_SimLogger = SimLogger; };
362 
363  void initializeWare(const WareID_t& ID);
364 
365  void finalizeWare();
366 
368  { m_PreviousTimeIndex = TimeIndex; };
369 
370 };
371 
372 
373 } } // openfluid::ware
374 
375 
376 #endif /* __OPENFLUID_WARE_SIMULATIONDRIVENWARE_HPP__ */
Definition: SimulationLogger.hpp:57
Class for management of date and time information.
Definition: DateTime.hpp:116
virtual ~SimulationDrivenWare()
Definition: SimulationDrivenWare.hpp:355
SimulationDrivenWare(WareType WType)
Definition: SimulationDrivenWare.hpp:348
SchedulingConstraint
Definition: SimulationStatus.hpp:62
Definition: PluggableWare.hpp:97
virtual bool isLinked() const
Definition: PluggableWare.hpp:123
Definition: ExceptionContext.hpp:53
std::string WareID_t
Definition: TypeDefs.hpp:49
Definition: SimulationDrivenWare.hpp:232
void setPreviousTimeIndex(const openfluid::core::TimeIndex_t &TimeIndex)
Definition: SimulationDrivenWare.hpp:367
virtual bool isLinked() const
Definition: SimulationDrivenWare.hpp:246
void linkToSimulationLogger(openfluid::base::SimulationLogger *SimLogger)
Definition: SimulationDrivenWare.hpp:360
SimulationStage
Definition: SimulationStatus.hpp:59
WareType
Definition: TypeDefs.hpp:60
Definition: ApplicationException.hpp:47
std::uint64_t TimeIndex_t
Definition: TypeDefs.hpp:181
Definition: SimulationStatus.hpp:55
std::uint64_t Duration_t
Definition: TypeDefs.hpp:186
#define OPENFLUID_API
Definition: dllexport.hpp:86
LogType
Definition: FileLogger.hpp:72