FileHandler Class Reference

Interface to handle file input/output for any SimComponent. More...

#include <filehandler.h>

Inheritance diagram for FileHandler:

Inheritance graph
[legend]
Collaboration diagram for FileHandler:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 FileHandler (const char *ext)
virtual ~FileHandler ()
virtual void init ()
 Called by notifier during simulation setup, performs file checking.
virtual bool ifExist ()
 Checks if any file associated with the current file name already exists on disk.
virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path)
 Sets the hanlder parameters.
virtual void FHwrite ()=0
 Default behavior of the class, called by Handler::update().
virtual void FHread (string &filename)=0
 Default input function.
virtual void update ()
 Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file.
Accessors
Metapopget_pop_ptr ()
 Returns the pointer to the current metapop through the FileServices interface.
void set_pop_ptr (Metapop *pop_ptr)
FileServicesget_service ()
 Returns pointer to the FileServices.
void set_service (FileServices *srv)
std::string & get_path ()
void set_path ()
std::string & get_extension ()
void set_extension (const char *ext)
std::string & get_filename ()
 Builds and returns the current file name depending on the periodicity of the file.
bool get_isInputHandler ()
void set_isInputHandler (bool val)
bool get_isReplicatePeriodic ()
void set_isReplicatePeriodic (bool val)
unsigned int get_ReplicateOccurrence ()
void set_ReplicateOccurrence (unsigned int val)
bool get_isGenerationPeriodic ()
void set_isGenerationPeriodic (bool val)
unsigned int get_GenerationOccurrence ()
void set_GenerationOccurrence (unsigned int val)
unsigned int get_ExecRank ()
 unused yet.
void set_ExecRank (int val)
bool get_isMasterExec ()
void set_isMasterExec (bool is)

Protected Attributes

Metapop_pop
 Pointer to the current metapop, set during initialization within the init function.

Private Attributes

FileServices_service
 Link to the files manager.
bool _isMasterExec
 Flag telling if the file must be written by the master or the slave node.
bool _isInputHandler
 Writing mode flag.
bool _isReplicatePeriodic
 Flag telling if the file should be written each replicate.
bool _isGenerationPeriodic
 Flag setting the per generation periodicity behaviour of the file.
unsigned int _ReplicateOccurrence
 Tells every what replicate the file should be written.
unsigned int _GenerationOccurrence
 Tells every what generation the file should be written.
unsigned int _current_replicate
 The current replicate number of the simulation.
unsigned int _current_generation
 The current generation number of the simulation.
unsigned int _ExecRank
 unused.
std::string _path
 File path as set during initialization.
std::string _extension
 File extension, should be specific to the implementation.
std::string _current_filename
 The current filename as set by FileHandler::get_filename().


Detailed Description

Interface to handle file input/output for any SimComponent.

Stores the periodicity parameters and the file path and extension. The replicate file name is given by the FileServices. A file handler might be set to write output at a specific generation of a specific replicate or at some periodic time during the simulation. The default writing mode of a FileHandler is in output and is added as a 'writer' to the File Services as such using the FileServices::attach mode. The _isInputHandler flag is thus set to FALSE by default. To set a FileHandler as a reader instead of a writer, the method FileHandler::set_isInputHandler(TRUE) must be called prior to attaching it to the FileServices using the FileServices::attach_reader method. Furthermore, a single instance of a FileHandler can be attached as both a writer and a reader by setting the _isInputHandler flag to TRUE and then using the FileServices::attach method to add it to the list of both the reader and writers. Note on memory management: the FileServices class only deals with its lists of FileHandlers pointers and will never try to delete any one of those. All memory management must thus be taken care of by the user class (typically the TraitPrototype...).

Definition at line 52 of file filehandler.h.


Constructor & Destructor Documentation

FileHandler::FileHandler ( const char *  ext  )  [inline]

Definition at line 99 of file filehandler.h.

00100     : _service(0), 
00101 #ifdef USE_MPI
00102     _isMasterExec(false), 
00103 #else
00104     _isMasterExec(true), 
00105 #endif
00106     _isInputHandler(0),
00107     _isReplicatePeriodic(0), _isGenerationPeriodic(0),
00108     _ReplicateOccurrence(0), _GenerationOccurrence(0),
00109     _current_replicate(0), _current_generation(0), _ExecRank(0),
00110     _path(), _extension(ext), _current_filename(), _pop(0)
00111     {}

