Nemo  2.3.46
ParamManager Class Reference

Class to manage the sets of parameters of the simulation components. More...

#include <basicsimulation.h>

+ Inheritance diagram for ParamManager:
+ Collaboration diagram for ParamManager:

Public Member Functions

 ParamManager ()
 Cstor. More...
 
 ~ParamManager ()
 
void add_paramset (ParamSet *paramset)
 Adds a ParamSet to the list of the parameter sets of the simulation. More...
 
ParamSetget_paramset (string &name)
 Looks for paramset with "name" in the list of parameter sets. More...
 
void build_allParams ()
 Clears and fills the _allParams list with the ParamSet's of the simulation components. More...
 
list< ParamSet * > & get_allParams ()
 Accessor of the whole list of the parameter sets. More...
 
bool set_parameters (map< string, string > &simparams, bool silent)
 Sets the parameters of the simulation with the argument strings given in input. More...
 
bool param_consistency_check ()
 Checks if all the mandatory parameters are set so that the simulation can be launched. More...
 
void build_records (map< string, vector< string > > &initParams)
 Builds the list of simulation parameters from the parsed input file(s). More...
 
list< map< string, string > > & get_simRecords ()
 Accessor to the simulations parameter list. More...
 
map< string, string > & get_firstRecord ()
 Accessor to the first element in the simulations parameter list. More...
 
int get_nbSims ()
 Accessor to the size of the simulations parameter list, i.e. More...
 
- Public Member Functions inherited from ComponentManager
 ComponentManager ()
 
 ~ComponentManager ()
 
void build_component_list ()
 Clears and builds the list of all components from the lists of trait prototype templates and life cycle event templates. More...
 
void add_component (SimComponent *cmpt)
 Push a component at the back of the component list. More...
 
void add_trait (TraitPrototype *trait)
 Add a trait prototype to the template and component lists. More...
 
void add_LCE (LifeCycleEvent *event)
 Add a life cycle event to the template and component lists. More...
 
TraitPrototypeget_trait (string name)
 Search for component with "name" in the trait prototype list. More...
 
LifeCycleEventget_LCE (string name)
 Search for component with "name" in the life cycle events list. More...
 

Protected Attributes

list< ParamSet * > _allParams
 A list of all the parameter sets of all the simulation components loaded in the _component list of the ComponentManager. More...
 
map< string, string > _inputParams
 A map of the parameters and their arguments of the current (running) simulation. More...
 
map< unsigned int, list< pair
< string, string > > > 
_temporalParams
 Lists of parameters to be updated during a simulation indexed by generation update time. More...
 
list< map< string, string > > _simRecords
 Sets of parameters of all the simulations to perform. More...
 
ParamSet _paramSet
 The ParamSet param set of the simulation. More...
 
- Protected Attributes inherited from ComponentManager
list< SimComponent * > _components
 List of all the simulation components. More...
 
list< TraitPrototype * > _TTrait_Templates
 List of all trait prototypes of the simulation, a subset of _components list. More...
 
list< LifeCycleEvent * > _LCE_Templates
 List of all the life-cycle events of the simulation, a subset of _components list. More...
 

Private Member Functions

string setFilename (string &fstring, unsigned int sim, vector< string > &args, vector< unsigned int > &arg_no, bool check_arg_no)
 
string stripFormatString (string &str, unsigned int &index)
 
string setArgString (string &fmt, string &arg, unsigned int arg_pos)
 
string lowercase (string &input)
 

Detailed Description

Class to manage the sets of parameters of the simulation components.

This class performs parameters setting and checking for the whole set of the simulation components. Provides access to derived classes to the complete list of parameter sets. Also sets the list of simulations parameters in case of sequential parameters found in input. It stores and builds the simulation parameters set.

See also
ParamsParser and derived classes. These are the input parameters providers.

Constructor & Destructor Documentation

ParamManager::ParamManager ( )

Cstor.

Builds the simulation PramaSet.

References _paramSet, ParamSet::add_param(), INT, ParamSet::setIsRequired(), ParamSet::setName(), ParamSet::setOwner(), and STR.

