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/APPMemento.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //
00004 // Description:
00005 //      Class AppMemento
00006 //      Do not use this for templated class (foo<T>).  use TemplateTemplate.hh
00007 //      instead.
00008 //
00009 // Environment:
00010 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00011 //
00012 // Author List:
00013 //      Akbar Mokhtarani        originator
00014 //      <Author2>               <originator/contributor etc.>
00015 //
00016 // Copyright Information:
00017 //      Copyright (C) 1998      LBNL
00018 //
00019 //------------------------------------------------------------------------
00020 #include "BaBar/BaBar.hh"
00021 
00022 //-----------------------
00023 // This Class's Header --
00024 //-----------------------
00025 #include "Framework/APPMemento.hh"
00026 
00027 //-------------
00028 // C Headers --
00029 //-------------
00030 extern "C" {
00031 }
00032 
00033 //---------------
00034 // C++ Headers --
00035 //---------------
00036 #include <string.h>
00037 #include <iostream.h>
00038 #include <fstream.h>
00039 
00040 //-------------------------------
00041 // Collaborating Class Headers --
00042 //-------------------------------
00043 #include "FrameUtil/APPListIterator.hh"
00044 #include "FrameUtil/APPList.hh"
00045 #include "FrameUtil/FwkString.hh"
00046 #include "Framework/APPPath.hh"
00047 #include "Framework/APPSequence.hh"
00048 #include "Framework/APPExecutable.hh"
00049 #include "Framework/APPExecNode.hh"
00050 #include "Framework/APPModule.hh"
00051 #include "Framework/APPInputModule.hh"
00052 #include "Framework/APPOutputModule.hh"
00053 #include "Framework/AppAccountModule.hh"
00054 #include "Framework/AbsModuleMemento.hh"
00055 #include "Framework/APPPathMemento.hh"
00056 #include "Framework/APPSequenceMemento.hh"
00057 #include "Framework/APPModuleMemento.hh"
00058 #include "Framework/APPFrameMemento.hh"
00059 #include "Framework/APPFramework.hh"
00060 #include "Framework/AppAction.hh"
00061 #include "Framework/AppActionController.hh"
00062 #include "Framework/APPUserBuild.hh"
00063 
00064 //-----------------------------------------------------------------------
00065 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00066 //-----------------------------------------------------------------------
00067 
00068 //              ----------------------------------------
00069 //              -- Public Function Member Definitions --
00070 //              ----------------------------------------
00071 
00072 AppMemento::AppMemento( AppFramework* framework)
00073   : _execList( new APPList< AbsModuleMemento* >  )
00074   , _fs ("#")
00075   , _framework( framework )
00076   , _deleteAllPath(true)
00077 {
00078 }
00079 
00080 AppMemento::~AppMemento() {
00081   APPListDeleteAll(*_execList);
00082   delete _execList;
00083   APPListDeleteAll(_modulesNotFound);
00084   APPListDeleteAll(_modules);
00085 }
00086 
00087 void AppMemento::load() {
00088 
00089   AbsModuleMemento* theFwkMemento = 
00090     new AppFrameMemento(_framework);
00091   _execList->append(theFwkMemento);
00092   
00093   APPSequence** seq;
00094   APPListIterator<APPSequence*> iter2( *_framework->sequences() );
00095   while ( seq = iter2( ) ) {
00096     AbsModuleMemento* thisSeq = new AppSequenceMemento(*seq);
00097     _execList->append(thisSeq);
00098   }
00099 
00100   APPPath** path;
00101   APPListIterator<APPPath*> iter3( *_framework->paths( ) );
00102   while ( path = iter3( ) ) {
00103     if (strcmp((*path)->name(), "AllPath") != 0) {  // avoid default path
00104       AbsModuleMemento* thisPath = new AppPathMemento (*path);
00105       _execList->append(thisPath);
00106     } 
00107     else {
00108       _deleteAllPath = false;
00109     }
00110   }
00111 
00112   APPList<AppModule*> allAsModules;
00113   _framework->buildModuleList( allAsModules, *_framework->inputModules() );
00114   _framework->buildModuleList( allAsModules, *_framework->outputModules() );
00115   _framework->buildModuleList( allAsModules, *_framework->accountModules() );
00116   _framework->buildModuleList( allAsModules, *_framework->modules() );
00117 
00118   AppModule** mod;
00119   APPListIterator<AppModule*> iter(allAsModules);
00120   while ( mod = iter( ) ) {
00121     AbsModuleMemento* thisMod = new AppModuleMemento(*mod);
00122     _execList->append(thisMod);
00123   }
00124 
00125 }
00126 
00127 void AppMemento::DumpConfig( ostream& o) {
00128 
00129   load();
00130 
00131   if (_deleteAllPath) {
00132     o << endl;
00133     o << "D" << _fs << "AllPath" << endl;
00134   }
00135 
00136   AbsModuleMemento** exec;
00137   APPListIterator<AbsModuleMemento*> iter ( *_execList );
00138   while ( exec = iter( ) ){
00139     o << endl;
00140     (*exec)->Dump( o );
00141   }
00142 
00143   //dump the list of module names at the end
00144 
00145   APPList<AppModule*> allAsModules;
00146   _framework->buildModuleList( allAsModules, *_framework->inputModules() );
00147   _framework->buildModuleList( allAsModules, *_framework->outputModules() );
00148   _framework->buildModuleList( allAsModules, *_framework->accountModules() );
00149   _framework->buildModuleList( allAsModules, *_framework->modules() );
00150   int i = 0;
00151   o << endl;
00152   bool newLine = true;
00153   AppModule** mod;
00154   APPListIterator<AppModule*> iter1( allAsModules );
00155   while ( mod = iter1( ) ) {
00156     if (newLine) {
00157       o << "L";
00158       newLine = false;
00159     }
00160     o << _fs << (*mod)->name();
00161     i++;
00162     if ( (i % 5) ==0 ){    //5 names per line
00163       o << endl;
00164       newLine = true;
00165     }   
00166   }
00167   if (! newLine) o << endl;
00168 
00169 }
00170 
00171 void AppMemento::DumpConfigTcl( ostream& o) {
00172 
00173   load();
00174 
00175   if (_deleteAllPath) {
00176     o << endl;
00177     o << "path delete AllPath" << endl;
00178   }
00179 
00180   AbsModuleMemento** exec;
00181   APPListIterator<AbsModuleMemento*> iter ( *_execList );
00182   while ( exec = iter( ) ){
00183     o << endl;
00184     (*exec)->Dump_tcl( o );
00185   }
00186 
00187 }
00188 
00189 void  AppMemento::restoreConfig( fstream& infile) {
00190 
00191   int size = 1024;
00192   char  buff[1024]; 
00193   char* var[15];
00194   bool moduleMode;
00195 
00196   int id;
00197   for (id=0; id < 15; id++) {
00198     var[id] = 0;
00199   }
00200   
00201   while( !infile.eof() ){
00202     infile.getline(buff, size);
00203     if (*buff){                   //skip empty line
00204       parseLine(buff,_fs, var);
00205 
00206       if( strcmp(var[0] , "M") == 0){
00207         moduleMode= strcmp(var[2] , "Y") == 0 ? true : false;
00208         APPExecutable::ExecType  exectype = 
00209           (APPExecutable::ExecType)(atoi(var[3]) );     
00210         if (exectype == APPExecutable::APP_framework) {
00211           restoreFramework(infile);
00212         }
00213         else {
00214           AppModule* thisMod =  _framework->fetchModule(var[1]);
00215           if (thisMod != 0) {
00216             thisMod->setEnabled( moduleMode );
00217             restoreStates(infile, thisMod);
00218           }
00219           else {
00220             _modulesNotFound.append(new FwkString(var[1]));
00221           }
00222         }
00223       }
00224       
00225       else if( strcmp(var[0] , "S") == 0 ){
00226         moduleMode = strcmp(var[2] , "Y") == 0 ? true : false;
00227         APPSequence* thisSeq = _framework->fetchSequence(var[1]);
00228         thisSeq->setEnabled( moduleMode );
00229         read_seq_nodes(thisSeq, (int)atoi(var[4]), infile);
00230       }
00231 
00232       else if ( strcmp(var[0] , "P") == 0 ) {
00233         moduleMode = strcmp(var[2] , "Y") == 0 ? true : false;
00234         APPPath* thisPath = _framework->fetchPath(var[1]);
00235         thisPath->setEnabled( moduleMode );
00236         read_seq_nodes(thisPath, (int)atoi(var[4]), infile);
00237       }
00238 
00239       else if ( strcmp(var[0] , "D") == 0 ) {
00240         APPPath* thePath = (APPPath*) _framework->fetchSequence(var[1]);
00241         if (thePath != 0) {
00242           _framework->remove(thePath);
00243           delete thePath;
00244         }
00245       }
00246 
00247       else if ( strcmp(var[0] , "L") == 0 ){     
00248         for(int i=0; i<10; i++){
00249           if( var[i+1] ){
00250             _modules.append(new FwkString(var[i+1]));
00251           }
00252         }
00253       }
00254 
00255       id = 0;
00256       while (0 != var[id]) {
00257         delete [] var[id];
00258         var[id] = 0;
00259         id++;
00260       }
00261 
00262     }
00263   }   
00264 
00265   //check for any missing modules in both the stored list and the framework
00266   //module list
00267 
00268   missingModules();
00269 
00270 }
00271 
00272 void AppMemento::missingModules()
00273 {
00274   //list modules that were stored but no match in framework was found
00275 
00276   int i;
00277   bool matched = false;
00278 
00279   if(! _modulesNotFound.isEmpty() ){
00280 
00281     _framework->fullReport("The following modules in stored configuration "
00282                            "were not matched :");
00283     APPListIterator<FwkString*> iterator(_modulesNotFound);
00284     FwkString** thisModName;
00285     while (thisModName = iterator() ) {
00286       _framework->partialReport((*thisModName)->c_str());
00287       _framework->partialReport("  ");
00288     }
00289     _framework->fullReport("");
00290   }
00291 
00292   //list modules in framework that didn't have a match stored list
00293   //
00294   APPList<AppModule*> allAsModules;
00295   _framework->buildModuleList( allAsModules, *_framework->inputModules() );
00296   _framework->buildModuleList( allAsModules, *_framework->outputModules() );
00297   _framework->buildModuleList( allAsModules, *_framework->accountModules() );
00298   _framework->buildModuleList( allAsModules, *_framework->modules() );
00299   AppModule** thisMod; 
00300   APPListIterator<AppModule*> iter( allAsModules );
00301   while ( thisMod = iter( ) ) {
00302     matched = false;
00303     APPListIterator<FwkString*> iterator(_modules);
00304     FwkString** thisModName;
00305     while (thisModName = iterator() ) {
00306       if ( *(*thisModName) == (*thisMod)->name() ) {
00307         matched = true;
00308         break;
00309       }
00310     }
00311     if ( !matched ){
00312       _framework->partialReport("Module ");
00313       _framework->partialReport((*thisMod)->name());
00314       _framework->fullReport(" in Framework does not have a match in"
00315                              " the stored list");
00316     }
00317   }
00318 
00319 }
00320 
00321 void AppMemento::read_seq_nodes(APPSequence* seq, int nodes, fstream& infile) {
00322 
00323   char buff[1024]; 
00324   int size=1024;
00325   char* var[15]; 
00326   
00327   int id;
00328   for (id=0; id<15; id++){
00329     var[id]=0;
00330   }
00331 
00332   for(int i=0; i < nodes; i++){
00333     infile.getline(buff, size);
00334     if (*buff){                   //skip empty line
00335       parseLine(buff,_fs, var);      
00336 
00337       seq->append( var[1] );
00338 
00339       id = 0;
00340       while (0 != var[id]) {
00341         delete [] var[id];
00342         var[id] = 0;
00343         id++;
00344       }      
00345     }
00346 
00347     else
00348       i--;    //don't count the empty lines as nodes in this sequence
00349 
00350   }
00351 }
00352 
00353 void AppMemento::makeClone(char* targetname, char* clonename) {
00354 
00355   AppModule** thisMod; 
00356   bool cloneFound = false;
00357   
00358   APPListIterator<AppModule*> iter( *_framework->modules() );
00359   while ( thisMod = iter( ) ) {
00360     if ( strcmp( (*thisMod)->name() , targetname) == 0 ){
00361       AppModule* theClone =  (*thisMod)->clone(clonename);
00362       if ( theClone != 0){
00363         cloneFound = true;
00364         _framework->appBuilder()->add(theClone); // Add clone to the Framework
00365         AppCloneRecorder* cloneRecorder = 
00366           new AppCloneRecorder(targetname, clonename );
00367         _framework->add(cloneRecorder);
00368       }
00369       break;
00370     }
00371   }
00372   if(!cloneFound){
00373     _framework->partialReport("Clonning failed: module name= ");
00374     _framework->partialReport(targetname);
00375     _framework->partialReport(" clone name= ");
00376     _framework->fullReport(clonename);
00377   }
00378   
00379 }
00380 
00381 void 
00382 AppMemento::restoreStates(fstream& infile, AppModule* theModule) {
00383 
00384   int size = 1024;
00385   char  buff[1024]; 
00386   char* var[15];
00387 
00388   int id;
00389   for (id=0; id<15; id++){
00390     var[id]=0;
00391   }
00392 
00393   APPListIterator<APPCommand*> theIterator( *theModule->commands( ) );
00394   APPCommand** thecommand;
00395 
00396   while ('C' == (char)infile.peek() ) {
00397     infile.getline(buff, size);
00398     parseLine(buff,_fs, var);
00399     
00400     theIterator.rewind();
00401     while ( thecommand = theIterator( ) ) {
00402       if ( ( (*thecommand)->isShowable( ) ) &&
00403            ( strcmp( (*thecommand)->command() , var[1]) == 0 ) ) {
00404         (*thecommand)->restoreState( var+1 );
00405       }
00406     }
00407 
00408     id = 0;
00409     while (0 != var[id]) {
00410       delete [] var[id];
00411       var[id] = 0;
00412       id++;
00413     }
00414 
00415   }
00416 
00417 }               
00418 
00419 void 
00420 AppMemento::restoreFramework(fstream& infile) {
00421 
00422   int size = 1024;
00423   char  buff[1024]; 
00424   char* var[15];
00425 
00426   int id;
00427   for (id=0; id<15; id++){
00428     var[id]=0;
00429   }
00430 
00431   APPListIterator<AppActionController*> 
00432     actionIter( *_framework->actionControllers());
00433   AppActionController** anActionController;
00434     
00435   char nextCh = (char)infile.peek();
00436   while ('F' == nextCh || 'C' == nextCh) {
00437     infile.getline(buff, size);
00438     parseLine(buff,_fs, var);
00439         
00440     if(strcmp("C" , var[0]) == 0 ) {    
00441       restoreStates(infile, _framework);
00442     }
00443 
00444     else if(strcmp("FC" , var[0]) == 0 ) {
00445       makeClone(var[2], var[1]);
00446     }
00447 
00448     else if(strcmp("FS" , var[0]) == 0 ) {
00449       APPSequence* thisSeq = new APPSequence(var[1], 0);
00450       _framework->add(thisSeq);
00451     }
00452 
00453     else if(strcmp("FP" , var[0]) == 0 ) {
00454       APPPath* thisPath = new APPPath(var[1], 0);
00455       _framework->add(thisPath);    
00456     }
00457           
00458     else if(strcmp("FA" , var[0]) == 0 ) {
00459       bool actionMode = (strcmp(var[1] , "Y") == 0) ? true : false;
00460       actionIter.rewind();
00461       while ( anActionController=actionIter() ) {           
00462         if (strcmp(var[2], (*anActionController)->action()->name())== 0) {
00463           (*anActionController)->setDoAction( actionMode );
00464         }
00465       }
00466     }
00467 
00468     else if (strcmp("FI" , var[0]) == 0 ) {
00469       _framework->setInputModule(var[0]);
00470     } 
00471 
00472     else if (strcmp("FO" , var[0]) == 0 ) {
00473       _framework->setOutputModule(var[1]);
00474     }
00475 
00476     else if (strcmp("FAM" , var[0]) == 0 ) {
00477       _framework->setAccountModule(var[1]);
00478     }
00479 
00480     id = 0;
00481     while (0 != var[id]) {
00482       delete [] var[id];
00483       var[id] = 0;
00484       id++;
00485     }
00486     
00487     nextCh = (char)infile.peek();
00488   }
00489 
00490 }               
00491 
00492 void parseLine(char* buff, char* fs, char *field[]) {
00493   int i=0, j=0, k=0;
00494   char temp[1024]; 
00495   
00496   while(buff[i] != 0 ) {
00497     for(k=0; buff[i] != *fs && buff[i] != 0; k++ ){
00498       temp[k] = buff[i]; 
00499       i++;  
00500     }
00501 
00502     temp[k]='\0';
00503     field[j] = new char[k+1];
00504     strcpy(field[j],temp);
00505     j++; 
00506     if(buff[i] != 0)i++;
00507     field[j]=0;
00508     
00509   }
00510 }
00511 
00512 
00513 
00514 
00515 
00516 

 


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

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