Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/SessionManager/include/DatabaseManager.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: DatabaseManager.h,v 1.1 2000/03/03 01:35:08 bormotov Exp $
00003  */
00004 
00005 // Copyright (C) 1998 The New York Group Theory Cooperative
00006 // See magnus/doc/COPYRIGHT for the full notice.
00007 
00008 // Contents: Interface of class DataBase State
00009 //
00010 // Principal Author: Dmitry Pechkin
00011 //
00012 // Status: in progress
00013 //
00014 // Revision History:
00015 //
00016 
00017 
00018 //
00019 //  
00020 // 
00021 #ifndef _DATABASE_MANAGER_H_
00022 #define _DATABASE_MANAGER_H_
00023 
00024 #include "Chars.h"
00025 #include "Vector.h"
00026 #include "OID.h"
00027 
00028 enum DB2FE_MESSAGE { NO_MESSAGE,        MSG_NEW_FILENAME,  
00029                      MSG_NEW_DATABASE,  MSG_OPEN_DATABASE,
00030                      MSG_SAVE_DATABASE, MSG_SAVE_DATABASE_AS,
00031                      MSG_SAVE_ON_CLOSE, MSG_CLOSE_DATABASE,
00032                      MSG_ADD_OBJECTS,   MSG_GET_OBJECTS
00033 };
00034 
00035 enum FE2DB_MESSAGE { DB_NO_EVENT,
00036                      DB_NEW, DB_OPEN, DB_SAVE, DB_SAVE_AS, DB_CLOSE, 
00037                      DB_ADD_OBJECTS, DB_GET_OBJECTS, 
00038                      DB_CANCEL, DB_SUCCESS, DB_FAILURE, 
00039                      DB_YES, DB_NO, DB_SELECT_OBJECTS, DB_OBJECT_DEFINITION 
00040 };
00041 
00042 struct DBEvent {
00043   FE2DB_MESSAGE key;
00044   Chars str;
00045   DBEvent() : key(DB_NO_EVENT), str() {}
00046 };
00047 
00048 ostream& operator<<( ostream& ostr, const DBEvent& event );
00049 
00050 class DBState 
00051 {
00052 public:
00053 
00054   // Standard constructors, operator= and destructor.
00055 
00056   DBState( ) : theNextState(0) { }
00057   // State( const State *nstate, const State *onfailure );
00058 
00059   DBState( const DBState& state );
00060   const DBState& operator=( const DBState& state );
00061   virtual ~DBState();
00062 
00063   // Accessors
00064 
00065   virtual void init( ) { }
00066   //virtual void Event getEvent( ) const = 0;
00067   virtual void handleEvent( const DBEvent& event ) = 0;
00068 
00069   const DBState* nextState( ) const { return theNextState; }
00070   virtual DBState* clone() const = 0;
00071 
00072   virtual bool isFinal( ) const = 0;
00073   virtual DB2FE_MESSAGE initMessage() const { return NO_MESSAGE; }
00074 
00075   virtual void printOn( ostream& ostr ) const { ostr << "DBState"; }
00076 protected:
00077   //void setNextState( const DBState *nstate ) { theNextState = nstate; }
00078 
00079   //private:
00080   DBState *theNextState;
00081   //DBState *theFailure;
00082 };
00083 
00084 
00085 class MainState : public DBState 
00086 {
00087 public:
00088   MainState( ) : DBState() { }
00089   //MainState( const DBState *nstate, const DBState *onFailure );
00090 };
00091 
00092 class IntermediateState : public DBState
00093 {
00094 public:
00095   IntermediateState( ) : theFailure(0), theSecondState(0) { }
00096   IntermediateState( DBState *onFailure, DBState *secondState )
00097     : theFailure(onFailure), theSecondState(secondState) {}
00098 
00099   IntermediateState( const IntermediateState& state );
00100   ~IntermediateState();
00101   const IntermediateState& operator=( const IntermediateState& state );
00102 
00103   bool isFinal( ) const { return false; }
00104 
00105   void printOn( ostream& ostr ) const { ostr << "IntermediateState"; }
00106 protected:
00107 
00108   void handleFailure();
00109   void useSecondState( DBState *state );
00110 
00111   DBState *theFailure;
00112   DBState *theSecondState;
00113 };
00114 
00115 
00116 //
00117 // User defined states
00118 //
00119 
00120 class DatabaseClosed : public MainState
00121 {
00122 public:
00123   DatabaseClosed() { }
00124   //DatabaseClosed( DBState *nstate, DBState *onFailure );
00125   
00126   bool isFinal( ) const { return true; }
00127 
00128   DB2FE_MESSAGE initMessage() const { return MSG_CLOSE_DATABASE; }
00129   void handleEvent( const DBEvent& event );
00130   DBState *clone() const { return new DatabaseClosed(*this); }
00131 
00132   void printOn( ostream& ostr ) const { ostr << "DatabaseClosed"; }
00133 };
00134 
00135 class DatabaseSaved : public MainState
00136 {
00137 public:
00138   DatabaseSaved() { }
00139   //DatabaseSaved( DBState *nstate, DBState *onFailure );
00140 
00141   bool isFinal( ) const { return true; }
00142 
00143   void handleEvent( const DBEvent& event );
00144   DBState *clone() const { return new DatabaseSaved(*this); }
00145   void printOn( ostream& ostr ) const { ostr << "DatabaseSaved"; }
00146 };
00147 
00148 class DatabaseModified : public MainState
00149 {
00150 public:
00151   DatabaseModified() { }
00152   //DatabaseModified( DBState *nstate, DBState *onFailure );
00153 
00154   bool isFinal( ) const { return false; }
00155 
00156   void handleEvent( const DBEvent& event );
00157   DBState *clone() const { return new DatabaseModified(*this); }
00158   void printOn( ostream& ostr ) const { ostr << "DatabaseModified"; }
00159 };
00160 
00161 
00162 
00163 
00164 class DatabaseCreating : public IntermediateState
00165 {
00166 public:
00167   DatabaseCreating() { }
00168   DatabaseCreating( DBState *onFailure, DBState *secondState = 0 )
00169     : IntermediateState(onFailure,secondState) {}
00170 
00171   DB2FE_MESSAGE initMessage() const { return MSG_NEW_FILENAME; }
00172   void handleEvent( const DBEvent& event );
00173   DBState *clone() const { return new DatabaseCreating(*this); }
00174   void printOn( ostream& ostr ) const { ostr << "DatabaseCreating"; }
00175 };
00176 
00177 class DatabaseOpening : public IntermediateState
00178 {
00179 public:
00180   DatabaseOpening() { }
00181   DatabaseOpening( DBState *onFailure, DBState *secondState = 0 )
00182     : IntermediateState(onFailure,secondState) {}
00183 
00184   DB2FE_MESSAGE initMessage() const { return MSG_OPEN_DATABASE; }
00185   void handleEvent( const DBEvent& event );
00186   DBState *clone() const { return new DatabaseOpening(*this); }
00187   void printOn( ostream& ostr ) const { ostr << "DatabaseOpening"; }
00188 };
00189 
00190 class DatabaseSaving : public IntermediateState
00191 {
00192 public:
00193   DatabaseSaving() { }
00194   DatabaseSaving( DBState *onFailure, DBState *secondState = 0 )
00195     : IntermediateState(onFailure,secondState) {}
00196 
00197   DB2FE_MESSAGE initMessage() const { return MSG_SAVE_DATABASE; }
00198   void handleEvent( const DBEvent& event );
00199   DBState *clone() const { return new DatabaseSaving(*this); }
00200   void printOn( ostream& ostr ) const { ostr << "DatabaseSaving"; }
00201 };
00202 
00203 
00204 class DatabaseSavingAs : public IntermediateState
00205 {
00206 public:
00207   DatabaseSavingAs() { }
00208   DatabaseSavingAs( DBState *onFailure, DBState *secondState = 0 )
00209     : IntermediateState(onFailure,secondState) {}
00210 
00211   DB2FE_MESSAGE initMessage() const { return MSG_SAVE_DATABASE_AS; }
00212   void handleEvent( const DBEvent& event );
00213   DBState *clone() const { return new DatabaseSavingAs(*this); }
00214   void printOn( ostream& ostr ) const { ostr << "DatabaseSavingAs"; }
00215 };
00216 
00217 class DatabaseClosing : public IntermediateState
00218 {
00219 public:
00220   DatabaseClosing() { }
00221   DatabaseClosing( DBState *onFailure, DBState *secondState = 0 )
00222     : IntermediateState(onFailure,secondState) {}
00223 
00224   DB2FE_MESSAGE initMessage() const { return MSG_SAVE_ON_CLOSE; }
00225   void handleEvent( const DBEvent& event );
00226   DBState *clone() const { return new DatabaseClosing(*this); }
00227   void printOn( ostream& ostr ) const { ostr << "DatabaseClosing"; }
00228 };
00229 
00230 class DatabaseAddingObjects : public IntermediateState
00231 {
00232 public:
00233   DatabaseAddingObjects() { }
00234   DatabaseAddingObjects( DBState *onFailure, DBState *secondState = 0 )
00235     : IntermediateState(onFailure,secondState) {}
00236 
00237   DB2FE_MESSAGE initMessage() const { return MSG_ADD_OBJECTS; }
00238   void handleEvent( const DBEvent& event );
00239   DBState *clone() const { return new DatabaseAddingObjects(*this); }
00240   void printOn( ostream& ostr ) const { ostr << "DatabaseAddingObjects"; }
00241 };
00242 
00243 class DatabaseGettingObjects : public IntermediateState
00244 {
00245 public:
00246   DatabaseGettingObjects() { }
00247   DatabaseGettingObjects( DBState *onFailure, DBState *secondState = 0)
00248     : IntermediateState(onFailure,secondState) {}
00249 
00250   DB2FE_MESSAGE initMessage() const { return MSG_GET_OBJECTS; }
00251   void handleEvent( const DBEvent& event );
00252   DBState *clone() const { return new DatabaseGettingObjects(*this); }
00253   void printOn( ostream& ostr ) const { ostr << "DatabaseGettingObjects"; }
00254 };
00255 
00256 
00257 inline DBState *copyState( const DBState *state )
00258 {
00259   return (state ? state->clone() : 0 );
00260 }
00261 
00262 inline ostream& operator<<(ostream& ostr, const DBState& state)
00263 {
00264   state.printOn( ostr );
00265   return ostr;
00266 }
00267 
00268 
00269 //
00270 //  Class DatabaseObjectType
00271 //
00272 
00273 class DatabaseObjectCategory {
00274 public:
00275   enum Category
00276   {
00277     NO_CATEGORY = -1, 
00278     // Group types -- 
00279     FP, FREE, ABELIAN, NILPOTENT, SMALL_CANCELLATION, ONE_RELATOR,
00280     AP_FREE, AP_FREE_CYCLIC, FREE_NILPOTENT, HNN_FREE, 
00281     ONE_RELATOR_WITH_TORSION,
00282     // Other object types
00283     SUBGROUP, WORD, MAP, MAP2, HOMOMORPHISM, HOMOMORPHISM2,
00284     EQUATION, EQUATION2, SET_OF_WORDS, VECTOR_OF_WORDS, 
00285   };
00286 
00287   DatabaseObjectCategory(Category cath = NO_CATEGORY) :theCategory(cath) {}
00288 
00289   Chars str() const { return DatabaseObjectCategory::str(theCategory); }
00290   Category id() const { return theCategory; }
00291 
00292   static Chars str(const Category cath);
00293 
00294   static Chars getSMObjectCategory( class SMObject *smo );
00295 
00296 
00297   friend istream& operator>>(istream& istr, DatabaseObjectCategory& cath ) {
00298     int i;
00299     istr >> i;
00300     cath.theCategory = (DatabaseObjectCategory::Category)i;
00301   }
00302 
00303   friend ostream& operator<<(ostream& ostr, 
00304                              const DatabaseObjectCategory& cath ) {
00305     ostr << (int)cath.theCategory;
00306   }
00307 
00308 private:
00309   Category theCategory;
00310 };
00311 
00312 
00313 //
00314 //  Class DatabaseObjectSmith
00315 //
00316 
00317 class SMObject;
00318 
00319 class DatabaseObjectSmith {
00320 public:
00321   // Default ctor, copy ctor, operator=, dtor supplied by compiler.
00322 
00323   void checkinObjectFromDatabase(istream& istr);
00324 
00325 private:
00326 
00327   void reset();
00328   void checkinObjectProperties( istream& istr );
00329   void checkinObjectDefinition( istream& istr );
00330 
00331   SMObject* checkinFPGroup(istream& istr, DatabaseObjectCategory type );
00332   SMObject* checkinWord(istream& istr);
00333   SMObject* checkinSubgroup(istream& istr);
00334   SMObject* checkinSetOfWords(istream& istr);
00335   SMObject* checkinVectorOfWords(istream& istr);
00336   SMObject* checkinMap(istream& istr, DatabaseObjectCategory type);
00337   SMObject* checkinHomomorphism(istream& istr, DatabaseObjectCategory type);
00338   SMObject* checkinEquation(istream& istr, DatabaseObjectCategory type);
00339 
00340   void parseError(const Chars& str) { theError = str; }
00341 
00342   // data members:
00343 
00344   Chars theError;
00345   Chars theObjectName;
00346   VectorOf<SMObject *> theExperiment;
00347   ListOf<OID> dependencies;
00348 };
00349 
00350 // 
00351 //   Class DatabaseManager
00352 //
00353 
00354 class DatabaseManager 
00355 {
00356 public:
00357 
00358   DatabaseManager(); //( const Chars& filename );
00359 
00360   ~DatabaseManager( );
00361 
00362   void takeControl( );
00363 
00364   enum ManagerState { WORKING, STOPPING, STOPPED };
00365 
00366   ManagerState state() const { return theExternalState; }
00367 
00368   Chars getFileName() const { return theFileName; } 
00369   Chars getSecondaryFileName() const;
00370 
00371   static void printGlobalMessageTemplate( ostream& ostr );
00372 
00373   //private:
00374   //friend void SessionManager::readMessage(istream& istr);
00375   void forceToFinish();
00376 
00377   //private:
00378   //friend void SessionManager::readMessages( );
00379   void readMessage( istream& istr );
00380 
00381 private:
00382   friend void DatabaseCreating::handleEvent( const DBEvent& event );
00383   friend void DatabaseSaving::handleEvent( const DBEvent& event );
00384   friend void DatabaseSavingAs::handleEvent( const DBEvent& event );
00385   void setFileName( const Chars& filename );
00386 
00387 private:
00388   DatabaseManager( const DatabaseManager& );
00389   // Hidden, not to implement.
00390 
00391   DatabaseManager& operator=( const DatabaseManager& );
00392   // Hidden, not to implement.
00393 
00394   void postMessage( const DB2FE_MESSAGE message ) const;
00395 
00396   //static Chars getObjectCategory( SMObject *smo ); 
00397   static void printDatabaseObjectCategories( ostream& ostr );
00398 
00399   // Data members
00400 
00401   DBState *theCurrentState;
00402   Chars  theFileName;
00403   ManagerState theExternalState;
00404   DBEvent event;
00405 };
00406 
00407 extern DatabaseManager TheDatabaseManager;
00408 #endif

Generated at Tue Jun 19 09:49:37 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001