Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

agent.c

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  file:  agent.c
00004  *
00005  * =======================================================================
00006  *  Initialization for the agent structure.  Also the cleanup routine
00007  *  when an agent is destroyed.  These routines are usually replaced
00008  *  by the same-named routines in the Tcl interface file soarAgent.c
00009  *  The versions in this file are used only when not linking in Tcl.
00010  *  HOWEVER, this code should be maintained, and the agent structure
00011  *  must be kept up to date.
00012  * =======================================================================
00013  *
00014  * Copyright 1995-2003 Carnegie Mellon University,
00015  *                                                                               University of Michigan,
00016  *                                                                               University of Southern California/Information
00017  *                                                                               Sciences Institute. All rights reserved.
00018  *                                                                              
00019  * Redistribution and use in source and binary forms, with or without
00020  * modification, are permitted provided that the following conditions are met:
00021  *
00022  * 1.   Redistributions of source code must retain the above copyright notice,
00023  *              this list of conditions and the following disclaimer. 
00024  * 2.   Redistributions in binary form must reproduce the above copyright notice,
00025  *              this list of conditions and the following disclaimer in the documentation
00026  *              and/or other materials provided with the distribution. 
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE SOAR CONSORTIUM ``AS IS'' AND ANY EXPRESS OR
00029  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00030  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
00031  * EVENT SHALL THE SOAR CONSORTIUM  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00032  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00033  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00034  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00035  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00036  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038  * The views and conclusions contained in the software and documentation are
00039  * those of the authors and should not be interpreted as representing official
00040  * policies, either expressed or implied, of Carnegie Mellon University, the
00041  * University of Michigan, the University of Southern California/Information
00042  * Sciences Institute, or the Soar consortium.
00043  * =======================================================================
00044  */
00045 /* ===================================================================
00046                      Agent-related functions
00047    =================================================================== */
00048 
00049 #include "soarkernel.h"
00050 #include "scheduler.h"
00051 #include "sysdep.h"
00052 #include "rhsfun_examples.h"
00053 
00054 agent *soar_agent;
00055 
00056 list *all_soar_agents = NIL;
00057 
00058 int agent_counter = -1;
00059 int agent_count = -1;
00060 
00061 char *soar_version_string;
00062 
00063 extern int soar_agent_ids[];
00064 
00065 /*
00066   Try to assign a unique and previously unassigned id.  If none are
00067    available, assign a unique, but previously assigned id.
00068 */
00069 int next_available_agent_id()
00070 {
00071     int i;
00072 
00073     for (i = 0; i < MAX_SIMULTANEOUS_AGENTS; i++) {
00074         if (soar_agent_ids[i] == UNTOUCHED) {
00075             soar_agent_ids[i] = ALLOCATED;
00076             return i;
00077         }
00078     }
00079     for (i = 0; i < MAX_SIMULTANEOUS_AGENTS; i++) {
00080         if (soar_agent_ids[i] == TOUCHED) {
00081             soar_agent_ids[i] = ALLOCATED;
00082             return i;
00083         }
00084     }
00085     {
00086         char msg[MESSAGE_SIZE];
00087         snprintf(msg, MESSAGE_SIZE, "agent.c: Error: Too many simultaneous agents (> %d\n", MAX_SIMULTANEOUS_AGENTS);
00088         msg[MESSAGE_SIZE - 1] = 0;      /* snprintf doesn't set last char to null if output is truncated */
00089 
00090         abort_with_fatal_error(msg);
00091     }
00092     return -1;                  /* To placate compilier */
00093 }
00094 
00095 #ifdef ATTENTION_LAPSE
00096 /* RMJ;
00097    When doing attentional lapsing, we need a function that determines
00098    when (and for how long) attentional lapses should occur.  This
00099    will normally be provided as a user-defined TCL procedure.
00100 */
00101 
00102 long init_lapse_duration(TIMER_VALUE * tv)
00103 {
00104     int ret;
00105     long time_since_last_lapse;
00106     char buf[128];
00107 
00108     start_timer(current_real_time);
00109     timersub(current_real_time, tv, current_real_time);
00110     time_since_last_lapse = (long) (1000 * timer_value(tv));
00111 
00112     if (soar_exists_callback(soar_agent, INIT_LAPSE_DURATION_CALLBACK)) {
00113 
00114         /* SW 11.07.00
00115          *
00116          *  Modified this to use the generic soar interface, as opposed
00117          *  to being Tcl specific.  This requires a new callback
00118          *  in particular, here the callback receives the value 
00119          *  time_since_last_lapse, and must RESET this value to
00120          *  the appropriate number
00121          */
00122         soar_invoke_callback(soar_agent, INIT_LAPSE_DURATION_CALLBACK, (void *) &time_since_last_lapse);
00123 
00124         return time_since_last_lapse;
00125     }
00126     return 0;
00127 }
00128 
00129 #endif
00130 
00131 /* ===================================================================
00132    
00133                            Initialization Function
00134 
00135 =================================================================== */
00136 
00137 void init_soar_agent(void)
00138 {
00139 
00140     /* --- initialize everything --- */
00141     init_memory_utilities();
00142     init_symbol_tables();
00143     create_predefined_symbols();
00144     init_production_utilities();
00145     init_built_in_rhs_functions();
00146     /*add_bot_rhs_functions (soar_agent); */
00147     add_bot_rhs_functions();
00148     init_rete();
00149     init_lexer();
00150     init_firer();
00151     init_decider();
00152     init_soar_io();
00153     init_chunker();
00154     init_sysparams();
00155     init_tracing();
00156     init_explain();             /* AGR 564 */
00157 #ifdef REAL_TIME_BEHAVIOR
00158     /* RMJ */
00159     init_real_time();
00160 #endif
00161 #ifdef ATTENTION_LAPSE
00162     /* RMJ */
00163     init_attention_lapse();
00164 #endif
00165 
00166     /* --- add default object trace formats --- */
00167     add_trace_format(FALSE, FOR_ANYTHING_TF, NIL, "%id %ifdef[(%v[name])]");
00168     add_trace_format(FALSE, FOR_STATES_TF, NIL, "%id %ifdef[(%v[attribute] %v[impasse])]");
00169     {
00170         Symbol *evaluate_object_sym;
00171         evaluate_object_sym = make_sym_constant("evaluate-object");
00172         add_trace_format(FALSE, FOR_OPERATORS_TF, evaluate_object_sym, "%id (evaluate-object %o[object])");
00173         symbol_remove_ref(evaluate_object_sym);
00174     }
00175     /* --- add default stack trace formats --- */
00176     add_trace_format(TRUE, FOR_ANYTHING_TF, NIL, "%right[6,%dc]: %rsd[   ]==>S: %id %ifdef[(%v[name])]");
00177     add_trace_format(TRUE, FOR_STATES_TF, NIL, "%right[6,%dc]: %rsd[   ]==>S: %cs");
00178     add_trace_format(TRUE, FOR_OPERATORS_TF, NIL, "%right[6,%dc]: %rsd[   ]   O: %co");
00179     reset_statistics();
00180 }

Generated on Thu Dec 11 13:00:14 2003 for Soar Kernel by doxygen 1.3.5