Changeset 477 for vtcross/trunk/src
- Timestamp:
- 09/09/09 14:01:14 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/trunk/src/cognitive_engines/OSSIE_DEMO_CE/OSSIE_CE.cpp
r470 r477 13 13 #include <stdint.h> 14 14 #include <math.h> 15 #include <string> 15 16 16 17 #include "vtcross/cbr.h" … … 33 34 34 35 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 */ 35 43 class ossieCBR : public CBR 36 44 { … … 38 46 ossieCBR(); 39 47 40 ossieCBR( char * _filename, char * _tablename, char * _cols[], unsigned int _len);48 ossieCBR(string _filename, string _tablename, string _cols[], uint32_t _len); 41 49 42 50 ~ossieCBR(){}; 43 51 44 int32_t Update( char *_where[], char*_set[], float *_wherevals, float *_setvals,52 int32_t Update(string _where[], string _set[], float *_wherevals, float *_setvals, \ 45 53 unsigned int _wherelen, unsigned int _setlen); 46 54 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); 49 57 }; 50 58 51 59 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; 60 ossieCBR::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). */ 64 68 OpenDatabase(); 65 69 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. */ 87 85 ExecuteCommand(); 88 86 } 89 87 90 88 91 92 // cbr search93 89 int32_t 94 ossieCBR::Search( 95 char *_names[], 96 int * _ops, 97 float *_vals, 98 unsigned int _n, 99 float *_retvals, 100 int evf) 90 ossieCBR::Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ 91 float *_retvals, int32_t evf) 101 92 { 102 int rc;103 93 float lower_limit; 104 94 float upper_limit; 105 // generate command106 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 95 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 "; 124 106 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 "; 137 119 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); 151 126 } 152 127 … … 154 129 // update a row 155 130 int32_t 156 ossieCBR::Update( char *_where[], char*_set[], float *_wherevals, float *_setvals,131 ossieCBR::Update(string _where[], string _set[], float *_wherevals, float *_setvals, 157 132 uint32_t _wherelen, uint32_t _setlen) 158 133 { 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 += ", "; 174 146 } 175 147 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 196 167 197 168 static ossieCBR *myCBR; 169 198 170 199 171 CognitiveEngine::CognitiveEngine() … … 270 242 LOG("Cognitive Engine:: Receiving feedback.\n"); 271 243 272 uint32_t numberColumns = 273 radioInfo->numParameters; 244 uint32_t numberColumns = radioInfo->numParameters; 274 245 275 246 uint32_t utilColumns = radioInfo->numUtilities + 1; … … 277 248 float valList[numberColumns]; 278 249 float newUtilityVals[numberColumns]; 279 char *nameList[numberColumns];280 char *utilList[utilColumns];250 string nameList[numberColumns]; 251 string utilList[utilColumns]; 281 252 282 253 size_t columnUtilIndex = 0; 283 254 for (size_t i = 0; i < radioInfo->numUtilities; i++){ 284 utilList[columnUtilIndex] = (char*)uList[i].name.c_str();255 utilList[columnUtilIndex] = uList[i].name; 285 256 columnUtilIndex++; 286 257 } 287 utilList[columnUtilIndex] = (char*)"utility";258 utilList[columnUtilIndex] = "utility"; 288 259 289 260 size_t columnIndex = 0; 290 261 for (size_t i = 0; i < radioInfo->numParameters; i++){ 291 nameList[columnIndex] = (char*)parameters[i].name.c_str();262 nameList[columnIndex] = parameters[i].name; 292 263 columnIndex++; 293 264 } … … 799 770 LOG("Cognitive Engine:: Generating solution.\n"); 800 771 801 char *searchNames[radioInfo->numObservables];772 string searchNames[radioInfo->numObservables]; 802 773 803 774 for(size_t i = 0; i < radioInfo->numObservables; i++) { 804 searchNames[i] = (char*)observables[i].name.c_str();775 searchNames[i] = observables[i].name; 805 776 } 806 777 … … 811 782 } 812 783 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; 817 786 818 787 float returnValues[numberColumns]; … … 879 848 returnValues[returnValueIndex] = 0; 880 849 881 char *allNames[numberColumns];850 string allNames[numberColumns]; 882 851 size_t allNameIndex = 0; 883 852 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 884 allNames[allNameIndex] = (char*)uList[i].name.c_str();853 allNames[allNameIndex] = uList[i].name; 885 854 returnValues[allNameIndex] = uList[i].target; 886 855 allNameIndex++; 887 856 } 888 857 for(size_t i = 0; i < radioInfo->numParameters; i++) { 889 allNames[allNameIndex] = (char*)pList[i].name.c_str();858 allNames[allNameIndex] = pList[i].name; 890 859 allNameIndex++; 891 860 } 892 861 for(size_t i = 0; i < radioInfo->numObservables; i++) { 893 allNames[allNameIndex] = (char*)oList[i].name.c_str();862 allNames[allNameIndex] = oList[i].name; 894 863 returnValues[allNameIndex] = observables[i].value; 895 864 allNameIndex++; 896 865 } 897 allNames[allNameIndex] = (char *)"utility";866 allNames[allNameIndex] = "utility"; 898 867 899 868 // Add row to CBR. … … 925 894 CognitiveEngine::BuildCognitiveEngine() 926 895 { 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]; 936 903 937 904 size_t columnIndex = 0; 938 905 for (size_t i = 0; i < radioInfo->numUtilities; i++){ 939 cols[columnIndex] = (char*)uList[i].name.c_str();906 cols[columnIndex] = uList[i].name; 940 907 columnIndex++; 941 908 } 942 909 for (size_t i = 0; i < radioInfo->numParameters; i++){ 943 cols[columnIndex] = (char*)pList[i].name.c_str();910 cols[columnIndex] = pList[i].name; 944 911 columnIndex++; 945 912 } 946 913 for (size_t i = 0; i < radioInfo->numObservables; i++){ 947 cols[columnIndex] = (char*)oList[i].name.c_str();914 cols[columnIndex] = oList[i].name; 948 915 columnIndex++; 949 916 } 950 cols[columnIndex] = (char *)"utility";917 cols[columnIndex] = "utility"; 951 918 952 919 myCBR = new ossieCBR(filename, tablename, cols, numberColumns);