98 {
99  _paramSet.setName("simulation");
100  _paramSet.setIsRequired(true);
101  _paramSet.setOwner(NULL);
102  _paramSet.add_param("filename",STR,true,false,0,0);
103  _paramSet.add_param("root_dir",STR,false,false,0,0);
104  _paramSet.add_param("logfile",STR,false,false,0,0);
105  _paramSet.add_param("postexec_script",STR,false,false,0,0);
106  _paramSet.add_param("postexec_args",STR,false,false,0,0);
107  _paramSet.add_param("random_seed",INT,false,false,0,0);
108  _paramSet.add_param("replicates",INT,true,false,0,0);
109  _paramSet.add_param("generations",INT,true,false,0,0);
110  _paramSet.add_param("run_mode",STR,false,false,0,0);
111 }
ParamSet _paramSet
The ParamSet param set of the simulation.
Definition: basicsimulation.h:145
void setName(string value)
Sets the container's name.
Definition: param.h:267
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
void setOwner(SimComponent *owner)
Sets the pointer to the SimComponents that owns this set.
Definition: param.h:271
Definition: types.h:78
void add_param(Param *param)
Adds the param argument to the list.
Definition: param.h:238
Definition: types.h:78
ParamManager::~ParamManager ( )
inline
93 {/*message("ParamManager::~ParamManager\n");*/}

Member Function Documentation

void ParamManager::add_paramset ( ParamSet paramset)
inline

Adds a ParamSet to the list of the parameter sets of the simulation.

Referenced by BinaryDataLoader::extractPop().

95 {_allParams.push_back(paramset);}
list< ParamSet * > _allParams
A list of all the parameter sets of all the simulation components loaded in the _component list of th...
Definition: basicsimulation.h:137
void ParamManager::build_allParams ( )

Clears and fills the _allParams list with the ParamSet's of the simulation components.

References _allParams, ComponentManager::_components, and _paramSet.

Referenced by SIMenv::loadDefaultComponents(), SimRunner::run(), and SimBuilder::SimBuilder().

116 {
117  list< SimComponent* >::iterator cmpt = _components.begin();
118 
119  _allParams.clear();
120 
121  _allParams.push_back(&_paramSet);
122 
123  while(cmpt != _components.end()) {
124  _allParams.push_back( (*cmpt)->get_paramset() );
125  cmpt++;
126  }
127 }
ParamSet _paramSet
The ParamSet param set of the simulation.
Definition: basicsimulation.h:145
list< ParamSet * > _allParams
A list of all the parameter sets of all the simulation components loaded in the _component list of th...
Definition: basicsimulation.h:137
list< SimComponent * > _components
List of all the simulation components.
Definition: basicsimulation.h:71
void ParamManager::build_records ( map< string, vector< string > > &  initParams)

Builds the list of simulation parameters from the parsed input file(s).

References _simRecords, and setFilename().

Referenced by SimRunner::run().

