Soar Kernel  9.3.2 08-06-12
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
callback.h
Go to the documentation of this file.
1 /*************************************************************************
2  * PLEASE SEE THE FILE "license.txt" (INCLUDED WITH THIS SOFTWARE PACKAGE)
3  * FOR LICENSE AND COPYRIGHT INFORMATION.
4  *************************************************************************/
5 
6 /*
7  *=======================================================================
8  *
9  * File: callback.h
10  *
11  * Description: This file contains the callback facility header decls.
12  *
13  * Exported data types:
14  * SOAR_CALLBACK_TYPE
15  * soar_callback_data
16  * soar_call_data
17  * soar_callback_fn
18  *
19  * Exported functions:
20  * soar_add_callback
21  * soar_invoke_callbacks
22  * soar_remove_callback
23  *
24  * Each agent has a separate callback table. The table has one entry
25  * per callback type and the entry is a pointer to a list. The list
26  * contains installed callbacks, one callback per list cons cell.
27  *
28  * =======================================================================
29  */
30 
31 
32  /* Two points about this enumeration: a) The first entry is not */
33  /* a valid callback because 0 is used to indicate an invalid or */
34  /* missing callback and b) The last entry in the enum list */
35  /* indicates how many enums are defined. */
36 
37 #ifndef CALLBACK_H_INCLUDED /* Avoid duplicate includes */
38 #define CALLBACK_H_INCLUDED
39 
40 #ifdef __cplusplus
41 //extern "C"
42 //{
43 #endif
44 
45 /* First we define the possible callbacks in an enum. Then we */
46 /* describe how each one will be called in user code. */
47 
48 enum SOAR_CALLBACK_TYPE // if you change this, update soar_callback_names
49 {
50  NO_CALLBACK, /* Used for missing callback */
96  NUMBER_OF_CALLBACKS /* Not actually a callback */
97  /* type. Used to indicate */
98  /* list size and MUST ALWAYS */
99  /* BE LAST. */
100 } ;
101 
102 #define NUMBER_OF_MONITORABLE_CALLBACKS (NUMBER_OF_CALLBACKS - 2)
103 
104 /* --------------------------------------------------------------------
105 
106  All callback functions installed by the user must use the function
107  soar_add_callback. The type signature of all callback functions is
108  the same and has the following form:
109 
110  my_callback_fn(agent * a,
111  soar_callback_data scd,
112  soar_call_data call_data);
113 
114  where "a" is a pointer to the agent structure for the agent in which
115  the callback was invoked, "scd" is the data structure given in the
116  installation when soar_add_callback was called, and "call_data" is
117  the data relevant to this particular call. To use the "call_data"
118  parameter, it should first be cast to the appropriate type, which
119  is mentioned below. Since only the "call_data" argument varies for
120  all callbacks, only that argument will be mentioned in the following.
121 
122  SYSTEM_STARTUP_CALLBACK
123 
124  This function is called only once, at system startup time. It can
125  be used to set up any Soar I/O routines and install any interface
126  commands. This function is called after most of the system has been
127  initialized, but before any agent initialization file is loaded.
128  The "call_data" argument is given as NULL.
129 
130 
131  Unused: SYSTEM_TERMINATION_CALLBACK
132 
133  This function is called only once, just before the system exits back
134  to the shell. The "call_data" parameter is of type "Bool" which is
135  TRUE if the system is exiting normally, and FALSE if the exit is
136  happening because some fatal error situation was detected. Typically,
137  this routine should do any final cleanup (closing files, etc.)
138  necessary.
139 
140  AFTER_INIT_AGENT_CALLBACK
141 
142  This function is called only once per each agent after the agent
143  has been initialized. This function is called after most of the
144  agent data structures have been initialized, but before the agent
145  initialization file is loaded. The "call_data" argument is given
146  as NULL.
147 
148  BEFORE_INIT_SOAR_CALLBACK
149 
150  This function is called just before any init-soar is done. (This
151  includes not only the init-soar command, but also excise-task and
152  excise-all, which do an init-soar.) The "call_data" argument is
153  given as NULL.
154 
155  AFTER_INIT_SOAR_CALLBACK
156 
157  This function is called just after any init-soar is done. (This
158  includes not only the init-soar command, but also excise-task and
159  excise-all, which do an init-soar.) The "call_data" argument is
160  given as NULL.
161 
162  AFTER_HALT_SOAR_CALLBACK
163 
164  This function is called after Soar halts; i.e., after the preference
165  phase in which the RHS function "halt" is executed. The "call_data"
166  argument is given as NULL.
167 
168  BEFORE_SCHEDULE_CYCLE_CALLBACK
169 
170  This function is called just before the agent is scheduled. The
171  "call_data" argument is given as NULL.
172 
173  AFTER_SCHEDULE_CYCLE_CALLBACK
174 
175  This function is called just after the agent is scheduled. The
176  "call_data" argument is given as NULL.
177 
178  BEFORE_DECISION_CYCLE_CALLBACK
179 
180  This function is called at the start of each decision cycle. The
181  "call_data" argument is given as NULL.
182 
183  AFTER_DECISION_CYCLE_CALLBACK
184 
185  This function is called at the end of each decision cycle. The
186  "call_data" argument is given as NULL.
187 
188  BEFORE_INPUT_PHASE_CALLBACK
189 
190  This function is called at the start of each input phase. This
191  is called even if the input cycle is effectively null because
192  there is no top state. The "call_data" argument is given as NULL.
193 
194  INPUT_PHASE_CALLBACK
195 
196  This function is called during each input phase. The "call_data"
197  argument contains the input mode currently being used.
198 
199  AFTER_INPUT_PHASE_CALLBACK
200 
201  This function is called at the end of each input phase. This
202  is called even if the input cycle is effectively null because
203  there is no top state. The "call_data" argument is given as NULL.
204 
205  BEFORE_PREFERENCE_PHASE_CALLBACK
206 
207  This function is called at the start of each preference phase.
208  The "call_data" argument is given as NULL.
209 
210  AFTER_PREFERENCE_PHASE_CALLBACK
211 
212  This function is called at the end of each preference phase.
213  The "call_data" argument is given as NULL.
214 
215  BEFORE_WM_PHASE_CALLBACK
216 
217  This function is called at the start of each working memory phase.
218  The "call_data" argument is given as NULL.
219 
220  AFTER_WM_PHASE_CALLBACK
221 
222  This function is called at the end of each working memory phase.
223  The "call_data" argument is given as NULL.
224 
225  BEFORE_OUTPUT_PHASE_CALLBACK
226 
227  This function is called at the start of each Soar output cycle.
228  This is called even if the output cycle is effectively null because
229  there is no top state. The "call_data" argument is given as NULL.
230 
231  OUTPUT_PHASE_CALLBACK
232 
233  This function is called during each Soar output cycle. The "call_data"
234  argument is a structure which contains both the output mode being
235  used and the list of output wmes.
236 
237  AFTER_OUTPUT_PHASE_CALLBACK
238 
239  This function is called at the end of each Soar output cycle.
240  This is called even if the output cycle is effectively null because
241  there is no top state. The "call_data" argument is given as NULL.
242 
243  BEFORE_DECISION_PHASE_CALLBACK
244 
245  This function is called at the start of each decision phase.
246  The "call_data" argument is given as NULL.
247 
248  AFTER_DECISION_PHASE_CALLBACK
249 
250  This function is called at the end of each decision phase.
251  The "call_data" argument is given as NULL.
252 
253  WM_CHANGES_CALLBACK
254 
255  This function is called just before changes are made to working
256  memory. The pre-callback hook function passed two arguments to
257  the corresponding hook function. However, the "call_data"
258  argument is given as NULL, in this callback. The wmes_being_added
259  and wmes_being_removed can be retrieved from the agent structure
260  already being passed.
261 
262  CREATE_NEW_CONTEXT_CALLBACK
263 
264  This function is called after a new goal context is created. The
265  "call_data" argument is a pointer to a "Symbol" which is the new
266  goal identifier. This goal identifier is equal to the agent
267  variable bottom_goal.
268 
269  POP_CONTEXT_STACK_CALLBACK
270 
271  This function is called just before the context stack is popped.
272  The "call_data" argument is a pointer to a "Symbol" which is the
273  identifier of the goal about to be removed. This goal identifier
274  is equal to the aget variable bottom_goal. If the stack is popped
275  k levels at once, this routine is called k times in bottom-up order.
276 
277  CREATE_NEW_ATTRIBUTE_IMPASSE_CALLBACK
278 
279  This function is called just after an attribute impasse is created.
280  The "call_data" argument is a pointer to a "slot" which is the
281  impassed slot.
282 
283  REMOVE_ATTRIBUTE_IMPASSE_CALLBACK
284 
285  This function is called just before an attribute impasse is removed.
286  The "call_data" argument is a pointer to a "slot" which is the
287  impassed slot.
288 
289  PRODUCTION_JUST_ADDED_CALLBACK
290 
291  This function is called just after a production (including chunks
292  and justifications) is added to the system. The "call_data" argument
293  is a pointer to a "production" which is the production just added.
294 
295  PRODUCTION_JUST_ABOUT_TO_BE_EXCISED_CALLBACK
296 
297  This function is called just before a production (including chunks
298  and justifications) is excised from the system. The "call_data"
299  argument is a pointer to a "production" which is the production
300  just about to be removed.
301 
302  FIRING_CALLBACK
303 
304  This function is called after every production firing. The
305  "call_data" argument is a pointer to an "instantiation" which
306  is the newly created instantiation.
307 
308  RETRACTION_CALLBACK
309 
310  This function is called before every production retraction. The
311  "call_data" argument is a pointer to an "instantiation" which
312  is the instantiation about to be retracted.
313 
314  SYSTEM_PARAMETER_CHANGED_CALLBACK
315 
316  This function is called after any change to one of the agent system
317  parameters (e.g., learn on/off). See soarkernel.h for a list of these
318  system parameters. The "call_data" argument is an "int" which
319  indicates which system parameter is being changed. This function
320  should examine the new value of the parameter by looking at the
321  appropriate agent variable. (For most parameters, this means
322  looking at the sysparams[] array.)
323 
324 -------------------------------------------------------------------- */
325 
326 #ifndef CALLBACK_H
327 #define CALLBACK_H
328 
329 //typedef list * soar_callback_array[NUMBER_OF_CALLBACKS];
330 typedef char Bool;
331 typedef const char * soar_callback_id;
332 typedef void * soar_callback_data;
333 typedef void * soar_call_data;
335 
336 typedef struct agent_struct agent;
337 
338 typedef void (*soar_callback_fn)(agent*,
342 typedef void (*soar_callback_free_fn)(soar_callback_data); // JRV 2008 This is a function pointer used to free the user data but currently nobody uses it
343 typedef struct cons_struct cons;
344 typedef cons list;
345 typedef struct agent_struct agent;
346 
347 #include <string>
348 
349 typedef struct callback_struct
350 {
351  std::string id;
356 } soar_callback;
357 
358 extern void soar_add_callback (agent* thisAgent,
366 extern const char * soar_callback_enum_to_name (SOAR_CALLBACK_TYPE, Bool);
368 extern void soar_destroy_callback(soar_callback *);
370 extern soar_callback * soar_exists_callback_id (agent* the_agent,
371  SOAR_CALLBACK_TYPE callback_type,
372  soar_callback_id id);
373 extern void soar_init_callbacks (agent*);
374 extern void soar_invoke_callbacks (agent* thisAgent,
377 extern void soar_invoke_first_callback (agent* thisAgent,
380 extern void soar_list_all_callbacks (agent*,
381  Bool);
382 extern void soar_list_all_callbacks_for_event (agent* thisAgent,
384 extern void soar_pop_callback (agent* the_agent,
385  SOAR_CALLBACK_TYPE callback_type);
386 extern void soar_push_callback (agent* the_agent,
387  SOAR_CALLBACK_TYPE callback_type,
388  soar_callback_fn fn,
389  soar_callback_data data,
390  soar_callback_free_fn free_fn);
391 extern void soar_remove_all_monitorable_callbacks (agent* thisAgent);
393 extern void soar_remove_callback (agent* thisAgent,
397 
399 #endif
400 
401 #ifdef __cplusplus
402 //}
403 #endif
404 
405 #endif