root/vtcross/trunk/src/cognitive_engines/DSA_CE/cbr.c @ 391

Revision 391, 10.5 KB (checked in by trnewman, 15 years ago)

Fleshed out DSA reference implementation

Line 
1//
2// Case-based reasoner
3//
4
5#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
8
9#include "vtcross/cbr.h"
10#include "vtcross/common.h"
11
12
13struct cbr_s {
14    char filename[64];
15    char tablename[64];
16    char command[2048];
17    sqlite3 *db;
18    unsigned int num_columns;
19};
20
21
22// open a database or create a database if it does not exist
23int OpenDatabase(cbr _cbr){
24    int rc;
25    //sqlite3 **db;
26
27    //printf("database name: %s\n", _cbr->filename);
28    //rc = sqlite3_open(_cbr->filename, db);
29    rc = sqlite3_open(_cbr->filename, &(_cbr->db));
30    if (rc) {
31        //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db));
32        fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db));
33        sqlite3_close(_cbr->db);
34        exit(1);
35    } else{
36        //printf("database opened.\n");
37    }
38    //_cbr->db = *db;
39    return rc;
40}
41
42
43// simple callback function, display result
44int callback(void *notUsed, int argc, char **argv, char **azColName){
45    int i;
46    for(i=0; i<argc; i++){
47        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
48    }
49    printf("\n");
50    return 0;
51}
52
53
54// execute command
55int ExecuteCommand(cbr _cbr){
56    int rc;
57    char *zErrMsg = 0;
58
59    //printf("command: %s\n", _cbr->command);
60    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg);
61    if( rc!=SQLITE_OK){
62        fprintf(stderr, "SQL error: %s: %s\n", zErrMsg,_cbr->command);
63        sqlite3_free(zErrMsg);
64    } else{
65        //printf("command executed.\n");
66    }
67    return rc;
68}
69
70
71// execute search command
72int ExecuteSearchCommand(cbr _cbr, float *_retvals){
73    int rc;
74    unsigned int i;
75
76    sqlite3_stmt * pStatement;
77    rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL);
78    if (rc == SQLITE_OK){
79        if (sqlite3_step(pStatement) == SQLITE_ROW){
80            for (i=0; i<_cbr->num_columns; ++i)
81                _retvals[i] = sqlite3_column_double(pStatement, i);
82        } else {
83                    printf("CBR:: No matched results returning default.\n");
84                        rc=31337;
85                }
86    } else {
87                printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,_cbr->command);
88    }
89
90    sqlite3_finalize(pStatement);
91   
92    return rc;
93}
94
95// create database
96cbr cbr_create_with_primary(char * _filename, char * _tablename,
97        char * _cols[], char * _primcols[], unsigned int _len,
98        unsigned int _primlen)
99{
100    // cbr is a pointer to struct cbr_s
101    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
102
103    // create database
104
105    // copy filename
106    unsigned int i=0;
107    strcpy(_cbr->filename, _filename);
108
109    // execute create database command
110    // database handle
111    //_cbr->db = NULL;
112    OpenDatabase(_cbr);
113
114    // create table
115
116    // copy tablename
117    strcpy(_cbr->tablename, _tablename);
118
119    // number of columns in the table
120    _cbr->num_columns = _len;
121
122    // generate command
123    strcpy(_cbr->command, "CREATE TABLE ");
124    strcat(_cbr->command, _cbr->tablename);
125    strcat(_cbr->command, "(");
126    for (i=0; i<_cbr->num_columns; i++) {
127        strcat(_cbr->command, _cols[i]);
128        strcat(_cbr->command, " FLOAT");
129        strcat(_cbr->command, ", ");
130    }
131   strcat(_cbr->command, "PRIMARY KEY (");
132    for (i=0; i<_primlen; i++) {
133        strcat(_cbr->command, _primcols[i]);
134        if (i != _primlen-1) // not last entry
135            strcat(_cbr->command, ", ");
136    }
137    strcat(_cbr->command, "));");
138
139    // execute create table command
140    ExecuteCommand(_cbr);
141
142    return _cbr;
143}
144
145// create database
146cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
147{
148    // cbr is a pointer to struct cbr_s
149    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
150
151    // create database
152
153    // copy filename
154    unsigned int i=0;
155    strcpy(_cbr->filename, _filename);
156
157    // execute create database command
158    // database handle
159    //_cbr->db = NULL;
160    OpenDatabase(_cbr);
161
162    // create table
163
164    // copy tablename
165    strcpy(_cbr->tablename, _tablename);
166
167    // number of columns in the table
168    _cbr->num_columns = _len;
169
170    // generate command
171    strcpy(_cbr->command, "CREATE TABLE ");
172    strcat(_cbr->command, _cbr->tablename);
173    strcat(_cbr->command, "(");
174    for (i=0; i<_cbr->num_columns; i++) {
175        strcat(_cbr->command, _cols[i]);
176        strcat(_cbr->command, " FLOAT");
177        if (i != _cbr->num_columns-1) // not last entry
178            strcat(_cbr->command, ", ");
179    }
180    strcat(_cbr->command, ");");
181
182    // execute create table command
183    ExecuteCommand(_cbr);
184
185    return _cbr;
186}
187
188
189// free space
190void cbr_free(cbr _cbr)
191
192    // generate command, remove a table with its content
193    strcpy(_cbr->command, "drop table ");
194    strcat(_cbr->command, _cbr->tablename);
195
196    // execute delete command
197    ExecuteCommand(_cbr);
198
199    // clean the database
200    strcpy(_cbr->command, "vacuum");
201    ExecuteCommand(_cbr);
202
203    free(_cbr);
204}
205
206
207// print
208void cbr_print(cbr _cbr)
209{
210    // generate commandi
211    strcpy(_cbr->command, "select ");
212    strcat(_cbr->command, _cbr->tablename);
213    strcat(_cbr->command, ".* from ");
214    strcat(_cbr->command, _cbr->tablename);
215    strcat(_cbr->command, ";");
216
217    // execute print (select all)  command
218    ExecuteCommand(_cbr);
219    printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
220}
221
222
223/*//static
224int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
225{
226    return 0;
227}*/
228
229
230const char * ops_str[] = {
231    "==", "!=", ">", ">=", "<", "<="};
232
233
234// cbr search for a sum
235int cbr_search_sum(
236    cbr _cbr,
237    char *_name,
238    float *_retvals)
239{   
240    int rc;
241    // generate command
242    strcpy(_cbr->command, "select SUM( ");
243    strcat(_cbr->command, _cbr->tablename);
244    strcat(_cbr->command, ".");
245    strcat(_cbr->command, _name);
246    strcat(_cbr->command, ") from ");
247    strcat(_cbr->command, _cbr->tablename);
248    strcat(_cbr->command, ";");
249
250    //printf("search command: %s\n", _cbr->command);
251
252    rc = ExecuteSearchCommand(_cbr, _retvals);
253    /*printf("search result: ");
254    for (int i=0; i<_cbr->num_columns; i++)
255        printf("%f, ",_retvals[i]);
256    printf("\n");
257    */
258    return rc;
259}
260
261
262// cbr search
263int cbr_search_rand(
264    cbr _cbr,
265    char *_names[],
266    int * _ops,
267    float *_vals,
268    unsigned int _n,
269    float *_retvals)
270{   
271    int rc;
272    // generate command
273    strcpy(_cbr->command, "select ");
274    strcat(_cbr->command, _cbr->tablename);
275    strcat(_cbr->command, ".* from ");
276    strcat(_cbr->command, _cbr->tablename);
277    strcat(_cbr->command, " where ");
278
279    unsigned int i;
280    char str_buffer[64];
281    for (i=0; i<_n; i++) {
282        // ensure valid ops value
283        if (_ops[i] < 0 || _ops[i] > 5) {
284            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
285            exit(1);
286        }
287
288        strcat(_cbr->command, _names[i]);
289        strcat(_cbr->command, ops_str[_ops[i]]);
290        sprintf(str_buffer, "%E", _vals[i]);
291        strcat(_cbr->command, str_buffer);
292
293        if (i<_n-1)
294            strcat(_cbr->command, " AND ");
295        else
296            strcat(_cbr->command, " order by RAND();");
297    }
298
299    //printf("search command: %s\n", _cbr->command);
300
301    rc = ExecuteSearchCommand(_cbr, _retvals);
302   
303    /*printf("search result: ");
304    for (i=0; i<_cbr->num_columns; i++)
305        printf("%f, ",_retvals[i]);
306    printf("\n");
307*/
308    return rc;
309}
310
311
312// cbr search
313int cbr_search(
314    cbr _cbr,
315    char *_names[],
316    int * _ops,
317    float *_vals,
318    unsigned int _n,
319    float *_retvals)
320{   
321    int rc;
322    // generate command
323    strcpy(_cbr->command, "select ");
324    strcat(_cbr->command, _cbr->tablename);
325    strcat(_cbr->command, ".* from ");
326    strcat(_cbr->command, _cbr->tablename);
327    strcat(_cbr->command, " where ");
328
329    unsigned int i;
330    char str_buffer[64];
331    for (i=0; i<_n; i++) {
332        // ensure valid ops value
333        if (_ops[i] < 0 || _ops[i] > 5) {
334            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
335            exit(1);
336        }
337
338        strcat(_cbr->command, _names[i]);
339        strcat(_cbr->command, ops_str[_ops[i]]);
340        sprintf(str_buffer, "%E", _vals[i]);
341        strcat(_cbr->command, str_buffer);
342
343        if (i<_n-1)
344            strcat(_cbr->command, " AND ");
345        else
346            strcat(_cbr->command, " order by utility desc;");
347    }
348
349    //printf("search command: %s\n", _cbr->command);
350
351    rc = ExecuteSearchCommand(_cbr, _retvals);
352   
353    /*printf("search result: ");
354    for (i=0; i<_cbr->num_columns; i++)
355        printf("%f, ",_retvals[i]);
356    printf("\n");*/
357
358    return rc;
359}
360
361
362// update a row
363int cbr_update(cbr _cbr, char *_where[], char*_set[], float *_wherevals, float *_setvals,
364                unsigned int _wherelen, unsigned int _setlen)
365{
366    unsigned int i;
367   
368    // generate command
369    //printf("%s\n", _cbr->command);
370    strcpy(_cbr->command, "UPDATE ");
371    strcat(_cbr->command, _cbr->tablename);
372   
373    strcat(_cbr->command, " SET ");
374    for (i=0; i<_setlen; i++) {
375        strcat(_cbr->command, _set[i]);
376        strcat(_cbr->command, " = ");
377        sprintf(_cbr->command, "%s%f", _cbr->command, _setvals[i]);
378        strcat(_cbr->command, "  ");
379        if (i != _setlen-1) // not last entry
380            strcat(_cbr->command, ", ");
381    }
382    strcat(_cbr->command, " WHERE ");
383
384    for (i=0; i<_wherelen; i++) {
385        strcat(_cbr->command, _where[i]);
386        strcat(_cbr->command, " = ");
387        sprintf(_cbr->command, "%s%f", _cbr->command, _wherevals[i]);
388        strcat(_cbr->command, "  ");
389        if (i != _wherelen-1) // not last entry
390            strcat(_cbr->command, "AND ");
391    }
392    strcat(_cbr->command, ";");
393   
394    //printf("search command: %s\n", _cbr->command);
395    // execute add command
396    ExecuteCommand(_cbr);
397
398    return 0;
399}
400
401// cbr add a row
402int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
403{
404    unsigned int i;
405   
406    // generate command
407    //printf("%s\n", _cbr->command);
408    strcpy(_cbr->command, "replace into ");
409    strcat(_cbr->command, _cbr->tablename);
410   
411    strcat(_cbr->command, " (");
412    for (i=0; i<_len; i++) {
413        strcat(_cbr->command, _cols[i]);
414        if (i != _cbr->num_columns-1) // not last entry
415            strcat(_cbr->command, ", ");
416    }
417    strcat(_cbr->command, ") ");
418
419    strcat(_cbr->command, " values(");
420    for (i=0; i<_len; i++) {
421        // ???? how to fill the values if _cbr->num_columns != _len
422        // assume = in the following
423        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
424        if (i != _cbr->num_columns-1) // not last entry
425            strcat(_cbr->command, ", ");
426    }
427    strcat(_cbr->command, ");");
428   
429    //printf("search command: %s\n", _cbr->command);
430    // execute add command
431    ExecuteCommand(_cbr);
432
433    return 0;
434}
435
Note: See TracBrowser for help on using the browser.