199 {
200  map< string, string > params;
201  map< string, string > paramsToExpand;
202  map< string, string >::iterator param_iter;
203  map< string, vector<string> >::iterator Pit;
204  unsigned int RecNb = 1, ArgNb, SeqParam = 0, BlockSize, seq_pos, num_seq_arg = 0;
205  vector<unsigned int> sequence; //stores the number of args of each sequence parameters
206  vector<string> currSeqArg;
207  vector<unsigned int> currSeqPos;
208  vector<string> currCombinArg;
209  vector<unsigned int> currCombinPos;
210  string NAME;
211  string ARG;
212  bool SEQUENCE = 0;
213 
214  //find sequential and combinatorial parameters:
215  for(Pit = initParams.begin(); Pit != initParams.end(); ++Pit) {
216 
217  if(Pit->second.size() > 1) {
218  //fetch the number of the current sequence parameter:
219  sequence.push_back(Pit->second.size());
220  num_seq_arg++;
221  //increase the total number of simulations records:
222  RecNb *= Pit->second.size();
223 
224  } else if(Pit->second[0].find_first_of('[') != string::npos) {
225  //@TODO process true sequential arg
226  }
227 
228  }
229 
230  if(RecNb > 1)
231  //we have a sequence of simulations:
232  SEQUENCE = true;
233 
234  for(unsigned int i = 0; i < RecNb; ++i) {
235  //now build the simulation records with the right params!
236  //the map 'param' will get all the params used for one simulation
237  //it is then added to the list of simulations' parameters map
238 
239  SeqParam = 0;//used as index of the 'sequence' vector
240 
241  for(Pit = initParams.begin(); Pit != initParams.end(); ++Pit) {
242 
243  if(!(Pit->first.compare("filename")==0) &&
244  !(Pit->first.compare("stat")==0) &&
245  !(Pit->first.compare("postexec_args")==0)) {
246 
247  //get the number of arguments for the current parameter:
248  ArgNb = Pit->second.size();
249 
250  if(ArgNb > 1) {
251  //the current param is a sequence param
252  //increase the index of the sequence parameter
253  SeqParam++;
254  //then compute the right argument to give to the current simulation record:
255  BlockSize = RecNb;
256 
257  for(unsigned int j = 0; j < SeqParam; ++j)
258  BlockSize /= sequence[j];
259 
260  seq_pos = (i/BlockSize) % ArgNb;
261  //we store the argument position in its sequence and the actual argument string to build the filename:
262  currSeqPos.push_back( seq_pos );
263  currSeqArg.push_back(Pit->second[ seq_pos ]);
264 
265  //assign the right argument to the parameter:
266  //params[Pit->first] = Pit->second[ seq_pos ];
267 
268  ARG = Pit->second[ seq_pos ];
269  //we can expand a string argument of a sequential parameter as well
270  if(ARG.find_first_of('%') != string::npos)
271  paramsToExpand[Pit->first] = ARG;
272  else
273  params[Pit->first] = ARG;
274 
275  } else if (ArgNb == 1) {
276  //the current param isn't a sequence param but gets an argument
277  //we might have to do some name expansion:
278  ARG = Pit->second[0];
279 
280  if(ARG.find_first_of('%') != string::npos)
281  paramsToExpand[Pit->first] = ARG;
282  else
283  params[Pit->first] = ARG;
284 
285  } else
286  //current param has no argument (bool type param) we give it value 1 (true)
287  params[Pit->first] = "1";
288 
289  } else if (Pit->first.compare("stat") == 0){
290 
291  params["stat"].assign("");
292  //build a string with all the stats arguments
293  for(unsigned int k = 0; k < Pit->second.size(); ++k)
294  params["stat"] += Pit->second[k] + " ";
295 
296  } else if (Pit->first.compare("filename")==0) {
297 
298  if(SEQUENCE)
299  NAME = Pit->second[0];
300  else
301  params["filename"] = Pit->second[0];
302 
303  } else if (Pit->first.compare("postexec_args") == 0){
304 
305  ARG = "";
306 
307  //build a string with all the postexec arguments
308  for(unsigned int k = 0; k < Pit->second.size(); ++k)
309  ARG += Pit->second[k] + " ";
310 
311  params["postexec_args"] = ARG;
312 
313 #ifdef NEMOSUB
314  if(ARG.find_first_of('%') != string::npos)
315  paramsToExpand["postexec_args"] = ARG;
316 #endif
317 
318  }
319  }
320 
321  if(SEQUENCE) { //perform expansion of param arguments containing the '%' marker
322 
323  params["filename"] = setFilename(NAME, i+1, currSeqArg, currSeqPos, true);
324 
325  for(param_iter = paramsToExpand.begin(); param_iter != paramsToExpand.end(); param_iter++)
326  params[param_iter->first] = setFilename(param_iter->second, i+1, currSeqArg, currSeqPos, false);
327  }
328 
329  //add all the params previously computed to the main simulation recorder list (of maps)
330  _simRecords.push_back(params);
331 
332  currSeqArg.clear();
333  currSeqPos.clear();
334  }
335  /*
336  RecNb = 1;
337  for(RecIt = SimRecorder.begin();RecIt != SimRecorder.end();++RecIt){
338  message("\nSimulation "<<RecNb++;
339  for(P2 = RecIt->begin();P2 != RecIt->end();++P2)
340  message("\n "<<P2->first<<"\t"<<P2->second;
341  }
342 */
343 }
list< map< string, string > > _simRecords
Sets of parameters of all the simulations to perform.
Definition: basicsimulation.h:143
string setFilename(string &fstring, unsigned int sim, vector< string > &args, vector< unsigned int > &arg_no, bool check_arg_no)
Definition: basicsimulation.cc:347
list<ParamSet*>& ParamManager::get_allParams ( )
inline

