Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Framework/src/APPModule.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: APPModule.cc,v 1.52 2002/03/28 22:04:47 desilva Exp $
00003 //
00004 // Description:
00005 //      Class AppModule. This class describes modules within the BaBar
00006 //      Application Framework.
00007 //
00008 // Environment:
00009 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00010 //
00011 // Author List:
00012 //      David R. Quarrie                Original Author
00013 //      Bob Jacobsen                    Brought into AbsEvent conformance
00014 //      Bob Jacobsen                    _commands must be unowned list
00015 //      P.Murat                         add handling of menu files
00016 //      Marc Turcotte                   Added new style begin<Job/Run>
00017 //                                      Added new style event.
00018 //      Marc Turcotte                   Added execute member function
00019 //                                      to support dynamic dispatch 3/98
00020 //      Marc Turcotte                   Introduced AppStopType 3/20/98
00021 //      Marc Turcotte                   Renamed execute to frame 3/26/98
00022 //      Marc Turcotte                   Modified to pass frames 'round 4/7/98 
00023 //      Marc Turcotte                   Modified to not barf if zero
00024 //                                      frame passes thru meaning someone
00025 //                                      forgot to line up a provider
00026 //                                      in the time ordered series 4/8/98                                    
00027 //      Marc Turcotte                   Added AppActionController 4/13/98
00028 //      Marc Turcotte                   Modified setActionEnabled to set
00029 //                                      Action Controllers. 4/15/98
00030 //      Marc Turcotte                   Removed static init of _theFrame 4/30/98
00031 //      Marc Turcotte                   Merged R.Kapur's TK interface 5/98
00032 //      Marc Turcotte                   Modified for AppStatus 6/20/98
00033 //      Marc Turcotte                   Moved dispatch from frame to here 6/22/98
00034 //      Marc Turcotte                   Added input/output Frame 6/22/98
00035 //      Marc Turcotte                   Added theDispatchStopType to
00036 //                                      signatures 6/23/98
00037 //      Marc Turcotte                   Changed AppStatus to AppResult 6/23/98
00038 //      Marc Turcotte                   Modified for new AbsEvent sigs 6/24/98
00039 //      Marc Turcotte                   Added clone command 8/6/98
00040 //      Marc Turcotte                   Modified used of getStopTypeKey 
00041 //                                      not to cause a leak. 8/21/98
00042 //
00043 // Copyright Information:
00044 //      Copyright (C) 1994, 1995        Lawrence Berkeley Laboratory
00045 //      Copyright (C) 1998              University of Texas at Dallas
00046 //
00047 //------------------------------------------------------------------------
00048 #include "Experiment/Experiment.hh"
00049 
00050 //-----------------------
00051 // This Class's Header --
00052 //-----------------------
00053 #include "Framework/APPModule.hh"
00054 
00055 //-------------
00056 // C Headers --
00057 //-------------
00058 #include <assert.h>
00059 #include <stdlib.h>
00060 #include <string.h>
00061 #include <strstream.h>
00062 
00063 //-------------------------------
00064 // Collaborating Class Headers --
00065 //-------------------------------
00066 #include "Framework/APPFramework.hh"
00067 #include "Framework/APPEchoCommand.hh"
00068 #include "Framework/APPExitCommand.hh"
00069 #include "Framework/APPHelpCommand.hh"
00070 #include "Framework/AppStopType.hh"
00071 //? #include "Framework/AppRecord.hh"
00072 #include "Framework/AppMethodBase.hh"
00073 #include "Framework/AppMethod.hh"
00074 class AppFrame;
00075 #include "Framework/APPHash.hh"
00076 #include "FrameUtil/AbsInterp.hh"
00077 
00078 #include "FrameUtil/APPList.hh"
00079 #include "FrameUtil/APPListIterator.hh"
00080 
00081 #include <iostream.h>
00082 
00083 //-----------------------
00084 // Static Initializers --
00085 //-----------------------
00086 
00087 APPMenu   AppModule::moduleMenu;
00088 AbsEvent* AppModule::_theAbsEvent = 0; // <----- TO BE MIGRATED TO AppFramework
00089 
00090 //              ----------------------------------------
00091 //              -- Public Function Member Definitions --
00092 //              ----------------------------------------
00093 
00094 //----------------
00095 // Constructors --
00096 //----------------
00097 
00098 AppModule::AppModule( 
00099     const char* const theName, 
00100     const char* const theDescription )
00101     : APPExecutable( theName, theDescription ), 
00102       _exit("exit", this),
00103       _echo("echo", this),
00104       _help("help", this),
00105       _show("show", this),
00106       _sourceFileCmd( "sourceFoundFile", this ),
00107       _production("production", this, false), 
00108       _verbose("verbose", this, false),
00109       _enableFrames("enableFrames", this, false),      
00110       _menuFileName(NULL ),
00111       _error   (NULL ),
00112       _errorLen( 0 )
00113 {
00114     _execType = APP_module;
00115     _isInitialized = false;
00116 
00117     
00118     _commands = new APPList< APPCommand* >;
00119     _commands->append( &_echo );
00120     _commands->append( &_exit );
00121     _commands->append( &_help );
00122     _commands->append( &_show );
00123     _commands->append( &_sourceFileCmd);     
00124     _commands->append( &_verbose );
00125     _commands->append( &_production );
00126     _commands->append( &_enableFrames );
00127 
00128 
00129     _prompt = new char[strlen( theName )+3];
00130     strcpy( _prompt, theName );
00131     strcat( _prompt, "> ");
00132     
00133     // Dynamic Dispach Map
00134     
00135     _theDynamicDispatchMap = new APPHash < AppMethodBase >;
00136     
00137 }
00138 
00139 //--------------
00140 // Destructor --
00141 //--------------
00142 
00143 AppModule::~AppModule( )
00144 {
00145     delete _commands;
00146     
00147     if ( NULL != _prompt ) {
00148         delete [] _prompt;
00149     }
00150     if ( NULL != _error ) {
00151         delete [] _error;
00152     }
00153     
00154     if ( NULL != _menuFileName ) {
00155         delete [] _menuFileName;
00156     }
00157   
00158     delete _theDynamicDispatchMap; // delete the dispatch map
00159 
00160 }
00161 
00162 //--------------
00163 // Operations --
00164 //--------------
00165 
00166 AppResult
00167 AppModule::beginJob( AbsEvent* anEvent ){
00168   return AppResult::OK;
00169 }
00170 
00171 AppResult
00172 AppModule::beginRun( AbsEvent* anEvent)
00173 {
00174   return AppResult::OK;
00175 }
00176 
00177 AppResult
00178 AppModule::frame( AppFrame* aFrame,
00179                   const AppStopType& theDispatchStopType) {  // Dynamic dispatch 
00180   
00181   AppResult theResult = AppResult::OK;  
00182   
00183   if ( aFrame!=0 ) { // Zero frames go nowhere...
00184     
00185     char* theStopTypeKey = theDispatchStopType.getStopTypeKey();     
00186     if((_theDynamicDispatchMap->has(theStopTypeKey))==true) 
00187     {// there is there a binding
00188       AppMethodBase* theMethod = _theDynamicDispatchMap->fetch(theStopTypeKey);
00189       theResult=theMethod->execute(aFrame); // then just deliver the frame to the binding...
00190     }
00191     delete [] theStopTypeKey;
00192     
00193   } else {
00194    
00195    fullReport("AppModule:: Error: You are moving a null Frame pointer.");
00196   
00197   }
00198   
00199   return theResult.value(); 
00200 
00201 }
00202 
00203 AppResult
00204 AppModule::event( AbsEvent* anEvent )
00205 {
00206     return AppResult::OK;
00207 }
00208 
00209 AppResult
00210 AppModule::other( AbsEvent* anEvent )
00211 {
00212     return AppResult::OK;
00213 }
00214 
00215 AppResult
00216 AppModule::endRun( AbsEvent* anEvent) {
00217     return AppResult::OK;
00218 }
00219 
00220 AppResult
00221 AppModule::endJob( AbsEvent* anEvent){
00222     return AppResult::OK;
00223 }
00224 
00225 AppResult
00226 AppModule::abortJob( AbsEvent* anEvent ){
00227     return AppResult::OK;
00228 }
00229 
00230 void
00231 AppModule::talkTo( )
00232 {
00233     enableCommands( );
00234 }
00235 
00236 #ifndef CDF
00237 // for the Framework migration.  Will be removed soon.
00238 void
00239 AppModule::help( )
00240 {
00241 }
00242 void 
00243 AppModule::show()
00244 {
00245 }
00246 #endif
00247 
00248 void
00249 AppModule::help( int argc, char** argv )
00250 {
00251   // default behavior, unless overridden, is to list commands
00252   // available in this module
00253   partialReport( "Command(s) available in the \"");
00254   partialReport( name() );
00255   fullReport("\" module:\n" );
00256 
00257   APPCommand** command;
00258   APPListIterator<APPCommand*> theIterator( *commands( ) );
00259   while ( command = theIterator( ) ) 
00260   {
00261      if ( argc == 1 ) // Describe the whole list of commands
00262      {
00263         partialReport( "      " );
00264         partialReport((*command)->command() ) ;
00265         if ( strlen( (*command)->command() ) < 10 )
00266         {
00267            partialReport("\t\t");
00268         }
00269         else
00270         {
00271            partialReport("\t"); 
00272         }
00273         fullReport((*command)->description( ).c_str( ));
00274      }
00275      else  // Describe the one they asked for
00276      {
00277         if ( strcmp( (*command)->command( ), argv[1] ) == 0 )
00278         {
00279            partialReport( "      " );
00280            partialReport((*command)->command() ) ;
00281            partialReport("\t");
00282            fullReport((*command)->description( ).c_str( ));
00283         }
00284      }
00285   }
00286 }
00287 
00288 void
00289 AppModule::show( int argc, char** argv ) const
00290 {
00291   // default behavior, unless overridden, is to display the
00292   // current values of commands available in this module
00293   partialReport( "Current value of item(s) in the \"");
00294   partialReport( name() );
00295   fullReport("\" module:\n" );
00296 
00297   APPCommand** command;
00298   APPListIterator<APPCommand*> theIterator( *commands( ) );
00299   while ( command = theIterator( ) ) 
00300   {
00301      if ( argc == 1 ) // Show the whole list of showable commands
00302      {
00303         if ( (*command)->isShowable( ) )
00304         {
00305            partialReport( "      " );
00306            (*command)->show( );
00307         }
00308      }
00309      else  // Show the one they asked for
00310      {
00311         if ( strcmp( (*command)->command( ), argv[1] ) == 0 )
00312         {
00313            partialReport( "      " );
00314            (*command)->show( );
00315         }
00316      }
00317   }
00318 }
00319 
00320 AppModule* 
00321 AppModule::clone(const char* cloneName) {
00322   partialReport("cloning to ");
00323   partialReport(cloneName);
00324   partialReport(" has not been implemented by ");
00325   partialReport(this->name());
00326   partialReport(" user module...\n");
00327   return 0;
00328 }
00329 
00330 bool
00331 AppModule::verbose() const
00332 {
00333   return _verbose.value();
00334 }
00335 
00336 bool
00337 AppModule::production() const
00338 {
00339   return _production.value();
00340 }
00341 
00342 bool
00343 AppModule::isShowable( ) const 
00344 {
00345    return true; 
00346 }
00347 
00348 void AppModule::menuHandler(char* menu, char* command, int argc, char** argv) {
00349   partialReport(" -------------------- menuHandler entered module : "); 
00350   fullReport(this->name());
00351   partialReport("menu    = ");
00352   fullReport(menu);
00353   partialReport("command = ");
00354   fullReport(command);
00355   partialReport(" N(argv) = ");
00356   ostrstream strbuff;
00357   strbuff << argc << '\0';
00358   fullReport(strbuff.str());
00359   for (int i=0; i<argc; i++) {
00360     fullReport(argv[i]);
00361   }
00362 }
00363 
00364 void
00365 AppModule::exit( )
00366 {
00367     disableCommands( );
00368     framework( )->enableCommands( );
00369 }
00370 
00371 void
00372 AppModule::disableCommands( )
00373 {
00374    APPCommand** command;
00375    APPListIterator<APPCommand*> theIterator( *commands( ) );
00376    while ( command = theIterator( ) ) {
00377       (*command)->disable( );
00378    }
00379 }
00380 
00381 void
00382 AppModule::enableCommands( )
00383 {
00384     APPCommand** command;
00385     APPListIterator<APPCommand*> theIterator( *commands( ) );
00386     while ( command = theIterator( ) ) 
00387     {
00388       (*command)->enable( );
00389     }
00390     AbsInterp* interp = AbsInterp::theInterpreter( );
00391     interp->setCommandPrompt( prompt( ) );
00392 }
00393 
00394 //-------------
00395 // Selectors --
00396 //-------------
00397     
00398 APPList< APPCommand* >* 
00399 AppModule::commands ( ) const
00400 {
00401     return _commands;
00402 }
00403 
00404 bool
00405 AppModule::isInitialized( ) const
00406 {
00407     return _isInitialized;
00408 }
00409 
00410 bool
00411 AppModule::passed( ) const
00412 {
00413     return true;
00414 }
00415 
00416 char*
00417 AppModule::prompt( ) const
00418 {
00419     return _prompt;
00420 }
00421 
00422 char* 
00423 AppModule::menuFileName( ) const {
00424     return _menuFileName;
00425 }
00426 
00427 //-------------
00428 // Modifiers --
00429 //-------------
00430 
00431 void 
00432 AppModule::setError( const char* const theError )
00433 {
00434         if ( NULL != theError ) {
00435                 if ( _errorLen < strlen( theError )+1 ) {
00436                         if ( NULL != _error ) {
00437                                 delete [] _error;
00438                         }
00439                         _errorLen = strlen( theError )+1;
00440                         _error    = new char[_errorLen];
00441                 }
00442                 strcpy( _error, theError );
00443         } else {
00444                 if ( NULL != _error ) {
00445                         delete [] _error;
00446                         _error    = NULL;
00447                         _errorLen = 0;
00448                 }
00449         }
00450         AbsInterp* interp = AbsInterp::theInterpreter( );
00451         interp->setResult( _error );
00452 }
00453 
00454 void 
00455 AppModule::setInitialized( )
00456 {
00457     _isInitialized = true;
00458 }
00459     
00460 void 
00461 AppModule::setPrompt( const char* const thePrompt )
00462 {
00463     if ( NULL != _prompt ) {
00464                 delete [] _prompt;
00465                 _prompt = NULL;
00466     }
00467     if ( NULL != thePrompt ) {
00468                 _prompt = new char[strlen( thePrompt )+1];
00469                 strcpy( _prompt, thePrompt );
00470     }
00471 }
00472 //
00473 // Provide a means to bind a derived module's method to a particular frame state
00474 //
00475 void AppModule::bindMethodToState(const AppStopType& aStopType,
00476                                   const AppMethodBase* const aMethod) {
00477   // 
00478   // Make an entry in the hash table of AppMethodBase pointers indexed by the Frame State.
00479   //
00480   
00481   char* aStopTypeKey = aStopType.getStopTypeKey();
00482   
00483   assert(_theDynamicDispatchMap->add(aStopTypeKey,aMethod)==true); // cannot fail...    
00484   partialReport("AppModule: ");
00485   partialReport(name());
00486   partialReport(" added this hash key: ");
00487   fullReport(aStopTypeKey);
00488   
00489   delete [] aStopTypeKey;
00490                               
00491 }
00492 
00493 APPHash < AppMethodBase >* AppModule::dynamicDispatchMap() {
00494   return _theDynamicDispatchMap;
00495 }
00496 
00497 void AppModule::setMenuFileName(const char* const filename )
00498 {
00499   if (! filename) return;
00500   if (_menuFileName) {
00501     delete _menuFileName;
00502   }
00503   _menuFileName = new char[strlen(filename)+1];
00504   strcpy(_menuFileName,filename);
00505 }

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002