Changeset 200
- Timestamp:
- 10/15/06 13:27:55 (2 years ago)
- Files:
-
- 06/libkombilo/cpptest.cc (modified) (1 diff)
- 06/libkombilo/search.cc (modified) (15 diffs)
- 06/libkombilo/search.h (modified) (3 diffs)
- 06/libkombilo/testsearch.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
06/libkombilo/cpptest.cc
r196 r200 43 43 while (!infile.eof()) { 44 44 getline(infile, line); 45 sgf += line ;45 sgf += line + "\n"; 46 46 } 47 47 infile.close(); 06/libkombilo/search.cc
r199 r200 3193 3193 processVariations = true; 3194 3194 sgfInDB = true; 3195 rootNodeTags = "BR,CA,DT,EV,H C,KM,PB,PC,PW,RE,RO,RU,SZ,US,WR";3195 rootNodeTags = "BR,CA,DT,EV,HA,KM,PB,PC,PW,RE,RO,RU,SZ,US,WR"; 3196 3196 algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_FULL | ALGO_HASH_CORNER; 3197 3197 algo_hash_full_maxNumStones = 50; … … 3200 3200 3201 3201 ProcessOptions::ProcessOptions(string s) { 3202 if (s[0] == 't') processVariations = true; 3202 int p = 0; 3203 if (s[p++] == 't') processVariations = true; 3203 3204 else processVariations = false; 3204 3205 3205 int p = 2; 3206 if (s[p++] == 't') sgfInDB = true; 3207 else sgfInDB = false; 3208 3209 p++; 3206 3210 int pn = s.find('|', p) + 1; 3207 3211 algos = atoi(s.substr(p, pn-p-1).c_str()); … … 3220 3224 string ProcessOptions::asString() { 3221 3225 string result; 3222 if (processVariations) result += "t|"; 3223 else result += "f|"; 3226 if (processVariations) result += "t"; 3227 else result += "f"; 3228 if (sgfInDB) result += "t"; 3229 else result += "f"; 3224 3230 char buf[200]; 3225 sprintf(buf, " %d|%d|%d|%s", algos, algo_hash_full_maxNumStones, algo_hash_corner_maxNumStones, rootNodeTags.c_str());3231 sprintf(buf, "|%d|%d|%d|%s", algos, algo_hash_full_maxNumStones, algo_hash_corner_maxNumStones, rootNodeTags.c_str()); 3226 3232 result += buf; 3227 3233 return result; … … 3229 3235 3230 3236 void ProcessOptions::validate() { 3237 string::iterator it = rootNodeTags.begin(); 3238 while (it != rootNodeTags.end()) { 3239 if (*it == ' ') it = rootNodeTags.erase(it); 3240 else it++; 3241 } 3231 3242 if (rootNodeTags.find("PB") == string::npos) rootNodeTags += ",PB"; 3232 3243 if (rootNodeTags.find("PW") == string::npos) rootNodeTags += ",PW"; 3233 3244 if (rootNodeTags.find("RE") == string::npos) rootNodeTags += ",RE"; 3234 3245 if (rootNodeTags.find("DT") == string::npos) rootNodeTags += ",DT"; 3235 if (rootNodeTags.find("SZ") == string::npos) rootNodeTags += ",SZ"; 3246 } 3247 3248 vector<string>* ProcessOptions::SGFTagsAsStrings() { 3249 vector<string>* SGFtags = new vector<string>; 3250 int ctr = 0; 3251 unsigned int p = 0; 3252 unsigned int pn = rootNodeTags.find(',', p); 3253 while (pn != string::npos) { 3254 SGFtags->push_back(rootNodeTags.substr(p,pn-p)); 3255 ctr++; 3256 p = pn+1; 3257 pn = rootNodeTags.find(',', p); 3258 } 3259 SGFtags->push_back(rootNodeTags.substr(p)); 3260 return SGFtags; 3236 3261 } 3237 3262 … … 3753 3778 3754 3779 string GameList::getSGF(int i) throw(DBError) { 3755 if (!p_op->sgfInDB) return ""; 3756 if (i < 0 || i >= (int)currentList->size()) return ""; 3780 if (!p_op->sgfInDB) { 3781 // printf("not in db\n"); 3782 return ""; 3783 } 3784 if (i < 0 || i >= (int)currentList->size()) { 3785 // printf("index out of range\n"); 3786 return ""; 3787 } 3757 3788 int index = (*currentList)[i].second; 3758 3789 // int rc = sqlite3_open(dbname, &db); … … 3783 3814 sizeX = pattern.sizeX; // need this in lookupLabel 3784 3815 PatternList pl(pattern, searchOptions->fixedColor, searchOptions->nextMove); 3785 int rc; 3786 // rc = sqlite3_open(dbname, &db); 3816 // int rc = sqlite3_open(dbname, &db); 3787 3817 // if (rc) { 3788 3818 // sqlite3_close(db); … … 3790 3820 // throw DBError(); 3791 3821 // } 3792 rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 3793 if (rc) throw DBError(); 3794 rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 3795 if (rc) throw DBError(); 3796 3822 // rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 3823 // if (rc) throw DBError(); 3824 // rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 3825 // if (rc) throw DBError(); 3797 3826 3798 3827 if (boardsizes.size() != 1 || boardsizes[0] != pattern.boardsize) { … … 3844 3873 } 3845 3874 3875 void GameList::createGamesDB() throw(DBError) { 3876 SGFtags = p_op->SGFTagsAsStrings(); 3877 3878 string sql1 = "create table if not exists GAMES ( "; 3879 sql1 += "id integer primary key, "; 3880 sql1 += "path text, "; 3881 sql1 += "filename text, "; 3882 sql1 += "pos integer default 0, "; 3883 sql1 += "duplicate integer, "; 3884 sql1 += "dbtree text, "; 3885 sql1 += "date date"; 3886 3887 sql_ins_rnp = "insert into games (path, filename, pos, dbtree, date"; 3888 string question_marks = "?,?,?,?,?"; 3889 3890 if (p_op->sgfInDB) { 3891 sql1 += ", sgf text"; 3892 sql_ins_rnp += ", sgf"; 3893 question_marks += ",?"; 3894 } 3895 3896 SGFtagsSize = SGFtags->size(); 3897 int ctr = 0; 3898 posDT = posSZ = posWR = posBR = posHA = -1; 3899 for(vector<string>::iterator it = SGFtags->begin(); it != SGFtags->end(); it++) { 3900 sql1 += ", " + *it + " text"; 3901 sql_ins_rnp += ", " + *it; 3902 question_marks += ",?"; 3903 if (*it == "DT") posDT = ctr; 3904 3905 if (*it == "SZ") posSZ = ctr; 3906 if (*it == "WR") posWR = ctr; 3907 if (*it == "BR") posBR = ctr; 3908 if (*it == "HA") posHA = ctr; 3909 ctr++; 3910 } 3911 if (posDT == -1) throw DBError(); 3912 if (posSZ == -1) { 3913 posSZ = SGFtags->size(); 3914 SGFtags->push_back("SZ"); 3915 } 3916 if (posWR == -1) { 3917 posWR = SGFtags->size(); 3918 SGFtags->push_back("WR"); 3919 } 3920 if (posBR == -1) { 3921 posBR = SGFtags->size(); 3922 SGFtags->push_back("BR"); 3923 } 3924 if (posHA == -1) { 3925 posHA = SGFtags->size(); 3926 SGFtags->push_back("HA"); 3927 } 3928 3929 sql1 += ");"; 3930 sql_ins_rnp += ") values (" + question_marks + ");"; 3931 3932 int rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 3933 if(rc != SQLITE_OK) throw DBError(); 3934 3935 sql1 = "create table if not exists TAGS ( id integer primary key, name text, visible integer default 1 );"; 3936 rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 3937 if (rc != SQLITE_OK) throw DBError(); 3938 char sql[100]; 3939 sprintf(sql, "insert into TAGS (id, name) values (%d, '%d');", HANDI_TAG, HANDI_TAG); 3940 rc = sqlite3_exec(db, sql, 0, 0, 0); 3941 if (rc != SQLITE_OK) throw DBError(); 3942 sprintf(sql, "insert into TAGS (id, name) values (%d, '%d');", PROFESSIONAL_TAG, PROFESSIONAL_TAG); 3943 rc = sqlite3_exec(db, sql, 0, 0, 0); 3944 if (rc != SQLITE_OK) throw DBError(); 3945 sql1 = "create table if not exists GAME_TAGS ( id integer primary key, game_id integer, tag_id integer );"; 3946 rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 3947 if( rc!=SQLITE_OK ) throw DBError(); 3948 } 3846 3949 3847 3950 void GameList::start_processing(int PROCESSVARIATIONS) throw(DBError) { … … 3850 3953 else processVariations = p_op->processVariations; 3851 3954 readDBs = 0; 3852 string question_marks = "?,?,?,?"; // path, filename, pos, date3853 tags = new vector<string>;3854 int ctr = 0;3855 unsigned int p = 0;3856 unsigned int pn = p_op->rootNodeTags.find(',', p);3857 while (pn != string::npos) {3858 tags->push_back(p_op->rootNodeTags.substr(p,pn-p));3859 question_marks += ",?";3860 if (p_op->rootNodeTags.substr(p,pn-p) == "DT") posDT = ctr;3861 if (p_op->rootNodeTags.substr(p,pn-p) == "SZ") posSZ = ctr;3862 ctr++;3863 p = pn+1;3864 pn = p_op->rootNodeTags.find(',', p);3865 }3866 tags->push_back(p_op->rootNodeTags.substr(p));3867 question_marks += ",?";3868 if (p_op->rootNodeTags.substr(p) == "DT") posDT = ctr;3869 if (p_op->rootNodeTags.substr(p) == "SZ") posSZ = ctr;3870 3955 // printf("dt %d sz %d\n", posDT, posSZ); 3871 3956 3872 sql_ins_rnp = "insert into games (path, filename, pos, date, ";3873 if (p_op->sgfInDB) {3874 sql_ins_rnp += "sgf, ";3875 question_marks += ",?";3876 }3877 sql_ins_rnp += p_op->rootNodeTags + ") values (" + question_marks + ");";3878 3957 int rc; 3879 3958 // rc = sqlite3_open(dbname, &db); … … 3883 3962 // throw DBError(); 3884 3963 // } 3885 string sql1 = "create table if not exists GAMES ( id integer primary key, path text, filename text, pos integer default 0, duplicate integer, date date"; 3886 if (p_op->sgfInDB) sql1 += ", sgf text"; 3887 for(vector<string>::iterator it = tags->begin(); it != tags->end(); it++) sql1 += ", " + *it + " text"; 3888 sql1 += ");"; 3889 rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 3890 if( rc!=SQLITE_OK ) throw DBError(); 3891 3892 sql1 = "create table if not exists TAGS ( id integer primary key, slug text, description text, visible integer );"; 3893 rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 3894 if( rc!=SQLITE_OK ) throw DBError(); 3895 3896 sql1 = "create table if not exists GAME_TAGS ( id integer primary key, game_id integer, tag_id integer );"; 3897 rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 3898 if( rc!=SQLITE_OK ) throw DBError(); 3899 rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 3900 if (rc) throw DBError(); 3901 rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 3902 if (rc) throw DBError(); 3964 // rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 3965 // if (rc) throw DBError(); 3966 // rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 3967 // if (rc) throw DBError(); 3968 3969 createGamesDB(); 3903 3970 3904 3971 char* sql = "begin transaction;"; 3905 3972 rc = sqlite3_exec(db, sql, 0, 0, 0); 3906 if (rc) { 3907 sqlite3_close(db); 3908 db = 0; 3909 throw DBError(); 3910 } 3973 if (rc) throw DBError(); 3911 3974 current = 0; 3912 3975 for(unsigned int a=0; a < 20*boardsizes.size(); a++) if (algo_ps[a]) algo_ps[a]->initialize_process(db); … … 3935 3998 readDBs = 0; 3936 3999 readDB(); 3937 delete tags; 3938 } 3939 3940 int GameList::process(const char* sgf, const char* path, const char* fn) throw(SGFError,DBError) { 4000 delete SGFtags; 4001 } 4002 4003 int GameList::process(const char* sgf, const char* path, const char* fn, const char* DBTREE) throw(SGFError,DBError) { 4004 const char* dbtree = ""; 4005 if (DBTREE) dbtree = DBTREE; 3941 4006 3942 4007 Cursor* c = 0; … … 3968 4033 // } 3969 4034 // } 3970 vector<string>* rootNodeProperties = parseRootNode(root, tags);4035 vector<string>* rootNodeProperties = parseRootNode(root, SGFtags); 3971 4036 // for(vector<string>::iterator rnp = rootNodeProperties->begin(); rnp != rootNodeProperties->end(); rnp++) 3972 4037 // printf("rnp %s\n", rnp->c_str()); … … 4078 4143 rc = sqlite3_bind_int(ppStmt, stmt_ctr++, pos); 4079 4144 if (rc != SQLITE_OK) throw DBError(); 4145 rc = sqlite3_bind_text(ppStmt, stmt_ctr++, dbtree, -1, SQLITE_TRANSIENT); 4146 if (rc != SQLITE_OK) throw DBError(); 4080 4147 rc = sqlite3_bind_text(ppStmt, stmt_ctr++, date.c_str(), -1, SQLITE_TRANSIENT); 4081 4148 if (rc != SQLITE_OK) throw DBError(); … … 4086 4153 } 4087 4154 4088 for( vector<string>::iterator it = rootNodeProperties->begin(); it != rootNodeProperties->end(); it++) {4089 rc = sqlite3_bind_text(ppStmt, stmt_ctr++, it->c_str(), -1, SQLITE_TRANSIENT);4155 for(int i=0; i < SGFtagsSize; i++) { 4156 rc = sqlite3_bind_text(ppStmt, stmt_ctr++, (*rootNodeProperties)[i].c_str(), -1, SQLITE_TRANSIENT); 4090 4157 if (rc != SQLITE_OK) throw DBError(); 4091 4158 } … … 4095 4162 rc = sqlite3_finalize(ppStmt); 4096 4163 if (rc != SQLITE_OK) throw DBError(); 4164 int game_id = sqlite3_last_insert_rowid(db); 4165 4166 // evaluate tags 4167 if ((*rootNodeProperties)[posHA] != "") { // handicap game 4168 char sql[100]; 4169 sprintf(sql, "insert into GAME_TAGS (game_id, tag_id) values (%d, %d);", game_id, HANDI_TAG); 4170 rc = sqlite3_exec(db, sql, 0, 0, 0); 4171 if (rc != SQLITE_OK) throw DBError(); 4172 } 4173 if ((*rootNodeProperties)[posWR].find('p') != string::npos || 4174 (*rootNodeProperties)[posBR].find('p') != string::npos) { 4175 // at least one of the players is professional 4176 char sql[100]; 4177 sprintf(sql, "insert into GAME_TAGS (game_id, tag_id) values (%d, %d);", game_id, PROFESSIONAL_TAG); 4178 rc = sqlite3_exec(db, sql, 0, 0, 0); 4179 if (rc != SQLITE_OK) throw DBError(); 4180 } 4097 4181 4098 4182 delete rootNodeProperties; 4099 int game_id = sqlite3_last_insert_rowid(db);4100 4183 4101 4184 // printf("play through the game\n"); 06/libkombilo/search.h
r198 r200 520 520 std::string asString(); 521 521 void validate(); 522 std::vector<std::string>* SGFTagsAsStrings(); 522 523 523 524 ProcessOptions(); // sets default values which have to be overwritten … … 602 603 603 604 void start_processing(int PROCESSVARIATIONS=-1) throw(DBError); 604 int process(const char* sgf, const char* path, const char* fn ) throw(SGFError,DBError);605 int process(const char* sgf, const char* path, const char* fn, const char* DBTREE = 0) throw(SGFError,DBError); 605 606 void finalize_processing() throw(DBError); 606 607 … … 621 622 622 623 private: 624 void createGamesDB() throw(DBError); 623 625 void readDB() throw(DBError); 624 626 void addAlgos(int bs); 625 int posDT; // used when parsing the DT field during processing 626 int posSZ; // used when parsing the SZ field during processing 627 int posDT; // used when parsing the DT, SZ, BR, WR, HA fields during processing 628 int posSZ; 629 int posBR; 630 int posWR; 631 int posHA; 632 int SGFtagsSize; 627 633 int sizeX; // keeps track of width of search pattern during search 628 634 ProcessOptions* p_op; 629 std::vector<std::string>* tags;635 std::vector<std::string>* SGFtags; 630 636 std::string sql_ins_rnp; // sql string to insert root node properties 631 637 }; 632 638 639 const int HANDI_TAG = 1; 640 const int PROFESSIONAL_TAG = 2; 641 633 642 #endif 634 643 06/libkombilo/testsearch.py
r196 r200 48 48 print 'This search took %.2f seconds.' % (end - start) 49 49 50 print 'SGF of last game' 51 print gl.getSGF(gl.size()-1) 50 gl.reset() 51 print 'SGF of game 122' 52 print gl.getSGF(123) 52 53
