root/vtcross/branches/fixingCBR/src/cognitive_engines/cbr.c @ 238

Revision 238, 7.3 KB (checked in by bhilburn, 15 years ago)

Initial copy of function declerations from cbr.c to cbr.h

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
13// open a database or create a database if it does not exist
14int OpenDatabase(cbr _cbr){
15    int rc;
16    //sqlite3 **db;
17
18    //printf("database name: %s\n", _cbr->filename);
19    //rc = sqlite3_open(_cbr->filename, db);
20    rc = sqlite3_open(_cbr->filename, &(_cbr->db));
21    if (rc) {
22        //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db));
23        fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db));
24        sqlite3_close(_cbr->db);
25        exit(1);
26    } else{
27        //printf("database opened.\n");
28    }
29    //_cbr->db = *db;
30    return rc;
31}
32
33
34// simple callback function, display result
35int callback(void *notUsed, int argc, char **argv, char **azColName){
36    int i;
37    for(i=0; i<argc; i++){
38        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
39    }
40    printf("\n");
41    return 0;
42}
43
44
45// execute command
46int ExecuteCommand(cbr _cbr){
47    int rc;
48    char *zErrMsg = 0;
49
50    //printf("command: %s\n", _cbr->command);
51    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg);
52    if( rc!=SQLITE_OK){
53        fprintf(stderr, "SQL error: %s: %s\n", zErrMsg,_cbr->command);
54        sqlite3_free(zErrMsg);
55    } else{
56        //printf("command executed.\n");
57    }
58    return rc;
59}
60
61
62// execute search command
63int ExecuteSearchCommand(cbr _cbr, float *_retvals){
64    int rc;
65    unsigned int i;
66
67    sqlite3_stmt * pStatement;
68    rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL);
69    if (rc == SQLITE_OK){
70        if (sqlite3_step(pStatement) == SQLITE_ROW){
71            for (i=0; i<_cbr->num_columns; ++i)
72                _retvals[i] = sqlite3_column_double(pStatement, i);
73        } else {
74                    printf("CBR:: No matched results returning default.\n");
75                        rc=31337;
76                }
77    } else {
78                printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,_cbr->command);
79    }
80
81    sqlite3_finalize(pStatement);
82   
83    return rc;
84}
85
86
87// create database
88cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
89{
90    // cbr is a pointer to struct cbr_s
91    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
92
93    // create database
94
95    // copy filename
96    unsigned int i=0;
97    strcpy(_cbr->filename, _filename);
98
99    // execute create database command
100    // database handle
101    //_cbr->db = NULL;
102    OpenDatabase(_cbr);
103
104    // create table
105
106    // copy tablename
107    strcpy(_cbr->tablename, _tablename);
108
109    // number of columns in the table
110    _cbr->num_columns = _len;
111
112    // generate command
113    strcpy(_cbr->command, "CREATE TABLE ");
114    strcat(_cbr->command, _cbr->tablename);
115    strcat(_cbr->command, "(");
116    for (i=0; i<_cbr->num_columns; i++) {
117        strcat(_cbr->command, _cols[i]);
118        strcat(_cbr->command, " FLOAT");
119        if (i != _cbr->num_columns-1) // not last entry
120            strcat(_cbr->command, ", ");
121    }
122    strcat(_cbr->command, ");");
123
124    // execute create table command
125    ExecuteCommand(_cbr);
126
127    return _cbr;
128}
129
130
131// free space
132void cbr_free(cbr _cbr)
133
134    // generate command, remove a table with its content
135    strcpy(_cbr->command, "drop table ");
136    strcat(_cbr->command, _cbr->tablename);
137
138    // execute delete command
139    ExecuteCommand(_cbr);
140
141    // clean the database
142    strcpy(_cbr->command, "vacuum");
143    ExecuteCommand(_cbr);
144
145    free(_cbr);
146}
147
148
149// print
150void cbr_print(cbr _cbr)
151{
152    // generate commandi
153    strcpy(_cbr->command, "select ");
154    strcat(_cbr->command, _cbr->tablename);
155    strcat(_cbr->command, ".* from ");
156    strcat(_cbr->command, _cbr->tablename);
157    strcat(_cbr->command, ";");
158
159    // execute print (select all)  command
160    ExecuteCommand(_cbr);
161    printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
162}
163
164
165/*//static
166int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
167{
168    return 0;
169}*/
170
171
172// cbr search
173int cbr_search(
174    cbr _cbr,
175    char *_names[],
176    int * _ops,
177    float *_vals,
178    unsigned int _n,
179    float *_retvals)
180{   
181    int rc;
182    // generate command
183    strcpy(_cbr->command, "select ");
184    strcat(_cbr->command, _cbr->tablename);
185    strcat(_cbr->command, ".* from ");
186    strcat(_cbr->command, _cbr->tablename);
187    strcat(_cbr->command, " where ");
188
189    unsigned int i;
190    char str_buffer[64];
191    //printf("number of ops %d:\n", _n);
192    for (i=0; i<_n; i++) {
193        // ensure valid ops value
194        if (_ops[i] < 0 || _ops[i] > 5) {
195            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
196            exit(1);
197        }
198
199        strcat(_cbr->command, _names[i]);
200        strcat(_cbr->command, ops_str[_ops[i]]);
201        sprintf(str_buffer, "%E", _vals[i]);
202        strcat(_cbr->command, str_buffer);
203
204
205        if (i<_n-1)
206            strcat(_cbr->command, " AND ");
207        else
208            strcat(_cbr->command, " order by utility desc;");
209    }
210
211    //printf("search command: %s\n", _cbr->command);
212
213    //ExecuteCommand(_cbr);
214    rc = ExecuteSearchCommand(_cbr, _retvals);
215   
216    /*printf("search result: ");
217    for (i=0; i<_cbr->num_columns; i++)
218        printf("%f, ",_retvals[i]);
219    printf("\n");*/
220
221    return rc;
222}
223
224
225// update a row
226int cbr_update(cbr _cbr, char *_where[], char*_set[], float *_wherevals, float *_setvals,
227                unsigned int _wherelen, unsigned int _setlen)
228{
229    unsigned int i;
230   
231    // generate command
232    //printf("%s\n", _cbr->command);
233    strcpy(_cbr->command, "UPDATE ");
234    strcat(_cbr->command, _cbr->tablename);
235   
236    strcat(_cbr->command, " SET ");
237    for (i=0; i<_setlen; i++) {
238        strcat(_cbr->command, _set[i]);
239        strcat(_cbr->command, " = ");
240        sprintf(_cbr->command, "%s%f", _cbr->command, _setvals[i]);
241        strcat(_cbr->command, "  ");
242        if (i != _setlen-1) // not last entry
243            strcat(_cbr->command, ", ");
244    }
245    strcat(_cbr->command, " WHERE ");
246
247    for (i=0; i<_wherelen; i++) {
248        strcat(_cbr->command, _where[i]);
249        strcat(_cbr->command, " = ");
250        sprintf(_cbr->command, "%s%f", _cbr->command, _wherevals[i]);
251        strcat(_cbr->command, "  ");
252        if (i != _wherelen-1) // not last entry
253            strcat(_cbr->command, "AND ");
254    }
255    strcat(_cbr->command, ";");
256   
257    //printf("search command: %s\n", _cbr->command);
258    // execute add command
259    ExecuteCommand(_cbr);
260
261    return 0;
262}
263
264// cbr add a row
265int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
266{
267    unsigned int i;
268   
269    // generate command
270    //printf("%s\n", _cbr->command);
271    strcpy(_cbr->command, "insert into ");
272    strcat(_cbr->command, _cbr->tablename);
273   
274    strcat(_cbr->command, " (");
275    for (i=0; i<_len; i++) {
276        strcat(_cbr->command, _cols[i]);
277        if (i != _cbr->num_columns-1) // not last entry
278            strcat(_cbr->command, ", ");
279    }
280    strcat(_cbr->command, ") ");
281
282    strcat(_cbr->command, " values(");
283    for (i=0; i<_len; i++) {
284        // ???? how to fill the values if _cbr->num_columns != _len
285        // assume = in the following
286        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
287        if (i != _cbr->num_columns-1) // not last entry
288            strcat(_cbr->command, ", ");
289    }
290    strcat(_cbr->command, ");");
291   
292    //printf("search command: %s\n", _cbr->command);
293    // execute add command
294    ExecuteCommand(_cbr);
295
296    return 0;
297}
298
Note: See TracBrowser for help on using the browser.