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