Changeset 200

Show
Ignore:
Timestamp:
10/15/06 13:27:55 (2 years ago)
Author:
ug
Message:

Some more bug fixes. Started work on tagging support.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 06/libkombilo/cpptest.cc

    r196 r200  
    4343        while (!infile.eof()) { 
    4444          getline(infile, line); 
    45           sgf += line
     45          sgf += line + "\n"
    4646        } 
    4747        infile.close(); 
  • 06/libkombilo/search.cc

    r199 r200  
    31933193  processVariations = true; 
    31943194  sgfInDB = true; 
    3195   rootNodeTags = "BR,CA,DT,EV,HC,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"; 
    31963196  algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_FULL | ALGO_HASH_CORNER; 
    31973197  algo_hash_full_maxNumStones = 50; 
     
    32003200 
    32013201ProcessOptions::ProcessOptions(string s) { 
    3202   if (s[0] == 't') processVariations = true; 
     3202  int p = 0; 
     3203  if (s[p++] == 't') processVariations = true; 
    32033204  else processVariations = false; 
    32043205 
    3205   int p = 2; 
     3206  if (s[p++] == 't') sgfInDB = true; 
     3207  else sgfInDB = false; 
     3208 
     3209  p++; 
    32063210  int pn = s.find('|', p) + 1; 
    32073211  algos = atoi(s.substr(p, pn-p-1).c_str()); 
     
    32203224string ProcessOptions::asString() { 
    32213225  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"; 
    32243230  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()); 
    32263232  result += buf; 
    32273233  return result; 
     
    32293235 
    32303236void ProcessOptions::validate() { 
     3237  string::iterator it = rootNodeTags.begin(); 
     3238  while (it != rootNodeTags.end()) { 
     3239    if (*it == ' ') it = rootNodeTags.erase(it); 
     3240    else it++; 
     3241  } 
    32313242  if (rootNodeTags.find("PB") == string::npos) rootNodeTags += ",PB"; 
    32323243  if (rootNodeTags.find("PW") == string::npos) rootNodeTags += ",PW"; 
    32333244  if (rootNodeTags.find("RE") == string::npos) rootNodeTags += ",RE"; 
    32343245  if (rootNodeTags.find("DT") == string::npos) rootNodeTags += ",DT"; 
    3235   if (rootNodeTags.find("SZ") == string::npos) rootNodeTags += ",SZ"; 
     3246
     3247 
     3248vector<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; 
    32363261} 
    32373262 
     
    37533778 
    37543779string 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  } 
    37573788  int index = (*currentList)[i].second; 
    37583789  // int rc = sqlite3_open(dbname, &db);  
     
    37833814  sizeX = pattern.sizeX; // need this in lookupLabel 
    37843815  PatternList pl(pattern, searchOptions->fixedColor, searchOptions->nextMove); 
    3785   int rc; 
    3786   // rc = sqlite3_open(dbname, &db);  
     3816  // int rc = sqlite3_open(dbname, &db);  
    37873817  // if (rc) { 
    37883818  //   sqlite3_close(db); 
     
    37903820  //   throw DBError(); 
    37913821  // } 
    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(); 
    37973826 
    37983827  if (boardsizes.size() != 1 || boardsizes[0] != pattern.boardsize) { 
     
    38443873} 
    38453874 
     3875void 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} 
    38463949 
    38473950void GameList::start_processing(int PROCESSVARIATIONS) throw(DBError) { 
     
    38503953  else processVariations = p_op->processVariations; 
    38513954  readDBs = 0; 
    3852   string question_marks = "?,?,?,?"; // path, filename, pos, date 
    3853   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; 
    38703955  // printf("dt %d sz %d\n", posDT, posSZ); 
    38713956 
    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 + ");"; 
    38783957  int rc; 
    38793958  // rc = sqlite3_open(dbname, &db);  
     
    38833962  //   throw DBError(); 
    38843963  // } 
    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(); 
    39033970 
    39043971  char* sql = "begin transaction;"; 
    39053972  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(); 
    39113974  current = 0; 
    39123975  for(unsigned int a=0; a < 20*boardsizes.size(); a++) if (algo_ps[a]) algo_ps[a]->initialize_process(db); 
     
    39353998  readDBs = 0; 
    39363999  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 
     4003int 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; 
    39414006 
    39424007  Cursor* c = 0; 
     
    39684033    //  } 
    39694034    // } 
    3970     vector<string>* rootNodeProperties = parseRootNode(root, tags); 
     4035    vector<string>* rootNodeProperties = parseRootNode(root, SGFtags); 
    39714036    // for(vector<string>::iterator rnp = rootNodeProperties->begin(); rnp != rootNodeProperties->end(); rnp++) 
    39724037    // printf("rnp %s\n", rnp->c_str()); 
     
    40784143    rc = sqlite3_bind_int(ppStmt, stmt_ctr++, pos); 
    40794144    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(); 
    40804147    rc = sqlite3_bind_text(ppStmt, stmt_ctr++, date.c_str(), -1, SQLITE_TRANSIENT);  
    40814148    if (rc != SQLITE_OK) throw DBError(); 
     
    40864153    } 
    40874154 
    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);  
    40904157      if (rc != SQLITE_OK) throw DBError(); 
    40914158    } 
     
    40954162    rc = sqlite3_finalize(ppStmt); 
    40964163    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    } 
    40974181 
    40984182    delete rootNodeProperties; 
    4099     int game_id = sqlite3_last_insert_rowid(db); 
    41004183 
    41014184    // printf("play through the game\n"); 
  • 06/libkombilo/search.h

    r198 r200  
    520520    std::string asString(); 
    521521    void validate(); 
     522    std::vector<std::string>* SGFTagsAsStrings(); 
    522523 
    523524    ProcessOptions(); // sets default values which have to be overwritten 
     
    602603 
    603604    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); 
    605606    void finalize_processing() throw(DBError); 
    606607     
     
    621622 
    622623  private: 
     624    void createGamesDB() throw(DBError); 
    623625    void readDB() throw(DBError); 
    624626    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; 
    627633    int sizeX; // keeps track of width of search pattern during search 
    628634    ProcessOptions* p_op; 
    629     std::vector<std::string>* tags; 
     635    std::vector<std::string>* SGFtags; 
    630636    std::string sql_ins_rnp; // sql string to insert root node properties 
    631637}; 
    632638 
     639const int HANDI_TAG = 1; 
     640const int PROFESSIONAL_TAG = 2; 
     641 
    633642#endif 
    634643 
  • 06/libkombilo/testsearch.py

    r196 r200  
    4848print 'This search took %.2f seconds.' % (end - start) 
    4949 
    50 print 'SGF of last game' 
    51 print gl.getSGF(gl.size()-1) 
     50gl.reset() 
     51print 'SGF of game 122' 
     52print gl.getSGF(123) 
    5253