Nemo  2.3.46
FileServices Class Reference

A class to manage the files associated with each components of the simulation. More...

#include <fileservices.h>

+ Inheritance diagram for FileServices:
+ Collaboration diagram for FileServices:

Public Types

typedef std::list< FileHandler * >
::const_iterator 
file_it
 

Public Member Functions

 FileServices ()
 
virtual ~FileServices ()
 
virtual bool init ()
 
virtual void notify ()
 
bool init (list< ParamSet * > &params)
 Checks if files with _basename already exist and save the simulation parameters in log files. More...
 
virtual Metapopget_pop_ptr ()
 Accessor to the pointer to the main population. More...
 
virtual void set_pop_ptr (Metapop *pop)
 Sets the Metapop reference. More...
 
void setMode (unsigned int m)
 Mode setter, determines if file will get overwritten or not. More...
 
unsigned int getMode ()
 Writting mode getter. More...
 
void setBasename (string name)
 Sets the base file name of the simulation. More...
 
void setRootDir (string name)
 Sets the root directory of the simulation. More...
 
void log_simparams ()
 Saves the current simulation parameters to the default parameter logfile. More...
 
void log (string message)
 Write to the parameter logfile. More...
 
list< ParamSet * > & get_params ()
 Accessor to the list of the current parameters of the simulation. More...
 
file_it getFirstWriter ()
 Accessor to first element of the list of output FileHandlers. More...
 
file_it getLastWriter ()
 Accessor to last element of the list of output FileHandlers. More...
 
file_it getFirstReader ()
 Accessor to first element of the list of input FileHandlers. More...
 
file_it getLastReader ()
 Accessor to last element of the list of input FileHandlers. More...
 
FileHandlergetReader (string &type)
 Accessor to a specific file handler specified by its extension string. More...
 
string & getBaseFileName ()
 Accessor to the base file name of the simulation. More...
 
string & getRootDir ()
 Accessor to the name of the simulation's root output directory. More...
 
string getReplicateCounter ()
 Accessor to the current replicate counter string. More...
 
string & getFirstReplicateFileName ()
 Accessor to the first replicate file name. More...
 
string & getReplicateFileName ()
 Accessor to the current replicate file name. More...
 
string getGenerationCounter ()
 Accessor to the current generation counter string. More...
 
string getGenerationReplicateFileName ()
 Accessor to the current file name with generation and replicate counters added. More...
 
virtual void load (SimComponent *sc)
 Tells the SimComponent to load its file handlers. More...
 
virtual void attach (Handler *FH)
 Attaches the FileHandler to the current list (_writers) of the FileServices. More...
 
virtual void attach_reader (FileHandler *FH)
 Attaches the FileHandler to the current list (_readers) of the FileServices. More...
 
virtual void reset ()
 Clears the list of FileHandlers. More...
 
- Public Member Functions inherited from Service
 Service ()
 
virtual ~Service ()
 

Private Attributes

Metapop_popPtr
 a pointer to the current Metapop More...
 
FHLogWriter_logWriter
 a FileHandler used to save the simulation parameters on disk. More...
 
list< FileHandler * > _writers
 the list of the FileHandler's registered by the SimComponent in output mode More...
 
list< FileHandler * > _readers
 the list of the FileHandler's registered by the SimComponent in input mode More...
 
string _rep_filename
 the file name associated with the current simulation replicate More...
 
string _basename
 the base file name of the simulation, read from the init file (param "filename") More...
 
string _root_dir
 the root directory for the simulation's results, read from the init file (param "root_dir") More...
 
list< ParamSet * > _params
 the list of the parameters of the current simulation. More...
 
unsigned int _mode
 File mode, sets behavior when file must be overwritten or not. More...
 

Detailed Description

A class to manage the files associated with each components of the simulation.

Implements the Observer design pattern (is the concrete subject), stores the base filename of the simulation and updates the replicate filenames. It also performs files checking and saves the simulation parameters on init.

