All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #define REQUIRE_SIMULATION_STAGE(stage,msg) \
54  if (OPENFLUID_GetCurrentStage() != (stage)) \
55  { \
56  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
57  throw openfluid::base::FrameworkException(Context,msg); \
58  }
59 
60 #define REQUIRE_SIMULATION_STAGE_GE(stage,msg) \
61  if (OPENFLUID_GetCurrentStage() < (stage)) \
62  { \
63  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
64  throw openfluid::base::FrameworkException(Context,msg); \
65  }
66 
67 #define REQUIRE_SIMULATION_STAGE_LE(stage,msg) \
68  if (OPENFLUID_GetCurrentStage() > (stage)) \
69  { \
70  openfluid::base::ExceptionContext Context = computeFrameworkContext(OPENFLUID_CODE_LOCATION); \
71  throw openfluid::base::FrameworkException(Context,msg); \
72  }
73 
74 
75 // =====================================================================
76 // =====================================================================
77 
78 
79 #define _STREAMTOSTRING(_stream) ((static_cast<std::ostringstream&>(std::ostringstream().flush() << _stream)).str())
80 
81 
82 // Log macros for warnings
83 
84 #define OPENFLUID_LogWarning(_stream) \
85  appendToLog(openfluid::tools::FileLogger::LOG_WARNING,_STREAMTOSTRING(_stream))
86 
87 #define OPENFLUID_DisplayWarning(_stream) \
88  displayToConsole(openfluid::tools::FileLogger::LOG_WARNING,_STREAMTOSTRING(_stream))
89 
90 #define OPENFLUID_LogAndDisplayWarning(_stream) \
91  OPENFLUID_LogWarning(_stream); \
92  OPENFLUID_DisplayWarning(_stream)
93 
94 
95 // Log macros for infos
96 
97 #define OPENFLUID_LogInfo(_stream) \
98  appendToLog(openfluid::tools::FileLogger::LOG_INFO,_STREAMTOSTRING(_stream))
99 
100 #define OPENFLUID_DisplayInfo(_stream) \
101  displayToConsole(openfluid::tools::FileLogger::LOG_INFO,_STREAMTOSTRING(_stream))
102 
103 #define OPENFLUID_LogAndDisplayInfo(_stream) \
104  OPENFLUID_LogInfo(_stream); \
105  OPENFLUID_DisplayInfo(_stream)
106 
107 
108 // Log macros for debug
109 
110 #ifndef NDEBUG
111 
112 #define OPENFLUID_LogDebug(_stream) \
113  appendToLog(openfluid::tools::FileLogger::LOG_DEBUG,_STREAMTOSTRING(_stream))
114 
115 #define OPENFLUID_DisplayDebug(_stream) \
116  displayToConsole(openfluid::tools::FileLogger::LOG_DEBUG,_STREAMTOSTRING(_stream))
117 
118 #define OPENFLUID_LogAndDisplayDebug(_stream) \
119  OPENFLUID_LogDebug(_stream); \
120  OPENFLUID_DisplayDebug(_stream)
121 
122 #else
123 
124 #define OPENFLUID_LogDebug(_stream)
125 
126 #define OPENFLUID_DisplayDebug(_stream)
127 
128 #define OPENFLUID_LogAndDisplayDebug(_stream)
129 
130 #endif
131 
132 
133 // =====================================================================
134 // =====================================================================
135 
136 
137 namespace openfluid { namespace ware {
138 
139 
141 {
142  private:
143 
144  const openfluid::base::SimulationStatus* mp_SimStatus;
145 
146  openfluid::base::SimulationLogger* mp_SimLogger;
147 
148  openfluid::core::TimeIndex_t m_PreviousTimeIndex;
149 
150 
151  protected:
152 
153 
154  virtual bool isLinked() const
155  { return (PluggableWare::isLinked() && mp_SimLogger != NULL && mp_SimStatus != NULL); };
156 
157  void appendToLog(openfluid::tools::FileLogger::LogType LType, const std::string& Msg) const;
158 
159  void displayToConsole(openfluid::tools::FileLogger::LogType LType, const std::string& Msg) const;
160 
161 
162  openfluid::base::ExceptionContext computeWareContext(const std::string& CodeLoc = "") const;
163 
164  openfluid::base::ExceptionContext computeFrameworkContext(const std::string& CodeLoc = "") const;
165 
166 
167  /**
168  Returns the real beginning date of the simulated period
169  @return the date
170  */
171  openfluid::core::DateTime OPENFLUID_GetBeginDate() const;
172 
173  /**
174  Returns the real ending date of the simulated period
175  @return the date
176  */
177  openfluid::core::DateTime OPENFLUID_GetEndDate() const;
178 
179  /**
180  Returns the current real date corresponding to the current time index
181  @return the date
182  */
183  openfluid::core::DateTime OPENFLUID_GetCurrentDate() const;
184 
185  /**
186  Returns the simulation duration in seconds
187  @return the duration in seconds
188  */
189  openfluid::core::Duration_t OPENFLUID_GetSimulationDuration() const;
190 
191  /**
192  Returns the default DeltaT used by the scheduler
193  @return the deltaT in seconds
194  */
195  openfluid::core::Duration_t OPENFLUID_GetDefaultDeltaT() const;
196 
197  /**
198  Returns the current time index of the simulation, in seconds since the simulation started.
199  When the simulation starts, the time index is equal to zero.
200  @return the current time index in seconds
201  */
202  openfluid::core::TimeIndex_t OPENFLUID_GetCurrentTimeIndex() const;
203 
204  /**
205  Returns the time index of the simulation when the plugged ware was previously run
206  @return the time index in seconds
207  */
208  openfluid::core::TimeIndex_t OPENFLUID_GetPreviousRunTimeIndex() const;
209 
210  /**
211  Returns the current stage of the simulation
212  @return the stage
213  */
214  openfluid::base::SimulationStatus::SimulationStage OPENFLUID_GetCurrentStage() const;
215 
216  std::string OPENFLUID_GetCurrentStageAsString() const;
217 
218  /**
219  Returns the scheduling constraint applied to the simulation (may be NONE)
220  @return the constraint type
221  */
222  openfluid::base::SimulationStatus::SchedulingConstraint OPENFLUID_GetSchedulingConstraint() const;
223 
224 
225  /**
226  Raises a time-marked warning message to the kernel. This does not stops the simulation
227  @param[in] Msg the content of the message
228  */
229  virtual void OPENFLUID_RaiseWarning(const std::string& Msg);
230 
231  /**
232  Raises a time-marked warning message to the kernel. This does not stops the simulation
233  @param[in] Source the source of the message
234  @param[in] Msg the content of the message
235  @deprecated Since version 2.1.0.
236  Use openfluid::ware::SimulationDrivenWare::OPENFLUID_RaiseWarning(const std::string&)
237  or #OPENFLUID_LogWarning instead
238  */
239  virtual void OPENFLUID_RaiseWarning(const std::string& Source, const std::string& Msg) OPENFLUID_DEPRECATED;
240 
241  /**
242  Raises an error message to the kernel. This stops the simulation the next time the kernel has the control
243  @param[in] Msg the content of the message
244  */
245  virtual void OPENFLUID_RaiseError(const std::string& Msg);
246 
247  /**
248  Raises an error message to the kernel. This stops the simulation the next time the kernel has the control
249  @param[in] Source the source of the message
250  @param[in] Msg the content of the message
251  @deprecated Since version 2.1.0.
252  Use openfluid::ware::SimulationDrivenWare::OPENFLUID_RaiseError(const std::string&) instead
253  */
254  virtual void OPENFLUID_RaiseError(const std::string& Source, const std::string& Msg) OPENFLUID_DEPRECATED;
255 
257  mp_SimStatus(NULL), mp_SimLogger(NULL), m_PreviousTimeIndex(0) { };
258 
259 
260  public:
261 
263  { };
264 
265  void linkToSimulation(const openfluid::base::SimulationStatus* SimStatus);
266 
268  { mp_SimLogger = SimLogger; };
269 
270  void initializeWare(const WareID_t& ID);
271 
272  void finalizeWare();
273 
275  { m_PreviousTimeIndex = TimeIndex; };
276 
277 };
278 
279 
280 
281 } } // openfluid::ware
282 
283 
284 
285 #endif /* __OPENFLUID_WARE_SIMULATIONDRIVENWARE_HPP__ */
virtual bool isLinked() const
Definition: PluggableWare.hpp:127
LogType
Definition: FileLogger.hpp:69
Class for management of date and time information.
Definition: DateTime.hpp:132
Definition: SimulationStatus.hpp:55
unsigned long long Duration_t
Definition: DateTime.hpp:68
SimulationStage
Definition: SimulationStatus.hpp:59
Definition: SimulationLogger.hpp:59
Definition: PluggableWare.hpp:97
SchedulingConstraint
Definition: SimulationStatus.hpp:62
#define OPENFLUID_DEPRECATED
Definition: deprecation.hpp:54
WareType
Definition: PluggableWare.hpp:101
Definition: ExceptionContext.hpp:53
virtual bool isLinked() const
Definition: SimulationDrivenWare.hpp:154
void linkToSimulationLogger(openfluid::base::SimulationLogger *SimLogger)
Definition: SimulationDrivenWare.hpp:267
SimulationDrivenWare(WareType WType)
Definition: SimulationDrivenWare.hpp:256
#define OPENFLUID_API
Definition: dllexport.hpp:87
unsigned long long TimeIndex_t
Definition: DateTime.hpp:62
Definition: SimulationDrivenWare.hpp:140
virtual ~SimulationDrivenWare()
Definition: SimulationDrivenWare.hpp:262
void setPreviousTimeIndex(const openfluid::core::TimeIndex_t &TimeIndex)
Definition: SimulationDrivenWare.hpp:274
std::string WareID_t
Definition: TypeDefs.hpp:50