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

Revision 411, 11.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
18//
19// Case-based reasoner
20//
21
22#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
25
26#include "vtcross/cbr.h"
27#include "vtcross/common.h"
28
29
30struct cbr_s {
31    char filename[64];
32    char tablename[64];
33    char command[2048];
34    sqlite3 *db;
35    unsigned int num_columns;
36};
37
38
39// open a database or create a database if it does not exist
40int OpenDatabase(cbr _cbr){
41    int rc;
42    //sqlite3 **db;
43
44    //printf("database name: %s\n", _cbr->filename);
45    //rc = sqlite3_open(_cbr->filename, db);
46    rc = sqlite3_open(_cbr->filename, &(_cbr->db));
47    if (rc) {
48        //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db));
49        fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db));
50        sqlite3_close(_cbr->db);
51        exit(1);
52    } else{
53        //printf("database opened.\n");
54    }
55    //_cbr->db = *db;
56    return rc;
57}
58
59
60// simple callback function, display result
61int callback(void *notUsed, int argc, char **argv, char **azColName){
62    int i;
63    for(i=0; i<argc; i++){
64        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
65    }
66    printf("\n");
67    return 0;
68}
69
70
71// execute command
72int ExecuteCommand(cbr _cbr){
73    int rc;
74    char *zErrMsg = 0;
75
76    //printf("command: %s\n", _cbr->command);
77    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg);
78    if( rc!=SQLITE_OK){
79        fprintf(stderr, "SQL error: %s: %s\n", zErrMsg,_cbr->command);
80        sqlite3_free(zErrMsg);
81    } else{
82        //printf("command executed.\n");
83    }
84    return rc;
85}
86
87
88// execute search command
89int ExecuteSearchCommand(cbr _cbr, float *_retvals){
90    int rc;
91    unsigned int i;
92
93    sqlite3_stmt * pStatement;
94    rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL);
95    if (rc == SQLITE_OK){
96        if (sqlite3_step(pStatement) == SQLITE_ROW){
97            for (i=0; i<_cbr->num_columns; ++i)
98                _retvals[i] = sqlite3_column_double(pStatement, i);
99        } else {
100                    printf("CBR:: No matched results returning default.\n");
101                        rc=31337;
102                }
103    } else {
104                printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,_cbr->command);
105    }
106
107    sqlite3_finalize(pStatement);
108   
109    return rc;
110}
111
112// create database
113cbr cbr_create_with_primary(char * _filename, char * _tablename,
114        char * _cols[], char * _primcols[], unsigned int _len,
115        unsigned int _primlen)
116{
117    // cbr is a pointer to struct cbr_s
118    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
119
120    // create database
121
122    // copy filename
123    unsigned int i=0;
124    strcpy(_cbr->filename, _filename);
125
126    // execute create database command
127    // database handle
128    //_cbr->db = NULL;
129    OpenDatabase(_cbr);
130
131    // create table
132
133    // copy tablename
134    strcpy(_cbr->tablename, _tablename);
135
136    // number of columns in the table
137    _cbr->num_columns = _len;
138
139    // generate command
140    strcpy(_cbr->command, "CREATE TABLE ");
141    strcat(_cbr->command, _cbr->tablename);
142    strcat(_cbr->command, "(");
143    for (i=0; i<_cbr->num_columns; i++) {
144        strcat(_cbr->command, _cols[i]);
145        strcat(_cbr->command, " FLOAT");
146        strcat(_cbr->command, ", ");
147    }
148   strcat(_cbr->command, "PRIMARY KEY (");
149    for (i=0; i<_primlen; i++) {
150        strcat(_cbr->command, _primcols[i]);
151        if (i != _primlen-1) // not last entry
152            strcat(_cbr->command, ", ");
153    }
154    strcat(_cbr->command, "));");
155
156    // execute create table command
157    ExecuteCommand(_cbr);
158
159    return _cbr;
160}
161
162// create database
163cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
164{
165    // cbr is a pointer to struct cbr_s
166    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
167
168    // create database
169
170    // copy filename
171    unsigned int i=0;
172    strcpy(_cbr->filename, _filename);
173
174    // execute create database command
175    // database handle
176    //_cbr->db = NULL;
177    OpenDatabase(_cbr);
178
179    // create table
180
181    // copy tablename
182    strcpy(_cbr->tablename, _tablename);
183
184    // number of columns in the table
185    _cbr->num_columns = _len;
186
187    // generate command
188    strcpy(_cbr->command, "CREATE TABLE ");
189    strcat(_cbr->command, _cbr->tablename);
190    strcat(_cbr->command, "(");
191    for (i=0; i<_cbr->num_columns; i++) {
192        strcat(_cbr->command, _cols[i]);
193        strcat(_cbr->command, " FLOAT");
194        if (i != _cbr->num_columns-1) // not last entry
195            strcat(_cbr->command, ", ");
196    }
197    strcat(_cbr->command, ");");
198
199    // execute create table command
200    ExecuteCommand(_cbr);
201
202    return _cbr;
203}
204
205
206// free space
207void cbr_free(cbr _cbr)
208
209    // generate command, remove a table with its content
210    strcpy(_cbr->command, "drop table ");
211    strcat(_cbr->command, _cbr->tablename);
212
213    // execute delete command
214    ExecuteCommand(_cbr);
215
216    // clean the database
217    strcpy(_cbr->command, "vacuum");
218    ExecuteCommand(_cbr);
219
220    free(_cbr);
221}
222
223
224// print
225void cbr_print(cbr _cbr)
226{
227    // generate commandi
228    strcpy(_cbr->command, "select ");
229    strcat(_cbr->command, _cbr->tablename);
230    strcat(_cbr->command, ".* from ");
231    strcat(_cbr->command, _cbr->tablename);
232    strcat(_cbr->command, ";");
233
234    // execute print (select all)  command
235    ExecuteCommand(_cbr);
236    printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
237}
238
239
240/*//static
241int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
242{
243    return 0;
244}*/
245
246
247const char * ops_str[] = {
248    "==", "!=", ">", ">=", "<", "<="};
249
250
251// cbr search for a sum
252int cbr_search_sum(
253    cbr _cbr,
254    char *_name,
255    float *_retvals)
256{   
257    int rc;
258    // generate command
259    strcpy(_cbr->command, "select SUM( ");
260    strcat(_cbr->command, _cbr->tablename);
261    strcat(_cbr->command, ".");
262    strcat(_cbr->command, _name);
263    strcat(_cbr->command, ") from ");
264    strcat(_cbr->command, _cbr->tablename);
265    strcat(_cbr->command, ";");
266
267    //printf("search command: %s\n", _cbr->command);
268
269    rc = ExecuteSearchCommand(_cbr, _retvals);
270    /*printf("search result: ");
271    for (int i=0; i<_cbr->num_columns; i++)
272        printf("%f, ",_retvals[i]);
273    printf("\n");
274    */
275    return rc;
276}
277
278
279// cbr search
280int cbr_search_rand(
281    cbr _cbr,
282    char *_names[],
283    int * _ops,
284    float *_vals,
285    unsigned int _n,
286    float *_retvals)
287{   
288    int rc;
289    // generate command
290    strcpy(_cbr->command, "select ");
291    strcat(_cbr->command, _cbr->tablename);
292    strcat(_cbr->command, ".* from ");
293    strcat(_cbr->command, _cbr->tablename);
294    strcat(_cbr->command, " where ");
295
296    unsigned int i;
297    char str_buffer[64];
298    for (i=0; i<_n; i++) {
299        // ensure valid ops value
300        if (_ops[i] < 0 || _ops[i] > 5) {
301            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
302            exit(1);
303        }
304
305        strcat(_cbr->command, _names[i]);
306        strcat(_cbr->command, ops_str[_ops[i]]);
307        sprintf(str_buffer, "%E", _vals[i]);
308        strcat(_cbr->command, str_buffer);
309
310        if (i<_n-1)
311            strcat(_cbr->command, " AND ");
312        else
313            strcat(_cbr->command, " order by RAND();");
314    }
315
316    //printf("search command: %s\n", _cbr->command);
317
318    rc = ExecuteSearchCommand(_cbr, _retvals);
319   
320    /*printf("search result: ");
321    for (i=0; i<_cbr->num_columns; i++)
322        printf("%f, ",_retvals[i]);
323    printf("\n");
324*/
325    return rc;
326}
327
328
329// cbr search
330int cbr_search(
331    cbr _cbr,
332    char *_names[],
333    int * _ops,
334    float *_vals,
335    unsigned int _n,
336    float *_retvals)
337{   
338    int rc;
339    // generate command
340    strcpy(_cbr->command, "select ");
341    strcat(_cbr->command, _cbr->tablename);
342    strcat(_cbr->command, ".* from ");
343    strcat(_cbr->command, _cbr->tablename);
344    strcat(_cbr->command, " where ");
345
346    unsigned int i;
347    char str_buffer[64];
348    for (i=0; i<_n; i++) {
349        // ensure valid ops value
350        if (_ops[i] < 0 || _ops[i] > 5) {
351            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
352            exit(1);
353        }
354
355        strcat(_cbr->command, _names[i]);
356        strcat(_cbr->command, ops_str[_ops[i]]);
357        sprintf(str_buffer, "%E", _vals[i]);
358        strcat(_cbr->command, str_buffer);
359
360        if (i<_n-1)
361            strcat(_cbr->command, " AND ");
362        else
363            strcat(_cbr->command, " order by utility desc;");
364    }
365
366    //printf("search command: %s\n", _cbr->command);
367
368    rc = ExecuteSearchCommand(_cbr, _retvals);
369   
370    /*printf("search result: ");
371    for (i=0; i<_cbr->num_columns; i++)
372        printf("%f, ",_retvals[i]);
373    printf("\n");*/
374
375    return rc;
376}
377
378
379// update a row
380int cbr_update(cbr _cbr, char *_where[], char*_set[], float *_wherevals, float *_setvals,
381                unsigned int _wherelen, unsigned int _setlen)
382{
383    unsigned int i;
384   
385    // generate command
386    //printf("%s\n", _cbr->command);
387    strcpy(_cbr->command, "UPDATE ");
388    strcat(_cbr->command, _cbr->tablename);
389   
390    strcat(_cbr->command, " SET ");
391    for (i=0; i<_setlen; i++) {
392        strcat(_cbr->command, _set[i]);
393        strcat(_cbr->command, " = ");
394        sprintf(_cbr->command, "%s%f", _cbr->command, _setvals[i]);
395        strcat(_cbr->command, "  ");
396        if (i != _setlen-1) // not last entry
397            strcat(_cbr->command, ", ");
398    }
399    strcat(_cbr->command, " WHERE ");
400
401    for (i=0; i<_wherelen; i++) {
402        strcat(_cbr->command, _where[i]);
403        strcat(_cbr->command, " = ");
404        sprintf(_cbr->command, "%s%f", _cbr->command, _wherevals[i]);
405        strcat(_cbr->command, "  ");
406        if (i != _wherelen-1) // not last entry
407            strcat(_cbr->command, "AND ");
408    }
409    strcat(_cbr->command, ";");
410   
411    //printf("search command: %s\n", _cbr->command);
412    // execute add command
413    ExecuteCommand(_cbr);
414
415    return 0;
416}
417
418// cbr add a row
419int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
420{
421    unsigned int i;
422   
423    // generate command
424    //printf("%s\n", _cbr->command);
425    strcpy(_cbr->command, "replace into ");
426    strcat(_cbr->command, _cbr->tablename);
427   
428    strcat(_cbr->command, " (");
429    for (i=0; i<_len; i++) {
430        strcat(_cbr->command, _cols[i]);
431        if (i != _cbr->num_columns-1) // not last entry
432            strcat(_cbr->command, ", ");
433    }
434    strcat(_cbr->command, ") ");
435
436    strcat(_cbr->command, " values(");
437    for (i=0; i<_len; i++) {
438        // ???? how to fill the values if _cbr->num_columns != _len
439        // assume = in the following
440        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
441        if (i != _cbr->num_columns-1) // not last entry
442            strcat(_cbr->command, ", ");
443    }
444    strcat(_cbr->command, ");");
445   
446    //printf("search command: %s\n", _cbr->command);
447    // execute add command
448    ExecuteCommand(_cbr);
449
450    return 0;
451}
452
Note: See TracBrowser for help on using the browser.