virtual FileHandler::~FileHandler (  )  [inline, virtual]

Definition at line 113 of file filehandler.h.

00113 { }


Member Function Documentation

virtual void FileHandler::FHread ( string &  filename  )  [pure virtual]

virtual void FileHandler::FHwrite (  )  [pure virtual]

unsigned int FileHandler::get_ExecRank (  )  [inline]

unused yet.

..

Definition at line 157 of file filehandler.h.

00157 {return _ExecRank;}

std::string& FileHandler::get_extension (  )  [inline]

string & FileHandler::get_filename (  ) 

Builds and returns the current file name depending on the periodicity of the file.

Definition at line 138 of file filehandler.cc.

Referenced by BinaryDataSaver::finish(), BinaryDataSaver::printData(), BinaryDataSaver::printHeader(), and FHLogWriter::save_simparams().

unsigned int FileHandler::get_GenerationOccurrence (  )  [inline]

Definition at line 154 of file filehandler.h.

00154 {return _GenerationOccurrence;}

bool FileHandler::get_isGenerationPeriodic (  )  [inline]

Definition at line 151 of file filehandler.h.

00151 {return _isGenerationPeriodic;}

bool FileHandler::get_isInputHandler (  )  [inline]

Definition at line 142 of file filehandler.h.

Referenced by FileServices::attach().

00142 {return _isInputHandler;}

bool FileHandler::get_isMasterExec (  )  [inline]

Definition at line 160 of file filehandler.h.

00160 {return _isMasterExec;}

bool FileHandler::get_isReplicatePeriodic (  )  [inline]

Definition at line 145 of file filehandler.h.

00145 {return _isReplicatePeriodic;}

std::string& FileHandler::get_path (  )  [inline]

Metapop* FileHandler::get_pop_ptr (  )  [inline]

Returns the pointer to the current metapop through the FileServices interface.

Definition at line 123 of file filehandler.h.

Referenced by TTDeletMutBitstrFH::FHwrite().

00123 {return _pop;}

unsigned int FileHandler::get_ReplicateOccurrence (  )  [inline]

Definition at line 148 of file filehandler.h.

00148 {return _ReplicateOccurrence;}

FileServices* FileHandler::get_service (  )  [inline]

bool FileHandler::ifExist (  )  [virtual]

Checks if any file associated with the current file name already exists on disk.

Only checks wether the first replicate file is present.

Returns:
false if filename already exists on disk, true otherwise.

Reimplemented in LCE_StatFH.

Definition at line 69 of file filehandler.cc.

00070 {
00071   //check if the basefilename is already used on disk:
00072   string filename = _path + _service->getBaseFileName();
00073 
00074   ostringstream rpl, gen;
00075     
00076   gen.fill('0');
00077   gen.width( (int)log10((double)_pop->getGenerations()) + 1);
00078   gen<<_GenerationOccurrence;
00079   
00080   rpl.fill('0');
00081   rpl.width( (int)log10((double)_pop->getReplicates()) + 1);
00082   rpl<<_ReplicateOccurrence;    
00083   
00084   if(!_isReplicatePeriodic)
00085     filename += _extension;
00086   else if(!_isGenerationPeriodic)
00087     filename += "_" + rpl.str() + _extension;
00088   else
00089     filename += "_" + gen.str() + "_" + rpl.str() + _extension;
00090   
00091   ifstream ifXist;
00092   ifXist.setstate(ios::failbit);
00093   
00094   ifXist.open(filename.c_str(),ios::in);
00095   
00096   if(ifXist.is_open()) {
00097     warning("filename \"%s\" used by \"%s\"\n",_service->getBaseFileName().c_str(),filename.c_str());
00098     ifXist.close();
00099     return false;
00100   }
00101   ifXist.close();
00102 
00103   return true;
00104 }

void FileHandler::init (  )  [virtual]

Called by notifier during simulation setup, performs file checking.

