casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SIIterBot.h
Go to the documentation of this file.
00001 //# SIIterBot.h: This file contains the interface definition SIIterBot class
00002 //#
00003 //#  CASA - Common Astronomy Software Applications (http://casa.nrao.edu/)
00004 //#  Copyright (C) Associated Universities, Inc. Washington DC, USA 2011, All rights reserved.
00005 //#  Copyright (C) European Southern Observatory, 2011, All rights reserved.
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.1 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,
00020 //#  MA 02111-1307  USA
00021 //# $Id: $
00022 
00023 #ifndef SYNTHESIS_SIITERBOT
00024 #define SYNTHESIS_SIITERBOT
00025 
00026 // .casarc interface
00027 #include <casa/System/AipsrcValue.h>
00028 
00029 // System utilities (for profiling macros)
00030 #include <casa/OS/HostInfo.h>
00031 #include <sys/time.h>
00032 #include <dbus-c++/dbus.h> /*for DBus::Variant... probably can be removed with *_adaptor class*/
00033 
00034 
00035 // Boost Libraries for mutex and noncopyable semantics
00036 #include <boost/thread/thread.hpp>
00037 #include <boost/thread/recursive_mutex.hpp>
00038 #include <boost/utility.hpp>
00039 
00040 // Include files for the DBus Service
00041 //#include <casadbus/interfaces/SynthImager.adaptor.h>
00042 #include <tr1/memory>
00043 
00044 #ifdef INTERACTIVE_ITERATION
00045 #include <casadbus/interfaces/SynthImager.adaptor.h>
00046 #endif
00047 
00048 /* Future Decl */
00049 class casa::Record;
00050 
00051 namespace casa { //# NAMESPACE CASA - BEGIN
00052 
00053         class SIIterBot_adaptor;
00054 
00055         class SIIterBot_callback {
00056                 public:
00057                         SIIterBot_callback( ) : adaptor(0) { }
00058                         ~SIIterBot_callback( ) { }
00059                         void interactionRequired(bool);
00060 
00061                         void addHandler( SIIterBot_adaptor* );
00062                         void removeHandler( SIIterBot_adaptor* );
00063 
00064                 private:
00065                         boost::recursive_mutex mutex;
00066                         SIIterBot_adaptor *adaptor;
00067 
00068         };
00069 
00070         class SIIterBot_state {
00071                 private:
00072                         // make SIIterBot_state uncopyable...
00073                         SIIterBot_state( const SIIterBot_state & );
00074                         SIIterBot_state &operator=( const SIIterBot_state & );
00075 
00076                 public:
00077                         SIIterBot_state( std::tr1::shared_ptr<SIIterBot_callback> );
00078                         ~SIIterBot_state( );
00079 
00080                         /****
00081                         ***** allow or deny callbacks which are funneled through dbus,
00082                         ***** a shared pointer is explicitly NOT used here to avoid a cycle.
00083                         ****/
00084                         void acceptCallbacks( SIIterBot_adaptor *siba ) { callback->addHandler(siba); }
00085                         void denyCallbacks( SIIterBot_adaptor *siba ) { callback->removeHandler(siba); }
00086 
00087                         /* Wait for an Interactive Clean Cycle */
00088                         bool interactiveInputRequired();
00089                         void waitForInteractiveInput(); 
00090 
00091                         // virtual bool majorCycleRequired(Float currentPeakResidual);
00092 
00093                         bool cleanComplete();
00094     
00095                         /* --- Functions for interacting with Minor Cycle Control --- */
00096                         Record getMinorCycleControls();
00097                         void   mergeCycleInitializationRecord(Record&);
00098                         void   mergeCycleExecutionRecord(Record&);
00099     
00100 
00101                         //void mergeSubIterBot(SISubIterBot& subIterBot);
00102 
00103                         /* ------ Begin functions for runtime parameter modification ------ */
00104                         /* These are the control variables.  We can set them either through these
00105                            method or through the Record interface */
00106                         void changeNiter( Int niter );
00107                         void changeCycleNiter( Int cycleniter );
00108                         void changeInteractiveNiter(Int interactiveniter );
00109 
00110                         void changeThreshold( Float threshold );
00111                         void changeCycleThreshold( Float cyclethreshold );
00112                         void changeInteractiveThreshold( Float cyclethreshold );
00113                         
00114                         void changeLoopGain(Float loopgain );
00115                         void changeCycleFactor( Float cyclefactor);
00116 
00117                         void changeInteractiveMode(const bool& interactiveEnabled);
00118                         void changePauseFlag(const bool& pauseEnabled);
00119                         void changeStopFlag(const bool& stopEnabled);
00120 
00121                         /* As a convience the controls can also be updated from a Record.  
00122                            The following fields are supported:
00123                            - niter
00124                            - cycleniter
00125                            - interactiveniter
00126                            - threshold
00127                            - cyclethreshold
00128                            - interactivethreshold
00129                            - loopgain
00130                            - cyclefactor
00131                         */
00132                         void setControlsFromRecord(Record &recordIn);
00133 
00134                         Record getDetailsRecord(); 
00135                         //Record getSubIterBotRecord();
00136 
00137                         /* ------ END Functions for runtime parameter modification -------*/
00138 
00139                         /* Call these functions to keep track of cycles */
00140 
00141                         /* Note:  Incrementing the Major cycle will reset the cycleIterDone */
00142                         void incrementMajorCycleCount();
00143 
00144                         Int getMajorCycleCount();
00145 
00146 
00147 
00148                         /* This will calculate and set a new cycle threshold based
00149                            on the Peak Residual and the current values of the PSF values.*/
00150                         void updateCycleThreshold();
00151     
00152                         /* Values for the control of the cycle threshold */
00153                         /*     void setMaxPsfSidelobe( Float maxpsfsidelobe ); */
00154                         /*     Float getMaxPsfSidelobe(); */
00155 
00156                         /*     void setMaxPsfFraction(Float maxpsffraction ); */
00157                         /*     Float getMaxPsfFraction(); */
00158 
00159                         /*     void setMinPsfFraction(Float minpsffraction ); */
00160                         /*     Float getMinPsfFraction(); */
00161                         //Int getRemainingNiter();
00162                         //Int getCompletedNiter();
00163 
00164                         void addSummaryMajor();
00165 
00166                         /* -------- DBus Interface ------------- */
00167 
00168                         /* Publish the current details from the Iterbot to all clients */
00169                         void pushDetails();
00170 
00171                         /* Publish the current summary from the Iterbot to all clients */
00172                         void pushSummary();
00173 
00174                         /* These are the fuctions we provide through the dbus interface */
00175                         bool incrementController();
00176                         bool decrementController();
00177                         int  getNumberOfControllers();
00178 
00179                         std::string getDescription( );
00180                         void setDescription( const std::string &value );
00181 
00182 #ifdef INTERACTIVE_ITERATION
00183                         std::map<std::string,DBus::Variant> getDetails();
00184 #endif
00185                         DBus::Variant getSummary();
00186     
00187                         void interactionComplete();
00188 
00189 #ifdef INTERACTIVE_ITERATION
00190                         void controlUpdate(const std::map<std::string, DBus::Variant>& parameters);
00191 #endif
00192 
00193 
00194                         /* Methods to get the state of the iterbot as a Record*/
00195 
00196                         /* This record has all of the fields associated with the detail record 
00197                            but adds
00198                            - summaryminor
00199                            - summarymajor
00200                         */
00201                         Record getSummaryRecord();
00202 
00203                 protected:
00204 
00205                         void mergeMinorCycleSummary(const Array<Double>& summary);
00206 
00207                         /* For testing/debugging. Print out the contents of the iterbot */
00208                         void printOut(String prefix, Bool verbose); 
00209 
00210                         std::string itsDescription;
00211 
00212                         Float itsMinPsfFraction;
00213                         Float itsMaxPsfFraction;
00214                         Float itsMaxPsfSidelobe;
00215                         Float itsPeakResidual;
00216 
00217                         /* The number of Controllers Currently Connected */
00218                         int    itsControllerCount;
00219 
00220 
00221                         /* A recursive mutex which provides access control to the
00222                            underlying state variables
00223                         */
00224                         boost::recursive_mutex recordMutex;
00225                         boost::recursive_mutex descriptionMutex; 
00227                         /* Control Variables */
00228                         Int    itsNiter;
00229                         Int    itsCycleNiter;
00230                         Int    itsInteractiveNiter;
00231 
00232                         Float itsThreshold;
00233                         Float itsCycleThreshold;
00234                         Float itsInteractiveThreshold;
00235 
00236                         Float itsCycleFactor;
00237                         Float itsLoopGain;
00238     
00239                         Bool  itsStopFlag;
00240                         Bool  itsPauseFlag;
00241                         Bool  itsInteractiveMode;
00242                         Bool  itsUpdatedModelFlag;
00243 
00244                         Int   itsIterDone;
00245                         Int   itsInteractiveIterDone;
00246                         Int   itsMaxCycleIterDone;
00247                         Int   itsMajorDone;
00248 
00249                         /*
00250                           A condition variable used when we're waiting for interaction to 
00251                           complete
00252                         */
00253                         bool                      interactionPending;
00254                         bool                      updateNeeded;
00255                         boost::condition_variable interactionCond; 
00256                         boost::mutex              interactionMutex; 
00257 
00258 
00259                         /* Summary variables */
00260                         Int itsNSummaryFields;
00261                         Array<Double> itsSummaryMinor;
00262                         Array<Int>    itsSummaryMajor;
00263 
00264                         std::tr1::shared_ptr<SIIterBot_callback> callback;
00265         };
00266 
00267         class SIIterBot_adaptor
00268 #ifdef INTERACTIVE_ITERATION
00269                 : private edu::nrao::casa::SynthImager_adaptor,
00270                   public DBus::IntrospectableAdaptor,
00271                   public DBus::ObjectAdaptor
00272 #endif
00273                 {
00274                         public:
00275                                 SIIterBot_adaptor( std::tr1::shared_ptr<SIIterBot_state> state, const std::string &serviceName);
00276                                 ~SIIterBot_adaptor();
00277 
00278                                 bool incrementController( )     { return state->incrementController( ); }
00279                                 bool decrementController( )     { return state->decrementController( ); }
00280 
00281                                 void interactionRequired( const bool &val ) {
00282 #ifdef INTERACTIVE_ITERATION
00283                                         edu::nrao::casa::SynthImager_adaptor::interactionRequired( val );
00284 #endif
00285                                 }
00286                                 void controlUpdate(const std::map< std::string, ::DBus::Variant >& newParams)
00287                                                                                         {
00288 #ifdef INTERACTIVE_ITERATION
00289                                                                                                 state->controlUpdate(newParams);
00290 #endif
00291                                                                                         }
00292                                 void interactionComplete( )
00293                                                                                         { state->interactionComplete( ); }
00294                                 void changePauseFlag(const bool& pauseState)
00295                                                                                         { state->changePauseFlag(pauseState); }
00296                                 void changeStopFlag(const bool& stopState)
00297                                                                                         { state->changeStopFlag(stopState); }
00298                                 void changeInteractiveMode(const bool& interactiveMode)
00299                                                                                         { state->changeInteractiveMode(interactiveMode); }
00300                                 std::string getDescription( )
00301                                                                                         { return state->getDescription( ); }
00302                                 std::map< std::string, ::DBus::Variant > getDetails( )
00303                                                                                         {
00304 #ifdef INTERACTIVE_ITERATION
00305                                                                                                 return state->getDetails( );
00306 #else
00307                                                                                                 return std::map< std::string, ::DBus::Variant >( );
00308 #endif
00309                                                                                         }
00310                                 ::DBus::Variant getSummary( )
00311                                                                                         { return state->getSummary( ); }
00312 
00313                         private:
00314                                 std::tr1::shared_ptr<SIIterBot_state> state;
00315 
00316                 };
00317     
00318 } //# NAMESPACE CASA - END
00319 
00320 #endif /* FLAGDATAHANDLER_H_ */