Program: 12274
Project: 12274
Buggy source:
/** 
 * This the main chunking routine. It takes an instantiation, and a flag "allow_variablization"--if FALSE, the chunk will not be variablized. (If TRUE, it may still not be variablized, due to chunk-free-problem-spaces, ^quiescence t, etc.) <p>chunk.cpp:902:chunk_instantiation
 * @param inst
 * @param allow_variablization
 */
public void chunk_instantiation(Instantiation inst,boolean allow_variablization){
  if (inst.match_goal == null)   return;
  Preference pref=null;
  for (ListItem<Preference> i=inst.preferences_generated.first; i != null; i=i.next) {
    final Preference temp=i.item;
    if (temp.id.level < inst.match_goal_level) {
      pref=temp;
      break;
    }
  }
  if (pref == null)   return;
  final Trace trace=context.getTrace();
  if (this.learningOn.value.get()) {
    if (pref.id.level < (inst.match_goal_level - 1)) {
      allow_variablization=false;
      inst.okay_to_variablize=false;
      trace.print(Category.VERBOSE,"String_Node_Str");
    }
 else {
      allow_variablization=isLearningOn();
      inst.okay_to_variablize=isLearningOn();
      trace.print(Category.VERBOSE,"String_Node_Str",allow_variablization);
    }
  }
  Preference results=get_results_for_instantiation(inst);
  if (results == null) {
    return;
  }
  for (IdentifierImpl g=inst.match_goal.higher_goal; g != null && g.allow_bottom_up_chunks; g=g.higher_goal)   g.allow_bottom_up_chunks=false;
  int grounds_level=inst.match_goal_level - 1;
  backtrace.backtrace_number++;
  if (backtrace.backtrace_number == 0)   backtrace.backtrace_number=1;
  backtrace.grounds_tc++;
  if (backtrace.grounds_tc == 0)   backtrace.grounds_tc=1;
  backtrace.potentials_tc++;
  if (backtrace.potentials_tc == 0)   backtrace.potentials_tc=1;
  backtrace.locals_tc++;
  if (backtrace.locals_tc == 0)   backtrace.locals_tc=1;
  backtrace.grounds.clear();
  backtrace.positive_potentials.clear();
  backtrace.locals.clear();
  this.instantiations_with_nots.clear();
  if (allow_variablization && (!learningAllGoals))   allow_variablization=inst.match_goal.allow_bottom_up_chunks;
  this.chunk_free_flag=false;
  this.chunky_flag=false;
  if (allow_variablization) {
    if (learningExcept) {
      if (chunk_free_problem_spaces.contains(inst.match_goal)) {
        allow_variablization=false;
        this.chunk_free_flag=true;
      }
    }
 else     if (learningOnly) {
      if (chunky_problem_spaces.contains(inst.match_goal)) {
        allow_variablization=true;
        this.chunky_flag=true;
      }
 else {
        allow_variablization=false;
        this.chunky_flag=false;
      }
    }
  }
  this.variablize_this_chunk=allow_variablization;
  ExplainChunk temp_explain_chunk=null;
  if (this.explain.isEnabled()) {
    temp_explain_chunk=new ExplainChunk();
    this.explain.reset_backtrace_list();
  }
  for (pref=results; pref != null; pref=pref.next_result) {
    trace.print(Category.BACKTRACING,"String_Node_Str",pref);
    backtrace.backtrace_through_instantiation(pref.inst,grounds_level,null,0);
  }
  this.quiescence_t_flag=false;
  while (true) {
    backtrace.trace_locals(grounds_level);
    backtrace.trace_grounded_potentials();
    if (!backtrace.trace_ungrounded_potentials(grounds_level))     break;
  }
  backtrace.positive_potentials.clear();
  ByRef<ChunkCondition> top_cc=ByRef.create(null);
  ByRef<ChunkCondition> bottom_cc=ByRef.create(null);
  NotStruct nots=null;
{
    final Marker tc_for_grounds=DefaultMarker.create();
    build_chunk_conds_for_grounds_and_add_negateds(top_cc,bottom_cc,tc_for_grounds);
    nots=get_nots_for_instantiated_conditions(instantiations_with_nots,tc_for_grounds);
  }
  StringSymbol prod_name=null;
  ProductionType prod_type=null;
  boolean print_name=false;
  boolean print_prod=false;
  if (this.variablize_this_chunk) {
    this.chunks_this_d_cycle++;
    prod_name=generate_chunk_name_sym_constant(inst);
    prod_type=ProductionType.CHUNK;
    print_name=trace.isEnabled(Category.CHUNK_NAMES);
    trace.print(Category.CHUNK_NAMES,"String_Node_Str",prod_name);
    print_prod=trace.isEnabled(Category.CHUNKS);
  }
 else {
    prod_name=context.getSymbols().generateUniqueString("String_Node_Str",justification_count);
    prod_type=ProductionType.JUSTIFICATION;
    print_name=trace.isEnabled(Category.JUSTIFICATION_NAMES);
    trace.print(Category.JUSTIFICATION_NAMES,"String_Node_Str",prod_name);
    print_prod=trace.isEnabled(Category.JUSTIFICATIONS);
  }
  if (top_cc.value == null) {
    context.getPrinter().print("String_Node_Str");
    return;
  }
  if (this.chunks_this_d_cycle > maxChunks) {
    context.getPrinter().warn("String_Node_Str");
    this.maxChunksReached=true;
    return;
  }
  Condition lhs_top=top_cc.value.variablized_cond;
  Condition lhs_bottom=bottom_cc.value.variablized_cond;
  this.variableGenerator.reset(lhs_top,null);
  this.variablization_tc=DefaultMarker.create();
  variablize_condition_list(lhs_top);
  variablize_nots_and_insert_into_conditions(nots,lhs_top);
  Action rhs=copy_and_variablize_result_list(results);
  add_goal_or_impasse_tests(top_cc.value.next_prev);
  Production prod=new Production(prod_type,prod_name,lhs_top,lhs_bottom,rhs);
  try {
    context.getProductions().addChunk(prod);
  }
 catch (  ReordererException e) {
    final Printer p=context.getPrinter();
    p.print("String_Node_Str");
    Conditions.print_condition_list(p,lhs_top,2,false);
    p.print("String_Node_Str");
    Action.print_action_list(p,rhs,3,false);
    p.print("String_Node_Str");
    p.print("String_Node_Str");
    p.print("String_Node_Str");
    p.print("String_Node_Str");
    p.print("String_Node_Str");
    this.decisionCycle.halt("String_Node_Str");
    return;
  }
  Instantiation chunk_inst=null;
{
    ByRef<Condition> inst_lhs_top=ByRef.create(null);
    ByRef<Condition> inst_lhs_bottom=ByRef.create(null);
    reorder_instantiated_conditions(top_cc.value.next_prev.toListHead(),inst_lhs_top,inst_lhs_bottom);
    if (this.explain.isEnabled())     temp_explain_chunk.all_grounds=inst_lhs_top.value;
    chunk_inst=new Instantiation(prod,null,null);
    chunk_inst.top_of_instantiated_conditions=inst_lhs_top.value;
    chunk_inst.bottom_of_instantiated_conditions=inst_lhs_bottom.value;
    chunk_inst.nots=nots;
    chunk_inst.GDS_evaluated_already=false;
    if (!learningOnly) {
      if ((!this.variablize_this_chunk) && (this.chunk_free_flag) && (!this.quiescence_t_flag))       chunk_inst.okay_to_variablize=true;
 else       chunk_inst.okay_to_variablize=this.variablize_this_chunk;
    }
 else {
      if ((!this.variablize_this_chunk) && (!this.chunky_flag) && (!this.quiescence_t_flag))       chunk_inst.okay_to_variablize=true;
 else       chunk_inst.okay_to_variablize=this.variablize_this_chunk;
    }
    chunk_inst.in_ms=true;
    make_clones_of_results(results,chunk_inst);
    recMemory.fill_in_new_instantiation_stuff(chunk_inst,true,decider.top_goal);
  }
  if (this.explain.isEnabled()) {
    ByRef<Condition> new_top=ByRef.create(null);
    ByRef<Condition> new_bottom=ByRef.create(null);
    Condition.copy_condition_list(prod.condition_list,new_top,new_bottom);
    temp_explain_chunk.conds=new_top.value;
    temp_explain_chunk.actions=copy_and_variablize_result_list(results);
  }
  ProductionAddResult rete_addition_result=this.rete.add_production_to_rete(prod,chunk_inst,print_name,false);
  if (this.explain.isEnabled())   if ((rete_addition_result != ProductionAddResult.DUPLICATE_PRODUCTION) && ((prod_type != ProductionType.JUSTIFICATION) || (rete_addition_result != ProductionAddResult.REFRACTED_INST_DID_NOT_MATCH))) {
    temp_explain_chunk.name=prod_name.getValue();
    this.explain.explain_add_temp_to_chunk_list(temp_explain_chunk);
  }
 else {
  }
  if (print_prod && (rete_addition_result != ProductionAddResult.DUPLICATE_PRODUCTION)) {
    context.getPrinter().print("String_Node_Str");
    prod.print(context.getPrinter(),false);
  }
  if (rete_addition_result == ProductionAddResult.DUPLICATE_PRODUCTION) {
    context.getProductions().exciseProduction(prod,false);
  }
 else   if ((prod_type == ProductionType.JUSTIFICATION) && (rete_addition_result == ProductionAddResult.REFRACTED_INST_DID_NOT_MATCH)) {
    context.getProductions().exciseProduction(prod,false);
  }
  if (rete_addition_result != ProductionAddResult.REFRACTED_INST_MATCHED) {
    chunk_inst.in_ms=false;
  }
  if (rete_addition_result != ProductionAddResult.DUPLICATE_PRODUCTION) {
    context.getEventManager().fireEvent(new ProductionAddedEvent(context,prod));
  }
  chunk_inst.inProdList.insertAtHead(recMemory.newly_created_instantiations);
  if (!maxChunksReached)   chunk_instantiation(chunk_inst,variablize_this_chunk);
  return;
}

