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@supagro.inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_WARE_SIMULATIONDRIVENWARE_HPP__
41 #define __OPENFLUID_WARE_SIMULATIONDRIVENWARE_HPP__
42 
43 
49 #include <openfluid/dllexport.hpp>
51 
52 
53 /**
54  @internal
55 */
56 #define REQUIRE_SIMULATION_STAGE(stage,msg) \
57  if (OPENFLUID_GetCurrentStage() != (stage)) \
58  { \
59  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
60  throw openfluid::base::FrameworkException(Context,msg); \
61  }
62 
63 /**
64  @internal
65 */
66 #define REQUIRE_SIMULATION_STAGE_GE(stage,msg) \
67  if (OPENFLUID_GetCurrentStage() < (stage)) \
68  { \
69  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
70  throw openfluid::base::FrameworkException(Context,msg); \
71  }
72 
73 /**
74  @internal
75 */
76 #define REQUIRE_SIMULATION_STAGE_LE(stage,msg) \
77  if (OPENFLUID_GetCurrentStage() > (stage)) \
78  { \
79  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
80  throw openfluid::base::FrameworkException(Context,msg); \
81  }
82 
83 
84 // =====================================================================
85 // =====================================================================
86 
87 
88 /**
89  @internal
90 */
91 #define _STREAMTOSTRING(_stream) ((static_cast<std::ostringstream&>(std::ostringstream().flush() << _stream)).str())
92 
93 
94 // Log macros for warnings
95 
96 /**
97  Adds a warning message to simulation log file
98 
99  Exemple:
100  @code{.cpp}
101  OPENFLUID_LogWarning("This is a logged warning message for " << "TestUnits#" << TU->getID());
102  @endcode
103 */
104 #define OPENFLUID_LogWarning(_stream) \
105  appendToLog(openfluid::tools::FileLogger::LOG_WARNING,_STREAMTOSTRING(_stream))
106 
107 /**
108  Displays a warning message to stdout (on screen by default)
109 
110  Exemple:
111  @code{.cpp}
112  OPENFLUID_DisplayWarning("This is a displayed warning message for " << "TestUnits#" << TU->getID());
113  @endcode
114 */
115 #define OPENFLUID_DisplayWarning(_stream) \
116  displayToConsole(openfluid::tools::FileLogger::LOG_WARNING,_STREAMTOSTRING(_stream))
117 
118 /**
119  Adds a warning message to simulation log file and displays it to stdout (on screen by default)
120 
121  Exemple:
122  @code{.cpp}
123  OPENFLUID_LogAndDisplayWarning("This is a logged and displayed warning message for " <<
124  "TestUnits#" << TU->getID());
125  @endcode
126 */
127 #define OPENFLUID_LogAndDisplayWarning(_stream) \
128  OPENFLUID_LogWarning(_stream); \
129  OPENFLUID_DisplayWarning(_stream)
130 
131 
132 // Log macros for infos
133 
134 /**
135  Adds an information message to simulation log file
136 
137  Exemple:
138  @code{.cpp}
139  OPENFLUID_LogInfo("This is a logged information message for " << "TestUnits#" << TU->getID());
140  @endcode
141 */
142 #define OPENFLUID_LogInfo(_stream) \
143  appendToLog(openfluid::tools::FileLogger::LOG_INFO,_STREAMTOSTRING(_stream))
144 
145 /**
146  Displays an information message to stdout (on screen by default)
147 
148  Exemple:
149  @code{.cpp}
150  OPENFLUID_DisplayInfo("This is a displayed information message for " << "TestUnits#" << TU->getID());
151  @endcode
152 */
153 #define OPENFLUID_DisplayInfo(_stream) \
154  displayToConsole(openfluid::tools::FileLogger::LOG_INFO,_STREAMTOSTRING(_stream))
155 
156 /**
157  Adds an information message to simulation log file and displays it to stdout (on screen by default)
158 
159  Exemple:
160  @code{.cpp}
161  OPENFLUID_LogAndDisplayInfo("This is a logged and displayed information message for " <<
162  "TestUnits#" << TU->getID());
163  @endcode
164 */
165 #define OPENFLUID_LogAndDisplayInfo(_stream) \
166  OPENFLUID_LogInfo(_stream); \
167  OPENFLUID_DisplayInfo(_stream)
168 
169 
170 // Log macros for debug
171 
172 /**
173  @def OPENFLUID_LogDebug
174  Adds a debug message to simulation log file
175 
176  Exemple:
177  @code{.cpp}
178  OPENFLUID_LogDebug("This is a logged debug message for " << "TestUnits#" << TU->getID());
179  @endcode
180 */
181 
182 
183 /**
184  @def OPENFLUID_DisplayDebug
185  Displays a debug message to stdout (on screen by default)
186 
187  Exemple:
188  @code{.cpp}
189  OPENFLUID_DisplayDebug("This is a displayed debug message for " << "TestUnits#" << TU->getID());
190  @endcode
191 */
192 
193 /**
194  @def OPENFLUID_LogAndDisplayDebug
195  Adds a debug message to simulation log file and displays it to stdout (on screen by default)
196 
197  Exemple:
198  @code{.cpp}
199  OPENFLUID_LogAndDisplayDebug("This is a logged and displayed debug message for " << "TestUnits#" << TU->getID());
200  @endcode
201 */
202 
203 #ifndef NDEBUG
204 
205 #define OPENFLUID_LogDebug(_stream) \
206  appendToLog(openfluid::tools::FileLogger::LOG_DEBUG,_STREAMTOSTRING(_stream))
207 
208 #define OPENFLUID_DisplayDebug(_stream) \
209  displayToConsole(openfluid::tools::FileLogger::LOG_DEBUG,_STREAMTOSTRING(_stream))
210 
211 #define OPENFLUID_LogAndDisplayDebug(_stream) \
212  OPENFLUID_LogDebug(_stream); \
213  OPENFLUID_DisplayDebug(_stream)
214 
215 #else
216 
217 #define OPENFLUID_LogDebug(_stream)
218 
219 #define OPENFLUID_DisplayDebug(_stream)
220 
221 #define OPENFLUID_LogAndDisplayDebug(_stream)
222 
223 #endif
224 
225 
226 // =====================================================================
227 // =====================================================================
228 
229 
230 namespace openfluid { namespace ware {
231 
232 
234 {
235  private:
236 
237  const openfluid::base::SimulationStatus* mp_SimStatus;
238 
239  openfluid::base::SimulationLogger* mp_SimLogger;
240 
241  openfluid::core::TimeIndex_t m_PreviousTimeIndex;
242 
243 
244  protected:
245 
246 
247  virtual bool isLinked() const
248  { return (PluggableWare::isLinked() && mp_SimLogger != nullptr && mp_SimStatus != nullptr); };
249 
250  void appendToLog(openfluid::tools::FileLogger::LogType LType, const std::string& Msg) const;
251 
252  void displayToConsole(openfluid::tools::FileLogger::LogType LType, const std::string& Msg) const;
253 
254 
255  openfluid::base::ExceptionContext computeWareContext(const std::string& CodeLoc = "") const;
256 
257  openfluid::base::ExceptionContext computeFrameworkContext(const std::string& CodeLoc = "") const;
258 
259 
260  /**
261  Returns the real beginning date of the simulated period
262  @return the date
263  */
264  openfluid::core::DateTime OPENFLUID_GetBeginDate() const;
265 
266  /**
267  Returns the real ending date of the simulated period
268  @return the date
269  */
270  openfluid::core::DateTime OPENFLUID_GetEndDate() const;
271 
272  /**
273  Returns the current real date corresponding to the current time index
274  @return the date
275  */
276  openfluid::core::DateTime OPENFLUID_GetCurrentDate() const;
277 
278  /**
279  Returns the simulation duration in seconds
280  @return the duration in seconds
281  */
282  openfluid::core::Duration_t OPENFLUID_GetSimulationDuration() const;
283 
284  /**
285  Returns the default DeltaT used by the scheduler
286  @return the deltaT in seconds
287  */
288  openfluid::core::Duration_t OPENFLUID_GetDefaultDeltaT() const;
289 
290  /**
291  Returns the current time index of the simulation, in seconds since the simulation started.
292  When the simulation starts, the time index is equal to zero.
293  @return the current time index in seconds
294  */
295  openfluid::core::TimeIndex_t OPENFLUID_GetCurrentTimeIndex() const;
296 
297  /**
298  Returns the time index of the simulation when the plugged ware was previously run
299  @return the time index in seconds
300  */
301  openfluid::core::TimeIndex_t OPENFLUID_GetPreviousRunTimeIndex() const;
302 
303  /**
304  Returns the current stage of the simulation
305  @return the stage
306  */
307  openfluid::base::SimulationStatus::SimulationStage OPENFLUID_GetCurrentStage() const;
308 
309  std::string OPENFLUID_GetCurrentStageAsString() const;
310 
311  /**
312  Returns the scheduling constraint applied to the simulation (may be NONE)
313  @return the constraint type
314  */
315  openfluid::base::SimulationStatus::SchedulingConstraint OPENFLUID_GetSchedulingConstraint() const;
316 
317 
318  /**
319  Raises a time-marked warning message to the kernel. This does not stops the simulation
320  @param[in] Msg the content of the message
321  */
322  virtual void OPENFLUID_RaiseWarning(const std::string& Msg);
323 
324  /**
325  Raises a time-marked warning message to the kernel. This does not stops the simulation
326  @param[in] Source the source of the message
327  @param[in] Msg the content of the message
328  @deprecated Since version 2.1.0.
329  Use openfluid::ware::SimulationDrivenWare::OPENFLUID_RaiseWarning(const std::string&)
330  or #OPENFLUID_LogWarning instead
331  */
332  virtual void OPENFLUID_RaiseWarning(const std::string& Source, const std::string& Msg) OPENFLUID_DEPRECATED;
333 
334  /**
335  Raises an error message to the kernel. This stops the simulation the next time the kernel has the control
336  @param[in] Msg the content of the message
337  */
338  virtual void OPENFLUID_RaiseError(const std::string& Msg);
339 
340  /**
341  Raises an error message to the kernel. This stops the simulation the next time the kernel has the control
342  @param[in] Source the source of the message
343  @param[in] Msg the content of the message
344  @deprecated Since version 2.1.0.
345  Use openfluid::ware::SimulationDrivenWare::OPENFLUID_RaiseError(const std::string&) instead
346  */
347  virtual void OPENFLUID_RaiseError(const std::string& Source, const std::string& Msg) OPENFLUID_DEPRECATED;
348 
350  mp_SimStatus(nullptr), mp_SimLogger(nullptr), m_PreviousTimeIndex(0) { };
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 
374 } } // openfluid::ware
375 
376 
377 
378 #endif /* __OPENFLUID_WARE_SIMULATIONDRIVENWARE_HPP__ */
void linkToSimulationLogger(openfluid::base::SimulationLogger *SimLogger)
Definition: SimulationDrivenWare.hpp:360
unsigned long long Duration_t
Definition: DateTime.hpp:68
virtual ~SimulationDrivenWare()
Definition: SimulationDrivenWare.hpp:355
Definition: SimulationLogger.hpp:59
Definition: SimulationDrivenWare.hpp:233
#define OPENFLUID_DEPRECATED
Definition: deprecation.hpp:54
Definition: PluggableWare.hpp:97
std::string WareID_t
Definition: TypeDefs.hpp:50
void setPreviousTimeIndex(const openfluid::core::TimeIndex_t &TimeIndex)
Definition: SimulationDrivenWare.hpp:367
SimulationDrivenWare(WareType WType)
Definition: SimulationDrivenWare.hpp:349
virtual bool isLinked() const
Definition: PluggableWare.hpp:122
SchedulingConstraint
Definition: SimulationStatus.hpp:62
Definition: SimulationStatus.hpp:55
virtual bool isLinked() const
Definition: SimulationDrivenWare.hpp:247
Definition: ApplicationException.hpp:47
WareType
Definition: TypeDefs.hpp:62
#define OPENFLUID_API
Definition: dllexport.hpp:87
unsigned long long TimeIndex_t
Definition: DateTime.hpp:62
Definition: ExceptionContext.hpp:53
SimulationStage
Definition: SimulationStatus.hpp:59
LogType
Definition: FileLogger.hpp:69
Class for management of date and time information.
Definition: DateTime.hpp:132