#include #include "structures.h" #define NULL 0 struct proposition *get_prop(module_ptr agent,struct term *t, int type, int action_flag) { struct list *potentials; struct proposition *answer; if (agent==NULL) return NULL; potentials=agent->prop_table[t->functor]; while (potentials && !identical(((struct proposition *)(potentials->object))->term,t)) { potentials=potentials->next; } if (potentials) return potentials->object; else { switch (type) { case 'A': answer=make_abducible(agent,copy_term(t), action_flag); break; case 'R': case 'N': answer=make_complete(agent,copy_term(t),action_flag); break; case 'I': answer=make_incomplete(agent,copy_term(t),action_flag); break; } agent->prop_table[t->functor]= cons(answer,agent->prop_table[t->functor]); return answer; } } extern term_pointer eval_deref(term_pointer var_term, var_array *vars); /* ============================================================ | get_prop_deref() | |----------------------------------------------------------| | Params : 1)agent pointer, | | 2)a term | 3)a var array 4)the type of propsosition to be created (AB,NON_AB, RECURSIVE,INCOMPLETE, or DIRECT | Desc :creates a tms propostion after dereferencing the term with respect to the vars array, and evaluating the evaluable functions. | | | Returns: a pointer to the proposition created. | |==========================================================| */ struct proposition *get_prop_deref(module_ptr agent,struct term *t, var_array *vars, int type, int action_flag) { struct list *potentials; struct proposition *answer; potentials=agent->prop_table[t->functor]; while (potentials && no_bind_match(((struct proposition *)(potentials->object))->term,t,vars)== FAILURE) { potentials=potentials->next; } if (potentials) return potentials->object; else { struct term *deref_t=eval_deref(t,vars); switch (type) { case 'A': answer=make_abducible(agent,deref_t, action_flag); break; case 'R': case 'N': answer=make_complete(agent,deref_t,action_flag); break; case 'I': answer=make_incomplete(agent,deref_t,action_flag); break; } agent->prop_table[deref_t->functor]= cons(answer,agent->prop_table[deref_t->functor]); return answer; } } /* ============================================================ | matching_props() | |----------------------------------------------------------| | Params : 1)an agent pointer | | 2)a term | | 3)a var_array | | Desc : finds all the propositions which match the term | | | | Returns: a list of propositions | |==========================================================| */ struct list *matching_props(module_ptr agent, struct term *t, var_array *vars) { struct list *potentials; struct list *result=NULL; potentials=agent->prop_table[t->functor]; while (potentials) { if (no_bind_match( ((struct proposition *)(potentials->object))->term,t, vars)== SUCCESS) result=cons(potentials->object,result); potentials=potentials->next; } return result; } typedef struct { module_ptr agent; struct term *t; struct proposition *prop; } agent_term; /* ============================================================ | make_agent_term() | |----------------------------------------------------------| | Params : 1) an agent pointer | | 2) a term pointer 3) a var_array 4 a proposition pointer | | | | Returns: an agent term | |==========================================================| */ agent_term *make_agent_term(module_ptr agent, struct term *t, var_array *vars, struct proposition *prop) { struct term *d_t=eval_deref(t,vars); if (d_t==NULL || d_t->argument_array[0]->term_type!=INTEGER|| d_t->argument_array[0]->functoragent=agent; result->t=d_t; result->prop=prop; return result; } } /* ============================================================ | after_handler() | |----------------------------------------------------------| | Params : 1)a temporal 'after' term | Desc : handles the term | | |==========================================================| */ void after_handler(agent_term *t) { struct proposition *after_prop=get_prop(t->agent, t->t, 'N', NON_ACTION); make_fact(after_prop); back_chain(after_prop); } /* ============================================================ | know_handler() | |----------------------------------------------------------| | Params : 1)a 'know' term | |==========================================================| */ void know_handler(agent_term *t) { struct term *w_subject=t->t->argument_array[1]; if (t->prop!=NULL) { struct list * possible_props=matching_props(t->agent,w_subject,NULL); if (possible_props!=NULL) { struct proposition *subject_prop=possible_props->object; free(possible_props); if (subject_prop->state==FALSE) { change_to_false_complete(t->prop); } else { printf("making proposition true\n"); make_fact(t->prop); print_prop(t->prop); } } else change_to_false_complete(t->prop); } } /* the list of 'after' predicates */ struct list *after_predicates=NULL; /* the list of 'know' predicates */ struct list *know_predicates=NULL; /* ============================================================ | m_time() | |----------------------------------------------------------| | Params : 1) an agent temporal term | | Desc : finds the maturation time of a temporal term | | | | Returns: the maturation time | |==========================================================| */ int m_time(agent_term *t) { if (t==NULL) { printf("error: NULL argument to m_time\n"); return 0; } if (t->t->argument_array[0]->term_type==INTEGER) return t->t->argument_array[0]->functor; else { printf("error: argument of m_time is not a temporal term"); return 0; } } /* ============================================================ | add_t_queue() | |----------------------------------------------------------| | Params : 1)a temporal agent_term | | 2)pointer to a time queue of terms | | Desc : adds the term at the appropriate place in the queue | returns: the time of the element just added | |==========================================================| */ int add_t_queue(agent_term *t, struct list **queue) { struct list *temp=*queue; struct list *previous=NULL; int already_exists=FALSE; int element_time=-1; if (t==NULL) return -1; if (temp==NULL) { *queue=cons(t,NULL); element_time=m_time(t); } else { int maturation_time=m_time(t); if (maturation_time==0) return -1; while (temp && (maturation_time>=m_time(temp->object))) { agent_term *this_term=temp->object; if ((this_term->agent==t->agent)&&(identical(this_term->t,t->t))) already_exists=TRUE; previous=temp; temp=temp->next; } if(!already_exists) { if (previous==NULL) { *queue=cons(t,temp); } else { previous->next=cons(t,temp); element_time=maturation_time; } } } return element_time; } struct list *time_queue=NULL; int first_alarm=-1; /* ============================================================ | time_consider() | |----------------------------------------------------------| 1) an agent pointer | Params : 2) a temporal term 3) a binding array 4) a propositin pointer| | Desc : if appropriate, updates the relevant time queue | | | | Returns: | |==========================================================| */ void time_consider(module_ptr agent, struct term *t, var_array *vars, struct proposition *prop) { int e_time=-1; if (t->functor==KNOW||t->functor==AFTER) { e_time=add_t_queue(make_agent_term(agent,t,vars,prop),&time_queue); if (prop!=NULL) { /*struct proposition *subject_prop= get_prop_deref(agent, t->argument_array[0], vars, NON_AB, NON_ACTION); */ } } if (e_time!=-1) { if(first_alarm==-1||e_time>first_alarm) { alarm(first_alarm-time(NULL)); } } } void time_trim() { int current_time=time(NULL); struct list *garbage; while (time_queue!=NULL && m_time(time_queue->object)<=current_time) { switch(((agent_term *)(time_queue->object))->t->functor) { case AFTER: after_handler(time_queue->object); break; case KNOW: know_handler(time_queue->object); break; } garbage=time_queue; time_queue=time_queue->next; free(garbage->object); free(garbage); } if (time_queue!=NULL) { first_alarm=m_time(time_queue->object); alarm(m_time(time_queue->object)-time(NULL)); } }