ParamManager Class Reference

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

#include <basicsimulation.h>

Inheritance diagram for ParamManager:

Inheritance graph
[legend]
Collaboration diagram for ParamManager:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 ParamManager ()
 Cstor.
 ~ParamManager ()
void add_paramset (ParamSet *paramset)
 Adds a ParamSet to the list of the parameter sets of the simulation.
ParamSetget_paramset (string &name)
 Looks for paramset with "name" in the list of parameter sets.
void build_allParams ()
 Clears and fills the _allParams list with the ParamSet's of the simulation components.
list< ParamSet * > & get_allParams ()
 Accessor of the whole list of the parameter sets.
bool set_parameters (map< string, string > &simparams, bool silent)
 Sets the parameters of the simulation with the argument strings given in input.
bool param_consistency_check ()
 Checks if all the mandatory parameters are set so that the simulation can be launched.
void build_records (map< string, vector< string > > &initParams)
 Builds the list of simulation parameters from the parsed input file(s).
list< map< string, string > > & get_simRecords ()
 Accessor to the simulations parameter list.
map< string, string > & get_firstRecord ()
 Accessor to the first element in the simulations parameter list.
int get_nbSims ()
 Accessor to the size of the simulations parameter list, i.e.

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.
map< string, string > _inputParams
 A map of the parameters and their arguments of the current (running) simulation.
map< unsigned int, list< pair
< string, string > > > 
_temporalParams
 Lists of parameters to be updated during a simulation indexed by generation update time.
list< map< string, string > > _simRecords
 Sets of parameters of all the simulations to perform.
ParamSet _paramSet
 The ParamSet param set of the simulation.

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.

Definition at line 89 of file basicsimulation.h.


Constructor & Destructor Documentation

ParamManager::ParamManager (  ) 

Cstor.

Builds the simulation PramaSet.

Definition at line 97 of file basicsimulation.cc.

00098 {
00099   _paramSet.setName("simulation");
00100   _paramSet.setIsRequired(true);
00101   _paramSet.setOwner(NULL);
00102   _paramSet.add_param("filename",STR,true,false,0,0);
00103   _paramSet.add_param("root_dir",STR,false,false,0,0);
00104   _paramSet.add_param("logfile",STR,false,false,0,0);
00105   _paramSet.add_param("postexec_script",STR,false,false,0,0);
00106   _paramSet.add_param("random_seed",INT,false,false,0,0);
00107   _paramSet.add_param("replicates",INT,true,false,0,0);
00108   _paramSet.add_param("generations",INT,true,false,0,0);
00109   _paramSet.add_param("run_mode",STR,false,false,0,0);
00110 }

ParamManager::~ParamManager (  )  [inline]

Definition at line 93 of file basicsimulation.h.

