root/vtcross/trunk/src/cognitive_engines/OSSIE_DEMO_CE/cbr.c @ 449

Revision 449, 7.7 KB (checked in by trnewman, 15 years ago)

Added timestamp in OSSIE demo, probably should make this a common thing for all CBRs. Added configs directory to Makefile.am for packaging purposes.

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
96// create database
97cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
98{
99    // cbr is a pointer to struct cbr_s
100    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
101
102    // create database
103
104    // copy filename
105    unsigned int i=0;
106    strcpy(_cbr->filename, _filename);
107
108    // execute create database command
109    // database handle
110    //_cbr->db = NULL;
111    OpenDatabase(_cbr);
112
113    // create table
114
115    // copy tablename
116    strcpy(_cbr->tablename, _tablename);
117
118    // number of columns in the table
119    _cbr->num_columns = _len;
120
121    // generate command
122    strcpy(_cbr->command, "CREATE TABLE ");
123    strcat(_cbr->command, _cbr->tablename);
124    strcat(_cbr->command, "(");
125    for (i=0; i<_cbr->num_columns; i++) {
126        strcat(_cbr->command, _cols[i]);
127        strcat(_cbr->command, " FLOAT");
128        if (i != _cbr->num_columns-1) // not last entry
129            strcat(_cbr->command, ", ");
130    }
131    strcat(_cbr->command, ", timestamp DATE);");
132
133    // execute create table command
134    ExecuteCommand(_cbr);
135
136    return _cbr;
137}
138
139
140// free space
141void cbr_free(cbr _cbr)
142
143    // generate command, remove a table with its content
144    strcpy(_cbr->command, "drop table ");
145    strcat(_cbr->command, _cbr->tablename);
146
147    // execute delete command
148    ExecuteCommand(_cbr);
149
150    // clean the database
151    strcpy(_cbr->command, "vacuum");
152    ExecuteCommand(_cbr);
153
154    free(_cbr);
155}
156
157
158// print
159void cbr_print(cbr _cbr)
160{
161    // generate commandi
162    strcpy(_cbr->command, "select ");
163    strcat(_cbr->command, _cbr->tablename);
164    strcat(_cbr->command, ".* from ");
165    strcat(_cbr->command, _cbr->tablename);
166    strcat(_cbr->command, ";");
167
168    // execute print (select all)  command
169    ExecuteCommand(_cbr);
170    printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
171}
172
173
174/*//static
175int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
176{
177    return 0;
178}*/
179
180
181const char * ops_str[] = {
182    "==", "!=", ">", ">=", "<", "<="};
183
184
185// cbr search
186int cbr_search(
187    cbr _cbr,
188    char *_names[],
189    int * _ops,
190    float *_vals,
191    unsigned int _n,
192    float *_retvals)
193{   
194    int rc;
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
202    unsigned int i;
203    char str_buffer[64];
204    printf("number of ops %d:\n", _n);
205    for (i=0; i<_n; i++) {
206        // ensure valid ops value
207        if (_ops[i] < 0 || _ops[i] > 5) {
208            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
209            exit(1);
210        }
211
212        strcat(_cbr->command, _names[i]);
213        strcat(_cbr->command, ops_str[_ops[i]]);
214    printf("search command: %s\n", _cbr->command);
215        sprintf(str_buffer, "%E", _vals[i]);
216        strcat(_cbr->command, str_buffer);
217
218
219        if (i<_n-1)
220            strcat(_cbr->command, " AND ");
221        else
222            strcat(_cbr->command, " order by utility, timestamp desc );");
223    }
224
225    //printf("search command: %s\n", _cbr->command);
226
227    //ExecuteCommand(_cbr);
228    rc = ExecuteSearchCommand(_cbr, _retvals);
229   
230    /*printf("search result: ");
231    for (i=0; i<_cbr->num_columns; i++)
232        printf("%f, ",_retvals[i]);
233    printf("\n");*/
234
235    return rc;
236}
237
238
239// update a row
240int cbr_update(cbr _cbr, char *_where[], char*_set[], float *_wherevals, float *_setvals,
241                unsigned int _wherelen, unsigned int _setlen)
242{
243    unsigned int i;
244   
245    // generate command
246    //printf("%s\n", _cbr->command);
247    strcpy(_cbr->command, "UPDATE ");
248    strcat(_cbr->command, _cbr->tablename);
249   
250    strcat(_cbr->command, " SET ");
251    for (i=0; i<_setlen; i++) {
252        strcat(_cbr->command, _set[i]);
253        strcat(_cbr->command, " = ");
254        sprintf(_cbr->command, "%s%f", _cbr->command, _setvals[i]);
255        strcat(_cbr->command, "  ");
256        if (i != _setlen-1) // not last entry
257            strcat(_cbr->command, ", ");
258    }
259   
260    strcat(_cbr->command, "timestamp = DATETIME('NOW')");
261   
262    strcat(_cbr->command, " WHERE ");
263
264    for (i=0; i<_wherelen; i++) {
265        strcat(_cbr->command, _where[i]);
266        strcat(_cbr->command, " = ");
267        sprintf(_cbr->command, "%s%f", _cbr->command, _wherevals[i]);
268        strcat(_cbr->command, "  ");
269        if (i != _wherelen-1) // not last entry
270            strcat(_cbr->command, "AND ");
271    }
272    strcat(_cbr->command, ";");
273   
274    //printf("search command: %s\n", _cbr->command);
275    // execute add command
276    ExecuteCommand(_cbr);
277
278    return 0;
279}
280
281// cbr add a row
282int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
283{
284    unsigned int i;
285   
286    // generate command
287    //printf("%s\n", _cbr->command);
288    strcpy(_cbr->command, "insert into ");
289    strcat(_cbr->command, _cbr->tablename);
290   
291    strcat(_cbr->command, " (");
292    for (i=0; i<_len; i++) {
293        strcat(_cbr->command, _cols[i]);
294        if (i != _cbr->num_columns-1) // not last entry
295            strcat(_cbr->command, ", ");
296    }
297    strcat(_cbr->command, ") ");
298
299    strcat(_cbr->command, " values(");
300    for (i=0; i<_len; i++) {
301        // ???? how to fill the values if _cbr->num_columns != _len
302        // assume = in the following
303        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
304        if (i != _cbr->num_columns-1) // not last entry
305            strcat(_cbr->command, ", ");
306    }
307    strcat(_cbr->command, ");");
308   
309    //printf("search command: %s\n", _cbr->command);
310    // execute add command
311    ExecuteCommand(_cbr);
312
313    return 0;
314}
315
Note: See TracBrowser for help on using the browser.