Changeset 196
- Timestamp:
- 10/12/06 22:35:52 (2 years ago)
- Files:
-
- 06/libkombilo/Makefile (modified) (1 diff)
- 06/libkombilo/cpptest.cc (modified) (6 diffs)
- 06/libkombilo/search.cc (modified) (17 diffs)
- 06/libkombilo/search.h (modified) (3 diffs)
- 06/libkombilo/testhash.py (modified) (1 diff)
- 06/libkombilo/testsearch.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
06/libkombilo/Makefile
r195 r196 2 2 g++ -o cpptest -lsqlite3 -lboost_filesystem cpptest.o search.o sgfparser.o abstractboard.o 3 3 4 cpptest.o: 4 cpptest.o: cpptest.cc 5 5 g++ -c cpptest.cc 6 6 06/libkombilo/cpptest.cc
r195 r196 8 8 9 9 int main(int argc, char** argv) { 10 // ----------------- parse command line arguments --------------------------------- 10 11 int algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_FULL | ALGO_HASH_CORNER; 11 12 bool process = false; … … 16 17 } 17 18 19 // ----------------- set up processing options ----------------------------------- 18 20 ProcessOptions* p_op = new ProcessOptions; 19 21 p_op->algos = ALGO_FINALPOS | ALGO_MOVELIST; 20 22 21 GameList gl("t1.db", "id", "[[PW]] - [[PB]] ([[winner]]), [[filename.]]", 0); 23 // ----------------- create GameList instance ----------------------------------- 24 GameList gl("t1.db", "id", "[[PW]] - [[PB]] ([[winner]]), [[filename.]], ", 0); 25 delete p_op; 22 26 27 // ---------------- process SGF games --------------------------------------------- 23 28 if (process) { // process sgf's. must be first argument 24 29 gl.start_processing(); … … 49 54 printf("%d games.\n", gl.size()); 50 55 56 // ------------------- set up search pattern ---------------------------------------- 51 57 Pattern p(CORNER_NW_PATTERN,19,8,8,"...................X......X.......XO......OO...................."); 52 58 // Pattern p(CORNER_NW_PATTERN,19,7,7,".................X.....X......XO.....OO.........."); … … 61 67 // Pattern p(FULLBOARD_PATTERN, 19, 19, 19, ".....................O.O........OX......XO......X.OXX.XX...X,.OOXXX..OOOX.O....X.OXOOXOO..OX.......XOXXOXXOOXXOO....OX.XXXOOOXO.O.O...OXX..XOX..XO.X.XO...O.......X.....XO..O.,X....,.....XOO................X......X............X....O...........................................O.O...........O.....,.....X...........X.O.X............................................", contList); 62 68 // Pattern p(FULLBOARD_PATTERN, 19, 19, 19, "..O.O....X...XXXXX.OOXO....OXO.XXOOOXOXXXXOO.OOXO.OXO..O..X.X..OOX,X.XO.O.....XOOOXOXX..XO......X.XOXXX..XXXO........XOX..XXOOXO.OOO.....OOXOXOO.O...XX...X..OXXOO.XOX........O..OX.,..X..X.....X...OX...X..........O....XXXO...XO...X...OOOXOOXX...X....O..OX.O..OX..........OXX....OX..OO..O.OOOOX..O.OX..XX..OOXXXOX.XOOX..X....XXXXXOX...OX.......X.O.XO............."); 69 70 // -------------------- set up search options ---------------------------------- 63 71 SearchOptions so; 64 72 // so.searchInVariations = false; 73 74 // -------------------- do pattern search -------------------------------------- 65 75 gl.search(p, &so); 76 77 // ------------------- print some information about current list of games ------------ 66 78 printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 67 79 // vector<string> res = gl.currentEntriesAsStrings(); … … 69 81 // printf("%s\n", it->c_str()); 70 82 for(int i=0; i<gl.size(); i++) printf("%s\n", gl.currentEntryAsString(i).c_str()); 83 84 // ------------------- print some statistics ------------------------------------------ 71 85 printf("Search pattern:\n"); 72 86 printf("%s\n", p.printPattern().c_str()); … … 90 104 } 91 105 } 106 107 // ------------------- resetFormat ------------------------------------------ 108 gl.resetFormat("pb"); 109 vector<string> res = gl.currentEntriesAsStrings(0, 40); 110 for(vector<string>::iterator it = res.begin(); it != res.end(); it++) 111 printf("%s\n", it->c_str()); 92 112 } 06/libkombilo/search.cc
r195 r196 3293 3293 int dbinfo_callback(void *s, int argc, char **argv, char **asColName) { 3294 3294 char** cpp = (char**)s; 3295 if (argc && argv[0] && argv[0]) {3295 if (argc && argv[0]) { 3296 3296 // printf("dbi_cb %s\n", argv[0]); 3297 *cpp = new char[strlen(argv[0] +1)];3297 *cpp = new char[strlen(argv[0])+1]; 3298 3298 strcpy(*cpp, argv[0]); 3299 3299 } … … 3308 3308 3309 3309 // try to retrieve basic options from database 3310 sqlite3* db;3311 3310 int rc = sqlite3_open(dbname, &db); 3312 3311 if (rc) { … … 3325 3324 // printf("dbinfo: %s\n", dbinfo); 3326 3325 p_op = new ProcessOptions(dbinfo); 3326 delete [] dbinfo; 3327 3327 char* bsizes = 0; 3328 3328 rc = sqlite3_exec(db, "select * from db_info where rowid = 2;", dbinfo_callback, &bsizes, 0); … … 3331 3331 // printf("board sizes %s\n", bsizes); // should be a comma-sep. list of integers *ending w/ a comma* 3332 3332 string bsizes_str(bsizes); 3333 delete [] bsizes; 3333 3334 unsigned int p = 0; 3334 3335 unsigned int pn = bsizes_str.find(",",p); … … 3355 3356 if (rc != SQLITE_OK) throw DBError(); 3356 3357 } 3357 rc = sqlite3_close(db); 3358 if (rc != SQLITE_OK) throw DBError(); 3359 db = 0; 3360 3361 // printf("parse the FORMAT string\n"); 3358 // rc = sqlite3_close(db); 3359 // if (rc != SQLITE_OK) throw DBError(); 3360 // db = 0; 3361 3362 3363 // printf("set up Algorithm instances\n"); 3364 for(vector<int>::iterator it = boardsizes.begin(); it != boardsizes.end(); it++) 3365 addAlgos(*it); 3366 all = 0; 3367 currentList = oldList = 0; 3368 readDBs = 0; 3369 resetFormat(ORDERBY, FORMAT); 3370 } 3371 3372 void GameList::resetFormat(string ORDERBY, string FORMAT) { 3373 // printf("enter resetFormat\n"); 3362 3374 if (FORMAT == "") { // use default format string 3363 3375 numColumns = 5; 3364 3376 format1 = "id,re,pw,pb,dt"; 3365 format2 = "[[2 - [[3 ([[W), [[4 ";3377 format2 = "[[2 - [[3 ([[W), [[4, "; 3366 3378 } else { 3367 3379 char buf[10]; … … 3394 3406 if (ORDERBY == "" || ORDERBY == "id" || ORDERBY == "ID" || ORDERBY == "Id" || ORDERBY == "iD") orderby = "id"; 3395 3407 else orderby = ORDERBY + ",id"; 3396 3397 // printf("set up Algorithm instances\n"); 3398 for(vector<int>::iterator it = boardsizes.begin(); it != boardsizes.end(); it++) 3399 addAlgos(*it); 3400 all = 0; 3401 currentList = oldList = 0; 3408 // printf("finished parsing\n"); 3409 3402 3410 readDB(); 3403 3411 } 3412 3404 3413 3405 3414 void GameList::addAlgos(int bs) { … … 3436 3445 oldList = 0; 3437 3446 3438 db = 0; 3439 int rc = sqlite3_open(dbname, &db); 3440 if (rc) { 3441 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); 3442 sqlite3_close(db); 3443 db = 0; 3444 throw DBError(); 3445 } 3447 int rc; 3448 // db = 0; 3449 // rc = sqlite3_open(dbname, &db); 3450 // if (rc) { 3451 // fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); 3452 // sqlite3_close(db); 3453 // db = 0; 3454 // throw DBError(); 3455 // } 3446 3456 rc = sqlite3_busy_timeout(db, 200); 3447 3457 if (rc) throw DBError(); … … 3463 3473 throw DBError(); 3464 3474 } 3475 // printf("read.\n"); 3465 3476 // SQLITE_ERROR may occur since table might not yet exist 3466 3477 3467 if (rc == SQLITE_OK ) {3478 if (rc == SQLITE_OK && !readDBs) { 3468 3479 for(unsigned int a=0; a < 20*boardsizes.size(); a++) { 3469 if (a >= algo_ps.size()) printf("ouch\n");3470 3480 if (algo_ps[a]) algo_ps[a]->readDB(db); 3471 3481 } … … 3474 3484 rc = sqlite3_exec(db, "commit;", 0, 0, 0); 3475 3485 if (rc != SQLITE_OK) throw DBError(); 3476 3477 rc = sqlite3_close(db); 3478 if (rc != SQLITE_OK) throw DBError(); 3479 db = 0; 3486 // printf("read.\n"); 3487 3488 // rc = sqlite3_close(db); 3489 // if (rc != SQLITE_OK) throw DBError(); 3490 // db = 0; 3480 3491 reset(); 3481 3492 // printf("leave readDB\n"); … … 3497 3508 for(int i=0; i<20; i++) 3498 3509 if (algo_ps[i]) delete algo_ps[i]; 3510 if (db) sqlite3_close(db); 3511 db = 0; 3499 3512 // printf("leave ~GameList\n"); 3500 3513 } … … 3635 3648 } 3636 3649 3637 void GameList::resetFormat(char* FORMAT, char* ORDERBY) {3638 // FIXME3639 }3640 3641 3650 int gis_callback(void *gl, int argc, char **argv, char **azColName) { 3642 3651 if (!argc) throw DBError(); … … 3723 3732 if (start>end || end > (int)currentList->size()) return result; 3724 3733 for(int i=start; i<end; i++) { 3725 result.push_back((*all)[(*currentList)[i].second]->gameInfoStr + ", " +resultsStr((*all)[(*currentList)[i].second]));3734 result.push_back((*all)[(*currentList)[i].second]->gameInfoStr + resultsStr((*all)[(*currentList)[i].second])); 3726 3735 } 3727 3736 return result; … … 3730 3739 string GameList::currentEntryAsString(int i) { 3731 3740 if (i < 0 || i >= (int)currentList->size()) return ""; 3732 else return (*all)[(*currentList)[i].second]->gameInfoStr + ", " + resultsStr((*all)[(*currentList)[i].second]); 3741 else return (*all)[(*currentList)[i].second]->gameInfoStr + resultsStr((*all)[(*currentList)[i].second]); 3742 } 3743 3744 int getsgfcallback(void *s, int argc, char **argv, char **azColName) { 3745 char** sgf = (char**)s; 3746 if (argc && argv[0]) { 3747 *sgf = new char[strlen(argv[0])+1]; 3748 strcpy(*sgf, argv[0]); 3749 } 3750 return 0; 3751 } 3752 3753 string GameList::getSGF(int i) { 3754 if (i < 0 || i >= (int)currentList->size()) return ""; 3755 int index = (*currentList)[i].second; 3756 // int rc = sqlite3_open(dbname, &db); 3757 // if (rc) { 3758 // sqlite3_close(db); 3759 // db = 0; 3760 // throw DBError(); 3761 // } 3762 char* sgf = 0; 3763 char sql[200]; 3764 sprintf(sql, "select sgf from games where id = %d;", index); 3765 // printf("%s\n", sql); 3766 sqlite3_exec(db, sql, getsgfcallback, &sgf, 0); 3767 3768 // sqlite3_close(db); 3769 // db = 0; 3770 3771 if (!sgf) return ""; 3772 string sgf_str(sgf); 3773 delete [] sgf; 3774 return sgf_str; 3733 3775 } 3734 3776 … … 3739 3781 sizeX = pattern.sizeX; // need this in lookupLabel 3740 3782 PatternList pl(pattern, searchOptions->fixedColor, searchOptions->nextMove); 3741 sqlite3* db;3742 intrc = sqlite3_open(dbname, &db);3743 if (rc) {3744 sqlite3_close(db);3745 db = 0;3746 throw DBError();3747 }3783 int rc; 3784 // rc = sqlite3_open(dbname, &db); 3785 // if (rc) { 3786 // sqlite3_close(db); 3787 // db = 0; 3788 // throw DBError(); 3789 // } 3748 3790 rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 3749 3791 if (rc) throw DBError(); … … 3790 3832 } 3791 3833 } 3792 sqlite3_close(db);3793 db = 0;3834 // sqlite3_close(db); 3835 // db = 0; 3794 3836 if (labels) delete [] labels; 3795 3837 labels = pl.sortContinuations(); … … 3832 3874 } 3833 3875 sql_ins_rnp += p_op->rootNodeTags + ") values (" + question_marks + ");"; 3834 int rc = sqlite3_open(dbname, &db); 3835 if (rc) { 3836 sqlite3_close(db); 3837 db = 0; 3838 throw DBError(); 3839 } 3876 int rc; 3877 // rc = sqlite3_open(dbname, &db); 3878 // if (rc) { 3879 // sqlite3_close(db); 3880 // db = 0; 3881 // throw DBError(); 3882 // } 3840 3883 string sql1 = "create table if not exists GAMES ( id integer primary key, path text, filename text, pos integer default 0, duplicate integer, date date"; 3841 3884 if (p_op->sgfInDB) sql1 += ", sgf text"; … … 3886 3929 rc = sqlite3_exec(db, sql.c_str(), 0, 0, 0); 3887 3930 if (rc != SQLITE_OK) throw DBError(); 3888 sqlite3_close(db); 3889 db = 0; 3931 // sqlite3_close(db); 3932 // db = 0; 3933 readDBs = 0; 3890 3934 readDB(); 3891 3935 delete tags; 06/libkombilo/search.h
r195 r196 593 593 594 594 void reset(); // reset currentList to all 595 void resetFormat( char* FORMAT, char* ORDERBY); // NOT YET IMPLEMENTED595 void resetFormat(std::string ORDERBY="", std::string FORMAT=""); 596 596 void gisearch(char* sql) throw(DBError); 597 597 int size(); … … 599 599 std::string currentEntryAsString(int i); 600 600 std::vector<std::string> currentEntriesAsStrings(int start=0, int end=0); 601 std::string getSGF(int i); 601 602 int numHits(); 602 603 … … 623 624 void readDB() throw(DBError); 624 625 void addAlgos(int bs); 626 void parseFormatString(std::string ORDERBY, std::string FORMAT); 625 627 int posDT; // used when parsing the DT field during processing 626 628 int posSZ; // used when parsing the SZ field during processing 06/libkombilo/testhash.py
r195 r196 6 6 from libkombilo import * 7 7 8 gl = GameList('t1.db', '', '[[PW]] - [[PB]] ([[winner]]), [[filename]] ')8 gl = GameList('t1.db', '', '[[PW]] - [[PB]] ([[winner]]), [[filename]], ') 9 9 print gl.size(), 'games in the database' 10 10 06/libkombilo/testsearch.py
r195 r196 39 39 (gl.lookupLabel(x,y), cont.B, cont.wB, cont.lB, cont.W, cont.wW, cont.lW) 40 40 print 'This search took %.2f seconds.' % (end - start) 41 sys.exit()42 41 43 42 p = Pattern(CENTER_PATTERN, 19, 2, 2, 'XO' + 'OX') … … 49 48 print 'This search took %.2f seconds.' % (end - start) 50 49 50 print 'SGF of last game' 51 print gl.getSGF(gl.size()-1) 51 52
