#include "structures.h" #include #include #include #include #include "t_list.c" static char *strToUpper(char *s1); /* ============================================================ | intersect_two() | |----------------------------------------------------------| | Params : 1) a sorted tuple representing a set | | 2) a sorted tuple representing a set | | | | Desc : | | | | Returns: the intersection of the two arguments | |==========================================================| */ struct term *intersect_two(struct term *set1, struct term *set2) { struct list *garbage; struct list *l_result=NULL; struct term **result_tuple; struct term *result; int l,i=0,i1=0,i2=0; while(i1arity && i2arity) if ((set1->argument_array[i1]->functor < set2->argument_array[i2]->functor)) i1++; else if ((set2->argument_array[i2]->functor < set1->argument_array[i1]->functor)) i2++; else { l_result=cons((void *)copy_term(set1->argument_array[i1]), l_result); i1++; i2++; } l=list_length(l_result); result_tuple=maketermarray(l); i=l-1; while (l_result) { result_tuple[i]=l_result->object; garbage=l_result; l_result=l_result->next; free(garbage); i--; } result=maketerm(TUPLE,0,l,NULL,result_tuple); return result; } /* ============================================================ | intersection() | |----------------------------------------------------------| | Params : 1)an interpreter list of sets | | | | | | Desc : Assumes sets are represented by sorted tuples | | Assumes the list of sets is non-empty | | | | Returns: the intersection of the sets | |==========================================================| */ struct term *intersection(struct term *set_list) { struct term *result; struct term *new_result; if (t_empty_list(set_list)) return NULL; result=t_object(set_list); set_list=t_next(set_list); while (!t_empty_list(set_list)) { new_result=intersect_two(t_object(set_list),result); free_term(result); result=new_result; set_list=t_next(set_list); } return result; } /* ============================================================ | least_element() | |----------------------------------------------------------| | Params : 1) a tuple representing a non-empty set | | | | | | Desc : will return NULL if set is empty | | | | Returns: The smallest element of the set | |==========================================================| */ struct term *least_element(struct term *set) { if (set->arity>0) return set->argument_array[0]; else return NULL; } struct term *term_to_string(struct term *t) { char *st=TermToString(t); st[strlen(st)-1]='\0'; return maketerm(STRING,0,0,st,NULL); } /* ============================================================ | set_to_list() | |----------------------------------------------------------| | Params : 1)a tuple representing a set | | | | Returns: an interpreter list of the elements of the set | |==========================================================| */ struct term *set_to_list(struct term *set) { if(set->term_type!=TUPLE) return NULL; else { int i; struct term *result=maketerm(NONVAR,EMPTY_LIST,0,NULL,NULL); for(i=set->arity-1;i>=0;i--) result=t_cons(set->argument_array[i],result); return result; } } /* ============================================================ | concatenate() | |----------------------------------------------------------| | Params : 1) a term representing a list of strings | | Desc : | | | | Returns: a term representing the concatenation of the | | strings | |==========================================================| */ struct term *concatenate(struct term *input) { if (t_empty_list(input)) { return NULL; } else { char *buffer; int length = 0; struct term *temp, *res; /* Calculate the length of the concatinated strings. */ temp = input; while(!t_empty_list(temp)) { length += strlen(t_object(temp)->const_string); temp = t_next(temp); } /* Allocate the buffer. */ buffer = (char *)malloc(length+1); /* Initilize it */ buffer[0] = '\0'; /* Concatenate. */ while(!t_empty_list(input)) { strcat(buffer,t_object(input)->const_string); input=t_next(input); } res = maketerm(STRING,0,0,buffer,NULL); free(buffer); return(res); } } /* ============================================================ | self_strip | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ struct term *self_strip(struct term *input) { struct term *temp; char *name=sym_table[input->functor]->name; if (strcmp(name,"self")==0) return NULL; else return maketerm(STRING,0,0,copy_string(name+5),NULL); } /* ============================================================ | current_time() | |----------------------------------------------------------| | Params : 1)a dummy term | | | | Returns:The current Unix Standard Time | |==========================================================| */ struct term *current_time(struct term *dummy) { return maketerm(INTEGER,time(NULL),0,NULL,NULL); } /* ============================================================ | add() | |----------------------------------------------------------| | Params : 1)a t_list of terms representing integers | | | | Returns: a term representing the sum of the integers | |==========================================================| */ struct term *add(struct term *int_list) { if (t_empty_list(int_list)) return NULL; else { int res=0; while (!t_empty_list(int_list)) { res=res+t_object(int_list)->functor; int_list=t_next(int_list); } return maketerm(INTEGER,res,0,NULL,NULL); } } /* ============================================================ | times() | |----------------------------------------------------------| | Params : 1)a t_list of terms representing integers | | | | Returns: a term representing the product of the integers | |==========================================================| */ struct term *times(struct term *int_list) { if (t_empty_list(int_list)) return NULL; else { int res=1; while (!t_empty_list(int_list)) { res=res*t_object(int_list)->functor; int_list=t_next(int_list); } return maketerm(INTEGER,res,0,NULL,NULL); } } /* ============================================================ | subtract() | |----------------------------------------------------------| | Params : 1)a t_list of terms representing integers | | | | Returns: a term representing the first term minus sum | of the rest of the terms | |==========================================================| */ struct term *subtract(struct term *int_list) { if (t_empty_list(int_list)) return NULL; else { int res=t_object(int_list)->functor; int_list=t_next(int_list); while (!t_empty_list(int_list)) { res=res-t_object(int_list)->functor; int_list=t_next(int_list); } return maketerm(INTEGER,res,0,NULL,NULL); } } /* ============================================================ | divide() | |----------------------------------------------------------| | Params : 1)a t_list of terms representing integers | | | | Returns: a term representing the first term divided by | the rest of the terms | |==========================================================| */ struct term *divide(struct term *int_list) { if (t_empty_list(int_list)) return NULL; else { int res=t_object(int_list)->functor; int_list=t_next(int_list); while (!t_empty_list(int_list)) { res=res/t_object(int_list)->functor; int_list=t_next(int_list); } return maketerm(INTEGER,res,0,NULL,NULL); } } /* ============================================================ | less() | |----------------------------------------------------------| | Params : 1)a t_list of terms representing two integers | | | | Returns: 'yes' if first term less than second term, 'no' otherwise |==========================================================| */ struct term *less(struct term *int_list) { if (t_empty_list(int_list)) return NULL; else { int i1=t_object(int_list)->functor; int i2=t_object(t_next(int_list))->functor; return maketerm(NONVAR, ((i1term_type==TUPLE&&arg->arity==0)?YES:NO), 0,NULL,NULL); } } /* ============================================================ | equal() | |----------------------------------------------------------| | Params : 1)a t_list of terms representing two integers | | | | Returns: 'yes' if first term equal to second term, 'no' otherwise |==========================================================| */ struct term *equal(struct term *int_list) { if (t_empty_list(int_list)) return NULL; else { int i1=t_object(int_list)->functor; int i2=t_object(t_next(int_list))->functor; return maketerm(NONVAR, ((i1==i2)?YES:NO), 0,NULL,NULL); } } /* ============================================================ | search_substring() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Mon Sep 21 12:23:08 1992 | |==========================================================| */ struct term *search_substring(struct term *input) { /* input is: <["text1","text2",...,"textn"],["search1","search2",...,"searchk"]> */ struct term *searched_list, *substrings_list, *res, *temp, *t; int i,j; char *sub, *search; int case_sensative; /* Check the arguments. */ if(input->arity != 3) return NULL; searched_list = input->argument_array[0]; substrings_list =input->argument_array[1]; if((searched_list->functor != CONS_LIST)&& (searched_list->functor != EMPTY_LIST)) return NULL; if((substrings_list->functor != CONS_LIST)&& (substrings_list->functor != EMPTY_LIST)) return NULL; /* Check for case sensativity. */ case_sensative = input->argument_array[2]->functor; res = maketerm(NONVAR, EMPTY_LIST,0, NULL, NULL); /* Look over all the substrings */ while(substrings_list->functor == CONS_LIST) { if(case_sensative) sub = substrings_list->argument_array[0]->const_string; else sub = strToUpper(substrings_list->argument_array[0]->const_string); t = searched_list; while(t->functor == CONS_LIST) { if(case_sensative) search = t->argument_array[0]->const_string; else search = strToUpper(t->argument_array[0]->const_string); if(strstr(search, sub) != NULL) { /* Found match */ temp = maketerm(NONVAR,CONS_LIST,2,NULL,maketermarray(2)); temp->argument_array[1] = res; temp->argument_array[0] = maketerm(STRING,0,0, substrings_list->argument_array[0]->const_string,NULL); res = temp; if(!case_sensative) free(search); break; } else { t = t->argument_array[1]; if(!case_sensative) free(search); } } if(!case_sensative) free(sub); substrings_list = substrings_list->argument_array[1]; } return(res); } /* ============================================================ | strToUpper() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: avrami tzur | | At: Tue Sep 22 12:09:21 1992 | |==========================================================| */ static char *strToUpper(char *s1) { int length = strlen(s1)+1; char *s2 = (char *)malloc(length); int i; for(i=0;iarity;i++) sofar=identical(element,set->argument_array[i]); return maketerm(NONVAR,(sofar?YES:NO),0,NULL,NULL); } } /* ============================================================ | print_the_term() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | | Created by: Avrami Tzur | | At: Wed Sep 23 14:54:09 1992 | |==========================================================| */ struct term * print_the_term(struct term *input) { if (input==NULL) return NULL; else { print_term(input,sym_table); printf("\n"); return maketerm(NONVAR,YES,0,NULL,NULL); } } int occurs_on_list(struct term *t, struct term *list) { int found=FALSE; while(!t_empty_list(list)&&!found) { found=identical(t,t_object(list)); list=t_next(list); } return found; } int occurs_on_all_lists(struct term *t, struct term *lists) { int result=TRUE; while(result&&!t_empty_list(lists)) { result=occurs_on_list(t,t_object(lists)); lists=t_next(lists); } return result; } /* ============================================================ | common_element() | |----------------------------------------------------------| | Params : 1) a list of lists | | | | Returns: a term which occurs on all lists if one exists; NULL otherwise |==========================================================| */ struct term *common_element(struct term *lists) { struct term *list_1=t_object(lists); int found=FALSE; struct term *possible=NULL; lists=t_next(lists); while(!t_empty_list(list_1)&&!found) { possible=t_object(list_1); found=occurs_on_all_lists(possible,lists); list_1=t_next(list_1); } if (found) return possible; else return NULL; } extern int nodef_term_on_list(struct term *dref_t, struct list *l); /* ============================================================ | occurs_on_t_list() | |----------------------------------------------------------| | Params : 1) a term | | 2) a t_list | | | | Returns: TRUE if term is on list, False otherwise | |==========================================================| */ int occurs_on_t_list(struct term *t, struct term *list) { while(!t_empty_list(list)&&!identical(t,t_object(list))) list=t_next(list); return !t_empty_list(list); } /* ============================================================ | frequency() | |----------------------------------------------------------| | Params : 1) a term | | 2) a list of lists of terms | | | | Returns: the number of times the term occurs on a list | |==========================================================| */ int frequency(struct term *t, struct term *list) { int result=0; while(!t_empty_list(list)) { if (occurs_on_t_list(t,t_object(list))) result++; list=t_next(list); } return result; } /* ============================================================ | insert_by_frequency() | |----------------------------------------------------------| | Params : 1)a term | | 2)a t_list of t_lists | | 3)address o a list | | Desc : | | | | Returns: inserts the term into the third argument, sorted by frequency relative to second argument |==========================================================| */ void insert_by_frequency(struct term *element, struct term *ll, struct list **address) { struct list *temp; temp=*address; if (temp==NULL|| frequency(temp->object,ll)>=frequency(element,ll)) *address=cons(element,temp); else { while(temp->next&& frequency(temp->next->object,ll)next; } temp->next=cons(element,temp->next); } } /* ============================================================ | frequency_list() | |----------------------------------------------------------| | Params : 1) a list of lists | | Desc :counts the number of times each element occurs in each list, and constructs a list of the elements in the order of their frequency. | | | Returns: | |==========================================================| */ struct term *frequency_list(struct term *lists) { struct term *ll_pointer=lists; struct term *l_pointer; struct term *object; struct list *l_result=NULL; struct list *garbage; struct term *result=t_null(); while(!t_empty_list(ll_pointer)) { l_pointer=t_object(ll_pointer); ll_pointer=t_next(ll_pointer); while(!t_empty_list(l_pointer)) { object=t_object(l_pointer); l_pointer=t_next(l_pointer); if(!nodef_term_on_list(object,l_result)) insert_by_frequency(object,lists,&l_result); } } while(l_result) { garbage=l_result; result=t_cons(l_result->object,result); l_result=l_result->next; free(garbage); } return result; } struct term *string_term(struct term *st_term) { struct term *string_to_term(char *input); if (st_term->term_type!=STRING) return NULL; else return string_to_term(st_term->const_string); } struct term *weekday(struct term *date) { term_ptr *arg=date->argument_array; int year=arg[0]->functor; int month=arg[1]->functor; int day=arg[2]->functor; struct tm time_st; time_st.tm_sec=0; time_st.tm_min=0; time_st.tm_hour=8; time_st.tm_mday=day; time_st.tm_mon=month-1; time_st.tm_year=year; time_st.tm_isdst=0; mktime(&time_st); return maketerm(INTEGER,time_st.tm_wday,0,NULL,NULL); } /* ============================================================ | free_times() | |----------------------------------------------------------| | Params : 1) an integer term representing the day of the week (sunday 0) | Desc : reads the file .AOPfree_times to find the list of hours on which the user is free on that day of the week | | | Returns: the list of free times | |==========================================================| */ struct term * free_times(struct term *day) { char *days[]={"sunday","monday","tuesday","wednesday", "thursday","friday","saturday"}; FILE *f; char *day_name=days[day->functor]; char temp_day[10]; char g; int found=FALSE; int hour; struct term *result; if (day->term_type!=INTEGER||day->functor<0||day->functor>6) return NULL; f=fopen(".AOPfree_times","r"); if (f!=NULL) { result=t_null(); while(!found&&fscanf(f,"%s",temp_day)!=EOF) { if (strcasecmp(temp_day,day_name)==0) { found=TRUE; while (fscanf(f,"%d",&hour)==1) { result=t_cons(maketerm(INTEGER,hour,0,NULL,NULL), result); fscanf(f,"%*[ ,]"); } } g=fgetc(f); while(g!='\n'&&g!=EOF) g=fgetc(f); } } else result=maketerm(NONVAR,EMPTY,0,NULL,NULL); fclose(f); return result; } /* ============================================================ | string_integer() | |----------------------------------------------------------| | Params : 1) a string term representing an integer | | | | Returns: the corresponding integer term, fails if the term is not a string, 1 if the string does not denote an integer |==========================================================| */ struct term *string_integer(struct term *s) { int result=1; if (s->const_string==NULL) return NULL; sscanf(s->const_string,"%d",&result); return maketerm(INTEGER,result,0,NULL,NULL); } #include "system.aux" int SysCount=sizeof(sys_preds)/sizeof(sys_preds[0]);