示例#1
0
文件: tempmem.cpp 项目: GYGit/Soar
void remove_garbage_slots(agent* thisAgent)
{
    cons* c;
    slot* s;
    
    while (thisAgent->slots_for_possible_removal)
    {
        c = thisAgent->slots_for_possible_removal;
        thisAgent->slots_for_possible_removal = thisAgent->slots_for_possible_removal->rest;
        s = static_cast<slot_struct*>(c->first);
        free_cons(thisAgent, c);
        
        if (s->wmes || s->all_preferences)
        {
            /* --- don't deallocate it if it still has any wmes or preferences --- */
            s->marked_for_possible_removal = false;
            continue;
        }
        
        /* --- deallocate the slot --- */
#ifdef DEBUG_SLOTS
        print_with_symbols(thisAgent, "\nDeallocate slot %y ^%y", s->id, s->attr);
#endif
        
        /* MMA 9-2012 */
        if (s->CDPS && thisAgent->sysparams[CHUNK_THROUGH_EVALUATION_RULES_SYSPARAM])
        {
            clear_CDPS(thisAgent, s);
        }
        /* MMA 9-2012 end */
        
        if (s->changed && (!s->isa_context_slot))
        {
            remove_from_dll(thisAgent->changed_slots, s->changed, next, prev);
            free_with_pool(&thisAgent->dl_cons_pool, s->changed);
        }
        remove_from_dll(s->id->id->slots, s, next, prev);
        symbol_remove_ref(thisAgent, s->id);
        symbol_remove_ref(thisAgent, s->attr);
        if (s->wma_val_references != NIL)
        {
            s->wma_val_references->~wma_sym_reference_map();
            free_with_pool(&(thisAgent->wma_slot_refs_pool), s->wma_val_references);
            s->wma_val_references = NIL;
        }
        free_with_pool(&thisAgent->slot_pool, s);
    }
}
示例#2
0
文件: mem.cpp 项目: IvanLogvinov/soar
dl_list *extract_dl_list_elements (agent* thisAgent, dl_list **header, dl_cons_test_fn f) 
{
  dl_cons *first_extracted_element, *tail_of_extracted_elements;
  dl_cons *dc, *next_dc;

  first_extracted_element = NIL;
  tail_of_extracted_elements = NIL;

  for (dc=(*header); dc!=NIL; dc=next_dc) 
  {
    next_dc = dc->next;

    if (!f(dc,thisAgent)) 
       continue;
    
    remove_from_dll ((*header), dc, next, prev);
    
    if (first_extracted_element)
      tail_of_extracted_elements->next = dc;
    else
      first_extracted_element = dc;
    
    dc->prev = tail_of_extracted_elements;
    tail_of_extracted_elements = dc;
  }

/************************************************************************/

  if (first_extracted_element) 
     tail_of_extracted_elements->next = NIL;
  
  return first_extracted_element;
}
示例#3
0
void retract_instantiation (instantiation *inst) {
  preference *pref, *next;
  bool retracted_a_preference;
  bool trace_it;

  /* --- invoke callback function --- */
#ifndef NO_CALLBACKS
  soar_invoke_callbacks(soar_agent, 
			RETRACTION_CALLBACK,
			(soar_call_data) inst);
#endif
  
  retracted_a_preference = FALSE;
  
  trace_it = trace_firings_of_inst (inst);

  /* --- retract any preferences that are in TM and aren't o-supported --- */
  pref = inst->preferences_generated;
  while (pref!=NIL) {
    next = pref->inst_next;
    if (pref->in_tm && (! pref->o_supported)) {

      if (trace_it) {
        if (!retracted_a_preference) {
	  if (get_printer_output_column()!=1) print ("\n");  /* AGR 617/634 */
          print ("Retracting ");
          print_instantiation_with_wmes
            (inst, (wme_trace_type)current_agent(sysparams)[TRACE_FIRINGS_WME_TRACE_TYPE_SYSPARAM]);
          if (current_agent(sysparams)[TRACE_FIRINGS_PREFERENCES_SYSPARAM]) print (" -->");
        }
        if (current_agent(sysparams)[TRACE_FIRINGS_PREFERENCES_SYSPARAM]) {
          print (" ");
          print_preference (pref);
        }
      }

      remove_preference_from_tm (pref);
      retracted_a_preference = TRUE;
    }
    pref = next;
  }

  /* --- remove inst from list of instantiations of this production --- */
  remove_from_dll (inst->prod->instantiations, inst, next, prev);

  /* --- if retracting a justification, excise it --- */
  /*
   * if the reference_count on the production is 1 (or less) then the
   * only thing supporting this justification is the instantiation, hence
   * it has already been excised, and doing it again is wrong.
   */
  if (inst->prod->type==JUSTIFICATION_PRODUCTION_TYPE &&
      inst->prod->reference_count > 1)
    excise_production (inst->prod, FALSE);
  
  /* --- mark as no longer in MS, and possibly deallocate  --- */
  inst->in_ms = FALSE;
  possibly_deallocate_instantiation (inst);
}
示例#4
0
/* IMPORTANT: Any changes made to deallocate_preference should also be made to corresponding code in deallocate_instantiation */
void deallocate_preference(agent* thisAgent, preference* pref, bool dont_cache)
{
    /*  Remove from temporary memory and match goal if necessary */
    if (pref->in_tm) remove_preference_from_tm(thisAgent, pref);
    if (pref->on_goal_list) remove_from_dll(pref->inst->match_goal->id->preferences_from_goal, pref, all_of_goal_next, all_of_goal_prev);

    if (pref->inst)
    {
        if (!dont_cache) cache_preference_if_necessary(thisAgent, pref);
        remove_from_dll(pref->inst->preferences_generated, pref, inst_next, inst_prev);
        possibly_deallocate_instantiation(thisAgent, pref->inst);
    }

    /* Clean up contents and deallocate */
    deallocate_preference_contents(thisAgent, pref, dont_cache);

}
示例#5
0
void remove_preference_from_tm(agent* thisAgent, preference* pref)
{
    slot* s;

    s = pref->slot;

    /*  remove preference from the list for the slot */
    remove_from_dll(s->all_preferences, pref, all_of_slot_next, all_of_slot_prev);
    remove_from_dll(s->preferences[pref->type], pref, next, prev);

    /*  other miscellaneous stuff */
    pref->in_tm = false;
    pref->slot = NIL;      /* BUG shouldn't we use pref->slot in place of pref->in_tm? */
    mark_slot_as_changed(thisAgent, s);

    /*  if acceptable/require pref for context slot, we may need to remove
       a wme later */
    if ((s->isa_context_slot) &&
            ((pref->type == ACCEPTABLE_PREFERENCE_TYPE) ||
             (pref->type == REQUIRE_PREFERENCE_TYPE)))
    {
        mark_context_slot_as_acceptable_preference_changed(thisAgent, s);
    }

    /*  update identifier levels */
    if (pref->value->symbol_type == IDENTIFIER_SYMBOL_TYPE)
    {
        post_link_removal(thisAgent, pref->id, pref->value);
    }
    if (preference_is_binary(pref->type))
        if (pref->referent->symbol_type == IDENTIFIER_SYMBOL_TYPE)
        {
            post_link_removal(thisAgent, pref->id, pref->referent);
        }
    if (thisAgent->trace_settings[TRACE_FIRINGS_PREFERENCES_SYSPARAM])
    {
        thisAgent->outputManager->printa_sf(thisAgent,  "%e- ");
        print_preference(thisAgent, pref, false);
        thisAgent->outputManager->printa_sf(thisAgent,  " (%y)\n", pref->inst->prod_name);
    }
    /*  deallocate it and clones if possible */
    preference_remove_ref(thisAgent, pref);
}
示例#6
0
文件: io.c 项目: IvanLogvinov/soar
bool remove_input_wme(wme * w)
{
    wme *temp;

    /* --- a little bit of error checking --- */
    if (!w) {
        print("Error: an input routine called remove_input_wme on a NULL wme.\n");
        return FALSE;
    }
    for (temp = w->id->id.input_wmes; temp != NIL; temp = temp->next)
        if (temp == w)
            break;
    if (!temp) {
        print("Error: an input routine called remove_input_wme on a wme that\n");
        print("isn't one of the input wmes currently in working memory.\n");
        return FALSE;
    }

    /* SW NOTE:
     * Since we cannot capture wmes asserted during add_input_wme,
     * we will not capture wmes removed during this function call.
     */

    /* Note: for efficiency, it might be better to use a hash table for the
       above test, rather than scanning the linked list.  We could have one
       global hash table for all the input wmes in the system. */
    /* --- go ahead and remove the wme --- */
    remove_from_dll(w->id->id.input_wmes, w, next, prev);
    /* REW: begin 09.15.96 */
#ifndef SOAR_8_ONLY
    if (current_agent(operand2_mode)) {
#endif
        if (w->gds) {
            if (w->gds->goal != NIL) {
                if (current_agent(soar_verbose_flag))
                    print("\nremove_input_wme: Removing goal %d because element in GDS changed.\n",
                          w->gds->goal->id.level);
                gds_invalid_so_remove_goal(w);
                /* NOTE: the call to remove_wme_from_wm will take care
                   of checking if GDS should be removed */
            }
        }
#ifndef SOAR_8_ONLY
    }
#endif

    /* REW: end   09.15.96 */

    remove_wme_from_wm(w);

    return TRUE;
}
示例#7
0
	void remove_module_wme( agent *my_agent, wme *w )
	{
		slot *my_slot = find_slot( w->id, w->attr );

		if ( my_slot )
		{
			remove_from_dll( my_slot->wmes, w, next, prev );

			if ( w->gds ) 
			{
				if ( w->gds->goal != NIL )
				{	             
					gds_invalid_so_remove_goal( my_agent, w );
					
					/* NOTE: the call to remove_wme_from_wm will take care of checking if GDS should be removed */
				}
			}

			remove_wme_from_wm( my_agent, w );
		}
	}
