root/vtcross/trunk/src/cognitive_engines/cbr.c @ 230

Revision 230, 7.5 KB (checked in by trnewman, 15 years ago)

Added a loop in the demo to actually adapt.

Added simple adaptation functionality and proper db querying in the CE.

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, ");");
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        sprintf(str_buffer, "%E", _vals[i]);
215        strcat(_cbr->command, str_buffer);
216
217
218        if (i<_n-1)
219            strcat(_cbr->command, " AND ");
220        else
221            strcat(_cbr->command, " order by utility desc;");
222    }
223
224    //printf("search command: %s\n", _cbr->command);
225
226    //ExecuteCommand(_cbr);
227    rc = ExecuteSearchCommand(_cbr, _retvals);
228   
229    /*printf("search result: ");
230    for (i=0; i<_cbr->num_columns; i++)
231        printf("%f, ",_retvals[i]);
232    printf("\n");*/
233
234    return rc;
235}
236
237
238// update a row
239int cbr_update(cbr _cbr, char *_where[], char*_set[], float *_wherevals, float *_setvals,
240                unsigned int _wherelen, unsigned int _setlen)
241{
242    unsigned int i;
243   
244    // generate command
245    //printf("%s\n", _cbr->command);
246    strcpy(_cbr->command, "UPDATE ");
247    strcat(_cbr->command, _cbr->tablename);
248   
249    strcat(_cbr->command, " SET ");
250    for (i=0; i<_setlen; i++) {
251        strcat(_cbr->command, _set[i]);
252        strcat(_cbr->command, " = ");
253        sprintf(_cbr->command, "%s%f", _cbr->command, _setvals[i]);
254        strcat(_cbr->command, "  ");
255        if (i != _setlen-1) // not last entry
256            strcat(_cbr->command, ", ");
257    }
258    strcat(_cbr->command, " WHERE ");
259
260    for (i=0; i<_wherelen; i++) {
261        strcat(_cbr->command, _where[i]);
262        strcat(_cbr->command, " = ");
263        sprintf(_cbr->command, "%s%f", _cbr->command, _wherevals[i]);
264        strcat(_cbr->command, "  ");
265        if (i != _wherelen-1) // not last entry
266            strcat(_cbr->command, "AND ");
267    }
268    strcat(_cbr->command, ";");
269   
270    //printf("search command: %s\n", _cbr->command);
271    // execute add command
272    ExecuteCommand(_cbr);
273
274    return 0;
275}
276
277// cbr add a row
278int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
279{
280    unsigned int i;
281   
282    // generate command
283    //printf("%s\n", _cbr->command);
284    strcpy(_cbr->command, "insert into ");
285    strcat(_cbr->command, _cbr->tablename);
286   
287    strcat(_cbr->command, " (");
288    for (i=0; i<_len; i++) {
289        strcat(_cbr->command, _cols[i]);
290        if (i != _cbr->num_columns-1) // not last entry
291            strcat(_cbr->command, ", ");
292    }
293    strcat(_cbr->command, ") ");
294
295    strcat(_cbr->command, " values(");
296    for (i=0; i<_len; i++) {
297        // ???? how to fill the values if _cbr->num_columns != _len
298        // assume = in the following
299        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
300        if (i != _cbr->num_columns-1) // not last entry
301            strcat(_cbr->command, ", ");
302    }
303    strcat(_cbr->command, ");");
304   
305    //printf("search command: %s\n", _cbr->command);
306    // execute add command
307    ExecuteCommand(_cbr);
308
309    return 0;
310}
311
Note: See TracBrowser for help on using the browser.