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  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_logwarning
100 
101  @cond OpenFLUID:completion
102  {
103  "contexts" : ["SIMULATOR", "OBSERVER"],
104  "menupath" : ["Compute code", "Messages"],
105  "title" : "Log warning message",
106  "text" : "OPENFLUID_LogWarning(%%SEL_START%%\"message\"%%SEL_END%%)"
107  }
108  @endcond
109 */
110 #define OPENFLUID_LogWarning(_stream) \
111  appendToLog(openfluid::tools::FileLogger::LOG_WARNING,_STREAMTOSTRING(_stream))
112 
113 /**
114  Displays a warning message to stdout (on screen by default)
115 
116  Exemple:
117  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_displaywarning
118 
119  @cond OpenFLUID:completion
120  {
121  "contexts" : ["SIMULATOR", "OBSERVER"],
122  "menupath" : ["Compute code", "Messages"],
123  "title" : "Display warning message",
124  "text" : "OPENFLUID_DisplayWarning(%%SEL_START%%\"message\"%%SEL_END%%)"
125  }
126  @endcond
127 */
128 #define OPENFLUID_DisplayWarning(_stream) \
129  displayToConsole(openfluid::tools::FileLogger::LOG_WARNING,_STREAMTOSTRING(_stream))
130 
131 /**
132  Adds a warning message to simulation log file and displays it to stdout (on screen by default)
133 
134  Exemple:
135  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_logdisplaywarning
136 
137  @cond OpenFLUID:completion
138  {
139  "contexts" : ["SIMULATOR", "OBSERVER"],
140  "menupath" : ["Compute code", "Messages"],
141  "title" : "Log and warning debug message",
142  "text" : "OPENFLUID_LogAndDisplayWarning(%%SEL_START%%\"message\"%%SEL_END%%)"
143  }
144  @endcond
145 */
146 #define OPENFLUID_LogAndDisplayWarning(_stream) \
147  OPENFLUID_LogWarning(_stream); \
148  OPENFLUID_DisplayWarning(_stream)
149 
150 
151 // Log macros for infos
152 
153 /**
154  Adds an information message to simulation log file
155 
156  Exemple:
157  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_loginfo
158 
159  @cond OpenFLUID:completion
160  {
161  "contexts" : ["SIMULATOR", "OBSERVER"],
162  "menupath" : ["Compute code", "Messages"],
163  "title" : "Log information message",
164  "text" : "OPENFLUID_LogInfo(%%SEL_START%%\"message\"%%SEL_END%%)"
165  }
166  @endcond
167 */
168 #define OPENFLUID_LogInfo(_stream) \
169  appendToLog(openfluid::tools::FileLogger::LOG_INFO,_STREAMTOSTRING(_stream))
170 
171 /**
172  Displays an information message to stdout (on screen by default)
173 
174  Exemple:
175  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_displayinfo
176 
177  @cond OpenFLUID:completion
178  {
179  "contexts" : ["SIMULATOR", "OBSERVER"],
180  "menupath" : ["Compute code", "Messages"],
181  "title" : "Display information message",
182  "text" : "OPENFLUID_DisplayInfo(%%SEL_START%%\"message\"%%SEL_END%%)"
183  }
184  @endcond
185 */
186 #define OPENFLUID_DisplayInfo(_stream) \
187  displayToConsole(openfluid::tools::FileLogger::LOG_INFO,_STREAMTOSTRING(_stream))
188 
189 /**
190  Adds an information message to simulation log file and displays it to stdout (on screen by default)
191 
192  Exemple:
193  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_logdisplayinfo
194 
195  @cond OpenFLUID:completion
196  {
197  "contexts" : ["SIMULATOR", "OBSERVER"],
198  "menupath" : ["Compute code", "Messages"],
199  "title" : "Log and display information message",
200  "text" : "OPENFLUID_LogAndDisplayInfo(%%SEL_START%%\"message\"%%SEL_END%%)"
201  }
202  @endcond
203 */
204 #define OPENFLUID_LogAndDisplayInfo(_stream) \
205  OPENFLUID_LogInfo(_stream); \
206  OPENFLUID_DisplayInfo(_stream)
207 
208 
209 // Log macros for debug
210 
211 /**
212  @def OPENFLUID_LogDebug
213  Adds a debug message to simulation log file
214 
215  Exemple:
216  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_logdebug
217 
218  @cond OpenFLUID:completion
219  {
220  "contexts" : ["SIMULATOR", "OBSERVER"],
221  "menupath" : ["Compute code", "Messages"],
222  "title" : "Log debug message",
223  "text" : "OPENFLUID_LogDebug(%%SEL_START%%\"message\"%%SEL_END%%)"
224  }
225  @endcond
226 */
227 
228 /**
229  @def OPENFLUID_DisplayDebug
230  Displays a debug message to stdout (on screen by default)
231 
232  Exemple:
233  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_displaydebug
234 
235  @cond OpenFLUID:completion
236  {
237  "contexts" : ["SIMULATOR", "OBSERVER"],
238  "menupath" : ["Compute code", "Messages"],
239  "title" : "Display debug message",
240  "text" : "OPENFLUID_DisplayDebug(%%SEL_START%%\"message\"%%SEL_END%%)"
241  }
242  @endcond
243 */
244 
245 /**
246  @def OPENFLUID_LogAndDisplayDebug
247  Adds a debug message to simulation log file and displays it to stdout (on screen by default)
248 
249  @snippet help.snippets.infos-debug/InfosDebugSim.cpp macro_logdisplaydebug
250 
251  @cond OpenFLUID:completion
252  {
253  "contexts" : ["SIMULATOR", "OBSERVER"],
254  "menupath" : ["Compute code", "Messages"],
255  "title" : "Log and display debug message",
256  "text" : "OPENFLUID_LogAndDisplayDebug(%%SEL_START%%\"message\"%%SEL_END%%)"
257  }
258  @endcond
259 */
260 
261 #ifndef NDEBUG
262 
263 #define OPENFLUID_LogDebug(_stream) \
264  appendToLog(openfluid::tools::FileLogger::LOG_DEBUG,_STREAMTOSTRING(_stream))
265 
266 #define OPENFLUID_DisplayDebug(_stream) \
267  displayToConsole(openfluid::tools::FileLogger::LOG_DEBUG,_STREAMTOSTRING(_stream))
268 
269 #define OPENFLUID_LogAndDisplayDebug(_stream) \
270  OPENFLUID_LogDebug(_stream); \
271  OPENFLUID_DisplayDebug(_stream)
272 
273 #else
274 
275 #define OPENFLUID_LogDebug(_stream)
276 
277 #define OPENFLUID_DisplayDebug(_stream)
278 
279 #define OPENFLUID_LogAndDisplayDebug(_stream)
280 
281 #endif
282 
283 
284 // =====================================================================
285 // =====================================================================
286 
287 
288 namespace openfluid { namespace ware {
289 
290 
292 {
293  private:
294 
295  const openfluid::base::SimulationStatus* mp_SimStatus;
296 
297  openfluid::base::SimulationLogger* mp_SimLogger;
298 
299  openfluid::core::TimeIndex_t m_PreviousTimeIndex;
300 
301 
302  protected:
303 
304 
305  virtual bool isLinked() const
306  {
307  return (PluggableWare::isLinked() && mp_SimLogger != nullptr && mp_SimStatus != nullptr);
308  }
309 
310  void appendToLog(openfluid::tools::FileLogger::LogType LType, const std::string& Msg) const;
311 
312  void displayToConsole(openfluid::tools::FileLogger::LogType LType, const std::string& Msg) const;
313 
314 
315  openfluid::base::ExceptionContext computeWareContext(const std::string& CodeLoc = "") const;
316 
317  openfluid::base::ExceptionContext computeFrameworkContext(const std::string& CodeLoc = "") const;
318 
319 
320  /**
321  Returns the real beginning date of the simulated period
322  @return the date
323 
324  @cond OpenFLUID:completion
325  {
326  "contexts" : ["SIMULATOR", "OBSERVER"],
327  "menupath" : ["Compute code", "Time"],
328  "title" : "Get begin date of simulation",
329  "text" : "OPENFLUID_GetBeginDate()"
330  }
331  @endcond
332  */
333  openfluid::core::DateTime OPENFLUID_GetBeginDate() const;
334 
335  /**
336  Returns the real ending date of the simulated period
337  @return the date
338 
339  @cond OpenFLUID:completion
340  {
341  "contexts" : ["SIMULATOR", "OBSERVER"],
342  "menupath" : ["Compute code", "Time"],
343  "title" : "Get end date of simulation",
344  "text" : "OPENFLUID_GetEndDate()"
345  }
346  @endcond
347  */
348  openfluid::core::DateTime OPENFLUID_GetEndDate() const;
349 
350  /**
351  Returns the current real date corresponding to the current time index
352  @return the date
353 
354  @cond OpenFLUID:completion
355  {
356  "contexts" : ["SIMULATOR", "OBSERVER"],
357  "menupath" : ["Compute code", "Time"],
358  "title" : "Get begin current date of simulation",
359  "text" : "OPENFLUID_GetCurrentDate()"
360  }
361  @endcond
362  */
363  openfluid::core::DateTime OPENFLUID_GetCurrentDate() const;
364 
365  /**
366  Returns the simulation duration in seconds
367  @return the duration in seconds
368 
369  @cond OpenFLUID:completion
370  {
371  "contexts" : ["SIMULATOR", "OBSERVER"],
372  "menupath" : ["Compute code", "Time"],
373  "title" : "Get total simulation duration",
374  "text" : "OPENFLUID_GetSimulationDuration()"
375  }
376  @endcond
377  */
378  openfluid::core::Duration_t OPENFLUID_GetSimulationDuration() const;
379 
380  /**
381  Returns the default DeltaT used by the scheduler
382  @return the deltaT in seconds
383 
384  @cond OpenFLUID:completion
385  {
386  "contexts" : ["SIMULATOR", "OBSERVER"],
387  "menupath" : ["Compute code", "Time"],
388  "title" : "Get default deltaT",
389  "text" : "OPENFLUID_GetDefaultDeltaT()"
390  }
391  @endcond
392  */
393  openfluid::core::Duration_t OPENFLUID_GetDefaultDeltaT() const;
394 
395  /**
396  Returns the current time index of the simulation, in seconds since the simulation started.
397  When the simulation starts, the time index is equal to zero.
398  @return the current time index in seconds
399 
400  @cond OpenFLUID:completion
401  {
402  "contexts" : ["SIMULATOR", "OBSERVER"],
403  "menupath" : ["Compute code", "Time"],
404  "title" : "Get current time index",
405  "text" : "OPENFLUID_GetCurrentTimeIndex()"
406  }
407  @endcond
408  */
409  openfluid::core::TimeIndex_t OPENFLUID_GetCurrentTimeIndex() const;
410 
411  /**
412  Returns the time index of the simulation when the plugged ware was previously run
413  @return the time index in seconds
414 
415  @cond OpenFLUID:completion
416  {
417  "contexts" : ["SIMULATOR", "OBSERVER"],
418  "menupath" : ["Compute code", "Time"],
419  "title" : "Get time index of the previous ware run",
420  "text" : "OPENFLUID_GetPreviousRunTimeIndex()"
421  }
422  @endcond
423  */
424  openfluid::core::TimeIndex_t OPENFLUID_GetPreviousRunTimeIndex() const;
425 
426  /**
427  Returns the current stage of the simulation
428  @return the stage
429 
430  @cond OpenFLUID:completion
431  {
432  "contexts" : ["SIMULATOR", "OBSERVER"],
433  "menupath" : ["Compute code", "Time"],
434  "title" : "Get current simulation stage",
435  "text" : "OPENFLUID_GetCurrentStage()"
436  }
437  @endcond
438  */
439  openfluid::base::SimulationStatus::SimulationStage OPENFLUID_GetCurrentStage() const;
440 
441  /**
442  Returns the current stage of the simulation as a string
443  @return the stage name
444 
445  @cond OpenFLUID:completion
446  {
447  "contexts" : ["SIMULATOR", "OBSERVER"],
448  "menupath" : ["Compute code", "Time"],
449  "title" : "Get current simulation stage as a string",
450  "text" : "OPENFLUID_GetCurrentStageAsString()"
451  }
452  @endcond
453  */
454  std::string OPENFLUID_GetCurrentStageAsString() const;
455 
456  /**
457  Returns the scheduling constraint applied to the simulation (may be NONE)
458  @return the constraint type
459 
460  @cond OpenFLUID:completion
461  {
462  "contexts" : ["SIMULATOR", "OBSERVER"],
463  "menupath" : ["Compute code", "Time"],
464  "title" : "Get scheduling constraint mode",
465  "text" : "OPENFLUID_GetSchedulingConstraint()"
466  }
467  @endcond
468  */
469  openfluid::base::SimulationStatus::SchedulingConstraint OPENFLUID_GetSchedulingConstraint() const;
470 
471 
472  /**
473  Raises a time-marked warning message to the kernel. This does not stops the simulation
474  @param[in] Msg the content of the message
475  */
476  virtual void OPENFLUID_RaiseWarning(const std::string& Msg);
477 
478  /**
479  Raises a time-marked warning message to the kernel. This does not stops the simulation
480  @param[in] Source the source of the message
481  @param[in] Msg the content of the message
482  @deprecated Since version 2.1.0.
483  Use openfluid::ware::SimulationDrivenWare::OPENFLUID_RaiseWarning(const std::string&)
484  or #OPENFLUID_LogWarning instead
485  */
486  [[deprecated]] virtual void OPENFLUID_RaiseWarning(const std::string& Source, const std::string& Msg);
487 
488  /**
489  Raises an error message to the kernel. This stops the simulation the next time the kernel has the control
490  @param[in] Msg the content of the message
491 
492  @cond OpenFLUID:completion
493  {
494  "contexts" : ["SIMULATOR", "OBSERVER"],
495  "menupath" : ["Compute code", "Messages"],
496  "title" : "Raise fatal error",
497  "text" : "OPENFLUID_RaiseError(%%SEL_START%%\"message\"%%SEL_END%%)"
498  }
499  @endcond
500  */
501  virtual void OPENFLUID_RaiseError(const std::string& Msg);
502 
503  /**
504  Raises an error message to the kernel. This stops the simulation the next time the kernel has the control
505  @param[in] Source the source of the message
506  @param[in] Msg the content of the message
507  @deprecated Since version 2.1.0.
508  Use openfluid::ware::SimulationDrivenWare::OPENFLUID_RaiseError(const std::string&) instead
509  */
510  [[deprecated]] virtual void OPENFLUID_RaiseError(const std::string& Source, const std::string& Msg);
511 
513  mp_SimStatus(nullptr), mp_SimLogger(nullptr), m_PreviousTimeIndex(0)
514  { }
515 
516 
517  public:
518 
520  { }
521 
522  void linkToSimulation(const openfluid::base::SimulationStatus* SimStatus);
523 
525  {
526  mp_SimLogger = SimLogger;
527  }
528 
529  void initializeWare(const WareID_t& ID);
530 
531  void finalizeWare();
532 
534  {
535  m_PreviousTimeIndex = TimeIndex;
536  }
537 
538 };
539 
540 
541 } } // openfluid::ware
542 
543 
544 #endif /* __OPENFLUID_WARE_SIMULATIONDRIVENWARE_HPP__ */
SchedulingConstraint
Definition: SimulationStatus.hpp:62
void linkToSimulationLogger(openfluid::base::SimulationLogger *SimLogger)
Definition: SimulationDrivenWare.hpp:524
std::uint64_t TimeIndex_t
Definition: TypeDefs.hpp:298
Definition: ExceptionContext.hpp:53
std::string WareID_t
Definition: TypeDefs.hpp:49
Class for management of date and time information.
Definition: DateTime.hpp:87
SimulationDrivenWare(WareType WType)
Definition: SimulationDrivenWare.hpp:512
virtual bool isLinked() const
Definition: PluggableWare.hpp:123
Definition: SimulationDrivenWare.hpp:291
std::uint64_t Duration_t
Definition: TypeDefs.hpp:312
Definition: SimulationLogger.hpp:57
virtual bool isLinked() const
Definition: SimulationDrivenWare.hpp:305
WareType
Definition: TypeDefs.hpp:60
Definition: SimulationStatus.hpp:55
Definition: ApplicationException.hpp:47
SimulationStage
Definition: SimulationStatus.hpp:59
Definition: PluggableWare.hpp:97
virtual ~SimulationDrivenWare()
Definition: SimulationDrivenWare.hpp:519
void setPreviousTimeIndex(const openfluid::core::TimeIndex_t &TimeIndex)
Definition: SimulationDrivenWare.hpp:533
#define OPENFLUID_API
Definition: dllexport.hpp:86
LogType
Definition: FileLogger.hpp:72