- Timestamp:
- 09/07/09 17:28:20 (15 years ago)
- 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 18 18 #include <cstring> 19 19 #include <stdint.h> 20 #include <math.h> 21 20 #include <cmath> 21 22 #include "vtcross/cbr.h" 22 23 #include "vtcross/cognitive_engine.h" 23 24 #include "vtcross/common.h" … … 27 28 #include "vtcross/error.h" 28 29 #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 36 31 37 32 #define INTERFERENCE 0 … … 45 40 46 41 47 48 static cbr myCBR; 42 static CBR *myCBR; 49 43 50 44 … … 60 54 CognitiveEngine::~CognitiveEngine() 61 55 { 62 cbr_free(myCBR);56 delete myCBR; 63 57 delete [] pList; 64 58 delete [] oList; … … 161 155 std::string channel_name = "channel"; 162 156 searchNames[0] = (char *) channel_name.c_str(); 163 searchOps[0] = EQ;157 searchOps[0] = 0; 164 158 searchVals[0] = parameters[0].value; 165 159 166 160 /* Execute CBR search and put output into returnValues */ 167 cbr_search(myCBR,searchNames, searchOps, searchVals,161 myCBR->Search(searchNames, searchOps, searchVals, 168 162 1, returnValues); 169 163 … … 197 191 } 198 192 199 cbr_update(myCBR,nameList, obsList, valList, obsVals,193 myCBR->Update(nameList, obsList, valList, obsVals, 200 194 numberColumns, obsColumns); 201 195 } … … 697 691 698 692 searchNames[0] = (char*) pList[0].name.c_str(); 699 searchOps[0] = EQ;693 searchOps[0] = 0; 700 694 searchVals[0] = i+1; 701 695 702 uint32_t rc = cbr_search(myCBR,searchNames, searchOps, searchVals,696 uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals, 703 697 1, returnValues); 704 698 … … 728 722 /* Add the new optimized set to the CBR database */ 729 723 730 cbr_add_row(myCBR,rowNames, returnValues, numberColumns);724 myCBR->AddRow(rowNames, returnValues, numberColumns); 731 725 } 732 726 … … 737 731 // Get sum of all the channel utilities. 738 732 sumSearchName = (char *) utility_name.c_str(); 739 uint32_t rc = cbr_search_sum(myCBR,sumSearchName, sumRetVals);733 uint32_t rc = myCBR->SearchSum(sumSearchName, sumRetVals); 740 734 741 735 // Psuedo random channel selection based upon utility. … … 752 746 753 747 searchNames[0] = (char*) pList[0].name.c_str(); 754 searchOps[0] = EQ;748 searchOps[0] = 0; 755 749 searchVals[0] = channel; 756 750 757 rc = cbr_search(myCBR,searchNames, searchOps, searchVals,751 rc = myCBR->Search(searchNames, searchOps, searchVals, 758 752 1, returnValues); 759 753 … … 800 794 801 795 /* 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); 803 797 804 798 … … 863 857 cols[columnIndex] = (char *)utility_name.c_str(); 864 858 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 20 20 21 21 bin_PROGRAMS = DSA_Demo 22 include_HEADERS = cbr.c23 22 24 23 DSA_Demo_SOURCES = DSA_CognitiveEngine.cpp DSA_Demo.cpp -
vtcross/trunk/src/include/vtcross/cbr.h
r468 r469 34 34 #include <stdint.h> 35 35 #include <string> 36 36 37 #include <sqlite3.h> 37 38 … … 49 50 { 50 51 public: 51 /* Default constructor and preferred constructorfor the CBR class. */52 /* Constructors for the CBR class. */ 52 53 CBR(); 53 54 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); 54 57 55 58 /* Destructor for the CBR class. Note that this destructor will be … … 68 71 virtual int32_t Search(char *_names[], int32_t *_ops, float *_vals, \ 69 72 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); 70 76 71 77 virtual int32_t Update(char *_where[], char *_set[], float *_wherevals, \ … … 167 173 // execute create database command 168 174 // database handle 169 // _cbr->db = NULL;175 //db = NULL; 170 176 OpenDatabase(); 171 177 … … 193 199 ExecuteCommand(); 194 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 195 247 196 248 … … 276 328 } 277 329 330 // cbr search for a sum 331 int32_t 332 CBR::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 359 int32_t 360 CBR::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 278 410 279 411 // update a row … … 335 467 strcat(command, " values("); 336 468 for (i=0; i<_len; i++) { 337 // ???? how to fill the values if _cbr->numColumns != _len469 // ???? how to fill the values if numColumns != _len 338 470 // assume = in the following 339 471 sprintf(command, "%s%f", command, _vals[i]);