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