libkombilo  0.8
/home/ug/devel/kombilo-py-dev/kombilo/libkombilo/search.h
Go to the documentation of this file.
1 
33 #ifndef _SEARCH_H_
34 #define _SEARCH_H_
35 
36 #include <vector>
37 #include <utility>
38 #include <stack>
39 #include <fstream>
40 #include "sqlite3.h"
41 #include "pstdint.h"
42 #include <boost/unordered_map.hpp>
43 
44 #include "abstractboard.h"
45 #include "sgfparser.h"
46 #include "algos.h"
47 
80  public:
81  bool processVariations;
82  bool sgfInDB;
83  std::string rootNodeTags;
84  int algos;
85  int algo_hash_full_maxNumStones;
86  int algo_hash_corner_maxNumStones;
88 
89  std::string asString();
90  void validate();
91  std::vector<std::string>* SGFTagsAsStrings();
92 
93  ProcessOptions();
94  ProcessOptions(std::string s);
95 };
96 
98  public:
99  int fixedColor;
100  int nextMove; // 0 undetermined, 1 = next move must be black, 2 = next move must be white
101  int moveLimit;
102  bool trustHashFull;
103  bool searchInVariations;
104  int algos;
105 
106  SearchOptions();
107  SearchOptions(int FIXEDCOLOR, int NEXTMOVE=0, int MOVELIMIT=10000);
109  void to_snv(SnapshotVector& snv);
110 };
111 
113  public:
114  int id; // id within the concerning database
115  std::string gameInfoStr;
116  char winner;
117  int date;
118  std::vector<Hit* > * hits; // used for hits
119  std::vector<Candidate* > * candidates; // used for candidates
120 
121  GameListEntry(int ID, char WINNER, std::string GAMEINFOSTR, int DATE);
122  ~GameListEntry();
123 
124  void hits_from_snv(SnapshotVector& snv);
125  void set_hits(vector<Hit* > * hts);
126  void set_candidates(vector<Candidate* > * cands);
127 };
128 
129 class VarInfo {
130  public:
131  Node* n;
132  abstractBoard* b;
133  int i;
134 
135  VarInfo(Node* N, abstractBoard* B, int I);
136  VarInfo(const VarInfo& v);
137  ~VarInfo();
138 };
139 
144 
145 const int CHECK_FOR_DUPLICATES = 1;
147 const int OMIT_DUPLICATES = 4;
148 const int OMIT_GAMES_WITH_SGF_ERRORS = 8;
151 
156 const int UNACCEPTABLE_BOARDSIZE = 1; // (database not changed)
157 const int SGF_ERROR = 2;
162 const int IS_DUPLICATE = 4;
163 const int NOT_INSERTED_INTO_DB = 8;
164 const int INDEX_OUT_OF_RANGE = 16;
187 class GameList {
188  private:
189  std::vector<std::pair<int,int> > * currentList; // pair<int,int>: (database id, position within all )
190  // (usually sorted w.r.t. second component)
191  std::vector<std::pair<int,int> > * oldList;
192  public:
193  char* dbname;
194  std::string orderby;
195  std::string format1;
196  std::string format2;
197  int numColumns;
198  int processVariations;
199 
200  int boardsize;
201  std::vector<algo_p> algo_ps;
202  std::vector<GameListEntry* > * all;
203  int current;
204  sqlite3* db;
205  char* labels;
206  vector<Continuation* > continuations;
207  int num_hits;
208  int num_switched;
209  int Bwins;
210  int BwinsG;
211  int BwinsAll;
212  int Wwins;
213  int WwinsG;
214  int WwinsAll;
216  SearchOptions* searchOptions;
217  vector<int> dates_all;
218  vector<int> dates_all_per_year;
219  vector<int> dates_current;
220  // ----------------------------------------------------------------------------
221  // the following methods provide the user interface
222 
223  // ------- constructor --------------------------------------------------------
224  GameList(
225  const char* DBNAME,
236  std::string ORDERBY="",
259  std::string FORMAT="",
261 
310  ProcessOptions* p_options=0,
311  int BOARDSIZE=19,
312  int cache=100
313  ) throw(DBError);
314  ~GameList();
315 
316 
349  int process(const char* sgf, const char* path, const char* fn, std::vector<GameList* > glists, const char* DBTREE = 0, int flags=0) throw(SGFError,DBError);
350 
363  int process_results(unsigned int i=0); // result for i-th processed game in most recently processed SGF collection
364 
365  void start_processing(int PROCESSVARIATIONS=-1) throw(DBError);
366  void finalize_processing() throw(DBError);
370 
372  void search(Pattern& pattern, SearchOptions* options = 0) throw(DBError);
373  char lookupLabel(char x, char y);
374  void setLabel(char x, char y, char label);
375  Continuation lookupContinuation(char x, char y);
376  int numHits();
377 
379 
381  void sigsearch(char* sig) throw(DBError);
382  vector<int> sigsearchNC(char* sig) throw(DBError); // sig search in all; do not change currentList
383  std::string getSignature(int i) throw(DBError);
386 
388  void gisearch(const char* sql, int complete=0) throw(DBError);
393 
394  vector<int>* gisearchNC(const char* sql, int complete=0) throw(DBError);
399 
412  void tagsearch(int tag) throw(DBError);
413 
421  void tagsearchSQL(char* query) throw(DBError);
422  void setTag(int tag, int start=0, int end=0) throw(DBError);
424 
427 
428  void setTagID(int tag, int i) throw(DBError);
429 
436  vector<int> getTagsID(int i, int tag=0) throw(DBError);
437 
438  void deleteTag(int tag, int i = -1) throw(DBError);
439 
440 
449  std::vector<int> getTags(unsigned int i, int tag=0) throw(DBError); // note the order of arguments!
453  void export_tags(string tag_db_name, vector<int> which_tags);
454  void import_tags(string tag_db_name);
455 
456 
458 
459  int snapshot() throw(DBError);
460  void restore(int handle, bool del=false) throw(DBError);
461  void delete_snapshot(int handle) throw(DBError);
462  void delete_all_snapshots() throw(DBError);
465  // ------- misc ---------------------------------------------------------------
466  void reset();
467  void resetFormat(std::string ORDERBY="", std::string FORMAT="");
468  int size();
469  int size_all();
470  std::string get_resultsStr(unsigned int i);
471  std::string resultsStr(GameListEntry* gle);
472  int find_by_ID(int ID);
473 
474 
476 
477  std::string get_gameInfoStr(unsigned int i);
478  pair<int, int> get_currentList_entry(unsigned int i);
479  std::string currentEntryAsString(int i);
480  std::vector<std::string> currentEntriesAsStrings(int start=0, int end=0);
481  std::string getSGF(int i) throw(DBError);
482  std::string getCurrentProperty(int i, std::string tag) throw (DBError);
486 
488  int plSize();
489  std::string plEntry(int i);
493  friend class Algo_finalpos;
494  friend class Algo_movelist;
495  friend class Algo_hash_full;
496  friend class Algo_hash;
497  friend class Algo_hash_corner;
498  friend int gis_callback(void *gl, int argc, char **argv, char **azColName);
499  friend int gis_callbackNC(void *pair_gl_CL, int argc, char **argv, char **azColName);
500 
501  private:
502  void createGamesDB() throw(DBError);
503  void open_db() throw(DBError);
504  void readDB() throw(DBError);
505  void addAlgos(bool NEW);
506  int db_cache_size;
507  int posDT; // used when parsing the DT, SZ, BR, WR, HA fields during processing
508  int posSZ;
509  int posBR;
510  int posWR;
511  int posHA;
512  int SGFtagsSize;
513  ProcessOptions* p_op;
514  std::vector<std::string>* SGFtags;
515  std::string sql_ins_rnp; // sql string to insert root node properties
516  std::vector<std::string> pl; // list of all players
517  void readPlayersList() throw(DBError);
518  std::vector<std::vector<int> >* duplicates;
519  std::vector<int> process_results_vector;
520 
521  int startO();
522  int start();
523  int next();
524  int start_sorted();
525  int end_sorted();
526  void update_dates_current();
527  int get_current_index(int id, int* start); // returns the index in oldList of the game with game id "id"
528  // (if available, otherwise returns -1),
529  // use this between start_sorted and end_sorted
530  int get_current_index_CL(int id, int start=0); // returns the index in currentList of the game with game id "id"
531  // (if available, otherwise returns -1), requires currentList to
532  // be sorted wrt first component (see duplicates())
533  char getCurrentWinner();
534  int getCurrentDate();
535  std::vector<Candidate* > *getCurrentCandidateList();
536  void makeCurrentCandidate(std::vector<Candidate* > *candidates);
537  void makeCurrentHit(std::vector<Hit* > *hits);
538  void makeIndexCandidate(int index, std::vector<Candidate* > *candidates);
539  void makeIndexHit(int index, std::vector<Hit* > *hits, vector<int>* CL = 0);
543 
544  void setCurrentFromIndex(int index);
545  void readNumOfWins() throw(DBError);
546 };
547 
548 // ------- duplicates ---------------------------------------------------------
561 std::map<std::string, std::vector<int> > find_duplicates(std::vector<string> glists, bool strict=false, bool dupl_within_db=false) throw(DBError);
562 
563 
564 
565 const int HANDI_TAG = 1;
566 const int PROFESSIONAL_TAG = 2;
567 
568 #endif
569 
std::string rootNodeTags
a comma-separated list of those SGF tags which should be written to the database
Definition: search.h:83
Definition: pattern.h:86
Definition: pattern.h:119
Definition: sgfparser.h:74
int Bwins
number of hits in previous search where B wins (can exceed number of games if there are multiple hits...
Definition: search.h:209
vector< int > dates_current
a vector which counts, for each month between January 1600 and December 2020, the number of games in ...
Definition: search.h:219
const int OMIT_DUPLICATES
Omit games recognized as duplicates from the database.
Definition: search.h:147
Pattern * mrs_pattern
most recent search pattern
Definition: search.h:215
int WwinsAll
number of B wins in all games of the gamelist (independent of currentList)
Definition: search.h:214
Definition: algos.h:299
Definition: pattern.h:189
Definition: sgfparser.h:37
Definition: search.h:97
STL namespace.
Definition: pattern.h:242
Definition: algos.h:203
vector< int > dates_all_per_year
a vector which counts, for each year between 1600 and 2020, the number of games in the all list ...
Definition: search.h:218
Definition: abstractboard.h:77
int algos
algorithms to be used
Definition: search.h:84
vector< int > dates_all
a vector which counts, for each month between January 1600 and December 2020, the number of games in ...
Definition: search.h:217
The GameList class is the main interface to the libkombilo functionality.
Definition: search.h:187
Definition: pattern.h:330
Definition: search.h:79
Definition: pattern.h:321
const int CHECK_FOR_DUPLICATES
check for duplicates using the signature
Definition: search.h:145
int BwinsG
number of games in currentList where B wins
Definition: search.h:210
const int CHECK_FOR_DUPLICATES_STRICT
check for duplicates using the final position (if ALGO_FINAPOS is available)
Definition: search.h:146
Base class for hashing for general type patterns (currently works only for corner patterns)...
Definition: algos.h:454
int Wwins
number of hits in previous search where W wins (can exceed number of games if there are multiple hits...
Definition: search.h:212
std::map< std::string, std::vector< int > > find_duplicates(std::vector< string > glists, bool strict=false, bool dupl_within_db=false)
Definition: search.cpp:2145
int WwinsG
number of games in currentList where W wins
Definition: search.h:213
int professional_tag
whether to use "P" tag (0 = don&#39;t use; 1 = always use; 2 = use for players with 1p to 9p ranks) ...
Definition: search.h:87
int BwinsAll
number of B wins in all games of the gamelist (independent of currentList)
Definition: search.h:211
Definition: search.h:112
const int SGF_ERROR
Definition: search.h:157
ProcessOptions()
sets default values which can be overwritten individually
Definition: search.cpp:125
Hashing for full board patterns.
Definition: algos.h:369
Hashing for corner patterns.
Definition: algos.h:506
Definition: search.h:129