00001 #ifndef DATABASE_H 00002 #define DATABASE_H 00003 00004 #include <string> 00005 #include <vector> 00006 #include "Learn/WiimoteGR/Quantizer.h" 00007 #include "Learn/WiimoteGR/TimeSlot.h" 00008 #include "Learn/WiimoteGR/HMMLib.h" 00009 #include <sqlite3.h> 00010 00011 using namespace std; 00012 00013 namespace WiimoteGR{ 00014 00015 struct HMM; 00016 class Quantizer; 00017 class DefaultQuantizer; 00018 00019 class Database{ 00020 public: 00021 //lazy initialization 00022 static Database& Open(); 00023 static void Close(); 00024 00025 /* main functions */ 00026 void SaveGesture(const Gesture& gesture); 00027 void SaveObservationSequence(const TimeSlot& seq); 00028 //Save or replace if its gestureName,quantizer,modelStyle and trained are same as one HMM in database. 00029 void SaveHMM(const HMM& hmm); 00030 void LoadGestures(const char* gestureName, vector<Gesture>& gestureVec); 00031 void LoadObservationSequences(const char* gestureName, const Quantizer& quantizer, vector<TimeSlot>& seqVec); 00032 void LoadHMM(HMM& hmm); 00033 void LoadHMMs(const Quantizer& quantizer, const char* modelStyle, bool trained, vector<HMM>& hmmVec); 00034 void DeleteGestures(const char* gestureName); 00035 void DeleteObservationSequences(const char* gestureName, const Quantizer& quantizer); 00036 00037 protected: 00038 //for singleton pattern 00039 Database(); 00040 ~Database(); 00041 Database(const Database&); 00042 Database& operator= (const Database&); 00043 00044 private: 00045 enum ReservedSpaceForLoad{ 00046 gestureVecSpace = 20, 00047 seqVecSpace = 20, 00048 hmmVecSpace = 10 00049 }; 00050 00051 //singleton instance 00052 static Database* dbInstance; 00053 00054 //sqlite3 database 00055 sqlite3 *db; 00056 00057 //whether database is opened 00058 bool dbOpened; 00059 //result code of sqlite3 functions 00060 int rc; 00061 //error message of sqlite3 functions 00062 char *zErrMsg; 00063 00064 //statements 00065 sqlite3_stmt* saveGestureStmt; 00066 sqlite3_stmt* saveObservationSequenceStmt; 00067 sqlite3_stmt* isExistHMMStmt; 00068 sqlite3_stmt* updateHMMStmt; 00069 sqlite3_stmt* insertHMMStmt; 00070 sqlite3_stmt* loadGesturesStmt; 00071 sqlite3_stmt* loadObservationSequencesStmt; 00072 sqlite3_stmt* loadHMMStmt; 00073 sqlite3_stmt* loadHMMsStmt; 00074 }; 00075 } 00076 00077 #endif