void free_TypedListList( TypedListList *t ) { if ( t ) { if ( t->predicate ) { free( t->predicate ); t->predicate = NULL; } if ( t->args ) { free_TypedList( t->args ); t->args = NULL; } free_TypedListList( t->next ); free( t ); } }
void free_TypedList( TypedList *t ) { if ( t ) { if ( t->name ) { free( t->name ); t->name = NULL; } if ( t->type ) { free_TokenList( t->type ); t->type = NULL; } free_TypedList( t->next ); free( t ); } }
void normalize_tyl_in_pl( PlNode **n ) { PlNode *i; TypedList *tyl; PlNode *tmp_pl = NULL, *sons, *p_pl; TokenList *tmp_tl, *tl; if ( !(*n) ) { return; } switch( (*n)->connective ) { case ALL: case EX: /* we need to make a sequence of quantifiers ( ->sons ...) * out of the given sequence of TypedList elements, * with connected type names, var - name in TokenList * and KEEPING THE SAME ORDERING !! */ if ( !(*n)->parse_vars ) { printf("\n\nquantifier without argument !! check input files.\n\n"); exit( 1 ); } tmp_tl = new_TokenList(); tmp_tl->next = new_TokenList(); tmp_tl->item = copy_Token( (*n)->parse_vars->name ); if ( (*n)->parse_vars->type->next ) { tmp_tl->next->item = new_Token( MAX_LENGTH ); strcpy( tmp_tl->next->item, EITHER_STR ); for ( tl = (*n)->parse_vars->type; tl; tl = tl->next ) { strcat( tmp_tl->next->item, CONNECTOR ); strcat( tmp_tl->next->item, tl->item ); } } else { tmp_tl->next->item = copy_Token( (*n)->parse_vars->type->item ); } (*n)->atom = tmp_tl; /* now add list of sons */ sons = (*n)->sons; p_pl = *n; for ( tyl = (*n)->parse_vars->next; tyl; tyl = tyl->next ) { tmp_tl = new_TokenList(); tmp_tl->next = new_TokenList(); tmp_tl->item = copy_Token( tyl->name ); if ( tyl->type->next ) { tmp_tl->next->item = new_Token( MAX_LENGTH ); strcpy( tmp_tl->next->item, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( tmp_tl->next->item, CONNECTOR ); strcat( tmp_tl->next->item, tl->item ); } } else { tmp_tl->next->item = copy_Token( tyl->type->item ); } tmp_pl = new_PlNode( (*n)->connective ); tmp_pl->atom = tmp_tl; p_pl->sons = tmp_pl; p_pl = tmp_pl; } /* remove typed-list-of info */ free_TypedList( (*n)->parse_vars ); (*n)->parse_vars = NULL; /* the last son in list takes over ->sons */ p_pl->sons = sons; /* normalize this sons and get out */ normalize_tyl_in_pl( &(p_pl->sons) ); break; case AND: case OR: for ( i = (*n)->sons; i; i = i->next ) { normalize_tyl_in_pl( &i ); } break; case NOT: normalize_tyl_in_pl( &((*n)->sons) ); break; case ATOM: case TRU: case FAL: break; case WHEN: normalize_tyl_in_pl( &((*n)->sons) ); normalize_tyl_in_pl( &((*n)->sons->next) ); break; default: break; } }
void build_orig_constant_list( void ) { char *tmp = NULL; TypedList *tyl; TypedListList *tyll; TokenList *tl, *p_tl, *tmp_tl; PlOperator *po; int i, j, k, n, std; Bool m[MAX_TYPES][MAX_TYPES]; FactList *fl, *p_fl; lnum_types = 0; for ( tyl = gparse_types; tyl; tyl = tyl->next ) { if ( get_type( tyl->name ) == -1 ) { ltype_names[lnum_types++] = copy_Token( tyl->name ); } if ( tyl->type->next ) { tmp = new_Token( MAX_LENGTH ); strcpy( tmp, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( tmp, CONNECTOR ); strcat( tmp, tl->item ); } } else { tmp = copy_Token( tyl->type->item ); } if ( (n = get_type( tmp )) == -1 ) { tyl->n = lnum_types; ltype_names[lnum_types++] = copy_Token( tmp ); } else { tyl->n = n; } free( tmp ); tmp = NULL; } for ( tyl = gparse_constants; tyl; tyl = tyl->next ) { if ( tyl->type->next ) { tmp = new_Token( MAX_LENGTH ); strcpy( tmp, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( tmp, CONNECTOR ); strcat( tmp, tl->item ); } } else { tmp = copy_Token( tyl->type->item ); } if ( (n = get_type( tmp )) == -1 ) { tyl->n = lnum_types; ltype_names[lnum_types++] = copy_Token( tmp ); } else { tyl->n = n; } free( tmp ); tmp = NULL; } for ( tyl = gparse_objects; tyl; tyl = tyl->next ) { if ( tyl->type->next ) { tmp = new_Token( MAX_LENGTH ); strcpy( tmp, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( tmp, CONNECTOR ); strcat( tmp, tl->item ); } } else { tmp = copy_Token( tyl->type->item ); } if ( (n = get_type( tmp )) == -1 ) { tyl->n = lnum_types; ltype_names[lnum_types++] = copy_Token( tmp ); } else { tyl->n = n; } free( tmp ); tmp = NULL; } for ( tyll = gparse_predicates; tyll; tyll = tyll->next ) { for ( tyl = tyll->args; tyl; tyl = tyl->next ) { if ( tyl->type->next ) { tmp = new_Token( MAX_LENGTH ); strcpy( tmp, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( tmp, CONNECTOR ); strcat( tmp, tl->item ); } } else { tmp = copy_Token( tyl->type->item ); } if ( (n = get_type( tmp )) == -1 ) { tyl->n = lnum_types; ltype_names[lnum_types++] = copy_Token( tmp ); } else { tyl->n = n; } free( tmp ); tmp = NULL; } } collect_type_names_in_pl( gorig_goal_facts ); for ( po = gloaded_ops; po; po = po->next ) { collect_type_names_in_pl( po->preconds ); collect_type_names_in_pl( po->effects ); for ( tyl = po->parse_params; tyl; tyl = tyl->next ) { if ( tyl->type->next ) { tmp = new_Token( MAX_LENGTH ); strcpy( tmp, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( tmp, CONNECTOR ); strcat( tmp, tl->item ); } } else { tmp = copy_Token( tyl->type->item ); } if ( (n = get_type( tmp )) == -1 ) { tyl->n = lnum_types; ltype_names[lnum_types++] = copy_Token( tmp ); } else { tyl->n = n; } free( tmp ); tmp = NULL; } } /* now get the numbers of all composed either types */ for ( i = 0; i < lnum_types; i++ ) { lnum_either_ty[i] = 0; } for ( tyl = gparse_types; tyl; tyl = tyl->next ) { make_either_ty( tyl ); } for ( tyl = gparse_constants; tyl; tyl = tyl->next ) { make_either_ty( tyl ); } for ( tyl = gparse_objects; tyl; tyl = tyl->next ) { make_either_ty( tyl ); } for ( tyll = gparse_predicates; tyll; tyll = tyll->next ) { for ( tyl = tyll->args; tyl; tyl = tyl->next ) { make_either_ty( tyl ); } } make_either_ty_in_pl( gorig_goal_facts ); for ( po = gloaded_ops; po; po = po->next ) { make_either_ty_in_pl( po->preconds ); make_either_ty_in_pl( po->effects ); for ( tyl = po->parse_params; tyl; tyl = tyl->next ) { make_either_ty( tyl ); } } /* now, compute the transitive closure of all type inclusions. * first initialize the matrix. */ for ( i = 0; i < lnum_types; i++ ) { for ( j = 0; j < lnum_types; j++ ) { m[i][j] = ( i == j ? TRUE : FALSE ); } } std = -1; for ( i = 0; i < lnum_types; i++ ) { if ( strcmp( ltype_names[i], STANDARD_TYPE ) == SAME ) { std = i; break; } } for ( i = 0; i < lnum_types; i++ ) { m[i][std] = TRUE;/* all types are subtypes of OBJECT */ } for ( tyl = gparse_types; tyl; tyl = tyl->next ) { /* all inclusions as are defined in domain file */ m[get_type( tyl->name )][tyl->n] = TRUE; } /* compute transitive closure on inclusions matrix */ for ( j = 0; j < lnum_types; j++ ) { for ( i = 0; i < lnum_types; i++ ) { if ( m[i][j] ) { for ( k = 0; k < lnum_types; k++ ) { if ( m[j][k] ) { m[i][k] = TRUE; } } } } } /* union types are subsets of all those types that contain all * their components, and * all component types are subsets of the either type ! */ for ( i = 0; i < lnum_types; i++ ) { if ( lnum_either_ty[i] < 2 ) continue; for ( j = 0; j < lnum_types; j++ ) { if ( j == i ) continue; /* get supertypes of all component types */ for ( k = 0; k < lnum_either_ty[i]; k++ ) { if ( !m[leither_ty[i][k]][j] ) break; } if ( k < lnum_either_ty[i] ) continue; m[i][j] = TRUE; /* make components subtypes of either type */ for ( k = 0; k < lnum_either_ty[i]; k++ ) { m[leither_ty[i][k]][i] = TRUE; } } } /* and again, compute transitive closure on inclusions matrix. * I guess, this won't change anything (?), but it also won't need * any remarkable computation time, so why should one think about it ? */ for ( j = 0; j < lnum_types; j++ ) { for ( i = 0; i < lnum_types; i++ ) { if ( m[i][j] ) { for ( k = 0; k < lnum_types; k++ ) { if ( m[j][k] ) { m[i][k] = TRUE; } } } } } /* now build FactList of ALL constant -> type pairs. * for each constant / object, let it appear separately * for each type it is a member of; compute type * membership based on propagating constants / objects * through inclusions matrix. * * this might make the same pair appear doubly, if an object * is declared in type T as well as in some supertype T'. * such cases will be filtered out in string collection. */ for ( tyl = gparse_constants; tyl; tyl = tyl->next ) { fl = new_FactList(); fl->item = new_TokenList(); fl->item->next = new_TokenList(); fl->item->item = copy_Token( tyl->name ); if ( tyl->type->next ) { fl->item->next->item = new_Token( MAX_LENGTH ); strcpy( fl->item->next->item, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( fl->item->next->item, CONNECTOR ); strcat( fl->item->next->item, tl->item ); } } else { fl->item->next->item = copy_Token( tyl->type->item ); } fl->next = gorig_constant_list; gorig_constant_list = fl; /* now add constant to all supertypes */ n = get_type( fl->item->next->item ); for ( i = 0; i < lnum_types; i++ ) { if ( i == n || !m[n][i] ) continue; fl = new_FactList(); fl->item = new_TokenList(); fl->item->next = new_TokenList(); fl->item->item = copy_Token( tyl->name ); fl->item->next->item = copy_Token( ltype_names[i] ); fl->next = gorig_constant_list; gorig_constant_list = fl; } } for ( tyl = gparse_objects; tyl; tyl = tyl->next ) { fl = new_FactList(); fl->item = new_TokenList(); fl->item->next = new_TokenList(); fl->item->item = copy_Token( tyl->name ); if ( tyl->type->next ) { fl->item->next->item = new_Token( MAX_LENGTH ); strcpy( fl->item->next->item, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( fl->item->next->item, CONNECTOR ); strcat( fl->item->next->item, tl->item ); } } else { fl->item->next->item = copy_Token( tyl->type->item ); } fl->next = gorig_constant_list; gorig_constant_list = fl; /* now add constant to all supertypes */ n = get_type( fl->item->next->item ); for ( i = 0; i < lnum_types; i++ ) { if ( i == n || !m[n][i] ) continue; fl = new_FactList(); fl->item = new_TokenList(); fl->item->next = new_TokenList(); fl->item->item = copy_Token( tyl->name ); fl->item->next->item = copy_Token( ltype_names[i] ); fl->next = gorig_constant_list; gorig_constant_list = fl; } } /* now, normalize all typed-list-of s in domain and problem def, * i.e., in all PlNode quantifiers and in op parameters * * at the same time, remove typed-listof structures in these defs */ normalize_tyl_in_pl( &gorig_goal_facts ); for ( po = gloaded_ops; po; po = po->next ) { normalize_tyl_in_pl( &po->preconds ); normalize_tyl_in_pl( &po->effects ); /* be careful to maintain parameter ordering ! */ if ( !po->parse_params ) { continue;/* no params at all */ } fl = new_FactList(); fl->item = new_TokenList(); fl->item->next = new_TokenList(); fl->item->item = copy_Token( po->parse_params->name ); if ( po->parse_params->type->next ) { fl->item->next->item = new_Token( MAX_LENGTH ); strcpy( fl->item->next->item, EITHER_STR ); for ( tl = po->parse_params->type; tl; tl = tl->next ) { strcat( fl->item->next->item, CONNECTOR ); strcat( fl->item->next->item, tl->item ); } } else { fl->item->next->item = copy_Token( po->parse_params->type->item ); } po->params = fl; p_fl = fl; for ( tyl = po->parse_params->next; tyl; tyl = tyl->next ) { fl = new_FactList(); fl->item = new_TokenList(); fl->item->next = new_TokenList(); fl->item->item = copy_Token( tyl->name ); if ( tyl->type->next ) { fl->item->next->item = new_Token( MAX_LENGTH ); strcpy( fl->item->next->item, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( fl->item->next->item, CONNECTOR ); strcat( fl->item->next->item, tl->item ); } } else { fl->item->next->item = copy_Token( tyl->type->item ); } p_fl->next = fl; p_fl = fl; } free_TypedList( po->parse_params ); po->parse_params = NULL; } /* finally, build gpredicates_and_types by chaining predicate names * together with the names of their args' types. */ for ( tyll = gparse_predicates; tyll; tyll = tyll->next ) { fl = new_FactList(); fl->item = new_TokenList(); fl->item->item = copy_Token( tyll->predicate ); fl->next = gpredicates_and_types; gpredicates_and_types = fl; if ( !tyll->args ) continue; /* add arg types; MAINTAIN ORDERING ! */ fl->item->next = new_TokenList(); if ( tyll->args->type->next ) { fl->item->next->item = new_Token( MAX_LENGTH ); strcpy( fl->item->next->item, EITHER_STR ); for ( tl = tyll->args->type; tl; tl = tl->next ) { strcat( fl->item->next->item, CONNECTOR ); strcat( fl->item->next->item, tl->item ); } } else { fl->item->next->item = copy_Token( tyll->args->type->item ); } p_tl = fl->item->next; for ( tyl = tyll->args->next; tyl; tyl = tyl->next ) { tmp_tl = new_TokenList(); if ( tyl->type->next ) { tmp_tl->item = new_Token( MAX_LENGTH ); strcpy( tmp_tl->item, EITHER_STR ); for ( tl = tyl->type; tl; tl = tl->next ) { strcat( tmp_tl->item, CONNECTOR ); strcat( tmp_tl->item, tl->item ); } } else { tmp_tl->item = copy_Token( tyl->type->item ); } p_tl->next = tmp_tl; p_tl = tmp_tl; } } /* now get rid of remaining typed-list-of parsing structures */ free_TypedList( gparse_types ); gparse_types = NULL; free_TypedList( gparse_constants ); gparse_constants = NULL; free_TypedList( gparse_objects ); gparse_objects = NULL; free_TypedListList( gparse_predicates ); gparse_predicates = NULL; }