00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include "soarkernel.h"
00049
00050 #include <ctype.h>
00051
00052 #include "explain.h"
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 #define add_to_grounds(cond) { \
00096 if ((cond)->bt.wme->grounds_tc != current_agent(grounds_tc)) { \
00097 (cond)->bt.wme->grounds_tc = current_agent(grounds_tc); \
00098 push ((cond), current_agent(grounds)); } }
00099
00100 #define add_to_potentials(cond) { \
00101 if ((cond)->bt.wme->potentials_tc != current_agent(potentials_tc)) { \
00102 (cond)->bt.wme->potentials_tc = current_agent(potentials_tc); \
00103 (cond)->bt.wme->chunker_bt_pref = (cond)->bt.trace; \
00104 push ((cond), current_agent(positive_potentials)); \
00105 } else if ((cond)->bt.wme->chunker_bt_pref != (cond)->bt.trace) { \
00106 push ((cond), current_agent(positive_potentials)); } }
00107
00108 #define add_to_locals(cond) { \
00109 if ((cond)->bt.wme->locals_tc != current_agent(locals_tc)) { \
00110 (cond)->bt.wme->locals_tc = current_agent(locals_tc); \
00111 (cond)->bt.wme->chunker_bt_pref = (cond)->bt.trace; \
00112 push ((cond), current_agent(locals)); \
00113 } else if ((cond)->bt.wme->chunker_bt_pref != (cond)->bt.trace) { \
00114 push ((cond), current_agent(locals)); } }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 void print_consed_list_of_conditions(list * c, int indent)
00133 {
00134 for (; c != NIL; c = c->rest) {
00135 if (get_printer_output_column() >= COLUMNS_PER_LINE - 20)
00136 print("\n ");
00137
00138
00139 print_spaces(indent);
00140 print_condition(c->first);
00141 }
00142 }
00143
00144
00145 void print_consed_list_of_condition_wmes(list * c, int indent)
00146 {
00147 for (; c != NIL; c = c->rest) {
00148 if (get_printer_output_column() >= COLUMNS_PER_LINE - 20)
00149 print("\n ");
00150
00151
00152 print_spaces(indent);
00153 print(" ");
00154 print_wme(((condition *) (c->first))->bt.wme);
00155 }
00156 }
00157
00158
00159
00160
00161
00162 void backtrace_through_instantiation(instantiation * inst,
00163 goal_stack_level grounds_level, condition * trace_cond, int indent)
00164 {
00165
00166 tc_number tc;
00167 tc_number tc2;
00168 condition *c;
00169 list *grounds_to_print, *pots_to_print, *locals_to_print, *negateds_to_print;
00170 bool need_another_pass;
00171 backtrace_str temp_explain_backtrace;
00172
00173 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00174
00175 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00176
00177
00178 print_spaces(indent);
00179 print("... BT through instantiation of ");
00180 if (inst->prod)
00181 print_with_symbols("%y\n", inst->prod->name);
00182 else
00183 print_string("[dummy production]\n");
00184 }
00185 #endif
00186
00187
00188 if (inst->backtrace_number == current_agent(backtrace_number)) {
00189
00190 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00191 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00192
00193
00194 print_spaces(indent);
00195 print_string("(We already backtraced through this instantiation.)\n");
00196 }
00197 #endif
00198
00199 return;
00200 }
00201 inst->backtrace_number = current_agent(backtrace_number);
00202
00203
00204
00205 if (current_agent(sysparams)[EXPLAIN_SYSPARAM]) {
00206 temp_explain_backtrace.trace_cond = trace_cond;
00207 if (trace_cond == NULL)
00208 temp_explain_backtrace.result = TRUE;
00209 else
00210 temp_explain_backtrace.result = FALSE;
00211
00212 temp_explain_backtrace.grounds = NIL;
00213 temp_explain_backtrace.potentials = NIL;
00214 temp_explain_backtrace.locals = NIL;
00215 temp_explain_backtrace.negated = NIL;
00216
00217 if (inst->prod) {
00218 strncpy(temp_explain_backtrace.prod_name, inst->prod->name->sc.name, PROD_NAME_SIZE);
00219 } else {
00220 strncpy(temp_explain_backtrace.prod_name, "Dummy production", PROD_NAME_SIZE);
00221 }
00222 temp_explain_backtrace.prod_name[PROD_NAME_SIZE - 1] = 0;
00223
00224 temp_explain_backtrace.next_backtrace = NULL;
00225 }
00226
00227
00228 if (!inst->okay_to_variablize)
00229 current_agent(variablize_this_chunk) = FALSE;
00230
00231
00232
00233 tc = get_new_tc_number();
00234 tc2 = get_new_tc_number();
00235 need_another_pass = FALSE;
00236
00237 for (c = inst->top_of_instantiated_conditions; c != NIL; c = c->next) {
00238 Symbol *id, *value;
00239
00240 if (c->type != POSITIVE_CONDITION)
00241 continue;
00242 id = referent_of_equality_test(c->data.tests.id_test);
00243
00244 if (id->id.tc_num == tc) {
00245
00246 value = referent_of_equality_test(c->data.tests.value_test);
00247 if (value->common.symbol_type == IDENTIFIER_SYMBOL_TYPE) {
00248
00249
00250 if (value->id.tc_num == tc2)
00251 need_another_pass = TRUE;
00252 value->id.tc_num = tc;
00253 }
00254 } else if ((id->id.isa_goal) && (c->bt.level <= grounds_level)) {
00255
00256 id->id.tc_num = tc;
00257 value = referent_of_equality_test(c->data.tests.value_test);
00258 if (value->common.symbol_type == IDENTIFIER_SYMBOL_TYPE) {
00259
00260
00261 if (value->id.tc_num == tc2)
00262 need_another_pass = TRUE;
00263 value->id.tc_num = tc;
00264 }
00265 } else {
00266
00267
00268 id->id.tc_num = tc2;
00269 }
00270 }
00271
00272
00273
00274
00275 while (need_another_pass) {
00276 Symbol *value;
00277
00278 need_another_pass = FALSE;
00279 for (c = inst->top_of_instantiated_conditions; c != NIL; c = c->next) {
00280 if (c->type != POSITIVE_CONDITION)
00281 continue;
00282 if (referent_of_equality_test(c->data.tests.id_test)->id.tc_num != tc)
00283 continue;
00284 value = referent_of_equality_test(c->data.tests.value_test);
00285 if (value->common.symbol_type == IDENTIFIER_SYMBOL_TYPE)
00286 if (value->id.tc_num != tc) {
00287 value->id.tc_num = tc;
00288 need_another_pass = TRUE;
00289 }
00290 }
00291 }
00292
00293
00294 grounds_to_print = NIL;
00295 pots_to_print = NIL;
00296 locals_to_print = NIL;
00297 negateds_to_print = NIL;
00298
00299
00300
00301 for (c = inst->top_of_instantiated_conditions; c != NIL; c = c->next) {
00302 if (c->type == POSITIVE_CONDITION) {
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 if (referent_of_equality_test(c->data.tests.id_test)->id.tc_num == tc) {
00332 add_to_grounds(c);
00333
00334 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00335 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM] || current_agent(sysparams)[EXPLAIN_SYSPARAM])
00336 #else
00337 if (current_agent(sysparams)[EXPLAIN_SYSPARAM])
00338 #endif
00339 push(c, grounds_to_print);
00340 } else if (c->bt.level <= grounds_level) {
00341 add_to_potentials(c);
00342 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM] || current_agent(sysparams)[EXPLAIN_SYSPARAM])
00343 push(c, pots_to_print);
00344 } else {
00345 add_to_locals(c);
00346 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00347 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM] || current_agent(sysparams)[EXPLAIN_SYSPARAM])
00348 #else
00349 if (current_agent(sysparams)[EXPLAIN_SYSPARAM])
00350 #endif
00351 push(c, locals_to_print);
00352 }
00353 } else {
00354
00355 add_to_chunk_cond_set(¤t_agent(negated_set), make_chunk_cond_for_condition(c));
00356 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00357 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM] || current_agent(sysparams)[EXPLAIN_SYSPARAM])
00358 #else
00359 if (current_agent(sysparams)[EXPLAIN_SYSPARAM])
00360 #endif
00361 push(c, negateds_to_print);
00362 }
00363 }
00364
00365
00366 if (inst->nots)
00367 push(inst, current_agent(instantiations_with_nots));
00368
00369
00370
00371
00372
00373
00374 if (current_agent(sysparams)[EXPLAIN_SYSPARAM])
00375 explain_add_temp_to_backtrace_list(&temp_explain_backtrace, grounds_to_print,
00376 pots_to_print, locals_to_print, negateds_to_print);
00377
00378 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00379
00380
00381 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00382 not *not1;
00383
00384
00385 print_spaces(indent);
00386 print_string(" -->Grounds:\n");
00387 print_consed_list_of_condition_wmes(grounds_to_print, indent);
00388 print("\n");
00389 print_spaces(indent);
00390 print_string("\n -->Potentials:\n");
00391 print_consed_list_of_condition_wmes(pots_to_print, indent);
00392 print("\n");
00393 print_spaces(indent);
00394 print_string(" -->Locals:\n");
00395 print_consed_list_of_condition_wmes(locals_to_print, indent);
00396 print("\n");
00397 print_spaces(indent);
00398 print_string(" -->Negated:\n");
00399 print_consed_list_of_conditions(negateds_to_print, indent);
00400 print("\n");
00401 print_spaces(indent);
00402 print_string(" -->Nots:\n");
00403
00404
00405 for (not1 = inst->nots; not1 != NIL; not1 = not1->next)
00406 print_with_symbols(" %y <> %y\n", not1->s1, not1->s2);
00407 }
00408 #endif
00409
00410
00411
00412
00413 free_list(grounds_to_print);
00414 free_list(pots_to_print);
00415 free_list(locals_to_print);
00416 free_list(negateds_to_print);
00417 }
00418
00419
00420
00421
00422
00423
00424
00425
00426 void trace_locals(goal_stack_level grounds_level)
00427 {
00428
00429
00430 cons *c, *prohibits;
00431 condition *cond;
00432 preference *bt_pref, *p;
00433
00434 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00435 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM])
00436 print_string("\n\n*** Tracing Locals ***\n");
00437 #endif
00438
00439 while (current_agent(locals)) {
00440 c = current_agent(locals);
00441 current_agent(locals) = current_agent(locals)->rest;
00442 cond = c->first;
00443 free_cons(c);
00444
00445 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00446
00447 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00448 print_string("\nFor local ");
00449 print_wme(cond->bt.wme);
00450 print_string(" ");
00451 }
00452 #endif
00453
00454 bt_pref = find_clone_for_level(cond->bt.trace, (goal_stack_level) (grounds_level + 1));
00455
00456 if (bt_pref) {
00457
00458 #ifdef NO_TOP_JUST
00459 if (bt_pref->inst) {
00460
00461
00462 backtrace_through_instantiation(bt_pref->inst, grounds_level, cond, 0);
00463
00464
00465 if (cond->bt.prohibits) {
00466 for (prohibits = cond->bt.prohibits; prohibits != NIL; prohibits = prohibits->rest) {
00467 p = prohibits->first;
00468
00469 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00470 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00471 print_string(" For prohibit preference: ");
00472 print_preference(p);
00473 }
00474 #endif
00475 if (p->inst)
00476 backtrace_through_instantiation(p->inst, grounds_level, cond, 6);
00477 }
00478 }
00479
00480 }
00481 #else
00482
00483
00484 backtrace_through_instantiation(bt_pref->inst, grounds_level, cond, 0);
00485
00486
00487 if (cond->bt.prohibits) {
00488 for (prohibits = cond->bt.prohibits; prohibits != NIL; prohibits = prohibits->rest) {
00489 p = prohibits->first;
00490
00491 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00492 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00493 print_string(" For prohibit preference: ");
00494 print_preference(p);
00495 }
00496 #endif
00497 backtrace_through_instantiation(p->inst, grounds_level, cond, 6);
00498 }
00499 }
00500
00501
00502 #endif
00503 continue;
00504 }
00505 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00506 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM])
00507 print_string("...no trace, can't BT");
00508 #endif
00509
00510
00511
00512 if (referent_of_equality_test(cond->data.tests.id_test)->id.isa_goal) {
00513 if ((referent_of_equality_test(cond->data.tests.attr_test) ==
00514 current_agent(quiescence_symbol)) &&
00515 (referent_of_equality_test(cond->data.tests.value_test) ==
00516 current_agent(t_symbol)) && (!cond->test_for_acceptable_preference)) {
00517 current_agent(variablize_this_chunk) = FALSE;
00518 current_agent(quiescence_t_flag) = TRUE;
00519 }
00520 continue;
00521 }
00522
00523
00524
00525 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00526 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM])
00527 print_string(" --> make it a potential.");
00528 #endif
00529 add_to_potentials(cond);
00530
00531 }
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 void trace_grounded_potentials(void)
00544 {
00545 tc_number tc;
00546 cons *c, *next_c, *prev_c;
00547 condition *pot;
00548 bool need_another_pass;
00549
00550 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00551
00552 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM])
00553 print_string("\n\n*** Tracing Grounded Potentials ***\n");
00554 #endif
00555
00556
00557 tc = get_new_tc_number();
00558 for (c = current_agent(grounds); c != NIL; c = c->rest)
00559 add_cond_to_tc(c->first, tc, NIL, NIL);
00560
00561 need_another_pass = TRUE;
00562 while (need_another_pass) {
00563 need_another_pass = FALSE;
00564
00565 prev_c = NIL;
00566 for (c = current_agent(positive_potentials); c != NIL; c = next_c) {
00567 next_c = c->rest;
00568 pot = c->first;
00569 if (cond_is_in_tc(pot, tc)) {
00570
00571
00572 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00573 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00574 print_string("\n-->Moving to grounds: ");
00575 print_wme(pot->bt.wme);
00576 }
00577 #endif
00578 if (prev_c)
00579 prev_c->rest = next_c;
00580 else
00581 current_agent(positive_potentials) = next_c;
00582 if (pot->bt.wme->grounds_tc != current_agent(grounds_tc)) {
00583 pot->bt.wme->grounds_tc = current_agent(grounds_tc);
00584 c->rest = current_agent(grounds);
00585 current_agent(grounds) = c;
00586 add_cond_to_tc(pot, tc, NIL, NIL);
00587 need_another_pass = TRUE;
00588 } else {
00589 free_cons(c);
00590 }
00591 } else {
00592 prev_c = c;
00593 }
00594 }
00595 }
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608 bool trace_ungrounded_potentials(goal_stack_level grounds_level)
00609 {
00610
00611
00612 cons *c, *next_c, *prev_c, *prohibits;
00613 cons *pots_to_bt;
00614 condition *potential;
00615 preference *bt_pref, *p;
00616
00617 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00618 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM])
00619 print_string("\n\n*** Tracing Ungrounded Potentials ***\n");
00620 #endif
00621
00622
00623
00624 pots_to_bt = NIL;
00625 prev_c = NIL;
00626 for (c = current_agent(positive_potentials); c != NIL; c = next_c) {
00627 next_c = c->rest;
00628 potential = c->first;
00629 bt_pref = find_clone_for_level(potential->bt.trace, (goal_stack_level) (grounds_level + 1));
00630 if (bt_pref) {
00631 if (prev_c)
00632 prev_c->rest = next_c;
00633 else
00634 current_agent(positive_potentials) = next_c;
00635 c->rest = pots_to_bt;
00636 pots_to_bt = c;
00637 } else {
00638 prev_c = c;
00639 }
00640 }
00641
00642
00643 if (!pots_to_bt)
00644 return FALSE;
00645
00646
00647 while (pots_to_bt) {
00648 c = pots_to_bt;
00649 pots_to_bt = pots_to_bt->rest;
00650 potential = c->first;
00651 free_cons(c);
00652
00653 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00654 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00655 print_string("\nFor ungrounded potential ");
00656 print_wme(potential->bt.wme);
00657 print_string(" ");
00658 }
00659 #endif
00660
00661 bt_pref = find_clone_for_level(potential->bt.trace, (goal_stack_level) (grounds_level + 1));
00662
00663 #ifdef NO_TOP_JUST
00664 if (bt_pref->inst)
00665 backtrace_through_instantiation(bt_pref->inst, grounds_level, potential, 0);
00666 #else
00667
00668
00669 backtrace_through_instantiation(bt_pref->inst, grounds_level, potential, 0);
00670 #endif
00671
00672 if (potential->bt.prohibits) {
00673 for (prohibits = potential->bt.prohibits; prohibits != NIL; prohibits = prohibits->rest) {
00674 p = prohibits->first;
00675
00676 #ifndef TRACE_CONTEXT_DECISIONS_ONLY
00677 if (current_agent(sysparams)[TRACE_BACKTRACING_SYSPARAM]) {
00678 print_string(" For prohibit preference: ");
00679 print_preference(p);
00680 }
00681 #endif
00682
00683 #ifdef NO_TOP_JUST
00684 if (p->inst)
00685 backtrace_through_instantiation(p->inst, grounds_level, potential, 6);
00686 #else
00687 backtrace_through_instantiation(p->inst, grounds_level, potential, 6);
00688 #endif
00689
00690 }
00691 }
00692
00693 }
00694 return TRUE;
00695 }