Accessor of the whole list of the parameter sets.

Returns
the reference to the list of ParamSet
105 {return _allParams;}
list< ParamSet * > _allParams
A list of all the parameter sets of all the simulation components loaded in the _component list of th...
Definition: basicsimulation.h:137
map< string,string >& ParamManager::get_firstRecord ( )
inline

Accessor to the first element in the simulations parameter list.

131 {return (*_simRecords.begin());}
list< map< string, string > > _simRecords
Sets of parameters of all the simulations to perform.
Definition: basicsimulation.h:143
int ParamManager::get_nbSims ( )
inline

Accessor to the size of the simulations parameter list, i.e.

the number of simulations to perform.

133 {return _simRecords.size();}
list< map< string, string > > _simRecords
Sets of parameters of all the simulations to perform.
Definition: basicsimulation.h:143
ParamSet * ParamManager::get_paramset ( string &  name)

Looks for paramset with "name" in the list of parameter sets.

Returns
NULL if parameter set "name" not found

References _allParams.

Referenced by SimRunner::Replicate_LOOP().

181 {
182  list<ParamSet*>::iterator pset = _allParams.begin();
183 
184  while(pset != _allParams.end()) {
185 
186  if( (*pset)->getName().compare(name) == 0)
187  return (*pset);
188 
189  pset++;
190  }
191 
192  return NULL;
193 }
list< ParamSet * > _allParams
A list of all the parameter sets of all the simulation components loaded in the _component list of th...
Definition: basicsimulation.h:137
list< map< string,string > >& ParamManager::get_simRecords ( )
inline

Accessor to the simulations parameter list.

129 {return _simRecords;}
list< map< string, string > > _simRecords
Sets of parameters of all the simulations to perform.
Definition: basicsimulation.h:143
string ParamManager::lowercase ( string &  input)
private
538 {
539  for(unsigned int i=0;i<input.size();++i)
540  input[i] = tolower(input[i]);
541  return input;
542 }
bool ParamManager::param_consistency_check ( )

Checks if all the mandatory parameters are set so that the simulation can be launched.

Returns
TRUE if all ParamSet::check_consistency() returned true

References _allParams, and error().

Referenced by set_parameters().

132 {
133  list<ParamSet*>::iterator current_paramset = _allParams.begin();
134 
135  bool check = true;
136 
137  while(current_paramset != _allParams.end()){
138  if(!(*current_paramset)->check_consistency()){
139  error("ParamManager::param_consistency_check::consistency not satisfied for \"%s\"\n",
140  (*current_paramset)->getName().c_str());
141  check = false;
142  }
143  current_paramset++;
144  }
145  return check;
146 }
int error(const char *str,...)
Definition: output.cc:73
list< ParamSet * > _allParams
A list of all the parameter sets of all the simulation components loaded in the _component list of th...
Definition: basicsimulation.h:137
bool ParamManager::set_parameters ( map< string, string > &  simparams,
bool  silent 
)

Sets the parameters of the simulation with the argument strings given in input.

Scans the _allParams list to set the parameters present in the input map simparams. Each ParamSet checks internally for the presence of a Param with the given name string and sets its value with the given argument, if present.

Note: all ParamSet owning a Param with the same name will use the same argument string. The input map is not a multimap, each param name is present only once.

Parameters
simparamsa map containing the parameter names and their argument string
silentwill be silent about parameters that could not be set.
Returns
the status of the param_consistency_check() function

