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