Soar Kernel  9.3.2 08-06-12
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
soar_module.cpp
Go to the documentation of this file.
1 #include <portability.h>
2 
3 /*************************************************************************
4  * PLEASE SEE THE FILE "license.txt" (INCLUDED WITH THIS SOFTWARE PACKAGE)
5  * FOR LICENSE AND COPYRIGHT INFORMATION.
6  *************************************************************************/
7 
8 /*************************************************************************
9  *
10  * file: soar_module.cpp
11  *
12  * =======================================================================
13  * Description : Useful functions for Soar modules
14  * =======================================================================
15  */
16 
17 #include "soar_module.h"
18 
19 #include "agent.h"
20 #include "gdatastructs.h"
21 #include "instantiations.h"
22 #include "tempmem.h"
23 #include "prefmem.h"
24 #include "mem.h"
25 #include "print.h"
26 #include "decide.h"
27 #include "xml.h"
28 #include "wmem.h"
29 #include "agent.h"
30 #include "soar_TraceNames.h"
31 #include "wma.h"
32 
33 wme *make_wme (agent* thisAgent, Symbol *id, Symbol *attr, Symbol *value, Bool acceptable);
34 typedef struct agent_struct agent;
35 
36 namespace soar_module
37 {
38  timer::timer( const char *new_name, agent *new_agent, timer_level new_level, predicate<timer_level> *new_pred, bool soar_control ): named_object( new_name ), my_agent( new_agent ), level( new_level ), pred( new_pred )
39  {
40  stopwatch.set_enabled( ( ( soar_control )?( &( new_agent->sysparams[ TIMERS_ENABLED ] ) ):( NULL ) ) );
41  reset();
42  }
43 
45  // Utility functions
47 
48  wme *add_module_wme( agent *my_agent, Symbol *id, Symbol *attr, Symbol *value )
49  {
50  slot *my_slot = make_slot( my_agent, id, attr );
51  wme *w = make_wme( my_agent, id, attr, value, false );
52 
53  insert_at_head_of_dll( my_slot->wmes, w, next, prev );
54  add_wme_to_wm( my_agent, w );
55 
56  return w;
57  }
58 
59  void remove_module_wme( agent *my_agent, wme *w )
60  {
61  slot *my_slot = find_slot( w->id, w->attr );
62 
63  if ( my_slot )
64  {
65  remove_from_dll( my_slot->wmes, w, next, prev );
66 
67  if ( w->gds )
68  {
69  if ( w->gds->goal != NIL )
70  {
71  gds_invalid_so_remove_goal( my_agent, w );
72 
73  /* NOTE: the call to remove_wme_from_wm will take care of checking if GDS should be removed */
74  }
75  }
76 
77  remove_wme_from_wm( my_agent, w );
78  }
79  }
80 
81  instantiation* make_fake_instantiation( agent* my_agent, Symbol* state, wme_set* conditions, symbol_triple_list* actions )
82  {
83  // make fake instantiation
84  instantiation* inst;
85  allocate_with_pool( my_agent, &( my_agent->instantiation_pool ), &inst );
86  inst->prod = NULL;
87  inst->next = inst->prev = NULL;
88  inst->rete_token = NULL;
89  inst->rete_wme = NULL;
90  inst->match_goal = state;
91  inst->match_goal_level = state->id.level;
92  inst->reliable = true;
93  inst->backtrace_number = 0;
94  inst->in_ms = FALSE;
96 
97  // create preferences
98  inst->preferences_generated = NULL;
99  {
100  preference* pref;
101 
102  for ( symbol_triple_list::iterator a_it=actions->begin(); a_it!=actions->end(); a_it++ )
103  {
104  pref = make_preference( my_agent, ACCEPTABLE_PREFERENCE_TYPE, (*a_it)->id, (*a_it)->attr, (*a_it)->value, NIL );
105  pref->o_supported = true;
106  symbol_add_ref( pref->id );
107  symbol_add_ref( pref->attr );
108  symbol_add_ref( pref->value );
109 
110  pref->inst = inst;
111  pref->inst_next = pref->inst_prev = NULL;
112 
113  insert_at_head_of_dll( inst->preferences_generated, pref, inst_next, inst_prev );
114  }
115  }
116 
117  // create conditions
118  {
119  condition *cond = NULL;
120  condition *prev_cond = NULL;
121 
122  for ( wme_set::iterator c_it=conditions->begin(); c_it!=conditions->end(); c_it++ )
123  {
124  // construct the condition
125  allocate_with_pool( my_agent, &( my_agent->condition_pool ), &cond );
126  cond->type = POSITIVE_CONDITION;
127  cond->prev = prev_cond;
128  cond->next = NULL;
129  if ( prev_cond != NULL )
130  {
131  prev_cond->next = cond;
132  }
133  else
134  {
135  inst->top_of_instantiated_conditions = cond;
137  inst->nots = NULL;
138  }
139  cond->data.tests.id_test = make_equality_test( (*c_it)->id );
140  cond->data.tests.attr_test = make_equality_test( (*c_it)->attr );
141  cond->data.tests.value_test = make_equality_test( (*c_it)->value );
142  cond->test_for_acceptable_preference = (*c_it)->acceptable;
143  cond->bt.wme_ = (*c_it);
144 
145  #ifndef DO_TOP_LEVEL_REF_CTS
146  if ( inst->match_goal_level > TOP_GOAL_LEVEL )
147  #endif
148  {
149  wme_add_ref( (*c_it) );
150  }
151 
152  cond->bt.level = (*c_it)->id->id.level;
153  cond->bt.trace = (*c_it)->preference;
154 
155  if ( cond->bt.trace )
156  {
157  #ifndef DO_TOP_LEVEL_REF_CTS
158  if ( inst->match_goal_level > TOP_GOAL_LEVEL )
159  #endif
160  {
161  preference_add_ref( cond->bt.trace );
162  }
163  }
164 
165  cond->bt.prohibits = NULL;
166 
167  prev_cond = cond;
168  }
169  }
170 
171  return inst;
172  }
173 
174 
176  // Memory Pool Allocators
178 
179  memory_pool* get_memory_pool( agent* my_agent, size_t size )
180  {
181  memory_pool* return_val = NULL;
182 
183  std::map< size_t, memory_pool* >::iterator it = my_agent->dyn_memory_pools->find( size );
184  if ( it == my_agent->dyn_memory_pools->end() )
185  {
186  memory_pool* newbie = new memory_pool;
187 
188  init_memory_pool( my_agent, newbie, size, "dynamic" );
189  my_agent->dyn_memory_pools->insert( std::make_pair< size_t, memory_pool* >( size, newbie ) );
190 
191  return_val = newbie;
192  }
193  else
194  {
195  return_val = it->second;
196  }
197 
198  return return_val;
199  }
200 }
201