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

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

Added DSA CBR reference implementation.
Added simple python example application for DSA CBR.
Added GNUradio python application that uses CROSS and the DSA CBR.

Fixed several bugs in the socket interface.

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