[411] | 1 | /* |
---|
| 2 | Copyright 2009 Virginia Polytechnic Institute and State University |
---|
| 3 | |
---|
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 5 | you may not use this file except in compliance with the License. |
---|
| 6 | You may obtain a copy of the License at |
---|
| 7 | |
---|
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 9 | |
---|
| 10 | Unless required by applicable law or agreed to in writing, software |
---|
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 13 | See the License for the specific language governing permissions and |
---|
| 14 | limitations under the License. |
---|
| 15 | */ |
---|
| 16 | |
---|
[471] | 17 | /* This file contains the full implementation of the CBR class - the default |
---|
| 18 | * VTCROSS case-based reasoner. |
---|
[468] | 19 | */ |
---|
[98] | 20 | |
---|
[468] | 21 | |
---|
[471] | 22 | |
---|
[161] | 23 | #ifndef CBR_H |
---|
| 24 | #define CBR_H |
---|
| 25 | |
---|
[468] | 26 | #include <cstdlib> |
---|
| 27 | #include <cstring> |
---|
| 28 | #include <cstdio> |
---|
| 29 | #include <stdint.h> |
---|
| 30 | #include <string> |
---|
[469] | 31 | |
---|
[468] | 32 | #include <sqlite3.h> |
---|
[98] | 33 | |
---|
[468] | 34 | #include "vtcross/common.h" |
---|
| 35 | #include "vtcross/debug.h" |
---|
| 36 | #include "vtcross/error.h" |
---|
[98] | 37 | |
---|
[468] | 38 | using namespace std; |
---|
[98] | 39 | |
---|
| 40 | |
---|
[468] | 41 | #define DATABASENAME "vtcross_cbr" |
---|
[98] | 42 | |
---|
| 43 | |
---|
[471] | 44 | /* This is an internal debugging function used by some sqlite3 function calls. |
---|
| 45 | * It is not used otherwise in the VTCROSS codebase. */ |
---|
| 46 | int32_t |
---|
| 47 | callback(void *notUsed, int argc, char **argv, char **azColName) |
---|
| 48 | { |
---|
| 49 | for(size_t i = 0; i < argc; i++) { |
---|
| 50 | LOG("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
---|
| 51 | } |
---|
| 52 | LOG("\n"); |
---|
| 53 | |
---|
| 54 | return 0; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | /* The CBR class is designed to used as either as-is, or as a parent class. All |
---|
| 59 | * functions are declared virtual, and internal members are 'protected' rather |
---|
| 60 | * than private. If you require functionality in a CBR not specifically provided |
---|
| 61 | * by this class, include this header in your source file, create a new class |
---|
| 62 | * that derives from CBR, and implement your desired functionality over the |
---|
| 63 | * original virtual functions as necessary. |
---|
| 64 | */ |
---|
[468] | 65 | class CBR |
---|
| 66 | { |
---|
| 67 | public: |
---|
[471] | 68 | /* Constructors for the CBR class. Note that the default constructor |
---|
| 69 | * must be defined inline here so that super-calls from child classes |
---|
| 70 | * don't fail (i.e. we cannot rely on the compiler-provided constructor. */ |
---|
[470] | 71 | CBR(){}; |
---|
[472] | 72 | CBR(string _filename, string _tablename, string _cols[], uint32_t _len); |
---|
| 73 | CBR(string _filename, string _tablename, string _cols[], \ |
---|
| 74 | string _primcols[], uint32_t _len, uint32_t _primlen); |
---|
[98] | 75 | |
---|
[468] | 76 | /* Destructor for the CBR class. Note that this destructor will be |
---|
| 77 | * called automatically by any derived classes, and so child classes |
---|
| 78 | * should not repeat the freeing actions performed in this function. */ |
---|
| 79 | virtual ~CBR(); |
---|
[98] | 80 | |
---|
[471] | 81 | /* This function opens the VTCROSS database, or if it has not been |
---|
| 82 | * created yet, creates it. */ |
---|
[468] | 83 | virtual int32_t OpenDatabase(); |
---|
| 84 | |
---|
[471] | 85 | /* Execute a sqlite3 command and return the sqlite3 return code. */ |
---|
[468] | 86 | virtual int32_t ExecuteCommand(); |
---|
| 87 | |
---|
[471] | 88 | /* Execute a sqlite3 search command and store the results in the passed |
---|
| 89 | * retvals argument. */ |
---|
[468] | 90 | virtual int32_t ExecuteSearchCommand(float *_retvals); |
---|
| 91 | |
---|
[471] | 92 | /* Print the VTCROSS sqlite3 database. */ |
---|
[468] | 93 | virtual void Print(); |
---|
| 94 | |
---|
[471] | 95 | /* Search the VTCROSS database for specific fields and store the results |
---|
| 96 | * in the passed retvals argument. */ |
---|
[472] | 97 | virtual int32_t Search(string _names[], int32_t *_ops, float *_vals, \ |
---|
[468] | 98 | uint32_t _n, float *_retvals); |
---|
[472] | 99 | virtual int32_t SearchSum(string _name, float *_retvals); |
---|
| 100 | virtual int32_t SearchRand(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ |
---|
[469] | 101 | float *_retvals); |
---|
[468] | 102 | |
---|
[471] | 103 | /* Update a row in the VTCROSS sqlite3 database. */ |
---|
[472] | 104 | virtual int32_t Update(string _where[], string _set[], float *_wherevals, \ |
---|
[468] | 105 | float *_setvals, uint32_t _wherelen, uint32_t _setlen); |
---|
| 106 | |
---|
[471] | 107 | /* Add a row to the VTCROSS sqlite3 database. */ |
---|
[472] | 108 | virtual int32_t AddRow(string _cols[], float *_vals, uint32_t _len); |
---|
[468] | 109 | |
---|
| 110 | protected: |
---|
[472] | 111 | string filename; |
---|
| 112 | string tablename; |
---|
| 113 | string command; |
---|
| 114 | |
---|
[468] | 115 | sqlite3 *db; |
---|
| 116 | uint32_t numColumns; |
---|
| 117 | }; |
---|
| 118 | |
---|
| 119 | |
---|
[472] | 120 | CBR::CBR(string _filename, string _tablename, string _cols[], uint32_t _len) |
---|
[468] | 121 | { |
---|
[472] | 122 | /* Store database properties. */ |
---|
| 123 | filename = _filename; |
---|
| 124 | tablename = _tablename; |
---|
| 125 | numColumns = _len; |
---|
[468] | 126 | |
---|
[472] | 127 | /* Create the database (or open it if it already exists). */ |
---|
[468] | 128 | OpenDatabase(); |
---|
| 129 | |
---|
[472] | 130 | /* Generate the command that will create the initial table within the |
---|
| 131 | * VTCROSS database. */ |
---|
| 132 | command = "CREATE TABLE " + tablename + "("; |
---|
| 133 | for(size_t i = 0; i < numColumns; i++) { |
---|
[496] | 134 | command += _cols[i] + " FLOAT"; |
---|
[468] | 135 | |
---|
[472] | 136 | /* If this column is not the last entry, add a comma to the command in |
---|
| 137 | * preperation for the next entry. */ |
---|
| 138 | if(i != numColumns - 1) |
---|
| 139 | command += ", "; |
---|
[468] | 140 | } |
---|
[472] | 141 | command += ");"; |
---|
[468] | 142 | |
---|
[472] | 143 | /* Execute the generated command. At this point, the database is ready for |
---|
| 144 | * use. */ |
---|
[468] | 145 | ExecuteCommand(); |
---|
| 146 | } |
---|
| 147 | |
---|
[472] | 148 | |
---|
| 149 | CBR::CBR(string _filename, string _tablename, string _cols[], \ |
---|
| 150 | string _primcols[], uint32_t _len, uint32_t _primlen) |
---|
[469] | 151 | { |
---|
[472] | 152 | /* Store database properties. */ |
---|
| 153 | filename = _filename; |
---|
| 154 | tablename = _tablename; |
---|
| 155 | numColumns = _len; |
---|
[468] | 156 | |
---|
[472] | 157 | /* Create the database (or open it if it already exists). */ |
---|
[469] | 158 | OpenDatabase(); |
---|
| 159 | |
---|
[472] | 160 | /* Generate the command that will create the initial table within the |
---|
| 161 | * VTCROSS database with primary keys. */ |
---|
| 162 | command = "CREATE TABLE " + tablename + "("; |
---|
| 163 | for(size_t i = 0; i < numColumns; i++) { |
---|
[497] | 164 | command += _cols[i] + " FLOAT, "; |
---|
[472] | 165 | } |
---|
[469] | 166 | |
---|
[472] | 167 | command += "PRIMARY KEY ("; |
---|
| 168 | for(size_t j = 0; j < _primlen; j++) { |
---|
| 169 | command += _primcols[j]; |
---|
[469] | 170 | |
---|
[472] | 171 | /* If this column is not the last entry, add a comma to the command in |
---|
| 172 | * preperation for the next entry. */ |
---|
| 173 | if(j != _primlen - 1) |
---|
| 174 | command += ", "; |
---|
[469] | 175 | } |
---|
[472] | 176 | command += "));"; |
---|
[469] | 177 | |
---|
[472] | 178 | /* Execute the generated command. At this point, the database is ready for |
---|
| 179 | * use. */ |
---|
[469] | 180 | ExecuteCommand(); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
[468] | 184 | CBR::~CBR() |
---|
| 185 | { |
---|
[472] | 186 | /* Generate the sqlite command to delete a table and all of its contents, |
---|
| 187 | * and then execute it. */ |
---|
| 188 | command = "drop table " + tablename; |
---|
[468] | 189 | ExecuteCommand(); |
---|
| 190 | |
---|
[472] | 191 | /* Tell sqlite to clean up the database. */ |
---|
| 192 | command = "vacuum"; |
---|
[468] | 193 | ExecuteCommand(); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | |
---|
[471] | 197 | int32_t |
---|
| 198 | CBR::OpenDatabase() |
---|
| 199 | { |
---|
[472] | 200 | int32_t rc = sqlite3_open(filename.c_str(), &db); |
---|
[471] | 201 | if(rc) { |
---|
| 202 | WARNING("Can't open database: %s\n", sqlite3_errmsg(db)); |
---|
| 203 | sqlite3_close(db); |
---|
| 204 | exit(1); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | return rc; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | int32_t |
---|
| 212 | CBR::ExecuteCommand() |
---|
| 213 | { |
---|
| 214 | char *zErrMsg = 0; |
---|
| 215 | |
---|
[472] | 216 | int32_t rc = sqlite3_exec(db, command.c_str(), callback, 0, &zErrMsg); |
---|
| 217 | if(rc != SQLITE_OK) { |
---|
| 218 | WARNING("SQL error: %s: %s\n", zErrMsg, command.c_str()); |
---|
[471] | 219 | sqlite3_free(zErrMsg); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | return rc; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | int32_t |
---|
| 227 | CBR::ExecuteSearchCommand(float *_retvals) |
---|
| 228 | { |
---|
[472] | 229 | sqlite3_stmt *pStatement; |
---|
[471] | 230 | |
---|
[472] | 231 | int32_t rc = sqlite3_prepare_v2(db, command.c_str(), -1, &pStatement, NULL); |
---|
| 232 | if(rc == SQLITE_OK) { |
---|
| 233 | if(sqlite3_step(pStatement) == SQLITE_ROW) { |
---|
| 234 | for(size_t i = 0; i < numColumns; ++i) { |
---|
[471] | 235 | _retvals[i] = sqlite3_column_double(pStatement, i); |
---|
[472] | 236 | } |
---|
[471] | 237 | } else { |
---|
[472] | 238 | LOG("CBR:: No matched results returning default.\n"); |
---|
| 239 | rc = 31337; |
---|
[471] | 240 | } |
---|
| 241 | } else { |
---|
[472] | 242 | WARNING("CBR:: Error executing SQL statement. rc = %i\n%s\n", rc, command.c_str()); |
---|
[471] | 243 | } |
---|
| 244 | |
---|
| 245 | sqlite3_finalize(pStatement); |
---|
| 246 | |
---|
| 247 | return rc; |
---|
| 248 | } |
---|
[472] | 249 | |
---|
| 250 | |
---|
[468] | 251 | void |
---|
| 252 | CBR::Print() |
---|
| 253 | { |
---|
[472] | 254 | /* Generate the sqlite command to print the database, which is effectively a |
---|
| 255 | * 'select all elements' command, and then execute it. */ |
---|
| 256 | command = "select " + tablename + ".* from " + tablename + ";"; |
---|
[468] | 257 | |
---|
| 258 | ExecuteCommand(); |
---|
[472] | 259 | LOG("database %s, table %s:\n", filename.c_str(), tablename.c_str()); |
---|
[468] | 260 | } |
---|
| 261 | |
---|
[472] | 262 | |
---|
[468] | 263 | int32_t |
---|
[472] | 264 | CBR::Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ |
---|
| 265 | float *_retvals) |
---|
[468] | 266 | { |
---|
| 267 | char str_buffer[64]; |
---|
[472] | 268 | const string ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; |
---|
| 269 | |
---|
| 270 | command = "select " + tablename + ".* from " + tablename + " where "; |
---|
| 271 | |
---|
| 272 | for(size_t i = 0; i < _n; i++) { |
---|
| 273 | /* Make sure that the passed ops value is valid. */ |
---|
| 274 | if((_ops[i] < 0) || (_ops[i] > 5)) { |
---|
| 275 | ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
---|
[468] | 276 | } |
---|
| 277 | |
---|
[476] | 278 | command += _names[i] + ops_str[_ops[i]]; |
---|
[468] | 279 | |
---|
| 280 | |
---|
[472] | 281 | sprintf(str_buffer, "%E", _vals[i]); |
---|
| 282 | command += string(str_buffer); |
---|
| 283 | |
---|
| 284 | if(i < _n - 1) |
---|
| 285 | command += " AND "; |
---|
[468] | 286 | else |
---|
[472] | 287 | command += " order by utility desc;"; |
---|
[507] | 288 | |
---|
[468] | 289 | } |
---|
[507] | 290 | LOG("CBR::Search - command: %s\n", command.c_str()); |
---|
[468] | 291 | |
---|
[472] | 292 | return ExecuteSearchCommand(_retvals); |
---|
[468] | 293 | } |
---|
| 294 | |
---|
[472] | 295 | |
---|
[469] | 296 | int32_t |
---|
[472] | 297 | CBR::SearchSum(string _name, float *_retvals) |
---|
[469] | 298 | { |
---|
[472] | 299 | command = "select SUM(" + tablename + "." + _name + ") from " + tablename + ";"; |
---|
[468] | 300 | |
---|
[472] | 301 | return ExecuteSearchCommand(_retvals); |
---|
[469] | 302 | } |
---|
| 303 | |
---|
| 304 | |
---|
| 305 | int32_t |
---|
[472] | 306 | CBR::SearchRand(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ |
---|
| 307 | float *_retvals) |
---|
[469] | 308 | { |
---|
| 309 | char str_buffer[64]; |
---|
[472] | 310 | const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; |
---|
| 311 | |
---|
| 312 | command = "select " + tablename + ".* from " + tablename + " where "; |
---|
| 313 | |
---|
| 314 | for(size_t i = 0; i < _n; i++) { |
---|
| 315 | /* Make sure that the passed ops value is valid. */ |
---|
| 316 | if((_ops[i] < 0) || (_ops[i] > 5)) { |
---|
| 317 | ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
---|
[469] | 318 | } |
---|
| 319 | |
---|
[476] | 320 | command += _names[i] + ops_str[_ops[i]]; |
---|
[472] | 321 | |
---|
[469] | 322 | sprintf(str_buffer, "%E", _vals[i]); |
---|
[472] | 323 | command += str_buffer; |
---|
[469] | 324 | |
---|
[472] | 325 | if(i < _n - 1) |
---|
| 326 | command += " AND "; |
---|
[469] | 327 | else |
---|
[472] | 328 | command += " order by RAND();"; |
---|
[469] | 329 | } |
---|
| 330 | |
---|
[472] | 331 | return ExecuteSearchCommand(_retvals); |
---|
[469] | 332 | } |
---|
| 333 | |
---|
| 334 | |
---|
[468] | 335 | int32_t |
---|
[472] | 336 | CBR::Update(string _where[], string _set[], float *_wherevals, float *_setvals, |
---|
| 337 | uint32_t _wherelen, uint32_t _setlen) |
---|
[468] | 338 | { |
---|
[472] | 339 | char str_buffer[64]; |
---|
| 340 | |
---|
| 341 | /* Generate the command to update the table. */ |
---|
| 342 | command = "UPDATE " + tablename + " SET "; |
---|
| 343 | |
---|
| 344 | for(size_t i = 0; i < _setlen; i++) { |
---|
| 345 | command += _set[i] + " = "; |
---|
| 346 | sprintf(str_buffer, "%f", _setvals[i]); |
---|
| 347 | command += string(str_buffer) + " "; |
---|
| 348 | |
---|
| 349 | if(i != _setlen - 1) |
---|
| 350 | command += ", "; |
---|
[468] | 351 | } |
---|
[472] | 352 | command += " WHERE "; |
---|
[468] | 353 | |
---|
[472] | 354 | for(size_t j = 0; j < _wherelen; j++) { |
---|
| 355 | command += _where[j] + " = "; |
---|
| 356 | sprintf(str_buffer, "%f", _wherevals[j]); |
---|
| 357 | command += string(str_buffer) + " "; |
---|
| 358 | |
---|
| 359 | if(j != _wherelen - 1) |
---|
| 360 | command += "AND "; |
---|
[468] | 361 | } |
---|
[472] | 362 | command += ";"; |
---|
[468] | 363 | |
---|
[472] | 364 | return ExecuteCommand(); |
---|
[468] | 365 | } |
---|
| 366 | |
---|
[472] | 367 | |
---|
[468] | 368 | int32_t |
---|
[472] | 369 | CBR::AddRow(string _cols[], float *_vals, uint32_t _len) |
---|
[468] | 370 | { |
---|
[472] | 371 | char str_buffer[64]; |
---|
| 372 | |
---|
| 373 | command = "insert into " + tablename + " ("; |
---|
| 374 | |
---|
| 375 | for(size_t i = 0; i < _len; i++) { |
---|
| 376 | command += _cols[i]; |
---|
| 377 | |
---|
| 378 | if(i != numColumns - 1) |
---|
| 379 | command += ", "; |
---|
[468] | 380 | } |
---|
[472] | 381 | command += ") values("; |
---|
[468] | 382 | |
---|
[472] | 383 | for(size_t j = 0; j < _len; j++) { |
---|
| 384 | // TODO I have no idea what the below question is about. |
---|
[469] | 385 | // ???? how to fill the values if numColumns != _len |
---|
[468] | 386 | // assume = in the following |
---|
[472] | 387 | sprintf(str_buffer, "%f", _vals[j]); |
---|
| 388 | command += str_buffer; |
---|
| 389 | |
---|
| 390 | if(j != numColumns - 1) |
---|
| 391 | command += ", "; |
---|
[468] | 392 | } |
---|
[472] | 393 | command += ");"; |
---|
[468] | 394 | |
---|
[472] | 395 | return ExecuteCommand(); |
---|
[468] | 396 | } |
---|
| 397 | |
---|
[161] | 398 | #endif |
---|