Member Typedef Documentation

typedef std::list< FileHandler* >::const_iterator FileServices::file_it

Constructor & Destructor Documentation

FileServices::FileServices ( )

References _logWriter, and attach().

45  : _popPtr(0), _rep_filename(""), _basename(""), _root_dir("")
46 {
47  _logWriter = new FHLogWriter();
49 }
virtual void attach(Handler *FH)
Attaches the FileHandler to the current list (_writers) of the FileServices.
Definition: fileservices.cc:57
File Handler used to save the simulation parameters to a log file.
Definition: filehandler.h:272
Metapop * _popPtr
a pointer to the current Metapop
Definition: fileservices.h:55
string _rep_filename
the file name associated with the current simulation replicate
Definition: fileservices.h:66
FHLogWriter * _logWriter
a FileHandler used to save the simulation parameters on disk.
Definition: fileservices.h:57
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:69
string _root_dir
the root directory for the simulation's results, read from the init file (param "root_dir") ...
Definition: fileservices.h:72
FileServices::~FileServices ( )
virtual

References _logWriter.

51  {
52  if(_logWriter) delete _logWriter;
53 }
FHLogWriter * _logWriter
a FileHandler used to save the simulation parameters on disk.
Definition: fileservices.h:57

Member Function Documentation

void FileServices::attach ( Handler FH)
virtual

Attaches the FileHandler to the current list (_writers) of the FileServices.

Parameters
FHthe FileHandler

Implements Service.

References _writers, attach_reader(), FileHandler::get_isInputHandler(), and FileHandler::set_service().

Referenced by FileServices(), BinaryDataSaver::loadFileServices(), TProtoBDMI::loadFileServices(), LCE_StatServiceNotifier::loadFileServices(), TProtoQuanti::loadFileServices(), TProtoNeutralGenes::loadFileServices(), LCE_Breed_Wolbachia::loadFileServices(), TProtoDeletMutations_bitstring::loadFileServices(), Metapop::loadFileServices(), and LCE_Selection_base::loadFileServices().

58 {
59 // Service::attach(FH);
60  FileHandler* FH = dynamic_cast<FileHandler*> (H);
61  _writers.push_back(FH);
62 
63  if(FH->get_isInputHandler()) attach_reader(FH);
64 
65  FH->set_service(this);
66 }
list< FileHandler * > _writers
the list of the FileHandler's registered by the SimComponent in output mode
Definition: fileservices.h:60
Interface to handle file input/output for any SimComponent.
Definition: filehandler.h:53
virtual void attach_reader(FileHandler *FH)
Attaches the FileHandler to the current list (_readers) of the FileServices.
Definition: fileservices.cc:70
void set_service(FileServices *srv)
Definition: filehandler.h:137
bool get_isInputHandler()
Definition: filehandler.h:150
void FileServices::attach_reader ( FileHandler FH)
virtual

Attaches the FileHandler to the current list (_readers) of the FileServices.

Parameters
FHthe FileHandler

References _readers, and FileHandler::set_service().

Referenced by attach(), LCE_Cross::loadFileServices(), TProtoNeutralGenes::loadFileServices(), and TProtoDeletMutations_bitstring::loadFileServices().

71 {
72  _readers.push_back(FH);
73  FH->set_service(this);
74 }
list< FileHandler * > _readers
the list of the FileHandler's registered by the SimComponent in input mode
Definition: fileservices.h:63
void set_service(FileServices *srv)
Definition: filehandler.h:137
list< ParamSet* >& FileServices::get_params ( )
inline

Accessor to the list of the current parameters of the simulation.

References _params.

Referenced by BinaryDataSaver::printHeader().

125 {return _params;};
list< ParamSet * > _params
the list of the parameters of the current simulation.
Definition: fileservices.h:76
virtual Metapop* FileServices::get_pop_ptr ( )
inlinevirtual

Accessor to the pointer to the main population.

Returns
the pointer to the current Metapop as set during simulation setup.

