Show
Ignore:
Timestamp:
09/07/09 17:28:20 (15 years ago)
Author:
bhilburn
Message:

Moved over the necessary CBR functionality for DSA_CE using the new C++
classes. Note that CBR code is messy and needs to be cleaned up.

Files:
1 modified

Legend:

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

    r468 r469  
    3434#include <stdint.h> 
    3535#include <string> 
     36 
    3637#include <sqlite3.h> 
    3738 
     
    4950{ 
    5051    public: 
    51         /* Default constructor and preferred constructor for the CBR class. */ 
     52        /* Constructors for the CBR class. */ 
    5253        CBR(); 
    5354        CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len); 
     55        CBR(char *_filename, char *_tablename, char *_cols[], \ 
     56                char *_primcols[], uint32_t _len, uint32_t  _primlen); 
    5457 
    5558        /* Destructor for the CBR class. Note that this destructor will be 
     
    6871        virtual int32_t Search(char *_names[], int32_t *_ops, float *_vals, \ 
    6972                uint32_t _n, float *_retvals); 
     73        virtual int32_t SearchSum(char *_name, float *_retvals); 
     74        virtual int32_t SearchRand(char *_names[], int * _ops, float *_vals, unsigned int _n, \ 
     75            float *_retvals); 
    7076 
    7177        virtual int32_t Update(char *_where[], char *_set[], float *_wherevals, \ 
     
    167173    // execute create database command 
    168174    // database handle 
    169     //_cbr->db = NULL; 
     175    //db = NULL; 
    170176    OpenDatabase(); 
    171177 
     
    193199    ExecuteCommand(); 
    194200} 
     201 
     202// create database 
     203CBR::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 
    195247 
    196248 
     
    276328} 
    277329 
     330// cbr search for a sum 
     331int32_t  
     332CBR::SearchSum( 
     333    char *_name, 
     334    float *_retvals) 
     335{    
     336    int rc; 
     337    // generate command 
     338    strcpy(command, "select SUM( "); 
     339    strcat(command, tablename); 
     340    strcat(command, "."); 
     341    strcat(command, _name); 
     342    strcat(command, ") from "); 
     343    strcat(command, tablename); 
     344    strcat(command, ";"); 
     345 
     346    //printf("search command: %s\n", command); 
     347 
     348    rc = ExecuteSearchCommand(_retvals); 
     349    /*printf("search result: "); 
     350    for (int i=0; i<numColumns; i++) 
     351        printf("%f, ",_retvals[i]); 
     352    printf("\n"); 
     353    */ 
     354    return rc; 
     355} 
     356 
     357 
     358// cbr search 
     359int32_t  
     360CBR::SearchRand( 
     361    char *_names[], 
     362    int * _ops, 
     363    float *_vals, 
     364    unsigned int _n, 
     365    float *_retvals) 
     366{    
     367        const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; 
     368    int rc; 
     369    // generate command 
     370    strcpy(command, "select "); 
     371    strcat(command, tablename); 
     372    strcat(command, ".* from "); 
     373    strcat(command, tablename); 
     374    strcat(command, " where "); 
     375 
     376    unsigned int i; 
     377    char str_buffer[64]; 
     378    for (i=0; i<_n; i++) { 
     379        // ensure valid ops value 
     380        if (_ops[i] < 0 || _ops[i] > 5) { 
     381            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); 
     382            exit(1); 
     383        } 
     384 
     385        strcat(command, _names[i]); 
     386        strcat(command, ops_str[_ops[i]]); 
     387        sprintf(str_buffer, "%E", _vals[i]); 
     388        strcat(command, str_buffer); 
     389 
     390        if (i<_n-1) 
     391            strcat(command, " AND "); 
     392        else 
     393            strcat(command, " order by RAND();"); 
     394    } 
     395 
     396    //printf("search command: %s\n", command); 
     397 
     398    rc = ExecuteSearchCommand(_retvals); 
     399     
     400    /*printf("search result: "); 
     401    for (i=0; i<numColumns; i++) 
     402        printf("%f, ",_retvals[i]); 
     403    printf("\n"); 
     404*/ 
     405    return rc; 
     406} 
     407 
     408 
     409 
    278410 
    279411// update a row  
     
    335467    strcat(command, " values("); 
    336468    for (i=0; i<_len; i++) { 
    337         // ???? how to fill the values if _cbr->numColumns != _len 
     469        // ???? how to fill the values if numColumns != _len 
    338470        // assume = in the following 
    339471        sprintf(command, "%s%f", command, _vals[i]);