00001 #include "soarkernel.h"
00002 #include "soar_ecore_utils.h"
00003
00004 extern Symbol *make_symbol_for_current_lexeme(void);
00005
00006 void cb_soar_PrintToFile(soar_callback_agent the_agent, soar_callback_data data, soar_call_data call_data)
00007 {
00008
00009 FILE *f = (FILE *) data;
00010
00011 the_agent = the_agent;
00012
00013 fputs((char *) call_data, f);
00014 }
00015
00016 bool wme_filter_component_match(Symbol * filterComponent, Symbol * wmeComponent)
00017 {
00018 if ((filterComponent->common.symbol_type == SYM_CONSTANT_SYMBOL_TYPE) && (!strcmp(filterComponent->sc.name, "*")))
00019 return TRUE;
00020 else
00021 return (bool) (filterComponent == wmeComponent);
00022 }
00023
00024 bool passes_wme_filtering(wme * w, bool isAdd)
00025 {
00026 cons *c;
00027 wme_filter *wf;
00028
00029
00030
00031 if (!current_agent(wme_filter_list))
00032 return TRUE;
00033 for (c = current_agent(wme_filter_list); c != NIL; c = c->rest) {
00034 wf = (wme_filter *) c->first;
00035
00036 if (((isAdd && wf->adds) || ((!isAdd) && wf->removes))
00037 && wme_filter_component_match(wf->id, w->id)
00038 && wme_filter_component_match(wf->attr, w->attr)
00039 && wme_filter_component_match(wf->value, w->value))
00040 return TRUE;
00041 }
00042 return FALSE;
00043 }
00044
00045 int parse_filter_type(const char *s, bool * forAdds, bool * forRemoves)
00046 {
00047 if (!strcmp(s, "-adds")) {
00048 *forAdds = TRUE;
00049 *forRemoves = FALSE;
00050 return SOAR_OK;
00051 } else if (!strcmp(s, "-removes")) {
00052 *forAdds = FALSE;
00053 *forRemoves = TRUE;
00054 return SOAR_OK;
00055 } else if (!strcmp(s, "-both")) {
00056 *forAdds = TRUE;
00057 *forRemoves = TRUE;
00058 return SOAR_OK;
00059 }
00060 return SOAR_ERROR;
00061 }
00062
00063
00064
00065 #ifdef USE_CAPTURE_REPLAY
00066
00067 void capture_input_wme(enum captured_action_type action, soarapi_wme * sw, wme * w)
00068 {
00069
00070 switch (action) {
00071
00072 case ADD_WME:
00073 fprintf(current_agent(capture_fileID),
00074 "%ld : %d : add-wme : %ld", current_agent(d_cycle_count), ADD_WME, sw->timetag);
00075
00076 fprintf(current_agent(capture_fileID), " : %s %s %s : ", sw->id, sw->attr, sw->value);
00077
00078 fprintf(current_agent(capture_fileID), "%s ", symbol_to_string(w->id, TRUE, NULL, 0 ));
00079
00080 fprintf(current_agent(capture_fileID), "%s ", symbol_to_string(w->attr, TRUE, NULL, 0));
00081
00082 fprintf(current_agent(capture_fileID), "%s\n", symbol_to_string(w->value, TRUE, NULL, 0));
00083
00084 break;
00085
00086 case REMOVE_WME:
00087 fprintf(current_agent(capture_fileID),
00088 "%ld : %d : remove-wme : : %ld\n", current_agent(d_cycle_count), REMOVE_WME, sw->timetag);
00089 break;
00090
00091 }
00092
00093 }
00094
00095 void replay_input_wme(soar_callback_agent agent, soar_callback_data dummy, soar_call_data mode)
00096 {
00097
00098
00099
00100
00101
00102
00103 long tt;
00104 psoar_wme psw;
00105 captured_action *c_action;
00106 soarapi_wme *c_wme;
00107 agent;
00108 dummy;
00109
00110 if (mode != (soar_call_data) NORMAL_INPUT_CYCLE)
00111 return;
00112
00113
00114
00115 if (current_agent(d_cycle_count) > current_agent(dc_to_replay)) {
00116 print("\n\nWarning: end of replay has been reached.\n");
00117 return;
00118 }
00119 for (c_action = current_agent(replay_actions)[current_agent(d_cycle_count)];
00120 c_action != NULL; c_action = c_action->next) {
00121
00122 switch (c_action->action) {
00123
00124 case ADD_WME:
00125 c_wme = ((soarapi_wme *) c_action->args);
00126
00127 tt = soar_cAddWme(c_wme->id, c_wme->attr, c_wme->value, FALSE, &psw);
00128
00129 current_agent(replay_timetags)[c_wme->timetag] = tt;
00130
00131 if (c_wme->timetag != tt && !current_agent(timetag_mismatch)) {
00132
00133 print("\n\nWarning: replayed timetag mismatch (%d %d)!!", c_wme->timetag, tt);
00134
00135 current_agent(timetag_mismatch) = TRUE;
00136
00137 }
00138
00139 break;
00140
00141 case REMOVE_WME:
00142
00143 soar_cRemoveWmeUsingTimetag(current_agent(replay_timetags)[((soarapi_wme *) c_action->args)->timetag]);
00144 break;
00145
00146 default:
00147 print("Warning: Tried to replay an input with an unknown action type\n");
00148 break;
00149
00150 }
00151
00152 }
00153 return;
00154 }
00155
00156 #endif
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 int compare_firing_counts(const void *e1, const void *e2)
00178 {
00179 production *p1, *p2;
00180 unsigned long count1, count2;
00181
00182 p1 = *((production **) e1);
00183 p2 = *((production **) e2);
00184
00185 count1 = p1->firing_count;
00186 count2 = p2->firing_count;
00187
00188 return (count1 < count2) ? -1 : (count1 > count2) ? 1 : 0;
00189 }
00190
00191 production_memory_use *print_memories_insert_in_list(production_memory_use * new, production_memory_use * list)
00192 {
00193 production_memory_use *ctr, *prev;
00194
00195
00196 if ((list == NULL) || (new->mem >= list->mem)) {
00197 new->next = list;
00198 return new;
00199 }
00200
00201
00202 prev = list;
00203 for (ctr = list->next; ctr != NULL; ctr = ctr->next) {
00204 if (new->mem >= ctr->mem) {
00205 prev->next = new;
00206 new->next = ctr;
00207 return list;
00208 }
00209 prev = ctr;
00210 }
00211
00212
00213 prev->next = new;
00214 new->next = NULL;
00215 return list;
00216 }
00217
00218 int read_wme_filter_component(const char *s, Symbol ** sym)
00219 {
00220 get_lexeme_from_string(s);
00221 if (current_agent(lexeme).type == IDENTIFIER_LEXEME) {
00222
00223 if ((*sym = find_identifier(current_agent(lexeme).id_letter, current_agent(lexeme).id_number)) == NIL) {
00224 return -1;
00225 }
00226 } else
00227 *sym = make_symbol_for_current_lexeme();
00228 return 0;
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 void soar_alternate_input(agent * ai_agent, const char *ai_string, const char *ai_suffix, bool ai_exit)
00251 {
00252 ai_agent->alternate_input_string = ai_string;
00253 ai_agent->alternate_input_suffix = ai_suffix;
00254 ai_agent->current_char = ' ';
00255 ai_agent->alternate_input_exit = ai_exit;
00256 return;
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 int read_attribute_from_string(Symbol * id, const char *the_lexeme, Symbol * *attr)
00274 {
00275 Symbol *attr_tmp;
00276 slot *s;
00277
00278
00279 if (*the_lexeme == '^') {
00280 the_lexeme++;
00281 }
00282
00283 get_lexeme_from_string(the_lexeme);
00284
00285 switch (current_agent(lexeme).type) {
00286 case SYM_CONSTANT_LEXEME:
00287 attr_tmp = find_sym_constant(current_agent(lexeme).string);
00288 break;
00289 case INT_CONSTANT_LEXEME:
00290 attr_tmp = find_int_constant(current_agent(lexeme).int_val);
00291 break;
00292 case FLOAT_CONSTANT_LEXEME:
00293 attr_tmp = find_float_constant(current_agent(lexeme).float_val);
00294 break;
00295 case IDENTIFIER_LEXEME:
00296 attr_tmp = find_identifier(current_agent(lexeme).id_letter, current_agent(lexeme).id_number);
00297 break;
00298 case VARIABLE_LEXEME:
00299 attr_tmp = read_identifier_or_context_variable();
00300 if (!attr_tmp)
00301 return SOAR_ERROR;
00302 break;
00303 default:
00304 return SOAR_ERROR;
00305 }
00306 s = find_slot(id, attr_tmp);
00307 if (s) {
00308 *attr = attr_tmp;
00309 return SOAR_OK;
00310 } else
00311 return SOAR_ERROR;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 void print_preference_and_source(preference * pref, bool print_source, wme_trace_type wtt)
00332 {
00333 print_string(" ");
00334 print_object_trace(pref->value);
00335 print(" %c", preference_type_indicator(pref->type));
00336 if (preference_is_binary(pref->type))
00337 print_object_trace(pref->referent);
00338 if (pref->o_supported)
00339 print(" :O ");
00340 print("\n");
00341 if (print_source) {
00342 print(" From ");
00343 print_instantiation_with_wmes(pref->inst, wtt);
00344 print("\n");
00345 }
00346 }