References _popPtr.

101 {return _popPtr;}
Metapop * _popPtr
a pointer to the current Metapop
Definition: fileservices.h:55
string & FileServices::getBaseFileName ( )

Accessor to the base file name of the simulation.

References _basename.

Referenced by LCE_StatFH::FHwrite(), BinaryDataSaver::finish(), LCE_StatFH::ifExist(), SimRunner::printLog(), LCE_StatFH::PrintStat_byGen(), and SimRunner::run().

227 {
228  return _basename;
229 }
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:69
file_it FileServices::getFirstReader ( )
inline

Accessor to first element of the list of input FileHandlers.

Referenced by getReader().

134 {return _readers.begin();}
list< FileHandler * > _readers
the list of the FileHandler's registered by the SimComponent in input mode
Definition: fileservices.h:63
string & FileServices::getFirstReplicateFileName ( )

Accessor to the first replicate file name.

References _basename, _popPtr, _rep_filename, Metapop::getReplicates(), and MPIenv::slaveRank().

195 {
196  ostringstream rpl;
197 
198  rpl.fill('0');
199  rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
200  rpl<< max( (unsigned)1, _myenv->slaveRank() );
201 
202  _rep_filename = _basename + "_" + rpl.str();
203 
204  return _rep_filename;
205 }
unsigned int slaveRank() const
Definition: MPImanager.h:129
Metapop * _popPtr
a pointer to the current Metapop
Definition: fileservices.h:55
string _rep_filename
the file name associated with the current simulation replicate
Definition: fileservices.h:66
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:69
unsigned int getReplicates()
Definition: metapop.h:269
MPIenv * _myenv
Definition: simulation.cc:47
file_it FileServices::getFirstWriter ( )
inline

Accessor to first element of the list of output FileHandlers.

Referenced by SimRunner::printLog().

128 {return _writers.begin();}
list< FileHandler * > _writers
the list of the FileHandler's registered by the SimComponent in output mode
Definition: fileservices.h:60
string FileServices::getGenerationCounter ( )

Accessor to the current generation counter string.

References _popPtr, Metapop::getCurrentGeneration(), and Metapop::getGenerations().

Referenced by getGenerationReplicateFileName().

254 {
255  ostringstream gen;
256 
257  gen.fill('0');
258  gen.width( (int)log10((double)_popPtr->getGenerations()) + 1);
260 
261  return gen.str();
262 }
unsigned int getCurrentGeneration()
Definition: metapop.h:286
Metapop * _popPtr
a pointer to the current Metapop
Definition: fileservices.h:55
unsigned int getGenerations()
Definition: metapop.h:267
string FileServices::getGenerationReplicateFileName ( )

Accessor to the current file name with generation and replicate counters added.

References _basename, getGenerationCounter(), and getReplicateCounter().

Referenced by TTDeletMutBitstrFH::FHwrite(), TTNeutralGenesFH::write_freq(), TTNeutralGenesFH::write_Fst_i(), TTNeutralGenesFH::write_FSTAT(), TTNeutralGenesFH::write_GENEPOP(), TTNeutralGenesFH::write_TAB(), and TTNeutralGenesFH::write_varcompWC().

219 {
220  string name = _basename + "_" + getGenerationCounter() + "_" + getReplicateCounter();
221  return name;
222 }
string getReplicateCounter()
Accessor to the current replicate counter string.
Definition: fileservices.cc:240
string getGenerationCounter()
Accessor to the current generation counter string.
Definition: fileservices.cc:253
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:69
file_it FileServices::getLastReader ( )
inline

Accessor to last element of the list of input FileHandlers.

Referenced by getReader().

137 {return _readers.end();}
list< FileHandler * > _readers
the list of the FileHandler's registered by the SimComponent in input mode
Definition: fileservices.h:63
file_it FileServices::getLastWriter ( )
inline

Accessor to last element of the list of output FileHandlers.

Referenced by SimRunner::printLog().

