17 | | /* This file contains the declaration *and* default implementation of a VTCROSS |
18 | | * case-based reasoner. |
19 | | * |
20 | | * This class can be used either as-is, or with re-implementations of some/all |
21 | | * of the functions. All functions are declared virtual, and so may be |
22 | | * overridden in the cognitive engine source file as necessary. In addition, |
23 | | * internal class members are declared 'protected', so that they may be |
24 | | * inherited by child implementations. |
| 17 | /* This file contains the full implementation of the CBR class - the default |
| 18 | * VTCROSS case-based reasoner. |
| 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 | */ |
91 | | int32_t |
92 | | callback(void *notUsed, int argc, char **argv, char **azColName) |
93 | | { |
94 | | int i; |
95 | | for(i=0; i<argc; i++){ |
96 | | printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
97 | | } |
98 | | printf("\n"); |
99 | | return 0; |
100 | | } |
101 | | |
102 | | |
103 | | // open a database or create a database if it does not exist |
| 119 | CBR::CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len) |
| 120 | { |
| 121 | // copy filename |
| 122 | unsigned int i=0; |
| 123 | strcpy(filename, _filename); |
| 124 | |
| 125 | // execute create database command |
| 126 | // database handle |
| 127 | //db = NULL; |
| 128 | OpenDatabase(); |
| 129 | |
| 130 | // create table |
| 131 | |
| 132 | // copy tablename |
| 133 | strcpy(tablename, _tablename); |
| 134 | |
| 135 | // number of columns in the table |
| 136 | numColumns = _len; |
| 137 | |
| 138 | // generate command |
| 139 | strcpy(command, "CREATE TABLE "); |
| 140 | strcat(command, tablename); |
| 141 | strcat(command, "("); |
| 142 | for (i=0; i<numColumns; i++) { |
| 143 | strcat(command, _cols[i]); |
| 144 | strcat(command, " FLOAT"); |
| 145 | if (i != numColumns-1) // not last entry |
| 146 | strcat(command, ", "); |
| 147 | } |
| 148 | strcat(command, ");"); |
| 149 | |
| 150 | // execute create table command |
| 151 | ExecuteCommand(); |
| 152 | } |
| 153 | |
| 154 | CBR::CBR(char * _filename, char * _tablename, |
| 155 | char * _cols[], char * _primcols[], unsigned int _len, |
| 156 | unsigned int _primlen) |
| 157 | { |
| 158 | // create database |
| 159 | |
| 160 | // copy filename |
| 161 | unsigned int i=0; |
| 162 | strcpy(filename, _filename); |
| 163 | |
| 164 | // execute create database command |
| 165 | // database handle |
| 166 | //db = NULL; |
| 167 | OpenDatabase(); |
| 168 | |
| 169 | // create table |
| 170 | |
| 171 | // copy tablename |
| 172 | strcpy(tablename, _tablename); |
| 173 | |
| 174 | // number of columns in the table |
| 175 | numColumns = _len; |
| 176 | |
| 177 | // generate command |
| 178 | strcpy(command, "CREATE TABLE "); |
| 179 | strcat(command, tablename); |
| 180 | strcat(command, "("); |
| 181 | for (i=0; i<numColumns; i++) { |
| 182 | strcat(command, _cols[i]); |
| 183 | strcat(command, " FLOAT"); |
| 184 | strcat(command, ", "); |
| 185 | } |
| 186 | strcat(command, "PRIMARY KEY ("); |
| 187 | for (i=0; i<_primlen; i++) { |
| 188 | strcat(command, _primcols[i]); |
| 189 | if (i != _primlen-1) // not last entry |
| 190 | strcat(command, ", "); |
| 191 | } |
| 192 | strcat(command, "));"); |
| 193 | |
| 194 | // execute create table command |
| 195 | ExecuteCommand(); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | CBR::~CBR() |
| 201 | { |
| 202 | // generate command, remove a table with its content |
| 203 | strcpy(command, "drop table "); |
| 204 | strcat(command, tablename); |
| 205 | |
| 206 | // execute delete command |
| 207 | ExecuteCommand(); |
| 208 | |
| 209 | // clean the database |
| 210 | strcpy(command, "vacuum"); |
| 211 | ExecuteCommand(); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
162 | | |
163 | | |
164 | | // create database |
165 | | CBR::CBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len) |
166 | | { |
167 | | // create database |
168 | | |
169 | | // copy filename |
170 | | unsigned int i=0; |
171 | | strcpy(filename, _filename); |
172 | | |
173 | | // execute create database command |
174 | | // database handle |
175 | | //db = NULL; |
176 | | OpenDatabase(); |
177 | | |
178 | | // create table |
179 | | |
180 | | // copy tablename |
181 | | strcpy(tablename, _tablename); |
182 | | |
183 | | // number of columns in the table |
184 | | numColumns = _len; |
185 | | |
186 | | // generate command |
187 | | strcpy(command, "CREATE TABLE "); |
188 | | strcat(command, tablename); |
189 | | strcat(command, "("); |
190 | | for (i=0; i<numColumns; i++) { |
191 | | strcat(command, _cols[i]); |
192 | | strcat(command, " FLOAT"); |
193 | | if (i != numColumns-1) // not last entry |
194 | | strcat(command, ", "); |
195 | | } |
196 | | strcat(command, ");"); |
197 | | |
198 | | // execute create table command |
199 | | ExecuteCommand(); |
200 | | } |
201 | | |
202 | | // create database |
203 | | CBR::CBR(char * _filename, char * _tablename, |
204 | | char * _cols[], char * _primcols[], unsigned int _len, |
205 | | unsigned int _primlen) |
206 | | { |
207 | | // create database |
208 | | |
209 | | // copy filename |
210 | | unsigned int i=0; |
211 | | strcpy(filename, _filename); |
212 | | |
213 | | // execute create database command |
214 | | // database handle |
215 | | //db = NULL; |
216 | | OpenDatabase(); |
217 | | |
218 | | // create table |
219 | | |
220 | | // copy tablename |
221 | | strcpy(tablename, _tablename); |
222 | | |
223 | | // number of columns in the table |
224 | | numColumns = _len; |
225 | | |
226 | | // generate command |
227 | | strcpy(command, "CREATE TABLE "); |
228 | | strcat(command, tablename); |
229 | | strcat(command, "("); |
230 | | for (i=0; i<numColumns; i++) { |
231 | | strcat(command, _cols[i]); |
232 | | strcat(command, " FLOAT"); |
233 | | strcat(command, ", "); |
234 | | } |
235 | | strcat(command, "PRIMARY KEY ("); |
236 | | for (i=0; i<_primlen; i++) { |
237 | | strcat(command, _primcols[i]); |
238 | | if (i != _primlen-1) // not last entry |
239 | | strcat(command, ", "); |
240 | | } |
241 | | strcat(command, "));"); |
242 | | |
243 | | // execute create table command |
244 | | ExecuteCommand(); |
245 | | } |
246 | | |
247 | | |
248 | | |
249 | | // free space |
250 | | CBR::~CBR() |
251 | | { |
252 | | // generate command, remove a table with its content |
253 | | strcpy(command, "drop table "); |
254 | | strcat(command, tablename); |
255 | | |
256 | | // execute delete command |
257 | | ExecuteCommand(); |
258 | | |
259 | | // clean the database |
260 | | strcpy(command, "vacuum"); |
261 | | ExecuteCommand(); |
262 | | } |
263 | | |
264 | | |
265 | | // print |