References _allParams, _inputParams, and param_consistency_check().

Referenced by SimBuilder::build_currentParams().

151 {
152  _inputParams = simparams;
153  list<ParamSet*>::iterator current_paramset = _allParams.begin();
154  map< string,string >::iterator IT = _inputParams.begin();
155  bool set = false;
156 
157  while(IT != _inputParams.end()) {
158 
159  current_paramset = _allParams.begin();
160 
161  while(current_paramset != _allParams.end()){
162  set |= (*current_paramset)->set_param((string&)IT->first,IT->second);
163  //is true if at least one paramset could set the param
164  current_paramset++;
165  }
166 
167  if(!set && !silent){
168  //error("ParamManager::could not set param \"%s\"\n",IT->first.c_str());
169  // return false;
170  }
171  set = false;
172  IT++;
173  }
174 
175  return param_consistency_check();
176 }
list< ParamSet * > _allParams
A list of all the parameter sets of all the simulation components loaded in the _component list of th...
Definition: basicsimulation.h:137
map< string, string > _inputParams
A map of the parameters and their arguments of the current (running) simulation.
Definition: basicsimulation.h:139
bool param_consistency_check()
Checks if all the mandatory parameters are set so that the simulation can be launched.
Definition: basicsimulation.cc:131
string ParamManager::setArgString ( string &  fmt,
string &  arg,
unsigned int  arg_pos 
)
private

References error(), and fatal().

Referenced by setFilename().

453 {
454  unsigned int width, digits;
455  double value;
456  bool is_dotless = 0;
457  string out, arg_str;
458  size_t pos = 0;
459 
460  if(fmt.size() != 0) {
461 
462  if(fmt[0] == '.') {
463  is_dotless = true;
464  fmt = fmt.substr(1, string::npos);
465  }
466 
467  if( !isdigit(fmt[0]) ) {
468  error("syntax error in format string for filename parameter:\n");
469  fatal("first character within \' \' must be a number\n");
470  }
471 
472  width = (unsigned int) strtol(fmt.c_str(), NULL, 10);
473 
474  digits = (unsigned int) log10((double)width) +1;
475 
476  fmt = fmt.substr(digits, string::npos);
477 
478  if(fmt[0] == '[') {
479 
480  if( (pos = fmt.find(']')) == string::npos){
481  error("syntax error in format string for filename parameter:\n");
482  fatal("enclosing ']' was not found\n");
483  }
484 
485  arg_str = fmt.substr(1, pos-1);
486 
487  if(arg_str[0] == '+') {
488 
489  ostringstream ostr;
490  ostr.width(width);
491  ostr.fill('0');
492  ostr << arg_pos+1;
493 
494  out = ostr.str();
495 
496  } else {
497 
498  if (arg_str.size() < (arg_pos+1)*width) {
499  error("syntax error in format string for filename parameter:\n");
500  fatal("the number of characters within [ ] is not sufficient.\n");
501  }
502 
503  for(unsigned int i = 0, start = arg_pos*width; i < width; i++)
504  out += arg_str[start + i];
505  }
506 
507  } else {
508 
509  value = strtod(arg.c_str(), 0);
510 
511  if(is_dotless) {
512  //take the decimal part:
513  double dummy;
514  value = modf(value, &dummy);
515  value *= pow(10.0, (double)width);
516  }
517 
518  ostringstream ostr;
519  ostr.width(width);
520  ostr.fill('0');
521  ostr << value;
522 
523  out = ostr.str();
524  }
525 
526  } else {
527 
528  out = arg;
529 
530  }
531 
532  return out;
533 }
void fatal(const char *str,...)
Definition: output.cc:90
int error(const char *str,...)
Definition: output.cc:73
string ParamManager::setFilename ( string &  fstring,
unsigned int  sim,
vector< string > &  args,
vector< unsigned int > &  arg_no,
bool  check_arg_no 
)
private

References fatal(), setArgString(), stripFormatString(), and warning().

Referenced by build_records().