131 {return _writers.end();}
list< FileHandler * > _writers
the list of the FileHandler's registered by the SimComponent in output mode
Definition: fileservices.h:60
unsigned int FileServices::getMode ( )
inline

Writting mode getter.

References _mode.

110 {return _mode;}
unsigned int _mode
File mode, sets behavior when file must be overwritten or not.
Definition: fileservices.h:79
FileHandler * FileServices::getReader ( string &  type)

Accessor to a specific file handler specified by its extension string.

References getFirstReader(), and getLastReader().

Referenced by Metapop::loadPopFromTraitFile().

285 {
286  file_it file = getFirstReader(), last = getLastReader() ;
287 
288  for(;file != last; file++) {
289  if( (*file)->get_extension().compare( type ) == 0) {
290  return (*file);
291  }
292  }
293  return 0;
294 }
file_it getLastReader()
Accessor to last element of the list of input FileHandlers.
Definition: fileservices.h:137
std::list< FileHandler * >::const_iterator file_it
Definition: fileservices.h:83
file_it getFirstReader()
Accessor to first element of the list of input FileHandlers.
Definition: fileservices.h:134
string FileServices::getReplicateCounter ( )

Accessor to the current replicate counter string.

References _popPtr, Metapop::getCurrentReplicate(), and Metapop::getReplicates().

Referenced by getGenerationReplicateFileName(), and getReplicateFileName().

241 {
242  ostringstream rpl;
243 
244  rpl.fill('0');
245  rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
247 
248  return rpl.str();
249 }
unsigned int getCurrentReplicate()
Definition: metapop.h:285
Metapop * _popPtr
a pointer to the current Metapop
Definition: fileservices.h:55
unsigned int getReplicates()
Definition: metapop.h:269
string & FileServices::getReplicateFileName ( )

Accessor to the current replicate file name.

References _basename, _rep_filename, and getReplicateCounter().

Referenced by TTQFreqExtractor::FHwrite().

210 {
212 
213  return _rep_filename;
214 }
string getReplicateCounter()
Accessor to the current replicate counter string.
Definition: fileservices.cc:240
string _rep_filename
the file name associated with the current simulation replicate
Definition: fileservices.h:66
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:69
std::string & FileServices::getRootDir ( )

Accessor to the name of the simulation's root output directory.

References _root_dir.

234 {
235  return _root_dir;
236 }
string _root_dir
the root directory for the simulation's results, read from the init file (param "root_dir") ...
Definition: fileservices.h:72
virtual bool FileServices::init ( )
inlinevirtual

Implements Service.

Referenced by SimRunner::run().

89 {return false;}
bool FileServices::init ( list< ParamSet * > &  params)

Checks if files with _basename already exist and save the simulation parameters in log files.

Parameters
paramsa ref to the list of the current parameters of the simulation
Returns
true if the files check is ok
false if the user wants to skip this simulation

References _basename, _logWriter, _mode, _params, _root_dir, _writers, fatal(), message(), FileHandler::set(), and warning().

