Nemo  2.2.0
Public Types | Public Member Functions | Private Attributes
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:

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.

References _logWriter, and attach().

FileServices::~FileServices ( ) [virtual]

Definition at line 51 of file fileservices.cc.

References _logWriter.

                            {
  if(_logWriter) delete _logWriter;
}

Member Function Documentation

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

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

Parameters:
FHthe FileHandler

Definition at line 70 of file fileservices.cc.

References _readers, and FileHandler::set_service().

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

{
  _readers.push_back(FH);
  FH->set_service(this);
}
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.

References _params.

Referenced by BinaryDataSaver::printHeader().

{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.

References _popPtr.

{return _popPtr;}
string & FileServices::getBaseFileName ( )

Accessor to the base file name of the simulation.

Definition at line 202 of file fileservices.cc.

References _basename.

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

{
  return _basename;
}
file_it FileServices::getFirstReader ( ) [inline]

Accessor to first element of the list of input FileHandlers.

Definition at line 131 of file fileservices.h.

References _readers.

Referenced by getReader().

{return _readers.begin();}
string & FileServices::getFirstReplicateFileName ( )

Accessor to the first replicate file name.

Definition at line 170 of file fileservices.cc.

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

{
  ostringstream rpl;

  rpl.fill('0');
  rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
  rpl<< max( (unsigned)1, _myenv->slaveRank() );

  _rep_filename = _basename + "_" + rpl.str();

  return _rep_filename;
}
file_it FileServices::getFirstWriter ( ) [inline]

Accessor to first element of the list of output FileHandlers.

Definition at line 125 of file fileservices.h.

References _writers.

Referenced by SimRunner::printLog().

{return _writers.begin();}
string FileServices::getGenerationCounter ( )

Accessor to the current generation counter string.

Definition at line 229 of file fileservices.cc.

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

Referenced by getGenerationReplicateFileName().

{
  ostringstream gen;
  
  gen.fill('0');
  gen.width( (int)log10((double)_popPtr->getGenerations()) + 1);
  gen<<_popPtr->getCurrentGeneration();
  
  return gen.str();
}
string FileServices::getGenerationReplicateFileName ( )

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

Definition at line 194 of file fileservices.cc.

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

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

{
  string name = _basename + "_" + getGenerationCounter() + "_" + getReplicateCounter();
  return name;
}
file_it FileServices::getLastReader ( ) [inline]

Accessor to last element of the list of input FileHandlers.

Definition at line 134 of file fileservices.h.

References _readers.

Referenced by getReader().

{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.

References _writers.

Referenced by SimRunner::printLog().

{return _writers.end();}  
unsigned int FileServices::getMode ( ) [inline]

Writting mode getter.

Definition at line 108 of file fileservices.h.

References _mode.

{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.

References getFirstReader(), and getLastReader().

Referenced by Metapop::loadPopFromTraitFile().

{
  file_it file = getFirstReader(), last = getLastReader() ;
    
  for(;file != last; file++) {
    if( (*file)->get_extension().compare( type ) == 0) {
      return (*file);
    }
  }
  return 0;
}
string FileServices::getReplicateCounter ( )

Accessor to the current replicate counter string.

Definition at line 216 of file fileservices.cc.

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

Referenced by getGenerationReplicateFileName(), and getReplicateFileName().

{
  ostringstream rpl;
  
  rpl.fill('0');
  rpl.width( (int)log10((double)_popPtr->getReplicates()) + 1);
  rpl<<_popPtr->getCurrentReplicate();
  
  return rpl.str();
}
string & FileServices::getReplicateFileName ( )

Accessor to the current replicate file name.

Definition at line 185 of file fileservices.cc.

References _basename, _rep_filename, and getReplicateCounter().

std::string & FileServices::getRootDir ( )

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

Definition at line 209 of file fileservices.cc.

References _root_dir.

{
  return _root_dir;
}
virtual bool FileServices::init ( ) [inline, virtual]

Inits internals.

Implements Service.

Definition at line 88 of file fileservices.h.

Referenced by SimRunner::run().

{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

Definition at line 82 of file fileservices.cc.

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

{
  char yn;
  bool ok;
  file_it HIT;

  _params = params;
  
  _logWriter->set(0, 0, 0, 0, 0, "");

  //first, build path and filename for each file handler
  HIT = _writers.begin();
  message("    outputs: %s{",_root_dir.c_str());
  while(HIT != _writers.end()) {
    message("%s%s*%s",
            (*HIT)->get_path().c_str(),
            ((*HIT)->get_path().size() !=0? "/":""),
            (*HIT)->get_extension().c_str());
    (*HIT)->init();
    HIT++;
    if(HIT != _writers.end()) message(", ");
  }
  message("}\n");
  //then check if any file already exists
check:

  ok = true;
  HIT = _writers.begin();
  //we have to skip the first writer, that is the log-writer
  while(++HIT != _writers.end()) {
    ok &= (*HIT)->ifExist();

  }

  if(!ok) {

#if defined(_R_OUTPUT_) || defined(LOW_VERBOSE) || defined(USE_MPI)
    yn = 's';
    message("\nPlease choose an other base filename or move the existing file(s)\n");
#else
    //file mode is set during simulation settup, in SimRunner::init()
    if(_mode == 0) {
      message("\nDo you want to overwrite all the files that use it ? (y/n/s(kip)): ");
      cin>>yn;
    } else if( _mode == 1 ) {
      message("Overwriting existing files.\n");
      yn = 'y';
    } else if( _mode == 2 ) {
      message("\nPlease choose another base filename or move the existing file(s)\n");
      yn = 's';
    } else if( _mode == 3 ) {
      //dryrun, pretend overwriting, silently
      yn = 'y';      
    } else {
      fatal("Run mode not properly set, check simulation parameter \"run_mode\".\n");
    }
#endif

    switch(yn) {
      case 'y':
        break;
      case 's':
        message("Skipping this simulation\n");
        return false;
      default: {
        message("Please give a new output filename : ");
        cin>>_basename;
        goto check;
      }
    }
  }

    _logWriter->save_simparams(params);
  
  return true;
}
void FileServices::load ( SimComponent sc) [virtual]

Tells the SimComponent to load its file handlers.

Parameters:
scthe SimComponent

Implements Service.

Definition at line 78 of file fileservices.cc.

References SimComponent::loadFileServices().

Referenced by SimRunner::register_component().

{sc->loadFileServices(this);}
void FileServices::reset ( ) [virtual]

Clears the list of FileHandlers.

Reimplemented from Service.

Definition at line 161 of file fileservices.cc.

References _logWriter, _readers, and _writers.

Referenced by SimRunner::reset_services().

{
  Service::reset(); 
  _writers.clear(); _writers.push_back(_logWriter);
  _readers.clear();
}
void FileServices::save_simparams ( list< ParamSet * > &  params,
std::string &  file 
)

Saves the current simulation parameters in log files.

Parameters:
paramsa ref to the list of the current parameters of the simulation
filethe 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.

References _popPtr.

Referenced by SimRunner::init_components().

{_popPtr = pop;}
void FileServices::setBasename ( string  name)

Sets the base file name of the simulation.

Definition at line 242 of file fileservices.cc.

References _basename.

Referenced by SimRunner::init().

{
    _basename = name;
}
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.

References _mode.

Referenced by SimRunner::init().

{_mode = m;}
void FileServices::setRootDir ( string  name)

Sets the root directory of the simulation.

Definition at line 249 of file fileservices.cc.

References _root_dir.

Referenced by SimRunner::init().

{
  _root_dir = name;
 
  if(_root_dir.size() != 0 && _root_dir[_root_dir.length()-1] != '/')
        _root_dir += "/";
  
}

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.2.0 by  doxygen 1.7.5.1 -- Nemo is hosted by  SourceForge.net Logo