libkombilo  0.7
/home/ug/devel/kombilo/lk/search.h
Go to the documentation of this file.
00001 
00033 #ifndef _SEARCH_H_
00034 #define _SEARCH_H_
00035 
00036 #include <vector>
00037 #include <utility>
00038 #include <stack>
00039 #include <fstream>
00040 #include <stdint.h>
00041 #include <sqlite3.h>
00042 #include "boost/unordered_map.hpp"
00043 
00044 #include "abstractboard.h"
00045 #include "sgfparser.h"
00046 #include "algos.h"
00047 
00079 class ProcessOptions {
00080   public:
00081     bool processVariations;
00082     bool sgfInDB;
00083     std::string rootNodeTags; 
00084     int algos;           
00085     int algo_hash_full_maxNumStones;
00086     int algo_hash_corner_maxNumStones;
00087     int professional_tag;               
00088 
00089     std::string asString();
00090     void validate();
00091     std::vector<std::string>* SGFTagsAsStrings();
00092 
00093     ProcessOptions(); 
00094     ProcessOptions(std::string s);
00095 };
00096 
00097 class SearchOptions {
00098   public:
00099     int fixedColor;
00100     int nextMove; // 0 undetermined, 1 = next move must be black, 2 = next move must be white
00101     int moveLimit;
00102     bool trustHashFull;
00103     bool searchInVariations;
00104     int algos;
00105 
00106     SearchOptions();
00107     SearchOptions(int FIXEDCOLOR, int NEXTMOVE=0, int MOVELIMIT=10000);
00108     SearchOptions(SnapshotVector& snv);
00109     void to_snv(SnapshotVector& snv);
00110 };
00111 
00112 class GameListEntry {
00113   public:
00114     int id; // id within the concerning database
00115     std::string gameInfoStr;
00116     char winner;
00117     std::vector<Hit* > * hits; // used for hits
00118     std::vector<Candidate* > * candidates; // used for candidates
00119 
00120     GameListEntry(int ID, char WINNER, std::string GAMEINFOSTR);
00121     ~GameListEntry();
00122 
00123     void hits_from_snv(SnapshotVector& snv);
00124 };
00125 
00126 class VarInfo {
00127   public:
00128     Node* n;
00129     abstractBoard* b;
00130     int i;
00131 
00132     VarInfo(Node* N, abstractBoard* B, int I);
00133     VarInfo(const VarInfo& v);
00134     ~VarInfo();
00135 };
00136 
00141 
00142 const int CHECK_FOR_DUPLICATES = 1; 
00143 const int CHECK_FOR_DUPLICATES_STRICT = 2; 
00144 const int OMIT_DUPLICATES = 4; 
00145 const int OMIT_GAMES_WITH_SGF_ERRORS = 8; 
00148 
00149 
00150 
00151 
00152 
00153 const int UNACCEPTABLE_BOARDSIZE = 1; // (database not changed) 
00154 const int SGF_ERROR = 2;
00159 const int IS_DUPLICATE = 4;
00160 const int NOT_INSERTED_INTO_DB = 8;
00161 const int INDEX_OUT_OF_RANGE = 16;
00181 class GameList {
00182   public:
00183     char* dbname;
00184     std::string orderby;
00185     std::string format1;
00186     std::string format2;
00187     int numColumns;
00188     int processVariations;
00189 
00190     int boardsize;
00191     std::vector<algo_p> algo_ps;
00192     std::vector<GameListEntry* > * all;
00193     std::vector<std::pair<int,int> > * currentList; // pair<int,int>: (database id, position within all )
00194                                                     // (usually sorted w.r.t. second component)
00195     std::vector<std::pair<int,int> > * oldList;
00196     int current;
00197     sqlite3* db;
00198     char* labels;
00199     Continuation* continuations;
00200     int num_hits;
00201     int num_switched;
00202     int Bwins;
00203     int BwinsAll; // number of B wins in all games of the gamelist (independent of currentList)
00204     int Wwins;
00205     int WwinsAll; // number of B wins in all games of the gamelist (independent of currentList)
00206     Pattern* mrs_pattern; // most recent search pattern
00207     SearchOptions* searchOptions;
00208     // ----------------------------------------------------------------------------
00209     // the following methods provide the user interface
00210 
00211     // ------- constructor --------------------------------------------------------
00212     GameList(
00213         const char* DBNAME, 
00224         std::string ORDERBY="",
00247         std::string FORMAT="",
00249 
00298         ProcessOptions* p_options=0, 
00299         int BOARDSIZE=19, 
00300         int cache=100 
00301           ) throw(DBError);
00302     ~GameList();
00303 
00304 
00337     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);
00338 
00351     int process_results(unsigned int i=0); // result for i-th processed game in most recently processed SGF collection
00352 
00353     void start_processing(int PROCESSVARIATIONS=-1) throw(DBError);
00354     void finalize_processing() throw(DBError);
00358 
00359 
00360     void search(Pattern& pattern, SearchOptions* options = 0) throw(DBError);
00361     char lookupLabel(char x, char y);
00362     void setLabel(char x, char y, char label);
00363     Continuation lookupContinuation(char x, char y);
00364     int numHits(); 
00365 
00367 
00368 
00369     void sigsearch(char* sig) throw(DBError);
00370     vector<int> sigsearchNC(char* sig) throw(DBError); // sig search in all; do not change currentList
00371     std::string getSignature(int i) throw(DBError);
00374 
00375 
00376     void gisearch(const char* sql, int complete=0) throw(DBError);
00381     
00382     vector<int>* gisearchNC(const char* sql, int complete=0) throw(DBError);
00387 
00400     void tagsearch(int tag) throw(DBError); 
00401 
00409     void tagsearchSQL(char* query) throw(DBError);
00410     void setTag(int tag, int start=0, int end=0) throw(DBError);
00412 
00415 
00416     void setTagID(int tag, int i) throw(DBError); 
00417 
00424     vector<int> getTagsID(int i, int tag=0) throw(DBError);
00425 
00426     void deleteTag(int tag, int i = -1) throw(DBError); 
00427 
00428 
00437     std::vector<int> getTags(unsigned int i, int tag=0) throw(DBError); // note the order of arguments!
00441     void export_tags(string tag_db_name, vector<int> which_tags);
00442     void import_tags(string tag_db_name);
00443 
00444 
00446 
00447     int snapshot() throw(DBError);
00448     void restore(int handle, bool del=false) throw(DBError);
00449     void delete_snapshot(int handle) throw(DBError);
00450     void delete_all_snapshots() throw(DBError);
00453     // ------- misc ---------------------------------------------------------------
00454     void reset(); 
00455     void resetFormat(std::string ORDERBY="", std::string FORMAT=""); 
00456     int size(); 
00457     int size_all(); 
00458     std::string resultsStr(GameListEntry* gle);
00459 
00461 
00462     std::string currentEntryAsString(int i);
00463     std::vector<std::string> currentEntriesAsStrings(int start=0, int end=0);
00464     std::string getSGF(int i) throw(DBError);
00465     std::string getCurrentProperty(int i, std::string tag) throw (DBError);
00469 
00470 
00471     int plSize();
00472     std::string plEntry(int i);
00476     friend class Algo_finalpos;
00477     friend class Algo_movelist;
00478     friend class Algo_hash_full;
00479     friend class Algo_hash;
00480     friend class Algo_hash_corner;
00481     friend int gis_callback(void *gl, int argc, char **argv, char **azColName);
00482     friend int gis_callbackNC(void *pair_gl_CL, int argc, char **argv, char **azColName);
00483 
00484   private:
00485     void createGamesDB() throw(DBError);
00486     void readDB() throw(DBError);
00487     void addAlgos(bool NEW);
00488     int posDT; // used when parsing the DT, SZ, BR, WR, HA fields during processing
00489     int posSZ;
00490     int posBR;
00491     int posWR;
00492     int posHA;
00493     int SGFtagsSize;
00494     ProcessOptions* p_op;
00495     std::vector<std::string>* SGFtags;
00496     std::string sql_ins_rnp; // sql string to insert root node properties
00497     std::vector<std::string> pl; // list of all players
00498     void readPlayersList() throw(DBError);
00499     std::vector<std::vector<int> >* duplicates;
00500     std::vector<int> process_results_vector;
00501 
00502     int startO();
00503     int start();
00504     int next();
00505     int start_sorted();
00506     int end_sorted();
00507     int get_current_index(int id, int* start); // returns the index in oldList of the game with game id "id" 
00508                                                // (if available, otherwise returns -1),
00509                                                // use this between start_sorted and end_sorted
00510     int get_current_index_CL(int id, int start=0); // returns the index in currentList of the game with game id "id" 
00511                                                    // (if available, otherwise returns -1), requires currentList to
00512                                                    // be sorted wrt first component (see duplicates())
00513     char getCurrentWinner();
00514     std::vector<Candidate* > *getCurrentCandidateList();
00515     void makeCurrentCandidate(std::vector<Candidate* > *candidates);
00516     void makeCurrentHit(std::vector<Hit* > *hits);
00517     void makeIndexCandidate(int index, std::vector<Candidate* > *candidates);
00518     void makeIndexHit(int index, std::vector<Hit* > *hits, vector<int>* CL = 0);
00522     
00523     void setCurrentFromIndex(int index);
00524     void readNumOfWins() throw(DBError);
00525 };
00526 
00527 // ------- duplicates ---------------------------------------------------------
00540 std::map<std::string, std::vector<int> >  find_duplicates(std::vector<string>
00541 glists, bool strict=false, bool dupl_within_db=false) throw(DBError);
00542 
00543 
00544 const int HANDI_TAG = 1;
00545 const int PROFESSIONAL_TAG = 2;
00546 
00547 #endif
00548 
 All Data Structures Files Functions Variables