Changeset 471

Show
Ignore:
Timestamp:
09/09/09 14:01:11 (15 years ago)
Author:
bhilburn
Message:

Larger-block reformats. About to start converting from c-strings to
std::string.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/include/vtcross/cbr.h

    r470 r471  
    1515*/ 
    1616 
    17 /* This file contains the declaration *and* default implementation of a VTCROSS 
    18  * case-based reasoner. 
    19  * 
    20  * This class can be used either as-is, or with re-implementations of some/all 
    21  * of the functions.  All functions are declared virtual, and so may be 
    22  * overridden in the cognitive engine source file as necessary. In addition, 
    23  * internal class members are declared 'protected', so that they may be 
    24  * inherited by child implementations. 
     17/* This file contains the full implementation of the CBR class - the default 
     18 * VTCROSS case-based reasoner. 
    2519 */ 
     20 
    2621 
    2722 
     
    4742 
    4843 
     44/* This is an internal debugging function used by some sqlite3 function calls. 
     45 * It is not used otherwise in the VTCROSS codebase. */ 
     46int32_t  
     47callback(void *notUsed, int argc, char **argv, char **azColName) 
     48{ 
     49    for(size_t i = 0; i < argc; i++) { 
     50        LOG("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 
     51    } 
     52    LOG("\n"); 
     53 
     54    return 0; 
     55} 
     56 
     57 
     58/* The CBR class is designed to used as either as-is, or as a parent class.  All 
     59 * functions are declared virtual, and internal members are 'protected' rather 
     60 * than private. If you require functionality in a CBR not specifically provided 
     61 * by this class, include this header in your source file, create a new class 
     62 * that derives from CBR, and implement your desired functionality over the 
     63 * original virtual functions as necessary. 
     64 */ 
    4965class CBR 
    5066{ 
    5167    public: 
    52         /* Constructors for the CBR class. */ 
     68        /* Constructors for the CBR class. Note that the default constructor 
     69         * must be defined inline here so that super-calls from child classes 
     70         * don't fail (i.e. we cannot rely on the compiler-provided constructor. */ 
    5371        CBR(){}; 
    5472        CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len); 
    5573        CBR(char *_filename, char *_tablename, char *_cols[], \ 
    56                 char *_primcols[], uint32_t _len, uint32_t  _primlen); 
     74                char *_primcols[], uint32_t _len, uint32_t _primlen); 
    5775 
    5876        /* Destructor for the CBR class. Note that this destructor will be 
     
    6179        virtual ~CBR(); 
    6280 
     81        /* This function opens the VTCROSS database, or if it has not been 
     82         * created yet, creates it. */ 
    6383        virtual int32_t OpenDatabase(); 
    6484 
     85        /* Execute a sqlite3 command and return the sqlite3 return code. */ 
    6586        virtual int32_t ExecuteCommand(); 
    6687 
     88        /* Execute a sqlite3 search command and store the results in the passed 
     89         * retvals argument. */ 
    6790        virtual int32_t ExecuteSearchCommand(float *_retvals); 
    6891 
     92        /* Print the VTCROSS sqlite3 database. */ 
    6993        virtual void Print(); 
    7094 
     95        /* Search the VTCROSS database for specific fields and store the results 
     96         * in the passed retvals argument. */ 
    7197        virtual int32_t Search(char *_names[], int32_t *_ops, float *_vals, \ 
    7298                uint32_t _n, float *_retvals); 
    7399        virtual int32_t SearchSum(char *_name, float *_retvals); 
    74         virtual int32_t SearchRand(char *_names[], int * _ops, float *_vals, unsigned int _n, \ 
     100        virtual int32_t SearchRand(char *_names[], int *_ops, float *_vals, uint32_t _n, \ 
    75101            float *_retvals); 
    76102 
     103        /* Update a row in the VTCROSS sqlite3 database. */ 
    77104        virtual int32_t Update(char *_where[], char *_set[], float *_wherevals, \ 
    78105                float *_setvals, uint32_t _wherelen, uint32_t _setlen); 
    79106 
     107        /* Add a row to the VTCROSS sqlite3 database. */ 
    80108        virtual int32_t AddRow(char *_cols[], float *_vals, uint32_t _len); 
    81109 
     
    89117 
    90118 
    91 int32_t  
    92 callback(void *notUsed, int argc, char **argv, char **azColName) 
    93 { 
    94     int i; 
    95     for(i=0; i<argc; i++){ 
    96         printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 
    97     } 
    98     printf("\n"); 
    99     return 0; 
    100 } 
    101  
    102  
    103 // open a database or create a database if it does not exist 
     119CBR::CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len) 
     120{ 
     121    // copy filename 
     122    unsigned int i=0; 
     123    strcpy(filename, _filename); 
     124 
     125    // execute create database command 
     126    // database handle 
     127    //db = NULL; 
     128    OpenDatabase(); 
     129 
     130    // create table 
     131 
     132    // copy tablename 
     133    strcpy(tablename, _tablename); 
     134 
     135    // number of columns in the table 
     136    numColumns = _len; 
     137 
     138    // generate command 
     139    strcpy(command, "CREATE TABLE "); 
     140    strcat(command, tablename); 
     141    strcat(command, "("); 
     142    for (i=0; i<numColumns; i++) { 
     143        strcat(command, _cols[i]); 
     144        strcat(command, " FLOAT"); 
     145        if (i != numColumns-1) // not last entry 
     146            strcat(command, ", "); 
     147    } 
     148    strcat(command, ");"); 
     149 
     150    // execute create table command 
     151    ExecuteCommand(); 
     152} 
     153 
     154CBR::CBR(char * _filename, char * _tablename,  
     155        char * _cols[], char * _primcols[], unsigned int _len, 
     156        unsigned int _primlen) 
     157{ 
     158    // create database 
     159 
     160    // copy filename 
     161    unsigned int i=0; 
     162    strcpy(filename, _filename); 
     163 
     164    // execute create database command 
     165    // database handle 
     166    //db = NULL; 
     167    OpenDatabase(); 
     168 
     169    // create table 
     170 
     171    // copy tablename 
     172    strcpy(tablename, _tablename); 
     173 
     174    // number of columns in the table 
     175    numColumns = _len; 
     176 
     177    // generate command 
     178    strcpy(command, "CREATE TABLE "); 
     179    strcat(command, tablename); 
     180    strcat(command, "("); 
     181    for (i=0; i<numColumns; i++) { 
     182        strcat(command, _cols[i]); 
     183        strcat(command, " FLOAT"); 
     184        strcat(command, ", "); 
     185    } 
     186   strcat(command, "PRIMARY KEY ("); 
     187    for (i=0; i<_primlen; i++) { 
     188        strcat(command, _primcols[i]); 
     189        if (i != _primlen-1) // not last entry 
     190            strcat(command, ", "); 
     191    } 
     192    strcat(command, "));"); 
     193 
     194    // execute create table command 
     195    ExecuteCommand(); 
     196} 
     197 
     198 
     199 
     200CBR::~CBR() 
     201 
     202    // generate command, remove a table with its content 
     203    strcpy(command, "drop table "); 
     204    strcat(command, tablename); 
     205 
     206    // execute delete command 
     207    ExecuteCommand(); 
     208 
     209    // clean the database 
     210    strcpy(command, "vacuum"); 
     211    ExecuteCommand(); 
     212} 
     213 
     214 
     215 
     216 
    104217int32_t  
    105218CBR::OpenDatabase() 
     
    118231 
    119232 
    120 // execute command 
    121233int32_t 
    122234CBR::ExecuteCommand() 
     
    135247 
    136248 
    137 // execute search command 
    138249int32_t  
    139250CBR::ExecuteSearchCommand(float *_retvals) 
     
    160271    return rc; 
    161272} 
    162  
    163  
    164 // create database 
    165 CBR::CBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len) 
    166 { 
    167     // create database 
    168  
    169     // copy filename 
    170     unsigned int i=0; 
    171     strcpy(filename, _filename); 
    172  
    173     // execute create database command 
    174     // database handle 
    175     //db = NULL; 
    176     OpenDatabase(); 
    177  
    178     // create table 
    179  
    180     // copy tablename 
    181     strcpy(tablename, _tablename); 
    182  
    183     // number of columns in the table 
    184     numColumns = _len; 
    185  
    186     // generate command 
    187     strcpy(command, "CREATE TABLE "); 
    188     strcat(command, tablename); 
    189     strcat(command, "("); 
    190     for (i=0; i<numColumns; i++) { 
    191         strcat(command, _cols[i]); 
    192         strcat(command, " FLOAT"); 
    193         if (i != numColumns-1) // not last entry 
    194             strcat(command, ", "); 
    195     } 
    196     strcat(command, ");"); 
    197  
    198     // execute create table command 
    199     ExecuteCommand(); 
    200 } 
    201  
    202 // create database 
    203 CBR::CBR(char * _filename, char * _tablename,  
    204         char * _cols[], char * _primcols[], unsigned int _len, 
    205         unsigned int _primlen) 
    206 { 
    207     // create database 
    208  
    209     // copy filename 
    210     unsigned int i=0; 
    211     strcpy(filename, _filename); 
    212  
    213     // execute create database command 
    214     // database handle 
    215     //db = NULL; 
    216     OpenDatabase(); 
    217  
    218     // create table 
    219  
    220     // copy tablename 
    221     strcpy(tablename, _tablename); 
    222  
    223     // number of columns in the table 
    224     numColumns = _len; 
    225  
    226     // generate command 
    227     strcpy(command, "CREATE TABLE "); 
    228     strcat(command, tablename); 
    229     strcat(command, "("); 
    230     for (i=0; i<numColumns; i++) { 
    231         strcat(command, _cols[i]); 
    232         strcat(command, " FLOAT"); 
    233         strcat(command, ", "); 
    234     } 
    235    strcat(command, "PRIMARY KEY ("); 
    236     for (i=0; i<_primlen; i++) { 
    237         strcat(command, _primcols[i]); 
    238         if (i != _primlen-1) // not last entry 
    239             strcat(command, ", "); 
    240     } 
    241     strcat(command, "));"); 
    242  
    243     // execute create table command 
    244     ExecuteCommand(); 
    245 } 
    246  
    247  
    248  
    249 // free space 
    250 CBR::~CBR() 
    251  
    252     // generate command, remove a table with its content 
    253     strcpy(command, "drop table "); 
    254     strcat(command, tablename); 
    255  
    256     // execute delete command 
    257     ExecuteCommand(); 
    258  
    259     // clean the database 
    260     strcpy(command, "vacuum"); 
    261     ExecuteCommand(); 
    262 } 
    263  
    264  
    265 // print  
    266273void  
    267274CBR::Print() 
     
    279286} 
    280287 
    281 // 
    282 // cbr search 
    283 //int cbr_search(cbr _cbr, char *_names[], int * _ops, float *_vals, unsigned int _n, float *_retvals); 
    284288int32_t  
    285289CBR::Search( 
     
    328332} 
    329333 
    330 // cbr search for a sum 
    331334int32_t  
    332335CBR::SearchSum( 
     
    356359 
    357360 
    358 // cbr search 
    359361int32_t  
    360362CBR::SearchRand( 
     
    409411 
    410412 
    411 // update a row  
    412413int32_t  
    413414CBR::Update(char *_where[], char*_set[], float *_wherevals, float *_setvals,  
     
    447448} 
    448449 
    449 // cbr add a row 
    450450int32_t  
    451451CBR::AddRow(char *_cols[], float *_vals, unsigned int _len)