EvolvingObjects
eoCheckPoint.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoCheckPoint.h
00005 // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000
00006 /*
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00022              Marc.Schoenauer@polytechnique.fr
00023              mkeijzer@dhi.dk
00024  */
00025 //-----------------------------------------------------------------------------
00026 
00027 #ifndef _eoCheckPoint_h
00028 #define _eoCheckPoint_h
00029 
00030 #include <eoContinue.h>
00031 #include <utils/eoUpdater.h>
00032 #include <utils/eoMonitor.h>
00033 #include <utils/eoStat.h>
00034 
00064 template <class EOT>
00065 class eoCheckPoint : public eoContinue<EOT>
00066 {
00067 public :
00068 
00069     eoCheckPoint(eoContinue<EOT>& _cont)
00070   {
00071     continuators.push_back(&_cont);
00072   }
00073 
00074     bool operator()(const eoPop<EOT>& _pop);
00075 
00076     void add(eoContinue<EOT>& _cont) { continuators.push_back(&_cont); }
00077     void add(eoSortedStatBase<EOT>& _stat) { sorted.push_back(&_stat); }
00078     void add(eoStatBase<EOT>& _stat) { stats.push_back(&_stat); }
00079     void add(eoMonitor& _mon)        { monitors.push_back(&_mon); }
00080     void add(eoUpdater& _upd)        { updaters.push_back(&_upd); }
00081 
00082     virtual std::string className(void) const { return "eoCheckPoint"; }
00083     std::string allClassNames() const ;
00084 
00085 private :
00086 
00087   std::vector<eoContinue<EOT>*>    continuators;
00088     std::vector<eoSortedStatBase<EOT>*>    sorted;
00089     std::vector<eoStatBase<EOT>*>    stats;
00090     std::vector<eoMonitor*> monitors;
00091     std::vector<eoUpdater*> updaters;
00092 };
00093 
00094 template <class EOT>
00095 bool eoCheckPoint<EOT>::operator()(const eoPop<EOT>& _pop)
00096 {
00097     unsigned i;
00098 
00099     std::vector<const EOT*> sorted_pop;
00100     if (!sorted.empty())
00101     {
00102       _pop.sort(sorted_pop);
00103 
00104       for (i = 0; i < sorted.size(); ++i)
00105       {
00106         (*sorted[i])(sorted_pop);
00107       }
00108     }
00109 
00110     for (i = 0; i < stats.size(); ++i)
00111         (*stats[i])(_pop);
00112 
00113     for (i = 0; i < updaters.size(); ++i)
00114         (*updaters[i])();
00115 
00116     for (i = 0; i < monitors.size(); ++i)
00117         (*monitors[i])();
00118 
00119     bool bContinue = true;
00120     for (i = 0; i < continuators.size(); ++i)
00121       if ( !(*continuators[i])(_pop) )
00122         bContinue = false;
00123 
00124     if (! bContinue)       // we're going to stop: lastCall, gentlemen
00125       {
00126         if (!sorted.empty())
00127           {
00128             for (i = 0; i < sorted.size(); ++i)
00129               {
00130                 sorted[i]->lastCall(sorted_pop);
00131               }
00132           }
00133         for (i = 0; i < stats.size(); ++i)
00134           stats[i]->lastCall(_pop);
00135 
00136         for (i = 0; i < updaters.size(); ++i)
00137           updaters[i]->lastCall();
00138 
00139         for (i = 0; i < monitors.size(); ++i)
00140           monitors[i]->lastCall();
00141       }
00142     return bContinue;
00143 }
00144 
00148 template <class EOT>
00149 std::string eoCheckPoint<EOT>::allClassNames() const
00150 {
00151     unsigned i;
00152     std::string s = "\n" + className() + "\n";
00153 
00154     s += "Sorted Stats\n";
00155     for (i = 0; i < sorted.size(); ++i)
00156         s += sorted[i]->className() + "\n";
00157     s += "\n";
00158 
00159     s += "Stats\n";
00160     for (i = 0; i < stats.size(); ++i)
00161         s += stats[i]->className() + "\n";
00162     s += "\n";
00163 
00164     s += "Updaters\n";
00165     for (i = 0; i < updaters.size(); ++i)
00166         s += updaters[i]->className() + "\n";
00167     s += "\n";
00168 
00169     s += "Monitors\n";
00170     for (i = 0; i < monitors.size(); ++i)
00171         s += monitors[i]->className() + "\n";
00172     s += "\n";
00173 
00174     s += "Continuators\n";
00175     for (i = 0; i < continuators.size(); ++i)
00176         s += continuators[i]->className() + "\n";
00177     s += "\n";
00178 
00179     return s;
00180 }
00181 
00183 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends