EvolvingObjects
eoFileSnapshot.h
00001 //-----------------------------------------------------------------------------
00002 // eoFileSnapshot.h
00003 // (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2001
00004 /*
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00020              Marc.Schoenauer@polytechnique.fr
00021              mkeijzer@dhi.dk
00022  */
00023 //-----------------------------------------------------------------------------
00024 
00025 #ifndef _eoFileSnapshot_h
00026 #define _eoFileSnapshot_h
00027 
00028 #include <cstdlib>
00029 #include <fstream>
00030 #include <string>
00031 
00032 #include <utils/eoParam.h>
00033 #include <utils/eoMonitor.h>
00034 #include <eoObject.h>
00035 
00036 
00054 class eoFileSnapshot : public eoMonitor
00055 {
00056 public :
00057   typedef std::vector<double> vDouble;
00058   typedef eoValueParam<std::vector<double> > vDoubleParam;
00059 
00060   eoFileSnapshot(std::string _dirname, unsigned _frequency = 1, std::string _filename = "gen",
00061               std::string _delim = " ", unsigned _counter = 0, bool _rmFiles = true):
00062     dirname(_dirname), frequency(_frequency),
00063     filename(_filename), delim(_delim), counter(_counter), boolChanged(true)
00064   {
00065     std::string s = "test -d " + dirname;
00066 
00067     int res = system(s.c_str());
00068     // test for (unlikely) errors
00069     if ( (res==-1) || (res==127) )
00070       throw std::runtime_error("Problem executing test of dir in eoFileSnapshot");
00071     // now make sure there is a dir without any genXXX file in it
00072     if (res)                    // no dir present
00073       {
00074         s = std::string("mkdir ")+dirname;
00075       }
00076     else if (!res && _rmFiles)
00077       {
00078         s = std::string("/bin/rm ")+dirname+ "/" + filename + "*";
00079       }
00080     else
00081       s = " ";
00082 
00083     res=system(s.c_str());
00084     // all done
00085   }
00086 
00089   virtual bool hasChanged() {return boolChanged;}
00090 
00093   unsigned getCounter() {return counter;}
00094 
00097   std::string getFileName() {return currentFileName;}
00098 
00101   void setCurrentFileName()
00102   {
00103       std::ostringstream oscount;
00104       oscount << counter;
00105       currentFileName = dirname + "/" + filename + oscount.str();
00106   }
00107 
00110   eoMonitor& operator()(void)
00111   {
00112     if (counter % frequency)
00113       {
00114         boolChanged = false;  // subclass with gnuplot will do nothing
00115         counter++;
00116         return (*this);
00117       }
00118     counter++;
00119     boolChanged = true;
00120     setCurrentFileName();
00121     std::ofstream os(currentFileName.c_str());
00122 
00123     if (!os)
00124       {
00125         std::string str = "eoFileSnapshot: Could not open " + currentFileName;
00126         throw std::runtime_error(str);
00127       }
00128 
00129     return operator()(os);
00130   }
00131 
00134   eoMonitor& operator()(std::ostream& _os)
00135   {
00136     const eoValueParam<std::vector<double> >  * ptParam =
00137       static_cast<const eoValueParam<std::vector<double> >* >(vec[0]);
00138 
00139     const std::vector<double>  v = ptParam->value();
00140     if (vec.size() == 1)           // only one std::vector: -> add number in front
00141       {
00142         for (unsigned k=0; k<v.size(); k++)
00143           _os << k << " " << v[k] << "\n" ;
00144       }
00145     else                           // need to get all other std::vectors
00146       {
00147         std::vector<std::vector<double> > vv(vec.size());
00148         vv[0]=v;
00149         for (unsigned i=1; i<vec.size(); i++)
00150           {
00151             ptParam = static_cast<const eoValueParam<std::vector<double> >* >(vec[1]);
00152             vv[i] = ptParam->value();
00153             if (vv[i].size() != v.size())
00154               throw std::runtime_error("Dimension error in eoSnapshotMonitor");
00155           }
00156         for (unsigned k=0; k<v.size(); k++)
00157           {
00158           for (unsigned i=0; i<vec.size(); i++)
00159             _os << vv[i][k] << " " ;
00160           _os << "\n";
00161           }
00162       }
00163     return *this;
00164    }
00165 
00166   virtual const std::string getDirName()           // for eoGnuPlot
00167   { return dirname;}
00168   virtual const std::string baseFileName()         // the title for eoGnuPlot
00169   { return filename;}
00170 
00172   void add(const eoParam& _param)
00173   {
00174     if (!dynamic_cast<const eoValueParam<std::vector<double> >*>(&_param))
00175     {
00176       throw std::logic_error(std::string("eoFileSnapshot: I can only monitor std::vectors of doubles, sorry. The offending parameter name = ") + _param.longName());
00177     }
00178     eoMonitor::add(_param);
00179   }
00180 
00181 private :
00182   std::string dirname;
00183   unsigned frequency;
00184   std::string filename;
00185   std::string delim;
00186   unsigned int counter;
00187   std::string currentFileName;
00188   bool boolChanged;
00189 };
00190 
00191 #endif
00192 
00193 
00194 // Local Variables:
00195 // coding: iso-8859-1
00196 // mode: C++
00197 // c-file-offsets: ((c . 0))
00198 // c-file-style: "Stroustrup"
00199 // fill-column: 80
00200 // End:
 All Classes Namespaces Files Functions Variables Typedefs Friends