00093 {/*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.

Definition at line 95 of file basicsimulation.h.

Referenced by BinaryDataLoader::extractPop().

00095 {_allParams.push_back(paramset);}

void ParamManager::build_allParams (  ) 

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

Definition at line 114 of file basicsimulation.cc.

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

00115 {
00116   list< SimComponent* >::iterator cmpt = _components.begin();
00117 
00118   _allParams.clear();
00119   
00120   _allParams.push_back(&_paramSet);
00121   
00122   while(cmpt != _components.end()) {
00123     _allParams.push_back( (*cmpt)->get_paramset() );
00124     cmpt++;
00125   }
00126 }

void ParamManager::build_records ( map< string, vector< string > > &  initParams  ) 

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

Definition at line 197 of file basicsimulation.cc.

Referenced by SimRunner::run().

00198 {
00199   map< string, string > params;
00200   map< string, string > paramsToExpand;
00201   map< string, string >::iterator param_iter;
00202   map< string, vector<string> >::iterator Pit;
00203   unsigned int RecNb = 1, ArgNb, SeqParam = 0, BlockSize, seq_pos, num_seq_arg = 0;
00204   vector<unsigned int> sequence; //stores the number of args of each sequence parameters
00205   vector<string> currSeqArg;
00206   vector<unsigned int> currSeqPos;
00207   vector<string> currCombinArg;
00208   vector<unsigned int> currCombinPos;
00209   string NAME;
00210   bool SEQUENCE = 0;
00211     
00212   //find sequencial and combinatorial parameters:
00213   for(Pit = initParams.begin(); Pit != initParams.end(); ++Pit) {
00214         if(Pit->second.size() > 1) {
00215       //fetch the number of the current sequence parameter:
00216       sequence.push_back(Pit->second.size());
00217       num_seq_arg++;
00218       //increase the total number of simulations records:
00219       RecNb *= Pit->second.size();
00220     } else if(Pit->second[0].find_first_of('[') != string::npos) {
00221       //process sequential arg
00222     }
00223 
00224   }
00225   
00226   if(RecNb > 1)
00227     //we have a sequence of simulations:
00228     SEQUENCE = true;
00229   
00230   for(unsigned int i = 0; i < RecNb; ++i) {
00231     //now build the simulation records with the right params!
00232     //the map 'param' will get all the params used for one simulation
00233     //it is then added to the list of simulations' parameters map
00234     
00235     SeqParam = 0;//used as index of the 'sequence' vector
00236     
00237     for(Pit = initParams.begin(); Pit != initParams.end(); ++Pit) {
00238       
00239       if(!(Pit->first.compare("filename")==0) &&
00240          !(Pit->first.compare("stat")==0)) {
00241         
00242         //get the number of arguments for the current parameter:
00243         ArgNb = Pit->second.size();
00244         
00245         if(ArgNb > 1) {
00246           //the current param is a sequence param
00247           //increase the index of the sequence parameter
00248           SeqParam++;
00249           //then compute the right argument to give to the current simulation record:
00250           BlockSize = RecNb;
00251           
00252           for(unsigned int j = 0; j < SeqParam; ++j)
00253             BlockSize /= sequence[j];
00254           
00255           seq_pos = (i/BlockSize) % ArgNb;
00256           //we store the argument position in its sequence and the actual argument string to build the filename:
00257           currSeqPos.push_back( seq_pos );
00258           currSeqArg.push_back(Pit->second[ seq_pos ]);
00259           //assign the right argument to the parameter:
00260           params[Pit->first] = Pit->second[ seq_pos ];
00261           
00262         } else if (ArgNb == 1) {
00263           //the current param isn't a sequence param but gets an argument
00264           //we might have to do some name expansion:
00265           string arg =  Pit->second[0];
00266           if(arg.find_first_of('%') != string::npos)
00267             paramsToExpand[Pit->first] = arg;
00268           else 
00269             params[Pit->first] = arg;
00270           
00271         } else
00272           //current param has no argument (bool type param) we give it value 1 (true)
00273           params[Pit->first] = "1";
00274         
00275       } else if (Pit->first.compare("stat") == 0){
00276         
00277         params["stat"].assign("");
00278         //build a string with all the stats arguments
00279         for(unsigned int k = 0; k < Pit->second.size(); ++k)
00280           params["stat"] += Pit->second[k] + " ";
00281         
00282       } else if (Pit->first.compare("filename")==0) {
00283         
00284         if(SEQUENCE)
00285           NAME = Pit->second[0];
00286         else
00287           params["filename"] = Pit->second[0];
00288         
00289       }
00290     }
00291     
00292     if(SEQUENCE) {
00293       params["filename"] = setFilename(NAME, i+1, currSeqArg, currSeqPos, true);
00294       
00295       for(param_iter = paramsToExpand.begin(); param_iter != paramsToExpand.end(); param_iter++)
00296         params[param_iter->first] = setFilename(param_iter->second, i+1, currSeqArg, currSeqPos, false); 
00297     }
00298     
00299     //add all the params previously computed to the main simulation recorder list (of maps)
00300     _simRecords.push_back(params);
00301     
00302     currSeqArg.clear();
00303     currSeqPos.clear();
00304   }
00305   /*
00306    RecNb = 1;
00307    for(RecIt = SimRecorder.begin();RecIt != SimRecorder.end();++RecIt){
00308      message("\nSimulation "<<RecNb++;
00309              for(P2 = RecIt->begin();P2 != RecIt->end();++P2)
00310              message("\n  "<<P2->first<<"\t"<<P2->second;
00311    }
00312 */
00313 }

list<ParamSet*>& ParamManager::get_allParams (  )  [inline]

Accessor of the whole list of the parameter sets.

Returns:
the reference to the list of ParamSet

Definition at line 105 of file basicsimulation.h.

00105 {return _allParams;}

map< string,string >& ParamManager::get_firstRecord (  )  [inline]

Accessor to the first element in the simulations parameter list.

Definition at line 131 of file basicsimulation.h.

00131 {return (*_simRecords.begin());}

int ParamManager::get_nbSims (  )  [inline]

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

the number of simulations to perform.

Definition at line 133 of file basicsimulation.h.

00133 {return _simRecords.size();}

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

Definition at line 179 of file basicsimulation.cc.