351 {
352  string out, tail, fmt;
353  string::size_type pos = fstring.find_first_of('%');
354  string::size_type next;
355  unsigned int index, nstr_args = (pos != string::npos ? 1 : 0);
356  bool add_sim_no = false;
357  static bool has_warned = false;
358 
359  while(pos != string::npos)
360  nstr_args += ( (pos = fstring.find('%', pos+1)) != string::npos);
361 
362  if(check_arg_no && nstr_args < args.size()) {
363  if(!has_warned) warning("missing sequential arguments in filename parameter, adding simulation number to filename.\n");
364  has_warned = true;
365  add_sim_no = true;
366  }
367 
368  pos = fstring.find_first_of('%');
369 
370  if(pos != string::npos) {
371 
372  if(pos > 0) out = fstring.substr(0, pos);
373 
374  tail = fstring.substr(pos+1, string::npos);
375 
376  fmt = stripFormatString(tail, index);
377 
378  if(index > args.size()) fatal("too many sequential arguments in \"%s\"\n",fstring.c_str());
379 
380  out += setArgString(fmt, args[index-1], arg_no[index-1]);
381 
382  next = tail.find_first_of('%');
383 
384  while(next != string::npos){
385 
386  out += tail.substr(0, next);
387 
388  tail = tail.substr(next+1, string::npos);
389 
390  fmt = stripFormatString(tail, index);
391 
392  if(index > args.size()) fatal("too many sequential arguments in \"%s\"\n",fstring.c_str());
393 
394  out += setArgString(fmt, args[index-1], arg_no[index-1]);
395 
396  next = tail.find_first_of('%');
397  }
398 
399  out += tail.substr(0, next);
400 
401  } else
402  out = fstring;
403 
404  if(add_sim_no) {
405 
406  ostringstream ostr;
407  ostr << sim;
408 
409  out += "-" + ostr.str();
410  }
411 
412  return out;
413 }
string stripFormatString(string &str, unsigned int &index)
Definition: basicsimulation.cc:417
void fatal(const char *str,...)
Definition: output.cc:90
void warning(const char *str,...)
Definition: output.cc:56
string setArgString(string &fmt, string &arg, unsigned int arg_pos)
Definition: basicsimulation.cc:452
string ParamManager::stripFormatString ( string &  str,
unsigned int index 
)
private

References fatal().

Referenced by setFilename().

418 {
419  string fmt;
420  unsigned int digits;
421  size_t fmt_end;
422 
423  //check for the presence of a format string, enclose by two \'
424  if(str[0] == '\'') {
425 
426  fmt_end = str.find('\'', 1);
427 
428  if(fmt_end == string::npos) fatal("format string not closed in filename parameter\n");
429 
430  fmt = str.substr(1, fmt_end-1);
431 
432  str = str.substr(fmt_end +1, string::npos);
433 
434  } else {
435 
436  fmt = "";
437 
438  }
439 
440  index = (unsigned int) strtol(str.c_str(), NULL, 10);
441 
442  digits = (unsigned int) log10((double)index) +1;
443 
444  str = str.substr(digits, string::npos);
445 
446  return fmt;
447 
448 }
void fatal(const char *str,...)
Definition: output.cc:90

Member Data Documentation

list< ParamSet* > ParamManager::_allParams
protected

A list of all the parameter sets of all the simulation components loaded in the _component list of the ComponentManager.

Referenced by build_allParams(), SimBuilder::build_currentParams(), get_paramset(), param_consistency_check(), SimRunner::reset(), and set_parameters().

map< string, string > ParamManager::_inputParams
protected

A map of the parameters and their arguments of the current (running) simulation.

Referenced by set_parameters().

ParamSet ParamManager::_paramSet
protected
list< map< string, string > > ParamManager::_simRecords
protected

Sets of parameters of all the simulations to perform.

Referenced by build_records(), and SimRunner::run().

map< unsigned int, list < pair< string, string> > > ParamManager::_temporalParams
protected

Lists of parameters to be updated during a simulation indexed by generation update time.


The documentation for this class was generated from the following files:

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