Changeset 469

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.

Location:
vtcross/trunk/src
Files:
1 removed
3 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp

    r465 r469  
    1818#include <cstring> 
    1919#include <stdint.h> 
    20 #include <math.h> 
    21  
     20#include <cmath> 
     21 
     22#include "vtcross/cbr.h" 
    2223#include "vtcross/cognitive_engine.h" 
    2324#include "vtcross/common.h" 
     
    2728#include "vtcross/error.h" 
    2829#include "vtcross/socketcomm.h" 
    29 #include "vtcross/cbr.h" 
    30  
    31 // TODO this is really bad; need to move to a proper cbr.h 
    32 #include "cbr.c" 
    33  
    34 #include "sqlite3.h" 
    35 #include "sqlite3ext.h" 
     30 
    3631 
    3732#define INTERFERENCE 0 
     
    4540 
    4641 
    47  
    48 static cbr myCBR; 
     42static CBR *myCBR; 
    4943 
    5044 
     
    6054CognitiveEngine::~CognitiveEngine() 
    6155{ 
    62     cbr_free(myCBR); 
     56    delete myCBR; 
    6357    delete [] pList; 
    6458    delete [] oList; 
     
    161155    std::string channel_name = "channel"; 
    162156    searchNames[0] = (char *) channel_name.c_str(); 
    163     searchOps[0] = EQ; 
     157    searchOps[0] = 0; 
    164158    searchVals[0] = parameters[0].value;  
    165159 
    166160    /* Execute CBR search and put output into returnValues */ 
    167     cbr_search(myCBR, searchNames, searchOps, searchVals, 
     161    myCBR->Search(searchNames, searchOps, searchVals, 
    168162            1, returnValues); 
    169163 
     
    197191    } 
    198192 
    199     cbr_update(myCBR, nameList, obsList, valList, obsVals,  
     193    myCBR->Update(nameList, obsList, valList, obsVals,  
    200194            numberColumns, obsColumns); 
    201195} 
     
    697691 
    698692        searchNames[0] = (char*) pList[0].name.c_str(); 
    699         searchOps[0] = EQ; 
     693        searchOps[0] = 0; 
    700694        searchVals[0] = i+1;  
    701695     
    702         uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
     696        uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals, 
    703697            1, returnValues); 
    704698 
     
    728722                /* Add the new optimized set to the CBR database */ 
    729723 
    730                 cbr_add_row(myCBR, rowNames, returnValues, numberColumns); 
     724                myCBR->AddRow(rowNames, returnValues, numberColumns); 
    731725        } 
    732726 
     
    737731    // Get sum of all the channel utilities. 
    738732    sumSearchName = (char *) utility_name.c_str(); 
    739     uint32_t rc = cbr_search_sum(myCBR, sumSearchName, sumRetVals); 
     733    uint32_t rc = myCBR->SearchSum(sumSearchName, sumRetVals); 
    740734 
    741735    // Psuedo random channel selection based upon utility. 
     
    752746         
    753747    searchNames[0] = (char*) pList[0].name.c_str(); 
    754     searchOps[0] = EQ; 
     748    searchOps[0] = 0; 
    755749    searchVals[0] = channel;  
    756750   
    757     rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
     751    rc = myCBR->Search(searchNames, searchOps, searchVals, 
    758752        1, returnValues); 
    759753 
     
    800794 
    801795    /* Add the new optimized set to the CBR database */ 
    802     cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 
     796    myCBR->AddRow(allNames, returnValues, returnValueIndex+1); 
    803797 
    804798 
     
    863857    cols[columnIndex] = (char *)utility_name.c_str(); 
    864858 
    865     myCBR = cbr_create_with_primary(filename, tablename, cols, paramCols, numberColumns, radioInfo->numParameters); 
    866 } 
    867  
     859    myCBR = new CBR(filename, tablename, cols, paramCols, numberColumns, radioInfo->numParameters); 
     860} 
     861 
  • vtcross/trunk/src/cognitive_engines/DSA_CE/Makefile.am

    r411 r469  
    2020 
    2121bin_PROGRAMS = DSA_Demo 
    22 include_HEADERS = cbr.c 
    2322 
    2423DSA_Demo_SOURCES = DSA_CognitiveEngine.cpp DSA_Demo.cpp 
  • 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]);