00180 {
00181   list<ParamSet*>::iterator pset = _allParams.begin();
00182   
00183   while(pset != _allParams.end()) {
00184     
00185     if( (*pset)->getName().compare(name) == 0)
00186       return (*pset);
00187     
00188     pset++;
00189   }
00190   
00191   return NULL;
00192 }

list< map< string,string > >& ParamManager::get_simRecords (  )  [inline]

Accessor to the simulations parameter list.

Definition at line 129 of file basicsimulation.h.

00129 {return _simRecords;}

string ParamManager::lowercase ( string &  input  )  [private]

Definition at line 495 of file basicsimulation.cc.

00496 {
00497   for(unsigned int i=0;i<input.size();++i)
00498     input[i] = tolower(input[i]);
00499   return input;
00500 }

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

Definition at line 130 of file basicsimulation.cc.

Referenced by set_parameters().

00131 {
00132   list<ParamSet*>::iterator current_paramset = _allParams.begin();
00133   
00134   bool check = true;
00135   
00136   while(current_paramset != _allParams.end()){
00137     if(!(*current_paramset)->check_consistency()){
00138       error("ParamManager::param_consistency_check::consistency not satisfied for \"%s\"\n",
00139             (*current_paramset)->getName().c_str());
00140       check = false;
00141     }
00142     current_paramset++;
00143   }
00144   return check;
00145 }

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:
simparams a map containing the parameter names and their argument string
silent will be silent about parameters that could not be set.
Returns:
the status of the param_consistency_check() function

Definition at line 149 of file basicsimulation.cc.

Referenced by SimBuilder::build_currentParams().

00150 {
00151   _inputParams = simparams;
00152   list<ParamSet*>::iterator current_paramset = _allParams.begin();
00153   map< string,string >::iterator IT = _inputParams.begin();
00154   bool set = false;
00155   
00156   while(IT != _inputParams.end()) {
00157     
00158     current_paramset = _allParams.begin();
00159     
00160     while(current_paramset != _allParams.end()){
00161       set |= (*current_paramset)->set_param((string&)IT->first,IT->second);
00162       //is true if at least one paramset could set the param
00163       current_paramset++;
00164     }
00165     
00166     if(!set && !silent){
00167       //error("ParamManager::could not set param \"%s\"\n",IT->first.c_str());
00168       //    return false;
00169     }
00170     set = false;
00171     IT++;
00172   }
00173   
00174   return param_consistency_check();
00175 }

string ParamManager::setArgString ( string &  fmt,
string &  arg,
unsigned int  arg_pos 
) [private]

Definition at line 423 of file basicsimulation.cc.

Referenced by setFilename().

00424 {
00425   unsigned int pos = 0, width, digits;
00426   double value;
00427   bool is_dotless = 0;
00428   string out, arg_str;
00429   
00430   if(fmt.size() != 0) {
00431     
00432     if(fmt[0] == '.') {
00433       is_dotless = true;
00434       fmt = fmt.substr(1, string::npos);
00435     }
00436     
00437     if( !isdigit(fmt[0]) ) fatal("syntax error in format string for filename parameter.\n---> first argument must be a number\n");
00438     
00439     width =  (unsigned int) strtol(fmt.c_str(), NULL, 10);
00440     
00441     digits = (unsigned int) log10((double)width) +1;
00442     
00443     fmt = fmt.substr(digits, string::npos);
00444         
00445     if(fmt[0] == '[') {
00446       
00447       if( (pos = fmt.find(']')) == string::npos)
00448         fatal("syntax error in format string for filename parameter.\n---> enclosing ']' not found\n");
00449       
00450       arg_str = fmt.substr(1, pos-1);
00451       
00452       if(arg_str[0] == '+') {
00453         
00454         ostringstream ostr;
00455         ostr.width(width);
00456         ostr.fill('0');
00457         ostr << arg_pos+1;
00458         
00459         out = ostr.str();
00460         
00461       } else
00462         for(unsigned int i = 0, start = arg_pos*width; i < width; i++)
00463           out += arg_str[start + i];
00464       
00465     } else {
00466       
00467       value = strtod(arg.c_str(), 0);
00468       
00469       if(is_dotless) {
00470         //take the decimal part:
00471         double dummy;
00472         value = modf(value, &dummy);
00473         value *= pow(10.0, (double)width);
00474       }
00475       
00476       ostringstream ostr;
00477       ostr.width(width);
00478       ostr.fill('0');
00479       ostr << value;
00480       
00481       out = ostr.str();
00482     }
00483     
00484   } else {
00485     
00486     out = arg;
00487     
00488   }
00489   
00490   return out;
00491 }

