root/vtcross/trunk/src/cognitive_engines/CBR_CE/cbr.c @ 411

Revision 411, 8.1 KB (checked in by trnewman, 15 years ago)

Adding Apache license information.

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