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