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

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1