83 {
84  char yn;
85  bool ok;
86  file_it HIT;
87 
88  _params = params;
89 
90  _logWriter->set(0, 0, 0, 0, 0, "");
91 
92  //first, build path and filename for each file handler
93  HIT = _writers.begin();
94  message(" outputs: %s{",_root_dir.c_str());
95  while(HIT != _writers.end()) {
96  message("%s%s*%s",
97  (*HIT)->get_path().c_str(),
98  ((*HIT)->get_path().size() !=0? "/":""),
99  (*HIT)->get_extension().c_str());
100  (*HIT)->init();
101  HIT++;
102  if(HIT != _writers.end()) message(", ");
103  }
104  message("}\n");
105  //then check if any file already exists
106 check:
107 
108  ok = true;
109  HIT = _writers.begin();
110  //we have to skip the first writer, that is the log-writer
111  while(++HIT != _writers.end()) {
112  ok &= (*HIT)->ifExist();
113 
114  }
115 
116  if(!ok) {
117 
118 #if defined(_R_OUTPUT_) || defined(LOW_VERBOSE) || defined(USE_MPI)
119  yn = 's';
120  message("\nPlease choose another base filename or move the existing file(s)\n");
121 #else
122  //file mode is set during simulation settup, in SimRunner::init()
123  if(_mode == 0) {
124  message("\nDo you want to overwrite all the files that use it ? (y/n/s(kip)): ");
125  cin>>yn;
126  } else if( _mode == 1 ) {
127  warning("Overwriting existing files.\n");
128  yn = 'y';
129  } else if( _mode == 2 ) {
130  message("\nPlease choose another base filename or move the existing file(s)\n");
131  yn = 's';
132  } else if( _mode == 3 || _mode == 4) {
133  //dryrun, pretend overwriting, silently
134  yn = 'y';
135  } else {
136  fatal("Run mode not properly set, check simulation parameter \"run_mode\".\n");
137  }
138 #endif
139 
140  switch(yn) {
141  case 'y':
142  break;
143  case 's':
144  message("Skipping this simulation\n");
145  return false;
146  default: {
147  message("Please give a new output filename : ");
148  cin>>_basename;
149  goto check;
150  }
151  }
152  }
153 
154  return true;
155 }
list< FileHandler * > _writers
the list of the FileHandler's registered by the SimComponent in output mode
Definition: fileservices.h:60
void message(const char *message,...)
Definition: output.cc:40
list< ParamSet * > _params
the list of the parameters of the current simulation.
Definition: fileservices.h:76
unsigned int _mode
File mode, sets behavior when file must be overwritten or not.
Definition: fileservices.h:79
void fatal(const char *str,...)
Definition: output.cc:90
void warning(const char *str,...)
Definition: output.cc:56
FHLogWriter * _logWriter
a FileHandler used to save the simulation parameters on disk.
Definition: fileservices.h:57
std::list< FileHandler * >::const_iterator file_it
Definition: fileservices.h:83
virtual void set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path)
Sets the hanlder parameters.
Definition: filehandler.h:193
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:69
string _root_dir
the root directory for the simulation's results, read from the init file (param "root_dir") ...
Definition: fileservices.h:72
void FileServices::load ( SimComponent sc)
virtual

Tells the SimComponent to load its file handlers.

Parameters
scthe SimComponent

Implements Service.

References SimComponent::loadFileServices().

Referenced by SimRunner::register_component().

78 {sc->loadFileServices(this);}
virtual void loadFileServices(FileServices *loader)=0
Loads the component's FileHandler onto the FileServices.
void FileServices::log ( string  message)

Write to the parameter logfile.

References _logWriter, and FHLogWriter::log_message().

Referenced by SimRunner::run(), and LCE_Selection_base::set_std_rate_of_change().

188 {
190 }
void message(const char *message,...)
Definition: output.cc:40
FHLogWriter * _logWriter
a FileHandler used to save the simulation parameters on disk.
Definition: fileservices.h:57
void log_message(string &logstr)
Definition: filehandler.cc:254
void FileServices::log_simparams ( )

Saves the current simulation parameters to the default parameter logfile.

References _logWriter, _mode, _params, FHLogWriter::createInitFile(), and FHLogWriter::save_simparams().

Referenced by SimRunner::run().

178 {
179  if(_mode == 4)
181  else
183 }
list< ParamSet * > _params
the list of the parameters of the current simulation.
Definition: fileservices.h:76
unsigned int _mode
File mode, sets behavior when file must be overwritten or not.
Definition: fileservices.h:79
void save_simparams(list< ParamSet * > &params)
Definition: filehandler.cc:189
void createInitFile(list< ParamSet * > &params)
Definition: filehandler.cc:218
FHLogWriter * _logWriter
a FileHandler used to save the simulation parameters on disk.
Definition: fileservices.h:57
void FileServices::notify ( )
virtual