Implements Handler.

Definition at line 51 of file filehandler.cc.

00052 {
00053   _pop = _service->get_pop_ptr();
00054   //check if the occurrences exceed the pop parameters:
00055   if(_GenerationOccurrence > _pop->getGenerations())
00056     _GenerationOccurrence = _pop->getGenerations();
00057 
00058   if(_ReplicateOccurrence > _pop->getReplicates())
00059     _ReplicateOccurrence = _pop->getReplicates();
00060 
00061   if ( _myenv->isMaster() != _isMasterExec ) return;
00062   //@todo check FileHandler::init with MPI, set_path() has moved from set() to init()!
00063   set_path();
00064 //  message("     %s*%s\n",get_path().c_str(),get_extension().c_str());
00065 }

virtual void FileHandler::set ( bool  rpl_per,
bool  gen_per,
int  rpl_occ,
int  gen_occ,
int  rank,
string  path 
) [inline, virtual]

Sets the hanlder parameters.

Parameters:
rpl_per replicate periodicity
gen_per generation periodicity
rpl_occ replicate occurence
gen_occ generation occurence
rank the rank in the life cycle, actualy unused...
path the file path

Definition at line 175 of file filehandler.h.

Referenced by FileServices::init(), LCE_StatFH::LCE_StatFH(), LCE_StatServiceNotifier::setParameters(), and BinaryDataSaver::setParameters().

00175                                                                                                  {
00176         set_isReplicatePeriodic(rpl_per); set_isGenerationPeriodic(gen_per); set_ReplicateOccurrence(rpl_occ);
00177         set_GenerationOccurrence(gen_occ); set_ExecRank(rank); _path = path;}

void FileHandler::set_ExecRank ( int  val  )  [inline]

Definition at line 158 of file filehandler.h.

00158 {_ExecRank = val;}

void FileHandler::set_extension ( const char *  ext  )  [inline]

Definition at line 137 of file filehandler.h.

Referenced by TProtoNeutralGenes::loadFileServices().

00137 {_extension = ext;}

void FileHandler::set_GenerationOccurrence ( unsigned int  val  )  [inline]

Definition at line 155 of file filehandler.h.

00155 {_GenerationOccurrence = val;}

void FileHandler::set_isGenerationPeriodic ( bool  val  )  [inline]

Definition at line 152 of file filehandler.h.

00152 {_isGenerationPeriodic = val;}

void FileHandler::set_isInputHandler ( bool  val  )  [inline]

void FileHandler::set_isMasterExec ( bool  is  )  [inline]

Definition at line 164 of file filehandler.h.

Referenced by LCE_StatFH::LCE_StatFH().

00164 {_isMasterExec = true;}

void FileHandler::set_isReplicatePeriodic ( bool  val  )  [inline]

Definition at line 146 of file filehandler.h.

00146 {_isReplicatePeriodic = val;}

void FileHandler::set_path (  ) 

Definition at line 108 of file filehandler.cc.

Referenced by init().

