Changeset 477

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

The OSSIE CBR is now properly using strings.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/cognitive_engines/OSSIE_DEMO_CE/OSSIE_CE.cpp

    r470 r477  
    1313#include <stdint.h> 
    1414#include <math.h> 
     15#include <string> 
    1516 
    1617#include "vtcross/cbr.h" 
     
    3334 
    3435 
     36/* ossieCBR Class  
     37 * 
     38 * Derives from the default VTCROSS CBR class to add the following functionality 
     39 * necessary for the OSSIE demo: 
     40 * 
     41 * TODO 
     42 */ 
    3543class ossieCBR : public CBR 
    3644{ 
     
    3846        ossieCBR(); 
    3947 
    40         ossieCBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len); 
     48        ossieCBR(string _filename, string _tablename, string _cols[], uint32_t _len); 
    4149 
    4250        ~ossieCBR(){}; 
    4351 
    44         int32_t Update(char *_where[], char*_set[], float *_wherevals, float *_setvals,  
     52        int32_t Update(string _where[], string _set[], float *_wherevals, float *_setvals, \ 
    4553                unsigned int _wherelen, unsigned int _setlen); 
    4654 
    47         int32_t Search(char *_names[], int * _ops, float *_vals, unsigned int _n, 
    48             float *_retvals, int evf); 
     55        int32_t Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ 
     56                float *_retvals, int32_t evf); 
    4957}; 
    5058 
    5159 
    52  
    53 ossieCBR::ossieCBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len) 
    54 { 
    55     // create database 
    56  
    57     // copy filename 
    58     unsigned int i=0; 
    59     strcpy(filename, _filename); 
    60  
    61     // execute create database command 
    62     // database handle 
    63     //db = NULL; 
     60ossieCBR::ossieCBR(string _filename, string _tablename, string _cols[], uint32_t _len) 
     61{ 
     62    /* Store database properties. */ 
     63    filename = _filename; 
     64    tablename = _tablename; 
     65    numColumns = _len; 
     66 
     67    /* Create the database (or open it if it already exists). */ 
    6468    OpenDatabase(); 
    6569 
    66     // create table 
    67  
    68     // copy tablename 
    69     strcpy(tablename, _tablename); 
    70  
    71     // number of columns in the table 
    72     numColumns = _len; 
    73  
    74     // generate command 
    75     strcpy(command, "CREATE TABLE "); 
    76     strcat(command, tablename); 
    77     strcat(command, "("); 
    78     for (i=0; i<numColumns; i++) { 
    79         strcat(command, _cols[i]); 
    80         strcat(command, " FLOAT"); 
    81         if (i != numColumns-1) // not last entry 
    82             strcat(command, ", "); 
    83     } 
    84     strcat(command, ", timestamp DATE, PRIMARY KEY(tx_power));"); 
    85  
    86     // execute create table command 
     70    /* Generate the command that will create the initial table within the 
     71     * VTCROSS database. */ 
     72    command = "CREATE TABLE " + tablename + "("; 
     73    for(size_t i = 0; i < numColumns; i++) { 
     74        command += _cols[i] + " FLOAT"; 
     75 
     76        /* If this column is not the last entry, add a comma to the command in 
     77         * preperation for the next entry. */ 
     78        if(i != numColumns - 1) 
     79            command += ", "; 
     80    } 
     81    command += ", timestamp DATE, PRIMARY KEY(tx_power));"; 
     82 
     83    /* Execute the generated command. At this point, the database is ready for 
     84     * use. */ 
    8785    ExecuteCommand(); 
    8886} 
    8987 
    9088 
    91  
    92 // cbr search 
    9389int32_t  
    94 ossieCBR::Search( 
    95     char *_names[], 
    96     int * _ops, 
    97     float *_vals, 
    98     unsigned int _n, 
    99     float *_retvals, 
    100     int evf) 
     90ossieCBR::Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ 
     91        float *_retvals, int32_t evf) 
    10192{    
    102     int rc; 
    10393    float lower_limit; 
    10494    float upper_limit; 
    105     // generate command 
    106     strcpy(command, "select "); 
    107     strcat(command, tablename); 
    108     strcat(command, ".* from "); 
    109     strcat(command, tablename); 
    110     strcat(command, " where "); 
    111  
    112     unsigned int i; 
    11395    char str_buffer[64]; 
    114     //printf("number of ops %d:\n", _n); 
    115     for (i=0; i<_n; i++) { 
    116         // ensure valid ops value 
    117         if (_ops[i] < 0 || _ops[i] > 5) { 
    118             printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); 
    119             exit(1); 
    120         } 
    121  
    122         strcat(command, _names[i]); 
    123         strcat(command, " between "); 
     96 
     97    command = "select " + tablename + ".* from " + tablename + " where "; 
     98 
     99    for(size_t i = 0; i < _n; i++) { 
     100        /* Make sure that the passed ops value is valid. */ 
     101        if((_ops[i] < 0) || (_ops[i] > 5)) { 
     102            ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]); 
     103        } 
     104 
     105        command += _names[i] + " between "; 
    124106         
    125         lower_limit = _vals[i]*(1-((float)evf/100)); 
    126         upper_limit = _vals[i]*(1+((float)evf/100)); 
    127  
    128         printf("%f %f %f\n", lower_limit, upper_limit, _vals[i]); 
    129  
    130         sprintf(command, "%s%f", command, lower_limit); 
    131         strcat(command, " and "); 
    132         sprintf(command, "%s%f", command, upper_limit); 
    133  
    134  
    135         if (i<_n-1) 
    136             strcat(command, " AND "); 
     107        lower_limit = _vals[i] * (1 - ((float) evf / 100)); 
     108        upper_limit = _vals[i] * (1 + ((float) evf / 100)); 
     109 
     110            LOG("%f %f %f\n", lower_limit, upper_limit, _vals[i]); 
     111 
     112        sprintf(str_buffer, "%f", lower_limit); 
     113        command += string(str_buffer) + " and "; 
     114        sprintf(str_buffer, "%f", upper_limit); 
     115        command += string(str_buffer); 
     116 
     117        if(i < _n - 1) 
     118            command += " AND "; 
    137119        else 
    138             strcat(command, " order by abs(utility) asc, timestamp desc;"); 
    139     } 
    140  
    141     printf("search command: %s\n", command); 
    142  
    143     rc = ExecuteSearchCommand(_retvals); 
    144      
    145     printf("search result: "); 
    146     for (i=0; i<numColumns +1; i++) 
    147         printf("%f, ",_retvals[i]); 
    148     printf("\n"); 
    149  
    150     return rc; 
     120            command += " order by abs(utility) asc, timestamp desc;"; 
     121    } 
     122 
     123    LOG("Search command: %s\n", command.c_str()); 
     124 
     125    return ExecuteSearchCommand(_retvals); 
    151126} 
    152127 
     
    154129// update a row  
    155130int32_t  
    156 ossieCBR::Update(char *_where[], char*_set[], float *_wherevals, float *_setvals,  
     131ossieCBR::Update(string _where[], string _set[], float *_wherevals, float *_setvals,  
    157132                uint32_t _wherelen, uint32_t _setlen) 
    158133{ 
    159     unsigned int i; 
    160      
    161     // generate command 
    162     //printf("%s\n", command); 
    163     strcpy(command, "UPDATE "); 
    164     strcat(command, tablename); 
    165      
    166     strcat(command, " SET "); 
    167     for (i=0; i<_setlen; i++) { 
    168         strcat(command, _set[i]); 
    169         strcat(command, " = "); 
    170         sprintf(command, "%s%f", command, _setvals[i]); 
    171         strcat(command, "  "); 
    172         if (i != _setlen-1) // not last entry 
    173             strcat(command, ", "); 
     134    char str_buffer[64]; 
     135 
     136    command = "UPDATE " + tablename + " SET "; 
     137 
     138    for(size_t i = 0; i < _setlen; i++) { 
     139        command += _set[i] + " = "; 
     140 
     141        sprintf(str_buffer, "%f", _setvals[i]); 
     142        command += string(str_buffer) + "  "; 
     143 
     144        if(i != _setlen - 1) 
     145            command += ", "; 
    174146    } 
    175147    
    176     strcat(command, ", timestamp = DATETIME('NOW')"); 
    177      
    178     strcat(command, " WHERE "); 
    179  
    180     for (i=0; i<_wherelen; i++) { 
    181         strcat(command, _where[i]); 
    182         strcat(command, " = "); 
    183         sprintf(command, "%s%f", command, _wherevals[i]); 
    184         strcat(command, "  "); 
    185         if (i != _wherelen-1) // not last entry 
    186             strcat(command, "AND "); 
    187     } 
    188     strcat(command, ";"); 
    189      
    190     //printf("update command: %s\n", command); 
    191     // execute add command 
    192     ExecuteCommand(); 
    193  
    194     return 0; 
    195 } 
     148    command += ", timestamp = DATETIME('NOW') WHERE "; 
     149     
     150    for(size_t j = 0; j < _wherelen; j++) { 
     151        command += _where[j] + " = "; 
     152 
     153        sprintf(str_buffer, "%f", _wherevals[j]); 
     154        command += string(str_buffer) + "  "; 
     155 
     156        if(j != _wherelen - 1) 
     157            command += "AND "; 
     158    } 
     159    command += ";"; 
     160     
     161    return ExecuteCommand(); 
     162} 
     163 
     164 
     165/******************** END DEFINITION OF OSSIE CBR CLASS ****************/ 
     166 
    196167 
    197168static ossieCBR *myCBR; 
     169 
    198170 
    199171CognitiveEngine::CognitiveEngine() 
     
    270242   LOG("Cognitive Engine:: Receiving feedback.\n"); 
    271243     
    272     uint32_t numberColumns =  
    273         radioInfo->numParameters; 
     244    uint32_t numberColumns = radioInfo->numParameters; 
    274245 
    275246    uint32_t utilColumns = radioInfo->numUtilities + 1; 
     
    277248    float valList[numberColumns]; 
    278249    float newUtilityVals[numberColumns]; 
    279     char *nameList[numberColumns]; 
    280     char *utilList[utilColumns]; 
     250    string nameList[numberColumns]; 
     251    string utilList[utilColumns]; 
    281252 
    282253    size_t columnUtilIndex = 0; 
    283254    for (size_t i = 0; i < radioInfo->numUtilities; i++){ 
    284         utilList[columnUtilIndex] = (char*)uList[i].name.c_str(); 
     255        utilList[columnUtilIndex] = uList[i].name; 
    285256        columnUtilIndex++; 
    286257    }   
    287     utilList[columnUtilIndex] = (char*) "utility"; 
     258    utilList[columnUtilIndex] = "utility"; 
    288259 
    289260    size_t columnIndex = 0; 
    290261    for (size_t i = 0; i < radioInfo->numParameters; i++){ 
    291         nameList[columnIndex] = (char*)parameters[i].name.c_str(); 
     262        nameList[columnIndex] = parameters[i].name; 
    292263        columnIndex++; 
    293264    }    
     
    799770    LOG("Cognitive Engine:: Generating solution.\n"); 
    800771 
    801     char *searchNames[radioInfo->numObservables]; 
     772    string searchNames[radioInfo->numObservables]; 
    802773 
    803774    for(size_t i = 0; i < radioInfo->numObservables; i++) { 
    804         searchNames[i] = (char*)observables[i].name.c_str(); 
     775        searchNames[i] = observables[i].name; 
    805776    } 
    806777 
     
    811782    } 
    812783 
    813     uint32_t numberColumns =  
    814         radioInfo->numUtilities + 
    815         radioInfo->numParameters + 
    816         radioInfo->numObservables + 1; 
     784    uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \ 
     785                             radioInfo->numObservables + 1; 
    817786     
    818787    float returnValues[numberColumns]; 
     
    879848    returnValues[returnValueIndex] = 0; 
    880849 
    881     char *allNames[numberColumns]; 
     850    string allNames[numberColumns]; 
    882851    size_t allNameIndex = 0; 
    883852    for(size_t i = 0; i < radioInfo->numUtilities; i++) { 
    884         allNames[allNameIndex] = (char*)uList[i].name.c_str(); 
     853        allNames[allNameIndex] = uList[i].name; 
    885854        returnValues[allNameIndex] = uList[i].target; 
    886855        allNameIndex++; 
    887856    } 
    888857    for(size_t i = 0; i < radioInfo->numParameters; i++) { 
    889         allNames[allNameIndex] = (char*)pList[i].name.c_str(); 
     858        allNames[allNameIndex] = pList[i].name; 
    890859        allNameIndex++; 
    891860    } 
    892861    for(size_t i = 0; i < radioInfo->numObservables; i++) { 
    893         allNames[allNameIndex] = (char*)oList[i].name.c_str(); 
     862        allNames[allNameIndex] = oList[i].name; 
    894863        returnValues[allNameIndex] = observables[i].value; 
    895864        allNameIndex++; 
    896865    } 
    897     allNames[allNameIndex] = (char *) "utility"; 
     866    allNames[allNameIndex] = "utility"; 
    898867 
    899868    // Add row to CBR.  
     
    925894CognitiveEngine::BuildCognitiveEngine() 
    926895{ 
    927     char filename[] = {"ex1"}; 
    928     char tablename[] = {"data"}; 
    929  
    930     uint32_t numberColumns =  
    931         radioInfo->numUtilities + 
    932         radioInfo->numParameters + 
    933         radioInfo->numObservables + 1; 
    934  
    935     char *cols[numberColumns]; 
     896    string filename = "ex1"; 
     897    string tablename = "data"; 
     898 
     899    uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \ 
     900                             radioInfo->numObservables + 1; 
     901 
     902    string cols[numberColumns]; 
    936903 
    937904    size_t columnIndex = 0; 
    938905    for (size_t i = 0; i < radioInfo->numUtilities; i++){ 
    939         cols[columnIndex] = (char*)uList[i].name.c_str(); 
     906        cols[columnIndex] = uList[i].name; 
    940907        columnIndex++; 
    941908    }    
    942909    for (size_t i = 0; i < radioInfo->numParameters; i++){ 
    943         cols[columnIndex] = (char*)pList[i].name.c_str(); 
     910        cols[columnIndex] = pList[i].name; 
    944911        columnIndex++; 
    945912    }    
    946913    for (size_t i = 0; i < radioInfo->numObservables; i++){ 
    947         cols[columnIndex] = (char*)oList[i].name.c_str(); 
     914        cols[columnIndex] = oList[i].name; 
    948915        columnIndex++; 
    949916    }    
    950     cols[columnIndex] = (char *)"utility"; 
     917    cols[columnIndex] = "utility"; 
    951918 
    952919    myCBR = new ossieCBR(filename, tablename, cols, numberColumns);