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

Revision 228, 6.3 KB (checked in by trnewman, 15 years ago)

Added sending current parameters in the libvt request optimization function.

Added guts to the CBR so it actually creates an sql db and searches it.

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// cbr add a row
239int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
240{
241    unsigned int i;
242   
243    // generate command
244    //printf("%s\n", _cbr->command);
245    strcpy(_cbr->command, "insert into ");
246    strcat(_cbr->command, _cbr->tablename);
247   
248    strcat(_cbr->command, " (");
249    for (i=0; i<_len; i++) {
250        strcat(_cbr->command, _cols[i]);
251        if (i != _cbr->num_columns-1) // not last entry
252            strcat(_cbr->command, ", ");
253    }
254    strcat(_cbr->command, ") ");
255
256    strcat(_cbr->command, " values(");
257    for (i=0; i<_len; i++) {
258        // ???? how to fill the values if _cbr->num_columns != _len
259        // assume = in the following
260        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
261        if (i != _cbr->num_columns-1) // not last entry
262            strcat(_cbr->command, ", ");
263    }
264    strcat(_cbr->command, ");");
265   
266    // execute add command
267    ExecuteCommand(_cbr);
268
269    return 0;
270}
271
Note: See TracBrowser for help on using the browser.