Implements Service.

References _writers.

Referenced by LCE_FileServicesNotifier::execute(), and SimRunner::Replicate_LOOP().

160 {
161  for (file_it file = _writers.begin(); file != _writers.end(); ++file) {
162  (*file)->update();
163  }
164 }
list< FileHandler * > _writers
the list of the FileHandler's registered by the SimComponent in output mode
Definition: fileservices.h:60
std::list< FileHandler * >::const_iterator file_it
Definition: fileservices.h:83
void FileServices::reset ( )
virtual

Clears the list of FileHandlers.

Implements Service.

References _logWriter, _readers, and _writers.

Referenced by SimRunner::reset_services().

169 {
170  //Service::reset();
171  _writers.clear(); _writers.push_back(_logWriter);
172  _readers.clear();
173 }
list< FileHandler * > _writers
the list of the FileHandler's registered by the SimComponent in output mode
Definition: fileservices.h:60
FHLogWriter * _logWriter
a FileHandler used to save the simulation parameters on disk.
Definition: fileservices.h:57
list< FileHandler * > _readers
the list of the FileHandler's registered by the SimComponent in input mode
Definition: fileservices.h:63
virtual void FileServices::set_pop_ptr ( Metapop pop)
inlinevirtual

Sets the Metapop reference.

Referenced by SimRunner::init_components().

104 {_popPtr = pop;}
Metapop * _popPtr
a pointer to the current Metapop
Definition: fileservices.h:55
void FileServices::setBasename ( string  name)

Sets the base file name of the simulation.

References _basename.

Referenced by SimRunner::init().

267 {
268  _basename = name;
269 }
string _basename
the base file name of the simulation, read from the init file (param "filename")
Definition: fileservices.h:69
void FileServices::setMode ( unsigned int  m)
inline

Mode setter, determines if file will get overwritten or not.

Referenced by SimRunner::init().

107 {_mode = m;}
unsigned int _mode
File mode, sets behavior when file must be overwritten or not.
Definition: fileservices.h:79
void FileServices::setRootDir ( string  name)

Sets the root directory of the simulation.

References _root_dir.

Referenced by SimRunner::init().

274 {
275  _root_dir = name;
276 
277  if(_root_dir.size() != 0 && _root_dir[_root_dir.length()-1] != '/')
278  _root_dir += "/";
279 
280 }
string _root_dir
the root directory for the simulation's results, read from the init file (param "root_dir") ...
Definition: fileservices.h:72

Member Data Documentation

string FileServices::_basename
private

the base file name of the simulation, read from the init file (param "filename")

Referenced by getBaseFileName(), getFirstReplicateFileName(), getGenerationReplicateFileName(), getReplicateFileName(), init(), and setBasename().

FHLogWriter* FileServices::_logWriter
private

a FileHandler used to save the simulation parameters on disk.

Referenced by FileServices(), init(), log(), log_simparams(), reset(), and ~FileServices().

unsigned int FileServices::_mode
private

File mode, sets behavior when file must be overwritten or not.

Referenced by getMode(), init(), and log_simparams().

list< ParamSet* > FileServices::_params
private

the list of the parameters of the current simulation.

The list is created when init() is called from SimRunner::run().

Referenced by get_params(), init(), and log_simparams().

Metapop* FileServices::_popPtr
private
list< FileHandler* > FileServices::_readers
private

the list of the FileHandler's registered by the SimComponent in input mode

Referenced by attach_reader(), and reset().

string FileServices::_rep_filename
private

the file name associated with the current simulation replicate

Referenced by getFirstReplicateFileName(), and getReplicateFileName().

string FileServices::_root_dir
private

the root directory for the simulation's results, read from the init file (param "root_dir")

Referenced by getRootDir(), init(), and setRootDir().

list< FileHandler* > FileServices::_writers
private

the list of the FileHandler's registered by the SimComponent in output mode

Referenced by attach(), init(), notify(), and reset().


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