Fixed version (ground truth):
/** 
 * This the main chunking routine. It takes an instantiation, and a flag "allow_variablization"--if FALSE, the chunk will not be variablized. (If TRUE, it may still not be variablized, due to chunk-free-problem-spaces, ^quiescence t, etc.) <p>chunk.cpp:902:chunk_instantiation
 * @param inst
 * @param allow_variablization
 */
public void chunk_instantiation(Instantiation inst,boolean allow_variablization){
  if (inst.match_goal == null)   return;
  Preference pref=null;
  for (ListItem<Preference> i=inst.preferences_generated.first; i != null; i=i.next) {
    final Preference temp=i.item;
    if (temp.id.level < inst.match_goal_level) {
      pref=temp;
      break;
    }
  }
  if (pref == null)   return;
  final Trace trace=context.getTrace();
  if (this.learningOn.value.get()) {
    if (pref.id.level < (inst.match_goal_level - 1)) {
      allow_variablization=false;
      inst.okay_to_variablize=false;
      trace.print(Category.VERBOSE,"String_Node_Str");
    }
 else {
      allow_variablization=isLearningOn();
      inst.okay_to_variablize=isLearningOn();
      trace.print(Category.VERBOSE,"String_Node_Str",allow_variablization);
    }
  }
  Preference results=get_results_for_instantiation(inst);
  if (results == null) {
    return;
  }
  for (IdentifierImpl g=inst.match_goal.higher_goal; g != null && g.allow_bottom_up_chunks; g=g.higher_goal)   g.allow_bottom_up_chunks=false;
  int grounds_level=inst.match_goal_level - 1;
  backtrace.backtrace_number++;
  if (backtrace.backtrace_number == 0)   backtrace.backtrace_number=1;
  backtrace.grounds_tc++;
  if (backtrace.grounds_tc == 0)   backtrace.grounds_tc=1;
  backtrace.potentials_tc++;
  if (backtrace.potentials_tc == 0)   backtrace.potentials_tc=1;
  backtrace.locals_tc++;
  if (backtrace.locals_tc == 0)   backtrace.locals_tc=1;
  backtrace.grounds.clear();
  backtrace.positive_potentials.clear();
  backtrace.locals.clear();
  this.instantiations_with_nots.clear();
  if (allow_variablization && (!learningAllGoals))   allow_variablization=inst.match_goal.allow_bottom_up_chunks;
  boolean chunk_free_flag=false;
  boolean chunky_flag=false;
  if (allow_variablization) {
    if (learningExcept) {
      if (chunk_free_problem_spaces.contains(inst.match_goal)) {
        context.getTrace().print(EnumSet.of(Category.VERBOSE,Category.WM_CHANGES),"String_Node_Str",inst.match_goal);
        allow_variablization=false;
        chunk_free_flag=true;
      }
    }
 else     if (learningOnly) {
      if (chunky_problem_spaces.contains(inst.match_goal)) {
        chunky_flag=true;
      }
 else {
        context.getTrace().print(EnumSet.of(Category.VERBOSE,Category.WM_CHANGES),"String_Node_Str",inst.match_goal);
        allow_variablization=false;
      }
    }
  }
  this.variablize_this_chunk=allow_variablization;
  ExplainChunk temp_explain_chunk=null;
  if (this.explain.isEnabled()) {
    temp_explain_chunk=new ExplainChunk();
    this.explain.reset_backtrace_list();
  }
  for (pref=results; pref != null; pref=pref.next_result) {
    trace.print(Category.BACKTRACING,"String_Node_Str",pref);
    backtrace.backtrace_through_instantiation(pref.inst,grounds_level,null,0);
  }
  this.quiescence_t_flag=false;
  while (true) {
    backtrace.trace_locals(grounds_level);
    backtrace.trace_grounded_potentials();
    if (!backtrace.trace_ungrounded_potentials(grounds_level))     break;
  }
  backtrace.positive_potentials.clear();
  ByRef<ChunkCondition> top_cc=ByRef.create(null);
  ByRef<ChunkCondition> bottom_cc=ByRef.create(null);
  NotStruct nots=null;
{
    final Marker tc_for_grounds=DefaultMarker.create();
    build_chunk_conds_for_grounds_and_add_negateds(top_cc,bottom_cc,tc_for_grounds);
    nots=get_nots_for_instantiated_conditions(instantiations_with_nots,tc_for_grounds);
  }
  StringSymbol prod_name=null;
  ProductionType prod_type=null;
  boolean print_name=false;
  boolean print_prod=false;
  if (this.variablize_this_chunk) {
    this.chunks_this_d_cycle++;
    prod_name=generate_chunk_name_sym_constant(inst);
    prod_type=ProductionType.CHUNK;
    print_name=trace.isEnabled(Category.CHUNK_NAMES);
    trace.print(Category.CHUNK_NAMES,"String_Node_Str",prod_name);
    print_prod=trace.isEnabled(Category.CHUNKS);
  }
 else {
    prod_name=context.getSymbols().generateUniqueString("String_Node_Str",justification_count);
    prod_type=ProductionType.JUSTIFICATION;
    print_name=trace.isEnabled(Category.JUSTIFICATION_NAMES);
    trace.print(Category.JUSTIFICATION_NAMES,"String_Node_Str",prod_name);
    print_prod=trace.isEnabled(Category.JUSTIFICATIONS);
  }
  if (top_cc.value == null) {
    context.getPrinter().print("String_Node_Str");
    return;
  }
  if (this.chunks_this_d_cycle > maxChunks) {
    context.getPrinter().warn("String_Node_Str");
    this.maxChunksReached=true;
    return;
  }
  Condition lhs_top=top_cc.value.variablized_cond;
  Condition lhs_bottom=bottom_cc.value.variablized_cond;
  this.variableGenerator.reset(lhs_top,null);
  this.variablization_tc=DefaultMarker.create();
  variablize_condition_list(lhs_top);
  variablize_nots_and_insert_into_conditions(nots,lhs_top);
  Action rhs=copy_and_variablize_result_list(results);
  add_goal_or_impasse_tests(top_cc.value.next_prev);
  Production prod=new Production(prod_type,prod_name,lhs_top,lhs_bottom,rhs);
  try {
    context.getProductions().addChunk(prod);
  }
 catch (  ReordererException e) {
    final Printer p=context.getPrinter();
    p.print("String_Node_Str");
    Conditions.print_condition_list(p,lhs_top,2,false);
    p.print("String_Node_Str");
    Action.print_action_list(p,rhs,3,false);
    p.print("String_Node_Str");
    p.print("String_Node_Str");
    p.print("String_Node_Str");
    p.print("String_Node_Str");
    p.print("String_Node_Str");
    this.decisionCycle.halt("String_Node_Str");
    return;
  }
  Instantiation chunk_inst=null;
{
    ByRef<Condition> inst_lhs_top=ByRef.create(null);
    ByRef<Condition> inst_lhs_bottom=ByRef.create(null);
    reorder_instantiated_conditions(top_cc.value.next_prev.toListHead(),inst_lhs_top,inst_lhs_bottom);
    if (this.explain.isEnabled())     temp_explain_chunk.all_grounds=inst_lhs_top.value;
    chunk_inst=new Instantiation(prod,null,null);
    chunk_inst.top_of_instantiated_conditions=inst_lhs_top.value;
    chunk_inst.bottom_of_instantiated_conditions=inst_lhs_bottom.value;
    chunk_inst.nots=nots;
    chunk_inst.GDS_evaluated_already=false;
    if (!learningOnly) {
      if ((!this.variablize_this_chunk) && (chunk_free_flag) && (!this.quiescence_t_flag))       chunk_inst.okay_to_variablize=true;
 else       chunk_inst.okay_to_variablize=this.variablize_this_chunk;
    }
 else {
      if ((!this.variablize_this_chunk) && (!chunky_flag) && (!this.quiescence_t_flag))       chunk_inst.okay_to_variablize=true;
 else       chunk_inst.okay_to_variablize=this.variablize_this_chunk;
    }
    chunk_inst.in_ms=true;
    make_clones_of_results(results,chunk_inst);
    recMemory.fill_in_new_instantiation_stuff(chunk_inst,true,decider.top_goal);
  }
  if (this.explain.isEnabled()) {
    ByRef<Condition> new_top=ByRef.create(null);
    ByRef<Condition> new_bottom=ByRef.create(null);
    Condition.copy_condition_list(prod.condition_list,new_top,new_bottom);
    temp_explain_chunk.conds=new_top.value;
    temp_explain_chunk.actions=copy_and_variablize_result_list(results);
  }
  ProductionAddResult rete_addition_result=this.rete.add_production_to_rete(prod,chunk_inst,print_name,false);
  if (this.explain.isEnabled())   if ((rete_addition_result != ProductionAddResult.DUPLICATE_PRODUCTION) && ((prod_type != ProductionType.JUSTIFICATION) || (rete_addition_result != ProductionAddResult.REFRACTED_INST_DID_NOT_MATCH))) {
    temp_explain_chunk.name=prod_name.getValue();
    this.explain.explain_add_temp_to_chunk_list(temp_explain_chunk);
  }
 else {
  }
  if (print_prod && (rete_addition_result != ProductionAddResult.DUPLICATE_PRODUCTION)) {
    context.getPrinter().print("String_Node_Str");
    prod.print(context.getPrinter(),false);
  }
  if (rete_addition_result == ProductionAddResult.DUPLICATE_PRODUCTION) {
    context.getProductions().exciseProduction(prod,false);
  }
 else   if ((prod_type == ProductionType.JUSTIFICATION) && (rete_addition_result == ProductionAddResult.REFRACTED_INST_DID_NOT_MATCH)) {
    context.getProductions().exciseProduction(prod,false);
  }
  if (rete_addition_result != ProductionAddResult.REFRACTED_INST_MATCHED) {
    chunk_inst.in_ms=false;
  }
  if (rete_addition_result != ProductionAddResult.DUPLICATE_PRODUCTION) {
    context.getEventManager().fireEvent(new ProductionAddedEvent(context,prod));
  }
  chunk_inst.inProdList.insertAtHead(recMemory.newly_created_instantiations);
  if (!maxChunksReached)   chunk_instantiation(chunk_inst,variablize_this_chunk);
  return;
}