00109 {
00110   string root_dir;
00111   
00112   root_dir = _service->getRootDir();
00113   
00114   if(_path.size() != 0 && _path[_path.length()-1] != '/')
00115         _path += "/";
00116   
00117   if(root_dir.size() != 0)
00118     _path = root_dir + _path;
00119   
00120   //check if we have to create the directory:
00121   if(_path.size() != 0 && opendir(_path.c_str()) == 0) {
00122 #ifdef _WINDOWS_
00123     mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
00124     if((mkdir(_path.c_str(), mode)) == -1){
00125 #else
00126     string cmd = "mkdir -p " + _path;
00127     if ( system( cmd.c_str() ) < 0 )
00128     {
00129 #endif
00130       error("could not create directory \"%s\", saving in wd.\n",_path.c_str());
00131       _path = "";
00132     }
00133   }
00134 }

void FileHandler::set_pop_ptr ( Metapop pop_ptr  )  [inline]

Definition at line 125 of file filehandler.h.

Referenced by Metapop::loadPopFromTraitFile().

00125 { _pop = pop_ptr;}

void FileHandler::set_ReplicateOccurrence ( unsigned int  val  )  [inline]

Definition at line 149 of file filehandler.h.

00149 {_ReplicateOccurrence = val;}

void FileHandler::set_service ( FileServices srv  )  [inline]

Definition at line 129 of file filehandler.h.

Referenced by FileServices::attach(), and FileServices::attach_reader().

00129 {_service = srv;}

void FileHandler::update (  )  [virtual]

Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file.

Implements Handler.

Definition at line 153 of file filehandler.cc.

00154 {
00155     _current_replicate = _pop->getCurrentReplicate();
00156 
00157     _current_generation = _pop->getCurrentGeneration();
00158 
00159     if ( ( (_isReplicatePeriodic && !(_current_replicate % _ReplicateOccurrence))
00160            || (_current_replicate == _ReplicateOccurrence) ) 
00161          && 
00162          ( (_isGenerationPeriodic && !(_current_generation % _GenerationOccurrence)) 
00163            || (_current_generation == _GenerationOccurrence) ) )
00164       if( _myenv->isMaster() == _isMasterExec)
00165         FHwrite();
00166 }


Member Data Documentation

std::string FileHandler::_current_filename [private]

The current filename as set by FileHandler::get_filename().

Is composed by putting together the following strings: FileHandler::_path, FileServices::_basename (+ replicate and generation counters if needed), and FileHandler::_extension.

Definition at line 91 of file filehandler.h.

Referenced by get_filename().

unsigned int FileHandler::_current_generation [private]

The current generation number of the simulation.

Set by FileHandler::update().

Definition at line 78 of file filehandler.h.

Referenced by update().

unsigned int FileHandler::_current_replicate [private]

The current replicate number of the simulation.

Set by FileHandler::update().

Definition at line 76 of file filehandler.h.

Referenced by update().

unsigned int FileHandler::_ExecRank [private]

unused.

.. yet

Definition at line 80 of file filehandler.h.

Referenced by get_ExecRank(), and set_ExecRank().

std::string FileHandler::_extension [private]

File extension, should be specific to the implementation.

Is set during construction.

Definition at line 85 of file filehandler.h.

Referenced by get_extension(), get_filename(), ifExist(), and set_extension().

unsigned int FileHandler::_GenerationOccurrence [private]

Tells every what generation the file should be written.

Set by the set() method below.

Definition at line 74 of file filehandler.h.

Referenced by get_GenerationOccurrence(), ifExist(), init(), set_GenerationOccurrence(), and update().

Flag setting the per generation periodicity behaviour of the file.

Definition at line 70 of file filehandler.h.

Referenced by get_filename(), get_isGenerationPeriodic(), ifExist(), set_isGenerationPeriodic(), and update().

Writing mode flag.

Must be true

Definition at line 66 of file filehandler.h.

Referenced by get_isInputHandler(), and set_isInputHandler().

Flag telling if the file must be written by the master or the slave node.

An example of master-only file is the stats files (.txt and _bygen.txt) that aggregate the stats from each node. Binary files or FSTAT files are written by the slave nodes, each file beeing written for one replicate only. The default behaviour is to be TRUE in the non-MPI version and FALSE in the MPI version.

Definition at line 64 of file filehandler.h.

Referenced by get_isMasterExec(), init(), set_isMasterExec(), and update().

Flag telling if the file should be written each replicate.

Definition at line 68 of file filehandler.h.

Referenced by get_filename(), get_isReplicatePeriodic(), ifExist(), set_isReplicatePeriodic(), and update().

std::string FileHandler::_path [private]

File path as set during initialization.

Usually user-defined.

Definition at line 82 of file filehandler.h.

Referenced by get_filename(), get_path(), ifExist(), and set_path().

Metapop* FileHandler::_pop [protected]

unsigned int FileHandler::_ReplicateOccurrence [private]

Tells every what replicate the file should be written.

Set by the set() method below.

Definition at line 72 of file filehandler.h.

Referenced by get_ReplicateOccurrence(), ifExist(), init(), set_ReplicateOccurrence(), and update().

Link to the files manager.

Definition at line 56 of file filehandler.h.

Referenced by get_filename(), get_service(), ifExist(), init(), set_path(), and set_service().


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