[76] | 1 | // |
---|
| 2 | // Case-based reasoner |
---|
| 3 | // |
---|
| 4 | |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | #include <stdio.h> |
---|
| 8 | |
---|
[130] | 9 | #include "vtcross/cbr.h" |
---|
[136] | 10 | #include "vtcross/common.h" |
---|
[76] | 11 | |
---|
| 12 | |
---|
| 13 | struct cbr_s { |
---|
| 14 | char filename[64]; |
---|
| 15 | char tablename[64]; |
---|
| 16 | char command[2048]; |
---|
| 17 | sqlite3 *db; |
---|
| 18 | unsigned int num_columns; |
---|
| 19 | }; |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | // open a database or create a database if it does not exist |
---|
| 23 | int OpenDatabase(cbr _cbr){ |
---|
| 24 | int rc; |
---|
| 25 | //sqlite3 **db; |
---|
| 26 | |
---|
[79] | 27 | //printf("database name: %s\n", _cbr->filename); |
---|
[76] | 28 | //rc = sqlite3_open(_cbr->filename, db); |
---|
| 29 | rc = sqlite3_open(_cbr->filename, &(_cbr->db)); |
---|
| 30 | if (rc) { |
---|
| 31 | //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db)); |
---|
| 32 | fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db)); |
---|
| 33 | sqlite3_close(_cbr->db); |
---|
| 34 | exit(1); |
---|
| 35 | } else{ |
---|
[79] | 36 | //printf("database opened.\n"); |
---|
[76] | 37 | } |
---|
| 38 | //_cbr->db = *db; |
---|
| 39 | return rc; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | // simple callback function, display result |
---|
| 44 | int callback(void *notUsed, int argc, char **argv, char **azColName){ |
---|
| 45 | int i; |
---|
| 46 | for(i=0; i<argc; i++){ |
---|
| 47 | printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
---|
| 48 | } |
---|
| 49 | printf("\n"); |
---|
| 50 | return 0; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | // execute command |
---|
| 55 | int ExecuteCommand(cbr _cbr){ |
---|
| 56 | int rc; |
---|
| 57 | char *zErrMsg = 0; |
---|
| 58 | |
---|
| 59 | //printf("command: %s\n", _cbr->command); |
---|
| 60 | rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); |
---|
| 61 | if( rc!=SQLITE_OK){ |
---|
| 62 | fprintf(stderr, "SQL error: %s\n", zErrMsg); |
---|
| 63 | sqlite3_free(zErrMsg); |
---|
| 64 | } else{ |
---|
| 65 | //printf("command executed.\n"); |
---|
| 66 | } |
---|
| 67 | return rc; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | // execute search command |
---|
| 72 | int ExecuteSearchCommand(cbr _cbr, float *_retvals){ |
---|
| 73 | int rc; |
---|
| 74 | //char *zErrMsg = 0; |
---|
| 75 | unsigned int i; |
---|
| 76 | |
---|
| 77 | //printf("command: %s\n", _cbr->command); |
---|
| 78 | sqlite3_stmt * pStatement; |
---|
| 79 | rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL); |
---|
[88] | 80 | //if (rc == SQLITE_OK && sqlite3_step(pStatement) == SQLITE_ROW){ |
---|
| 81 | if (rc == SQLITE_OK){ |
---|
| 82 | if (sqlite3_step(pStatement) == SQLITE_ROW){ |
---|
| 83 | printf("there is search return data\n"); |
---|
[76] | 84 | for (i=0; i<_cbr->num_columns; ++i) |
---|
| 85 | _retvals[i] = sqlite3_column_double(pStatement, i); |
---|
[88] | 86 | } else { |
---|
| 87 | printf("no matched rearch results. use default values\n"); |
---|
[89] | 88 | |
---|
[88] | 89 | _retvals[0] = 100; // throughput |
---|
| 90 | _retvals[1] = 1; // spectral_efficiency |
---|
| 91 | _retvals[2] = -2; // log10_ber |
---|
| 92 | _retvals[3] = 1; // mod_scheme |
---|
| 93 | _retvals[4] = -10; // tx_power |
---|
| 94 | _retvals[5] = 10; // snr |
---|
| 95 | _retvals[6] = 0.5; // utility |
---|
| 96 | } |
---|
[76] | 97 | } |
---|
[88] | 98 | |
---|
[76] | 99 | sqlite3_finalize(pStatement); |
---|
| 100 | |
---|
| 101 | /*rc = sqlite3_exec(_cbr->db, _cbr->command, SearchCallback, 0, &zErrMsg); |
---|
| 102 | if( rc!=SQLITE_OK){ |
---|
| 103 | fprintf(stderr, "SQL error: %s\n", zErrMsg); |
---|
| 104 | sqlite3_free(zErrMsg); |
---|
| 105 | } else{ |
---|
| 106 | printf("command executed.\n"); |
---|
| 107 | }*/ |
---|
| 108 | |
---|
| 109 | return rc; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | // create database |
---|
| 114 | cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len) |
---|
| 115 | { |
---|
| 116 | // cbr is a pointer to struct cbr_s |
---|
| 117 | cbr _cbr = (cbr) malloc(sizeof(struct cbr_s)); |
---|
| 118 | |
---|
| 119 | // create database |
---|
| 120 | |
---|
| 121 | // copy filename |
---|
| 122 | unsigned int i=0; |
---|
| 123 | strcpy(_cbr->filename, _filename); |
---|
| 124 | |
---|
| 125 | // execute create database command |
---|
| 126 | // database handle |
---|
| 127 | //_cbr->db = NULL; |
---|
| 128 | OpenDatabase(_cbr); |
---|
| 129 | |
---|
| 130 | // create table |
---|
| 131 | |
---|
| 132 | // copy tablename |
---|
| 133 | strcpy(_cbr->tablename, _tablename); |
---|
| 134 | |
---|
| 135 | // number of columns in the table |
---|
| 136 | _cbr->num_columns = _len; |
---|
| 137 | |
---|
| 138 | // generate command |
---|
| 139 | strcpy(_cbr->command, "CREATE TABLE "); |
---|
| 140 | strcat(_cbr->command, _cbr->tablename); |
---|
| 141 | strcat(_cbr->command, "("); |
---|
| 142 | for (i=0; i<_cbr->num_columns; i++) { |
---|
| 143 | strcat(_cbr->command, _cols[i]); |
---|
| 144 | strcat(_cbr->command, " FLOAT"); |
---|
| 145 | if (i != _cbr->num_columns-1) // not last entry |
---|
| 146 | strcat(_cbr->command, ", "); |
---|
| 147 | } |
---|
| 148 | strcat(_cbr->command, ");"); |
---|
| 149 | |
---|
| 150 | // execute create table command |
---|
| 151 | ExecuteCommand(_cbr); |
---|
| 152 | |
---|
| 153 | return _cbr; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | // free space |
---|
| 158 | void cbr_free(cbr _cbr) |
---|
| 159 | { |
---|
| 160 | // generate command, remove a table with its content |
---|
| 161 | strcpy(_cbr->command, "drop table "); |
---|
| 162 | strcat(_cbr->command, _cbr->tablename); |
---|
| 163 | |
---|
| 164 | // execute delete command |
---|
| 165 | ExecuteCommand(_cbr); |
---|
| 166 | |
---|
| 167 | // clean the database |
---|
| 168 | strcpy(_cbr->command, "vacuum"); |
---|
| 169 | ExecuteCommand(_cbr); |
---|
| 170 | |
---|
| 171 | free(_cbr); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | // print |
---|
| 176 | void cbr_print(cbr _cbr) |
---|
| 177 | { |
---|
| 178 | // generate commandi |
---|
| 179 | strcpy(_cbr->command, "select "); |
---|
| 180 | strcat(_cbr->command, _cbr->tablename); |
---|
| 181 | strcat(_cbr->command, ".* from "); |
---|
| 182 | strcat(_cbr->command, _cbr->tablename); |
---|
| 183 | strcat(_cbr->command, ";"); |
---|
| 184 | |
---|
| 185 | // execute print (select all) command |
---|
| 186 | ExecuteCommand(_cbr); |
---|
| 187 | //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | /*//static |
---|
| 192 | int cbr_callback(void *notUsed, int argc, char **argv, char **azColName) |
---|
| 193 | { |
---|
| 194 | return 0; |
---|
| 195 | }*/ |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | const char * ops_str[] = { |
---|
| 199 | "==", "!=", ">", ">=", "<", "<="}; |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | // cbr search |
---|
| 203 | int cbr_search( |
---|
| 204 | cbr _cbr, |
---|
| 205 | char *_names[], |
---|
| 206 | int * _ops, |
---|
| 207 | float *_vals, |
---|
| 208 | unsigned int _n, |
---|
| 209 | float *_retvals) |
---|
| 210 | { |
---|
| 211 | // generate command |
---|
| 212 | strcpy(_cbr->command, "select "); |
---|
| 213 | strcat(_cbr->command, _cbr->tablename); |
---|
| 214 | strcat(_cbr->command, ".* from "); |
---|
| 215 | strcat(_cbr->command, _cbr->tablename); |
---|
| 216 | strcat(_cbr->command, " where "); |
---|
| 217 | |
---|
| 218 | unsigned int i; |
---|
| 219 | char str_buffer[64]; |
---|
[88] | 220 | //printf("number of ops %d:\n", _n); |
---|
[76] | 221 | for (i=0; i<_n; i++) { |
---|
| 222 | // ensure valid ops value |
---|
| 223 | if (_ops[i] < 0 || _ops[i] > 5) { |
---|
| 224 | printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
---|
| 225 | exit(1); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | strcat(_cbr->command, _names[i]); |
---|
| 229 | strcat(_cbr->command, ops_str[_ops[i]]); |
---|
| 230 | sprintf(str_buffer, "%E", _vals[i]); |
---|
| 231 | strcat(_cbr->command, str_buffer); |
---|
| 232 | |
---|
| 233 | if (i<_n-1) |
---|
| 234 | strcat(_cbr->command, " AND "); |
---|
| 235 | else |
---|
| 236 | strcat(_cbr->command, " order by utility desc;"); |
---|
| 237 | } |
---|
| 238 | |
---|
[79] | 239 | //printf("search command: %s\n", _cbr->command); |
---|
[77] | 240 | |
---|
[76] | 241 | //ExecuteCommand(_cbr); |
---|
| 242 | ExecuteSearchCommand(_cbr, _retvals); |
---|
| 243 | |
---|
| 244 | /*printf("search result: "); |
---|
| 245 | for (i=0; i<_cbr->num_columns; i++) |
---|
| 246 | printf("%f, ",_retvals[i]); |
---|
| 247 | printf("\n");*/ |
---|
| 248 | |
---|
| 249 | return 0; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | // cbr add a row |
---|
| 254 | int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len) |
---|
| 255 | { |
---|
| 256 | unsigned int i; |
---|
| 257 | |
---|
| 258 | // generate command |
---|
| 259 | //printf("%s\n", _cbr->command); |
---|
| 260 | strcpy(_cbr->command, "insert into "); |
---|
| 261 | strcat(_cbr->command, _cbr->tablename); |
---|
| 262 | |
---|
| 263 | strcat(_cbr->command, " ("); |
---|
| 264 | for (i=0; i<_len; i++) { |
---|
| 265 | strcat(_cbr->command, _cols[i]); |
---|
| 266 | if (i != _cbr->num_columns-1) // not last entry |
---|
| 267 | strcat(_cbr->command, ", "); |
---|
| 268 | } |
---|
| 269 | strcat(_cbr->command, ") "); |
---|
| 270 | |
---|
| 271 | strcat(_cbr->command, " values("); |
---|
| 272 | for (i=0; i<_len; i++) { |
---|
| 273 | // ???? how to fill the values if _cbr->num_columns != _len |
---|
| 274 | // assume = in the following |
---|
| 275 | sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]); |
---|
| 276 | if (i != _cbr->num_columns-1) // not last entry |
---|
| 277 | strcat(_cbr->command, ", "); |
---|
| 278 | } |
---|
| 279 | strcat(_cbr->command, ");"); |
---|
| 280 | |
---|
| 281 | // execute add command |
---|
| 282 | ExecuteCommand(_cbr); |
---|
| 283 | |
---|
| 284 | return 0; |
---|
| 285 | } |
---|
| 286 | |
---|