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