#include "structures.h" #include "AOPcomm.h" #include "network.h" extern SystemActionDescriptor action_preds[]; extern int ActionCount; /* ============================================================ | GetSysActionDescriptor() | |----------------------------------------------------------| | Params : 1)a functor | | | | Returns: The pointer to the descriptor of the action | | predicate, if functor is an action predicate. | | NULL otherwise | |==========================================================| */ SystemActionDescriptor * GetSysActionDescriptor(int functor) { int k,i=0,j=ActionCount; SystemActionDescriptor *res=NULL; while(j-i>0 && res==NULL) { k=i+(j-i)/2; if(action_preds[k].functor==functor) res=&action_preds[k]; else if (action_preds[k].functorterm->argument_array; int a=action->term->arity; SystemActionDescriptor *f_dis= GetSysActionDescriptor(action->term->functor); if (f_dis==NULL) { printf("illegal action\n"); print_term(action->term,sym_table); printf("\n"); exit(0); } ((void (*)(int,module_ptr,struct term **))f_dis->funct) (a,action->agent,arg); } extern int message_trace; extern void stabilize(); extern void unrequest_goal(struct proposition *p); extern int identical(struct term *t1, struct term *t2); extern void *send_message(int message_type, int recepient, int sender, struct term *content) { int functor; struct proposition *message; struct term **args; if (message_trace) { printf("message from %s to %s\n",sym_table[sender]->name, sym_table[recepient]->name); printf("message type: %s\ncontent:",sym_table[message_type]); print_term(content,sym_table); printf("\n"); } if(sym_table[recepient]->agent_ref != NULL) { /* My agent. */ switch (message_type) { case INFORM: args=maketermarray(3); args[0]=maketerm(INTEGER,time(NULL),0,NULL,NULL); args[1]=maketerm(NONVAR,sender,0,NULL,NULL); args[2]=content; message=get_prop(sym_table[recepient]->agent_ref, maketerm(NONVAR,ASSIMILATE, 3,NULL,args), 'I', NON_ACTION); back_chain(message); make_fact(message); stabilize(); break; case REQUEST: args=maketermarray(3); args[0]=maketerm(INTEGER,time(NULL),0,NULL,NULL); args[1]=maketerm(NONVAR,sender,0,NULL,NULL); args[2]=content; message=get_prop(sym_table[recepient]->agent_ref, maketerm(NONVAR,COMPLY, 3,NULL,args), 'N', NON_ACTION); back_chain(message); make_goal(message); stabilize(); break; case B_INFORM: args=maketermarray(2); args[0]=maketerm(NONVAR,sender,0,NULL,NULL); args[1]=content; message=get_prop(sym_table[recepient]->agent_ref, maketerm(NONVAR,B_ASSIMILATE, 2,NULL,args), 'I', NON_ACTION); back_chain(message); make_fact(message); stabilize(); break; case B_REQUEST: args=maketermarray(2); args[0]=maketerm(NONVAR,sender,0,NULL,NULL); args[1]=content; message=get_prop(sym_table[recepient]->agent_ref, maketerm(NONVAR,B_COMPLY, 2,NULL,args), 'N', NON_ACTION); back_chain(message); make_goal(message); stabilize(); break; case UNREQUEST: { struct list *potentials; struct proposition *prop; potentials= sym_table[recepient]->agent_ref->prop_table[COMPLY]; while (potentials) { prop=potentials->object; if (prop->term->argument_array[1]->functor==sender&& identical(prop->term->argument_array[2],content)) { unrequest_goal(prop); stabilize(); } potentials=potentials->next; } potentials= sym_table[recepient]->agent_ref->prop_table[B_COMPLY]; while (potentials) { prop=potentials->object; if (prop->term->argument_array[0]->functor==sender&& identical(prop->term->argument_array[1],content)) { unrequest_goal(prop); stabilize(); } potentials=potentials->next; } } break; } } else { /* External agent. */ /* Resolve agent, user, machine */ full_address *addr; addr = resolveAgentAddress(sym_table[recepient]->name); if(addr == NULL) { /* Can not resolve the agent address. */ } else { char *term_string; term_ptr temp_term; args=maketermarray(2); args[0]=maketerm(STRING,0,0,addr->agent,NULL); args[1]=content; temp_term=maketerm(NONVAR,message_type,2,NULL,args); term_string = TermToString(temp_term); sendNetMessage(PACKET_TYPE_MESSAGE, sym_table[sender]->name, addr->machine, addr->user, addr->agent, term_string, strlen(term_string)+1); /* Garbage colection. */ free(addr->agent); free(addr); free_term(temp_term); free(term_string); } } } /* the main loop of the interpreter */ void dispatch_loop() { struct atom *message; char user_input[100]; #if !WINDOW_OUTPUT scanf("%s",user_input); #endif while (user_input[0]!='z') { time_trim(); #if !WINDOW_OUTPUT message=parse_interactive_input(user_input)->head; send_message(message->term->functor, message->term->argument_array[0]->functor, functor_string("user"), message->term->argument_array[1]); #endif process_actions(); #if WINDOW_OUTPUT getInterpreterEvent(); #else scanf("%s",user_input); #endif } }