This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_base.c
261 lines (245 loc) · 7.75 KB
/
rule_base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "rule_base.h"
#include <ctype.h>
#define BUF_SIZE 1000 // buffer for one line of characters
// it will terminate a string with proper char '\0'
void copyString (char **dest, char *src) {
int strlength = strlen(src);
*dest = malloc(strlength+1*sizeof(char));
memcpy(*dest, src, strlength);
(*dest)[strlength] = '\0';
}
void insertVarValPair (var_val_pair **head, var_val_pair **tail, char *name, char *value, char *con) {
var_val_pair *ptrNewNode = malloc(sizeof(var_val_pair));
if (!ptrNewNode) {
printf("\nNot enough memory.");
return;
}
copyString(&ptrNewNode->name, name);
copyString(&ptrNewNode->value, value);
if (con != NULL) {
copyString(&ptrNewNode->connective, con);
}
else {
ptrNewNode->connective = malloc(sizeof(char));
*(ptrNewNode->connective) = '\0';
}
if (*head == NULL) {
*head = ptrNewNode;
*tail = ptrNewNode;
}
else {
(*tail)->next = ptrNewNode;
*tail = ptrNewNode;
}
}
void insertRuleNode (rule_node **head, int num) {
rule_node *ptrNewNode = calloc(1,sizeof(rule_node));
if (!ptrNewNode) {
printf("\nNot enough memory.");
return;
}
ptrNewNode->num = num;
ptrNewNode->next = *head;
*head = ptrNewNode;
}
void addFuzzySet (fuzzy_set **sets_table, char *val_name, int *tuple) {
fuzzy_set *ptrNewFuzzySet;
HASH_FIND_STR(*sets_table, val_name, ptrNewFuzzySet);
if (!ptrNewFuzzySet) {
ptrNewFuzzySet = calloc(1,sizeof(fuzzy_set));
if (!ptrNewFuzzySet) {
printf("\nNot enough memory.");
return;
}
copyString(&ptrNewFuzzySet->val_name, val_name);
memcpy(ptrNewFuzzySet->tuple, tuple, 4 * sizeof(int));
HASH_ADD_KEYPTR(hh,*sets_table, ptrNewFuzzySet->val_name,
strlen(ptrNewFuzzySet->val_name), ptrNewFuzzySet);
return;
}
printf("\nThe hash table %s already exist. The original remains!\n", val_name);
}
fuzzy_set* findFuzzySet (fuzzy_set **sets_table, char *val_name) {
fuzzy_set *ptrFoundValSet;
HASH_FIND_STR(*sets_table, val_name, ptrFoundValSet);
return ptrFoundValSet;
}
var_sets* addVarSets (var_sets **var_table, char *var_name) {
var_sets *ptrNewVarSets;
HASH_FIND_STR(*var_table, var_name, ptrNewVarSets);
if (!ptrNewVarSets) {
ptrNewVarSets = calloc(1,sizeof(var_sets));
if (!ptrNewVarSets) {
printf("\nNot enough memory.");
return NULL;
}
copyString(&ptrNewVarSets->var_name, var_name);
ptrNewVarSets->sets_table = NULL;
HASH_ADD_KEYPTR(hh,*var_table, ptrNewVarSets->var_name,
strlen(ptrNewVarSets->var_name), ptrNewVarSets);
return ptrNewVarSets;
}
printf("\nThe hash table %s already exist. The original remains!\n", var_name);
return NULL;
}
var_sets* findVarSets (var_sets **var_table, char *var_name) {
var_sets *ptrFoundVarSets;
HASH_FIND_STR(*var_table, var_name, ptrFoundVarSets);
return ptrFoundVarSets;
}
void insertMeasurement (measurement **head, char *var_name, double value) {
measurement *ptrNewNode = calloc(1,sizeof(measurement));
if (!ptrNewNode) {
printf("\nNot enough memory.");
return;
}
copyString(&ptrNewNode->var_name, var_name);
ptrNewNode->value = value;
ptrNewNode->next = *head;
*head = ptrNewNode;
}
char* get_next_rule_token(char *(*ptrStrtok)(char *, const char*), char *token) {
token = ptrStrtok(NULL, " ");
while ( token != NULL &&
((strcmp(token, "the") == 0) ||
(strcmp(token, "is") == 0) ||
(strcmp(token, "will") == 0) ||
(strcmp(token, "be") == 0)) ) {
token = ptrStrtok(NULL, " ");
}
return token;
}
rule_base* loadRuleBase (FILE *f_rules) {
rule_base *ptrRuleBase = calloc(1,sizeof(rule_base));
char line[BUF_SIZE];
if (fgets (line, BUF_SIZE, f_rules) != NULL) {
// get name of rulebase
copyString(&ptrRuleBase->name, line);
// count empty lines
int empty_line_counter = 0;
// pointer for token from line
char *token;
var_sets *var_table;
// while there is a line read it to the buffer 'line'
while (fgets (line, BUF_SIZE, f_rules) != NULL) {
if (strcmp(line, "\n") != 0) {
for (char *p = line; *p; ++p) *p = tolower(*p);
// get rid of '\n' char at the end of the line
line[strlen(line)-1] = ' ';
// if empty line counter is 1 then Rule section starts here
if (empty_line_counter == 1) {
// working in rule section
token = strtok(line, " ");
// check if the keyword 'rule' is there
if (strcmp(token, "rule") == 0) {
// get rule number
token = strtok(NULL, " ");
int rule_num;
if (sscanf(token, "%d", &rule_num) == 1) {
// create new rule node in rule base
insertRuleNode( &(ptrRuleBase->rule_list), rule_num);
// get next token which should be If
token = strtok(NULL, " ");
// check 'if' token
if (strcmp(token, "if") == 0) {
// get next token which should be 'the' or name of variable
token = strtok(NULL, " ");
// if there is 'the' just ignore it and get new token
if (strcmp(token, "the") == 0) {
token = strtok(NULL, " ");
}
// while there is a variable value pair then add them to linked
// list
// declare new variable used to copy names, etc...
char *var_name;
char *value_name;
char *con;
while (token != NULL) {
// copy variable name
copyString(&var_name, token);
// get new token which is value of the variable
token = get_next_rule_token(&strtok, token);
copyString(&value_name, token);
// get new token which is either [and | or | then | EOL]
token = get_next_rule_token(&strtok, token);
if (token != NULL) {
copyString(&con, token);
}
// create new variable value pair for the rule
insertVarValPair( &(ptrRuleBase->rule_list->head_of_rule),
&(ptrRuleBase->rule_list->tail_of_rule),
var_name, value_name, con );
free(var_name);
var_name = NULL;
free(value_name);
value_name = NULL;
if (con != NULL) {
free(con);
con = NULL;
token = get_next_rule_token(&strtok, token);
}
}
}
else printf("\nCannot find If keyword\n");
}
else printf("\nCannot read rule number\n");
}
else printf("\nError no rule found\n");
}
else {
// working with variable names and their sets
token = strtok(line, " ");
char *prev_token;
copyString(&prev_token, token);
token = strtok(NULL, " ");
// there is only variable name, create new hash entry for it
if (!token) {
var_table = addVarSets(&(ptrRuleBase->var_table), prev_token);
}
else {
// there is also second token that is tuple or value reading
// check if the second token is '='
if (strcmp(token, "=") == 0){
double value;
char *m_var_name;
copyString(&m_var_name, prev_token);
token = strtok(NULL, " ");
if (sscanf(token, "%lf", &value) == 1) {
insertMeasurement(&(ptrRuleBase->measurements), m_var_name, value);
}
else {
printf("\nValue reading for variable %s failed!\n", m_var_name);
}
}
else{
// copy the name of value
char *val_name;
copyString(&val_name, prev_token);
// copy the tuple values
int tuple[4];
for (int i=0; i<4; ++i) {
if (sscanf(token, "%d", &tuple[i]) == 1) {
token = strtok(NULL, " ");
}
else {
printf("\nReading of tuple for value %s failed!\n", val_name);
}
}
// now add new fuzzy set for the variable
addFuzzySet(&(var_table->sets_table), val_name, tuple);
var_table->number_of_sets += 1; // keep track of how many sets certain variable has
}
}
}
}
else {
++empty_line_counter;
}
}
}
else {
printf("\nProblem has occured while loading rule base file.\n");
return NULL;
}
return ptrRuleBase;
}