示例#8
0
void reorder_simplified_conditions (agent* thisAgent, 
									condition **top_of_conds,
                                    condition **bottom_of_conds,
                                    list *roots,
                                    tc_number bound_vars_tc_number,
                                    Bool reorder_nccs) {
  condition *remaining_conds;           /* header of dll */
  condition *first_cond, *last_cond;
  condition *cond, *next_cond;
  condition *min_cost_conds, *chosen;
  int64_t cost = 0;
  int64_t min_cost = 0;
  list *new_vars;

  remaining_conds = *top_of_conds;
  first_cond = NIL;
  last_cond = NIL;
  new_vars = NIL;
  
  /* repeat:  scan through remaining_conds
              rate each one
              if tie, call lookahead routine
              add min-cost item to conds
  */
  
  while (remaining_conds) {
    /* --- find min-cost set --- */
    min_cost_conds = NIL;
    min_cost = 0;
    for (cond=remaining_conds; cond!=NIL; cond=cond->next) {
      cost = cost_of_adding_condition (thisAgent, cond, bound_vars_tc_number, roots);
      if ((! min_cost_conds) || (cost < min_cost)) {
        min_cost = cost;
        min_cost_conds = cond;
        cond->reorder.next_min_cost = NIL;
      } else if (cost==min_cost) {
        cond->reorder.next_min_cost = min_cost_conds;
        min_cost_conds = cond;
      }
      /* if (min_cost <= 1) break;  This optimization needs to be removed,
                                    otherwise the tie set is not created.
                                    Without the tie set we can't check the
                                    canonical order. */
    }
    /* --- if min_cost==MAX_COST, print error message --- */
    if ((min_cost==MAX_COST) &&
        thisAgent->sysparams[PRINT_WARNINGS_SYSPARAM]) {
      print (thisAgent, "Warning:  in production %s,\n",
             thisAgent->name_of_production_being_reordered);
      print (thisAgent, "     The LHS conditions are not all connected.\n");
      /* BUGBUG I'm not sure whether this can ever happen. */

      // XML geneneration
      growable_string gs = make_blank_growable_string(thisAgent);
      add_to_growable_string(thisAgent, &gs, "Warning:  in production ");
      add_to_growable_string(thisAgent, &gs, thisAgent->name_of_production_being_reordered);
      add_to_growable_string(thisAgent, &gs, "\n     The LHS conditions are not all connected.");
      xml_generate_warning(thisAgent, text_of_growable_string(gs));
      free_growable_string(thisAgent, gs);

     }
    /* --- if more than one min-cost item, and cost>1, do lookahead --- */
    if ((min_cost > 1) && (min_cost_conds->reorder.next_min_cost)) {
      min_cost = MAX_COST + 1;
      for (cond=min_cost_conds, next_cond = cond->reorder.next_min_cost;
           cond!=NIL;
           cond=next_cond, next_cond=(cond?cond->reorder.next_min_cost:NIL)) {
        cost = find_lowest_cost_lookahead (thisAgent, remaining_conds, cond,
                                           bound_vars_tc_number, roots);
        if (cost < min_cost) {
          min_cost = cost;
          min_cost_conds = cond;
          cond->reorder.next_min_cost = NIL;
        } else {
/*******************************************************************
 These code segments find the condition in the tie set with the smallest
 value in the canonical order. This ensures that productions with the
 same set of conditions are ordered the same. Except if the variables
 are assigned differently.
*********************************************************************/
          if (cost == min_cost && cond->type == POSITIVE_CONDITION) {
              if (canonical_cond_greater(min_cost_conds,cond)) {
                min_cost = cost;
                min_cost_conds = cond;
                cond->reorder.next_min_cost = NIL;
              }
            }
        }
/*******************************************************************/

      }
    }
/*******************************************************************/
    if (min_cost == 1 && (min_cost_conds->reorder.next_min_cost)) {
      for (cond=min_cost_conds; cond!=NIL; cond=cond->reorder.next_min_cost) {
        if (cond->type == POSITIVE_CONDITION &&
          min_cost_conds->type == POSITIVE_CONDITION &&
          canonical_cond_greater(min_cost_conds,cond))
        {
          min_cost = cost;
          min_cost_conds = cond;
        }
       else if (cond->type != POSITIVE_CONDITION &&
                 min_cost_conds->type == POSITIVE_CONDITION)
        {
          min_cost = cost;
          min_cost_conds = cond;
        }
      }
    }
/*******************************************************************/

    /* --- install the first item in the min-cost set --- */
    chosen = min_cost_conds;
    remove_from_dll (remaining_conds, chosen, next, prev);
    if (!first_cond) first_cond = chosen;
    /* Note: args look weird on the next line, because we're really
       inserting the chosen item at the *end* of the list */
    insert_at_head_of_dll (last_cond, chosen, prev, next);

    /* --- if a conjunctive negation, recursively reorder its conditions --- */
    if ((chosen->type==CONJUNCTIVE_NEGATION_CONDITION) && reorder_nccs) {
      list *ncc_roots;
      ncc_roots = collect_root_variables (thisAgent, chosen->data.ncc.top,
                                          bound_vars_tc_number, TRUE);
      reorder_condition_list (thisAgent, &(chosen->data.ncc.top),
                              &(chosen->data.ncc.bottom),
                              ncc_roots,
                              bound_vars_tc_number,
                              reorder_nccs);
      free_list (thisAgent, ncc_roots);
    }

    /* --- update set of bound variables for newly added condition --- */
    add_bound_variables_in_condition (thisAgent, chosen, bound_vars_tc_number, &new_vars);
    
    /* --- if all roots are bound, set roots=NIL: don't need 'em anymore --- */
    if (roots) {
      cons *c;
      for (c=roots; c!=NIL; c=c->rest)
        if (static_cast<Symbol *>(c->first)->var.tc_num != bound_vars_tc_number)
          break;
      if (!c) roots=NIL;
    }

  } /* end of while (remaining_conds) */

  unmark_variables_and_free_list (thisAgent, new_vars);
  *top_of_conds = first_cond;
  *bottom_of_conds = last_cond;
}