Changeset 470

Show
Ignore:
Timestamp:
09/07/09 18:11:52 (15 years ago)
Author:
bhilburn
Message:

Converted the OSSIE CE over to the C++ CBR by creating a child class of
the base CBR. Code is ugly, needs to be cleaned.

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

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/cognitive_engines/OSSIE_DEMO_CE/Makefile.am

    r417 r470  
    66 
    77bin_PROGRAMS = OSSIE_Demo 
    8 include_HEADERS = cbr.c 
    98 
    109OSSIE_Demo_SOURCES = OSSIE_CE.cpp OSSIE_Demo.cpp 
  • vtcross/trunk/src/cognitive_engines/OSSIE_DEMO_CE/OSSIE_CE.cpp

    r465 r470  
    1414#include <math.h> 
    1515 
     16#include "vtcross/cbr.h" 
    1617#include "vtcross/cognitive_engine.h" 
    1718#include "vtcross/common.h" 
     
    2021#include "vtcross/error.h" 
    2122#include "vtcross/socketcomm.h" 
    22 #include "vtcross/cbr.h" 
    23  
    24 // TODO this is really bad; need to move to a proper cbr.h 
    25 #include "cbr.c" 
    26  
    27 #include "sqlite3.h" 
    28 #include "sqlite3ext.h" 
     23 
    2924 
    3025#define TXPOWER 1 
     
    3732#define EVF 20 
    3833 
    39 static cbr myCBR; 
    40  
     34 
     35class ossieCBR : public CBR 
     36{ 
     37    public: 
     38        ossieCBR(); 
     39 
     40        ossieCBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len); 
     41 
     42        ~ossieCBR(){}; 
     43 
     44        int32_t Update(char *_where[], char*_set[], float *_wherevals, float *_setvals,  
     45                unsigned int _wherelen, unsigned int _setlen); 
     46 
     47        int32_t Search(char *_names[], int * _ops, float *_vals, unsigned int _n, 
     48            float *_retvals, int evf); 
     49}; 
     50 
     51 
     52 
     53ossieCBR::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; 
     64    OpenDatabase(); 
     65 
     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 
     87    ExecuteCommand(); 
     88} 
     89 
     90 
     91 
     92// cbr search 
     93int32_t  
     94ossieCBR::Search( 
     95    char *_names[], 
     96    int * _ops, 
     97    float *_vals, 
     98    unsigned int _n, 
     99    float *_retvals, 
     100    int evf) 
     101{    
     102    int rc; 
     103    float lower_limit; 
     104    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; 
     113    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 "); 
     124         
     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 "); 
     137        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; 
     151} 
     152 
     153 
     154// update a row  
     155int32_t  
     156ossieCBR::Update(char *_where[], char*_set[], float *_wherevals, float *_setvals,  
     157                uint32_t _wherelen, uint32_t _setlen) 
     158{ 
     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, ", "); 
     174    } 
     175    
     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} 
     196 
     197static ossieCBR *myCBR; 
    41198 
    42199CognitiveEngine::CognitiveEngine() 
     
    50207CognitiveEngine::~CognitiveEngine() 
    51208{ 
    52     cbr_free(myCBR); 
     209    delete myCBR; 
     210 
    53211    delete [] pList; 
    54212    delete [] oList; 
     
    158316 
    159317    // cbr_update(database,where,set,whereval,setval,..) 
    160     cbr_update(myCBR, nameList, utilList, valList, newUtilityVals,  
     318    myCBR->Update(nameList, utilList, valList, newUtilityVals,  
    161319            numberColumns, utilColumns); 
    162320} 
     
    670828         */ 
    671829        if(strcmp(uList[i].goal.c_str(), "max") == 0) { 
    672             searchOps[i] = GT; 
     830            searchOps[i] = 2; 
    673831        } else if(strcmp(uList[i].goal.c_str(), "min") == 0) { 
    674             searchOps[i] = LT; 
    675         } 
    676     } 
    677  
    678     cbr_print(myCBR); 
     832            searchOps[i] = 4; 
     833        } 
     834    } 
     835 
     836    myCBR->Print(); 
    679837 
    680838    /* CBR specific call */ 
    681     uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
     839    uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals, 
    682840            radioInfo->numUtilities, returnValues, EVF); 
    683841 
     
    740898 
    741899    // Add row to CBR.  
    742     cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 
     900    myCBR->AddRow(allNames, returnValues, returnValueIndex+1); 
    743901 
    744902    return pList; 
     
    792950    cols[columnIndex] = (char *)"utility"; 
    793951 
    794     myCBR = cbr_create(filename, tablename, cols, numberColumns); 
    795 } 
    796  
     952    myCBR = new ossieCBR(filename, tablename, cols, numberColumns); 
     953} 
     954 
  • vtcross/trunk/src/include/vtcross/cbr.h

    r469 r470  
    5151    public: 
    5252        /* Constructors for the CBR class. */ 
    53         CBR(); 
     53        CBR(){}; 
    5454        CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len); 
    5555        CBR(char *_filename, char *_tablename, char *_cols[], \