Nemo  2.3.46
param.h
Go to the documentation of this file.
1 
30 #ifndef PARAM_H
31 #define PARAM_H
32 
33 #include <iostream>
34 #include <fstream>
35 #include <string>
36 #include <string.h>
37 #include <assert.h>
38 #include <map>
39 #include <deque>
40 #include <list>
41 #include <vector>
42 #include "handler.h"
43 #include "tmatrix.h"
44 #include "types.h"
45 #include "output.h"
46 
47 using namespace std;
48 class SimComponent;
49 class ParamUpdaterBase;
53 class Param
54  {
56  string _name;
58  string _arg;
60  string _input_arg;
64  bool _isSet;
66  bool _isBounded;
71  double _bounds[2];
73  unsigned int _setAtGeneration;
78 
80  vector< string > _externalFile;
82 
84  map< unsigned int, string > _temporalArgs;
87 
89  vector< string > _multiArgs;
91 
92  public:
103  Param (string& Name, param_t Type, bool mandatory, bool bounded, double low_bnd, double up_bnd,
104  SimComponent* owner, ParamUpdaterBase* updater);
105 
107  Param (const Param& P);
108 
109  virtual ~Param ();
110 
112  void reset ();
113 
114  //accessors:
116  void setName (string value) {_name = value;}
118  void setArg (string value) {_arg = value; _input_arg = value;}
120  void setType (param_t value) {_type = value;}
122  void setIsSet (bool value) {_isSet = value;}
124  void setIsBounded (bool value) {_isBounded = value;}
126  void setBounds (double low_bnd,double up_bnd) {_bounds[0]=low_bnd; _bounds[1]=up_bnd;}
128  void setOwner (SimComponent *owner) {_myOwner = owner;}
130  void setUpdater (ParamUpdaterBase* updater) {_myUpdater = updater;}
131 
132  void setAtGeneration (unsigned int generation) {_setAtGeneration = generation;}
133 
134 
135  string getName () {return _name;}
136  string getArg () {return _input_arg;}
137  param_t getType () {return _type;}
138  bool isSet () {return _isSet;}
139  bool isBounded () {return _isBounded;}
140  bool isRequired () {return _isRequired;}
141  bool isTemporal () {return _isTemporal;}
142  bool hasMultipleArgs () {return _hasMultipleArgs;}
143  bool hasExternalFile () {return _hasExternalFile;}
144  double getBound (unsigned int i) {return _bounds[i];}
145  SimComponent* getOwner () {return _myOwner;}
146  ParamUpdaterBase* getUpdater () {return _myUpdater;}
147 
148  deque< unsigned int > getUpdatingDates ();
149 
150  deque< string > getTemporalArgs();
151 
152  vector< string > getMultiArgs();
153 
154  vector< string > getExternalFiles() {return _externalFile;}
155 
159  bool set (string arg, string& errmsg);
160 
162  bool update (unsigned int generation);
163 
167  double getValue ();
168 
170  bool isMatrix ()
171  { return (_type == MAT || (_arg.size() != 0 ? _arg[0] == '{' : false) ); }
172 
177  void getMatrix (TMatrix* mat);
178 
179  void getVariableMatrix (vector< vector <double> >* mat);
180 
184  void parse_matrix (TMatrix* mat);
185  void parse_variable_matrix (vector< vector <double> >* mat);
186  bool parseArgument (string& arg);
187  bool parseTemporalArgument (const string& arg);
188  bool parseAgeSpecArgument (const string& arg);
189  bool parseSubParamArgument (const string& arg);
190  string checkArgumentForExpansion (string arg);
191  string getArgumentFromFile (string file);
192 
194  void show_up ();
195 
196  };
197 
198 
203 class ParamSet
204  {
205 
206  string _name;
207  bool _isSet;
209  map<string, Param*> _params;
212 
213  public:
214 
215  ParamSet ( ) : _isSet(0), _isRequired(0) { }
216  ParamSet (const ParamSet& PS);
217  ~ParamSet ( );
219  void reset ( );
220 
222  void clear ( ) {_params.clear();}
226  bool check_consistency ( );
228  void show_up ( );
230  void print (ofstream& FILE);
232  int size ( ) {return _params.size();}
234  map<string, Param*>& getAllParams ( ) {return _params;}
237 
238  void add_param (Param* param) {_params[param->getName()] = param;}
247  void add_param (string Name, param_t Type, bool mandatory, bool isBounded,
248  double low_bnd, double up_bnd)
249  {add_param(Name, Type, mandatory, isBounded, low_bnd, up_bnd, 0);}
250 
251  void add_param (string Name, param_t Type, bool mandatory, bool isBounded,
252  double low_bnd, double up_bnd, ParamUpdaterBase* updater);
259  bool set_param (string Name, string Arg);
263  Param* get_param (string name);
265  bool update_param (string Name, unsigned int generation);
267  void setName (string value) {_name = value;}
269  void setIsRequired (bool value) {_isRequired = value;}
271  void setOwner (SimComponent* owner) {_myOwner = owner;}
273  bool isSet () {return _isSet;}
275  string getName () {return _name;}
277  bool isRequired () {return _isRequired;}
279  bool isSet (string name) {return (get_param(name))->isSet();}
281  bool isMatrix (string name) {return (get_param(name))->isMatrix();}
283  bool isTemporal (string name) {return (get_param(name))->isTemporal();}
285  string getArg (string name) {return (get_param(name))->getArg();}
287  double getValue (string name) {return (get_param(name))->getValue();}
289  void getMatrix (string name, TMatrix* mat) {return (get_param(name))->getMatrix(mat);}
291  list<ParamUpdaterBase*> getUpdaters();
293  ParamSet& operator= (const ParamSet& PS);
294  };
295 
296 
297 //class ParamUpdaterBase
298 //
300 class ParamUpdaterBase : public Handler {
301 
302 protected:
304  list< Param* > _params;
305 
306 public:
307 
308  typedef list< Param* >::iterator PIT;
309 
310 public:
311 
313 
315  {
316  _params.assign(PU._params.begin(), PU._params.end());
317  }
318 
319  virtual ~ParamUpdaterBase() {}
320 
321  virtual void init () = 0;
322  virtual void update () {}
324  virtual bool update (unsigned int generation) = 0;
325 
326  virtual SimComponent* getComponent () = 0;
328  virtual void addParam (Param* param) {_params.push_back( param );}
330  virtual void reset ( ) {_params.clear();}
332  list< Param* > getParams ( ) {return _params;}
333 
334 };
335 
336 
337 //class ParamUpdater
338 //
348 template <class SC> class ParamUpdater : public virtual ParamUpdaterBase {
349 
350  bool (SC::*_myUpdaterFunction) (void);
352 
353 public:
354 
355  ParamUpdater(bool (SC::*updateFuncPtr) (void) )
356  {
357  _myUpdaterFunction = updateFuncPtr;
358  }
359 
361  {
362  _params.assign(PU._params.begin(), PU._params.end());
363  _myUpdaterFunction = PU._myUpdaterFunction;
364  _myComponent = PU._myComponent;
365  }
366 
367  virtual ~ParamUpdater ()
368  {
369  for(PIT pit = _params.begin(); pit != _params.end(); pit++)
370  if((*pit)->getUpdater() == this) (*pit)->setUpdater(0);
371  }
372 
374  virtual void init ()
375  {
376  _myComponent = dynamic_cast<SC*> ( (*_params.begin())->getOwner() );
377 
378  for(PIT pit = ++_params.begin(); pit != _params.end(); pit++)
379  assert(_myComponent == (*pit)->getOwner());
380  }
382  virtual bool update (unsigned int generation)
383  {
384  return ((( dynamic_cast<SC*> (_myComponent))->*_myUpdaterFunction) ());
385  }
387  void setFuncPtr ( bool (SC::*updateFuncPtr) (void))
388  {
389  _myUpdaterFunction = updateFuncPtr;
390  }
392  virtual SC* getComponent () {return _myComponent;}
393 };
394 
395 #endif
void setOwner(SimComponent *owner)
Sets the pointer to owner.
Definition: param.h:128
bool isSet(string name)
Accessor to the parameters status flag.
Definition: param.h:279
virtual bool update(unsigned int generation)
Calls the SimComponent's updating function using its pointer.
Definition: param.h:382
virtual ~ParamUpdater()
Definition: param.h:367
Implementation of the ParamUpdaterBase interface.
Definition: param.h:348
void setType(param_t value)
Sets the parameter's type (see types.h)
Definition: param.h:120
string getName()
Definition: param.h:135
void setArg(string value)
Sets the parameter's argument.
Definition: param.h:118
SC * _myComponent
Definition: param.h:351
void setName(string value)
Sets the container's name.
Definition: param.h:267
string _input_arg
The input argument string, kept untouched as a backup for logging.
Definition: param.h:60
double getBound(unsigned int i)
Definition: param.h:144
bool isTemporal(string name)
Check if the parameter "name" has temporal arguments.
Definition: param.h:283
virtual void update()
Definition: param.h:322
unsigned int _setAtGeneration
Generation at which the parameter has been set/updated.
Definition: param.h:73
Base class of the ParamUpdater class used to handle the temporal parameter argument values...
Definition: param.h:300
bool _isTemporal
Flag telling if this parameter has temporal arguments.
Definition: param.h:86
void setAtGeneration(unsigned int generation)
Definition: param.h:132
Definition: types.h:78
void getMatrix(string name, TMatrix *mat)
Accessor to the parameters matrix.
Definition: param.h:289
virtual void init()
Sets the pointer to the SimComponent.
Definition: param.h:374
ParamSet()
Definition: param.h:215
param_t
Param's types.
Definition: types.h:77
vector< string > _multiArgs
Multiple arguments.
Definition: param.h:89
bool _hasMultipleArgs
Definition: param.h:90
void setIsRequired(bool value)
Sets the _isRequired flag meaning this container is mandatory and must be set in order to run a simul...
Definition: param.h:269
list< Param * > _params
List of the parameters affected by this updater.
Definition: param.h:304
bool isMatrix()
Checks if the argument is of matrix type.
Definition: param.h:170
bool _isRequired
Definition: param.h:208
string getName()
Name accessor.
Definition: param.h:275
string _arg
The argument string, set by the ParamsParser upon initialization.
Definition: param.h:58
virtual SC * getComponent()
Accessor to the SimCimponent.
Definition: param.h:392
virtual void addParam(Param *param)
Adds a parameter to the stack.
Definition: param.h:328
bool isBounded()
Definition: param.h:139
vector< string > _externalFile
External argument file.
Definition: param.h:80
void clear()
Empties the parameter containers (no delete).
Definition: param.h:222
bool _isRequired
Flag telling if this parameter is mandatory.
Definition: param.h:69
void setBounds(double low_bnd, double up_bnd)
Sets the bounds.
Definition: param.h:126
void setOwner(SimComponent *owner)
Sets the pointer to the SimComponents that owns this set.
Definition: param.h:271
param_t getType()
Definition: param.h:137
bool _isSet
Definition: param.h:207
void add_param(string Name, param_t Type, bool mandatory, bool isBounded, double low_bnd, double up_bnd)
Adds a new param specified by arguments to the list.
Definition: param.h:247
SimComponent * _myOwner
Pointer to the component that declared this parameter.
Definition: param.h:75
Interface to all basic components of a simulation (traits, life cycle events, pop, etc. ).
Definition: simcomponent.h:45
Service handler (an observer).
Definition: handler.h:36
This structure stores one parameter, its definition and its string argument.
Definition: param.h:53
bool _isBounded
Flag telling if the parameter has a bounded argument value.
Definition: param.h:66
int size()
Returns the number of parameters contained.
Definition: param.h:232
bool isMatrix(string name)
Check if the parameter "name" is of matrix type.
Definition: param.h:281
list< Param * > getParams()
Returns the list of parameters.
Definition: param.h:332
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
bool hasExternalFile()
Definition: param.h:143
void setIsSet(bool value)
Sets the _isSet flag.
Definition: param.h:122
bool hasMultipleArgs()
Definition: param.h:142
bool isTemporal()
Definition: param.h:141
bool isRequired()
Definition: param.h:140
map< unsigned int, string > _temporalArgs
The temporal arguments.
Definition: param.h:84
bool(SC::* _myUpdaterFunction)(void)
Definition: param.h:350
void setName(string value)
Sets the parameter's name.
Definition: param.h:116
double getValue(string name)
Accessor the parameters value.
Definition: param.h:287
virtual void reset()
Clears the parameters stack.
Definition: param.h:330
vector< string > getExternalFiles()
Definition: param.h:154
string getArg()
Definition: param.h:136
ParamUpdaterBase * _myUpdater
Pointer to an ParamUpdater object.
Definition: param.h:77
string getArg(string name)
Accessor to the parameters argument string.
Definition: param.h:285
void setIsBounded(bool value)
Sets the _isBounded flag.
Definition: param.h:124
void setFuncPtr(bool(SC::*updateFuncPtr)(void))
Sets the pointer to the updating function.
Definition: param.h:387
bool isRequired()
Accessor to the mandatory flag.
Definition: param.h:277
ParamUpdaterBase()
Definition: param.h:312
map< string, Param * > _params
Definition: param.h:209
list< Param * >::iterator PIT
Definition: param.h:308
bool isSet()
Accessor to the status flag.
Definition: param.h:273
ParamUpdater(const ParamUpdater< SC > &PU)
Definition: param.h:360
bool _isSet
Flag set once the parameter has recieved the right argument.
Definition: param.h:64
void setUpdater(ParamUpdaterBase *updater)
Sets the pointer to the updater object.
Definition: param.h:130
bool _hasExternalFile
Definition: param.h:81
Parameters container, implemented in each SimComponent.
Definition: param.h:203
string _name
The name of the parameter as read in the init file.
Definition: param.h:56
SimComponent * getOwner()
Definition: param.h:145
string _name
Definition: param.h:206
ParamUpdaterBase(const ParamUpdaterBase &PU)
Definition: param.h:314
virtual ~ParamUpdaterBase()
Definition: param.h:319
param_t _type
The type of the argument, must one of DBL, INT, BOOL, STR or MAT (see types.h).
Definition: param.h:62
ParamUpdater(bool(SC::*updateFuncPtr)(void))
Definition: param.h:355
SimComponent * _myOwner
Pointer to the component that declared this parameter.
Definition: param.h:211
map< string, Param * > & getAllParams()
Returns the complete list of parameters.
Definition: param.h:234
void add_param(Param *param)
Adds the param argument to the list.
Definition: param.h:238
bool isSet()
Definition: param.h:138
ParamUpdaterBase * getUpdater()
Definition: param.h:146

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR