FileServices Class Reference

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

#include <fileservices.h>

Inheritance diagram for FileServices:

Inheritance graph
[legend]
Collaboration diagram for FileServices:

Collaboration graph
[legend]

List of all members.

Public Types

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

Public Member Functions

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

Private Attributes

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


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.

Definition at line 51 of file fileservices.h.


Member Typedef Documentation

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

Definition at line 82 of file fileservices.h.


Constructor & Destructor Documentation

FileServices::FileServices (  ) 

Definition at line 45 of file fileservices.cc.

00045                             : _popPtr(0), _rep_filename(""), _basename(""), _root_dir("") 
00046 {
00047   _logWriter = new FHLogWriter();
00048   attach(_logWriter);
00049 }

FileServices::~FileServices (  )  [virtual]

Definition at line 51 of file fileservices.cc.

00051                             {
00052   if(_logWriter) delete _logWriter;
00053 }


Member Function Documentation

void FileServices::attach ( FileHandler FH  )  [virtual]

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

Parameters:
FH the FileHandler

Definition at line 57 of file fileservices.cc.

Referenced by FileServices(), TProtoNeutralGenes::loadFileServices(), TProtoDeletMutations_bitstring::loadFileServices(), LCE_StatServiceNotifier::loadFileServices(), and BinaryDataSaver::loadFileServices().

00058 {
00059   Service::attach(FH);
00060   
00061   _writers.push_back(FH);
00062   
00063   if(FH->get_isInputHandler()) attach_reader(FH);
00064   
00065   FH->set_service(this);
00066 }

void FileServices::attach_reader ( FileHandler FH  )  [virtual]

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

Parameters:
FH the FileHandler

Definition at line 70 of file fileservices.cc.

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

00071 {
00072   _readers.push_back(FH);
00073   FH->set_service(this);
00074 }

list< ParamSet* >& FileServices::get_params (  )  [inline]

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

Definition at line 122 of file fileservices.h.

Referenced by BinaryDataSaver::printHeader().

00122 {return _params;};

virtual Metapop* FileServices::get_pop_ptr (  )  [inline, virtual]

Accessor to the pointer to the main population.

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

Definition at line 99 of file fileservices.h.

Referenced by FileHandler::init().

00099 {return _popPtr;}

string & FileServices::getBaseFileName (  ) 

Accessor to the base file name of the simulation.

Definition at line 202 of file fileservices.cc.

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

00203 {
00204   return _basename;
00205 }

file_it FileServices::getFirstReader (  )  [inline]

Accessor to first element of the list of input FileHandlers.

Definition at line 131 of file fileservices.h.

Referenced by getReader().

00131 {return _readers.begin();}

string & FileServices::getFirstReplicateFileName (  ) 

Accessor to the first replicate file name.

Definition at line 170 of file fileservices.cc.

00171 {
00172   ostringstream rpl;
00173 
00174   rpl.fill('0');
00175   rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
00176   rpl<< max( (unsigned)1, _myenv->slaveRank() );
00177 
00178   _rep_filename = _basename + "_" + rpl.str();
00179 
00180   return _rep_filename;
00181 }

file_it FileServices::getFirstWriter (  )  [inline]

Accessor to first element of the list of output FileHandlers.

Definition at line 125 of file fileservices.h.

Referenced by SimRunner::printLog().

00125 {return _writers.begin();}

string FileServices::getGenerationCounter (  ) 

Accessor to the current generation counter string.

Definition at line 229 of file fileservices.cc.

Referenced by getGenerationReplicateFileName().

00230 {
00231   ostringstream gen;
00232   
00233   gen.fill('0');
00234   gen.width( (int)log10((double)_popPtr->getGenerations()) + 1);
00235   gen<<_popPtr->getCurrentGeneration();
00236   
00237   return gen.str();
00238 }

string FileServices::getGenerationReplicateFileName (  ) 

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

Definition at line 194 of file fileservices.cc.

Referenced by TTDeletMutBitstrFH::FHwrite(), FileHandler::get_filename(), TTNeutralGenesFH::write_freq(), TTNeutralGenesFH::write_Fst_i(), TTNeutralGenesFH::write_FSTAT(), and TTNeutralGenesFH::write_varcompWC().

00195 {
00196   string name = _basename + "_" + getGenerationCounter() + "_" + getReplicateCounter();
00197   return name;
00198 }

file_it FileServices::getLastReader (  )  [inline]

Accessor to last element of the list of input FileHandlers.

Definition at line 134 of file fileservices.h.

Referenced by getReader().

00134 {return _readers.end();}

file_it FileServices::getLastWriter (  )  [inline]

Accessor to last element of the list of output FileHandlers.

Definition at line 128 of file fileservices.h.

Referenced by SimRunner::printLog().

00128 {return _writers.end();}  

unsigned int FileServices::getMode (  )  [inline]

Writting mode getter.

Definition at line 108 of file fileservices.h.

00108 {return _mode;}

FileHandler * FileServices::getReader ( string &  type  ) 

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

Definition at line 260 of file fileservices.cc.

Referenced by Metapop::loadPopFromTraitFile().

00261 {
00262   file_it file = getFirstReader(), last = getLastReader() ;
00263     
00264   for(;file != last; file++) {
00265     if( (*file)->get_extension().compare( type ) == 0) {
00266       return (*file);
00267     }
00268   }
00269   return 0;
00270 }

string FileServices::getReplicateCounter (  ) 

Accessor to the current replicate counter string.

Definition at line 216 of file fileservices.cc.

Referenced by getGenerationReplicateFileName(), and getReplicateFileName().

00217 {
00218   ostringstream rpl;
00219   
00220   rpl.fill('0');
00221   rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
00222   rpl<<_popPtr->getCurrentReplicate();
00223   
00224   return rpl.str();
00225 }

string & FileServices::getReplicateFileName (  ) 

Accessor to the current replicate file name.

Definition at line 185 of file fileservices.cc.

Referenced by FileHandler::get_filename().

00186 {
00187   _rep_filename = _basename + "_" + getReplicateCounter();
00188 
00189   return _rep_filename;
00190 }

std::string & FileServices::getRootDir (  ) 

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

Definition at line 209 of file fileservices.cc.

Referenced by FileHandler::set_path().

00210 {
00211   return _root_dir;
00212 }

bool FileServices::init ( list< ParamSet * > &  params  ) 

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

Parameters:
params a 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

Definition at line 82 of file fileservices.cc.

00083 {
00084   char yn;
00085   bool ok;
00086   file_it HIT;
00087 
00088   _params = params;
00089   
00090   _logWriter->set(0, 0, 0, 0, 0, "");
00091 
00092   //first, build path and filename for each file handler
00093   HIT = _writers.begin();
00094   message("    outputs: %s{",_root_dir.c_str());
00095   while(HIT != _writers.end()) {
00096     message("%s%s*%s",
00097             (*HIT)->get_path().c_str(),
00098             ((*HIT)->get_path().size() !=0? "/":""),
00099             (*HIT)->get_extension().c_str());
00100     (*HIT)->init();
00101     HIT++;
00102     if(HIT != _writers.end()) message(", ");
00103   }
00104   message("}\n");
00105   //then check if any file already exists
00106 check:
00107 
00108   ok = true;
00109   HIT = _writers.begin();
00110   //we have to skip the first writer, that is the log-writer
00111   while(++HIT != _writers.end()) {
00112     ok &= (*HIT)->ifExist();
00113 
00114   }
00115 
00116   if(!ok) {
00117 
00118 #if defined(_R_OUTPUT_) || defined(LOW_VERBOSE) || defined(USE_MPI)
00119     yn = 's';
00120     message("\nPlease choose an other base filename or move the existing file(s)\n");
00121 #else
00122     //file mode is set during simulation settup, in SimRunner::init()
00123     if(_mode == 0) {
00124       message("\nDo you want to overwrite all the files that use it ? (y/n/s(kip)): ");
00125       cin>>yn;
00126     } else if( _mode == 1 ) {
00127       message("Overwriting existing files.\n");
00128       yn = 'y';
00129     } else if( _mode == 2 ) {
00130       message("\nPlease choose another base filename or move the existing file(s)\n");
00131       yn = 's';
00132     } else if( _mode == 3 ) {
00133       //dryrun, pretend overwriting, silently
00134       yn = 'y';      
00135     } else {
00136       fatal("Run mode not properly set, check simulation parameter \"run_mode\".\n");
00137     }
00138 #endif
00139 
00140     switch(yn) {
00141       case 'y':
00142         break;
00143       case 's':
00144         message("Skipping this simulation\n");
00145         return false;
00146       default: {
00147         message("Please give a new output filename : ");
00148         cin>>_basename;
00149         goto check;
00150       }
00151     }
00152   }
00153 
00154     _logWriter->save_simparams(params);
00155   
00156   return true;
00157 }

virtual bool FileServices::init (  )  [inline, virtual]

Inits internals.

Implements Service.

Definition at line 88 of file fileservices.h.

Referenced by SimRunner::run().

00088 {return false;}

void FileServices::load ( SimComponent sc  )  [virtual]

Tells the SimComponent to load its file handlers.

Parameters:
sc the SimComponent

Implements Service.

Definition at line 78 of file fileservices.cc.

Referenced by SimRunner::register_component().

00078 {sc->loadFileServices(this);}

void FileServices::reset (  )  [virtual]

Clears the list of FileHandlers.

Reimplemented from Service.

Definition at line 161 of file fileservices.cc.

Referenced by SimRunner::reset_services().

00162 {
00163   Service::reset(); 
00164   _writers.clear(); _writers.push_back(_logWriter);
00165   _readers.clear();
00166 }

void FileServices::save_simparams ( list< ParamSet * > &  params,
std::string &  file 
)

Saves the current simulation parameters in log files.

Parameters:
params a ref to the list of the current parameters of the simulation
file the name of the log file

virtual void FileServices::set_pop_ptr ( Metapop pop  )  [inline, virtual]

Sets the Metapop reference.

Definition at line 102 of file fileservices.h.

Referenced by SimRunner::init_components().

00102 {_popPtr = pop;}

void FileServices::setBasename ( string  name  ) 

Sets the base file name of the simulation.

Definition at line 242 of file fileservices.cc.

Referenced by SimRunner::init().

00243 {
00244     _basename = name;
00245 }

void FileServices::setMode ( unsigned int  m  )  [inline]

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

Definition at line 105 of file fileservices.h.

Referenced by SimRunner::init().

00105 {_mode = m;}

void FileServices::setRootDir ( string  name  ) 

Sets the root directory of the simulation.

Definition at line 249 of file fileservices.cc.

Referenced by SimRunner::init().

00250 {
00251   _root_dir = name;
00252  
00253   if(_root_dir.size() != 0 && _root_dir[_root_dir.length()-1] != '/')
00254         _root_dir += "/";
00255   
00256 }


Member Data Documentation

string FileServices::_basename [private]

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

Definition at line 69 of file fileservices.h.

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

a FileHandler used to save the simulation parameters on disk.

Definition at line 57 of file fileservices.h.

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

unsigned int FileServices::_mode [private]

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

Definition at line 78 of file fileservices.h.

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

list< ParamSet* > FileServices::_params [private]

the list of the simulation parameters

Definition at line 75 of file fileservices.h.

Referenced by get_params(), and init().

a pointer to the current Metapop

Definition at line 55 of file fileservices.h.

Referenced by get_pop_ptr(), getFirstReplicateFileName(), getGenerationCounter(), getReplicateCounter(), and set_pop_ptr().

list< FileHandler* > FileServices::_readers [private]

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

Definition at line 63 of file fileservices.h.

Referenced by attach_reader(), getFirstReader(), getLastReader(), and reset().

string FileServices::_rep_filename [private]

the file name associated with the current simulation replicate

Definition at line 66 of file fileservices.h.

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")

Definition at line 72 of file fileservices.h.

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

list< FileHandler* > FileServices::_writers [private]

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

Definition at line 60 of file fileservices.h.

Referenced by attach(), getFirstWriter(), getLastWriter(), init(), and reset().


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