string ParamManager::setFilename ( string &  fstring,
unsigned int  sim,
vector< string > &  args,
vector< unsigned int > &  arg_no,
bool  check_arg_no 
) [private]

Definition at line 317 of file basicsimulation.cc.

Referenced by build_records().

00321 {
00322   string out, tail, fmt;
00323   string::size_type pos = fstring.find_first_of('%');
00324   string::size_type next;
00325   unsigned int index, nstr_args = (pos != string::npos ? 1 : 0);
00326   bool add_sim_no = false;
00327   static bool has_warned = false;
00328   
00329   while(pos != string::npos)
00330     nstr_args += ( (pos = fstring.find('%', pos+1)) != string::npos);
00331   
00332   if(check_arg_no && nstr_args < args.size()) {
00333     if(!has_warned) warning("missing sequential arguments in filename parameter, adding simulation number to filename.\n");
00334     has_warned = true;
00335     add_sim_no = true;
00336   }
00337   
00338 //  if(nstr_args > args.size()) fatal("too many sequential arguments in \"%s\"\n",fstring.c_str());
00339   
00340   pos = fstring.find_first_of('%');
00341   
00342   if(pos != string::npos) {
00343     
00344     if(pos > 0) out = fstring.substr(0, pos);
00345     
00346     tail = fstring.substr(pos+1, string::npos);
00347     
00348     fmt = stripFormatString(tail, index);
00349     
00350     if(index > args.size()) fatal("too many sequential arguments in \"%s\"\n",fstring.c_str());
00351     
00352     out += setArgString(fmt, args[index-1], arg_no[index-1]);
00353     
00354     next = tail.find_first_of('%');
00355     
00356     while(next != string::npos){
00357     
00358       out += tail.substr(0, next);
00359       
00360       tail = tail.substr(next+1, string::npos);
00361       
00362       fmt = stripFormatString(tail, index);
00363       
00364       if(index > args.size()) fatal("too many sequential arguments in \"%s\"\n",fstring.c_str());
00365 
00366       out += setArgString(fmt, args[index-1], arg_no[index-1]);
00367       
00368       next = tail.find_first_of('%');
00369     }
00370     
00371     out += tail.substr(0, next);
00372 
00373   } else
00374     out = fstring;
00375   
00376   if(add_sim_no) {
00377     
00378     ostringstream ostr;
00379     ostr << sim;    
00380     
00381     out += "-" + ostr.str();
00382   }
00383   
00384   return out;
00385 }

string ParamManager::stripFormatString ( string &  str,
unsigned int &  index 
) [private]

Definition at line 389 of file basicsimulation.cc.

Referenced by setFilename().

00390 {
00391   string fmt;
00392   unsigned int digits, fmt_end;
00393   
00394   //check for the presence of a format string, enclose by two \'
00395   if(str[0] == '\'') {
00396     
00397     fmt_end = str.find('\'', 1);
00398     
00399     if(fmt_end == string::npos) fatal("format string not closed in filename parameter\n");
00400     
00401     fmt = str.substr(1, fmt_end-1);
00402     
00403     str = str.substr(fmt_end +1, string::npos);
00404     
00405   } else {
00406     
00407     fmt = "";
00408     
00409   }
00410   
00411   index = (unsigned int) strtol(str.c_str(), NULL, 10);
00412   
00413   digits = (unsigned int) log10((double)index) +1;
00414   
00415   str = str.substr(digits, string::npos);
00416   
00417   return fmt;
00418   
00419 }


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.

Definition at line 137 of file basicsimulation.h.

Referenced by add_paramset(), build_allParams(), SimBuilder::build_currentParams(), get_allParams(), 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.

Definition at line 139 of file basicsimulation.h.

Referenced by set_parameters().

The ParamSet param set of the simulation.

Definition at line 145 of file basicsimulation.h.

Referenced by build_allParams(), SimRunner::init(), SimRunner::init_random_seed(), ParamManager(), and SimBuilder::SimBuilder().

list< map< string, string > > ParamManager::_simRecords [protected]

Sets of parameters of all the simulations to perform.

Definition at line 143 of file basicsimulation.h.

Referenced by build_records(), get_firstRecord(), get_nbSims(), get_simRecords(), 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.

Definition at line 141 of file basicsimulation.h.


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

Generated for Nemo v2.1.2 by  doxygen 1.5.8 -- Nemo is hosted by  SourceForge.net Logo