void free_WffNode( WffNode *w ) { if ( w ) { free_WffNode( w->son ); free_WffNode( w->sons ); free_WffNode( w->next ); if ( w->var_name ) { free( w->var_name ); } if ( w->fact ) free( w->fact ); free_ExpNode( w->lh ); free_ExpNode( w->rh ); free( w ); } }
void free_WffNode( WffNode *w ) { /* commented out due to memory mess too messy to figure out correctly -- sorry. */ return; if ( w ) { free_WffNode( w->son ); free_WffNode( w->sons ); free_WffNode( w->next ); if ( w->var_name ) { free( w->var_name ); } if ( w->fact ) { free( w->fact ); } free( w ); } }
void free_partial_Effect( Effect *e ) { if ( e ) { free_partial_Effect( e->next ); free_WffNode( e->conditions ); free( e ); } }
void create_hard_pseudo_effects( PseudoAction *a, Effect *e, int curr_var ) { int par, t, i, m; WffNode *tmp1, *w, *ww; PseudoActionEffect *tmp2; if ( curr_var < e->num_vars ) { par = a->operator->num_vars + curr_var; t = e->var_types[curr_var]; for ( i = 0; i < gtype_size[t]; i++ ) { linst_table[par] = gtype_consts[t][i]; create_hard_pseudo_effects( a, e, curr_var + 1 ); linst_table[par] = -1; } return; } tmp1 = instantiate_wff( e->conditions ); if ( tmp1->connective == FAL ) { free_WffNode( tmp1 ); return; } dnf( &tmp1 ); cleanup_wff( &tmp1 ); /* only debugging, REMOVE LATER */ if ( is_dnf( tmp1 ) == -1 ) { printf("\n\nILLEGAL DNF %s AFTER INSTANTIATION\n\n", a->operator->name); print_Wff( tmp1, 0 ); exit( 1 ); } switch ( tmp1->connective ) { case OR: for ( w = tmp1->sons; w; w = w->next ) { tmp2 = new_PseudoActionEffect(); if ( w->connective == AND ) { m = 0; for ( ww = w->sons; ww; ww = ww->next ) m++; tmp2->conditions = ( Fact * ) calloc( m, sizeof( Fact ) ); tmp2->num_conditions = m; m = 0; for ( ww = w->sons; ww; ww = ww->next ) { tmp2->conditions[m].predicate = ww->fact->predicate; for ( i = 0; i < garity[ww->fact->predicate]; i++ ) { tmp2->conditions[m].args[i] = ww->fact->args[i]; } m++; } } else { tmp2->conditions = ( Fact * ) calloc( 1, sizeof( Fact ) ); tmp2->num_conditions = 1; tmp2->conditions[0].predicate = w->fact->predicate; for ( i = 0; i < garity[w->fact->predicate]; i++ ) { tmp2->conditions[0].args[i] = w->fact->args[i]; } } make_instantiate_literals( tmp2, e->effects ); tmp2->next = a->effects; a->effects = tmp2; a->num_effects++; } break; case AND: tmp2 = new_PseudoActionEffect(); m = 0; for ( w = tmp1->sons; w; w = w->next ) m++; tmp2->conditions = ( Fact * ) calloc( m, sizeof( Fact ) ); tmp2->num_conditions = m; m = 0; for ( w = tmp1->sons; w; w = w->next ) { tmp2->conditions[m].predicate = w->fact->predicate; for ( i = 0; i < garity[w->fact->predicate]; i++ ) { tmp2->conditions[m].args[i] = w->fact->args[i]; } m++; } make_instantiate_literals( tmp2, e->effects ); tmp2->next = a->effects; a->effects = tmp2; a->num_effects++; break; case ATOM: tmp2 = new_PseudoActionEffect(); tmp2->conditions = ( Fact * ) calloc( 1, sizeof( Fact ) ); tmp2->num_conditions = 1; tmp2->conditions[0].predicate = tmp1->fact->predicate; for ( i = 0; i < garity[tmp1->fact->predicate]; i++ ) { tmp2->conditions[0].args[i] = tmp1->fact->args[i]; } make_instantiate_literals( tmp2, e->effects ); tmp2->next = a->effects; a->effects = tmp2; a->num_effects++; break; case TRU: tmp2 = new_PseudoActionEffect(); make_instantiate_literals( tmp2, e->effects ); tmp2->next = a->effects; a->effects = tmp2; a->num_effects++; break; default: printf("\n\nillegal connective %d in parsing DNF precond.\n\n", tmp1->connective); exit( 1 ); } free_WffNode( tmp1 ); }
WffNode *instantiate_wff( WffNode *w ) { WffNode *res = NULL, *tmp, *i; int j, c0, c1, m, h; Bool ok; switch ( w->connective ) { case AND: m = 0; i = w->sons; while ( i ) { tmp = instantiate_wff( i ); if ( tmp->connective == FAL ) { free_WffNode( res ); return tmp; } if ( tmp->connective == TRU ) { free( tmp ); i = i->next; continue; } tmp->next = res; if ( res ) { res->prev = tmp; } res = tmp; i = i->next; m++; } if ( m == 0 ) { res = new_WffNode( TRU ); break; } if ( m == 1 ) { break; } tmp = new_WffNode( AND ); tmp->sons = res; res = tmp; break; case OR: m = 0; i = w->sons; while ( i ) { tmp = instantiate_wff( i ); if ( tmp->connective == TRU ) { free_WffNode( res ); return tmp; } if ( tmp->connective == FAL ) { free( tmp ); i = i->next; continue; } tmp->next = res; if ( res ) { res->prev = tmp; } res = tmp; i = i->next; m++; } if ( m == 0 ) { res = new_WffNode( FAL ); break; } if ( m == 1 ) { break; } tmp = new_WffNode( OR ); tmp->sons = res; res = tmp; break; case NOT: /* must be non-equality */ c0 = ( w->son->fact->args[0] < 0 ) ? linst_table[DECODE_VAR( w->son->fact->args[0] )] : w->son->fact->args[0]; c1 = ( w->son->fact->args[1] < 0 ) ? linst_table[DECODE_VAR( w->son->fact->args[1] )] : w->son->fact->args[1]; if ( c0 < 0 || c1 < 0 ) { /* ef param while checking ef conds in inst op */ res = new_WffNode( ATOM ); res->fact = new_Fact(); res->fact->predicate = -2; res->fact->args[0] = ( c0 < 0 ) ? w->son->fact->args[0] : c0; res->fact->args[1] = ( c1 < 0 ) ? w->son->fact->args[1] : c1; break; } if ( c0 != c1 ) { res = new_WffNode( TRU ); } else { res = new_WffNode( FAL ); } break; case ATOM: res = new_WffNode( ATOM ); res->fact = new_Fact(); res->fact->predicate = w->fact->predicate; ok = TRUE; for ( j = 0; j < garity[res->fact->predicate]; j++ ) { h = ( w->fact->args[j] < 0 ) ? linst_table[DECODE_VAR( w->fact->args[j] )] : w->fact->args[j]; if ( h < 0 ) { ok = FALSE; res->fact->args[j] = w->fact->args[j]; } else { res->fact->args[j] = h; } } if ( !ok ) {/* contains ef params */ break; } if ( !full_possibly_negative( res->fact ) ) { free( res->fact ); res->fact = NULL; res->connective = TRU; break; } if ( !full_possibly_positive( res->fact ) ) { free( res->fact ); res->fact = NULL; res->connective = FAL; break; } break; case TRU: case FAL: res = new_WffNode( w->connective ); break; default: printf("\n\nillegal connective %d in instantiate formula\n\n", w->connective); exit( 1 ); } return res; }
void create_hard_mixed_operators( Operator *o, int curr_var ) { int t, i, m; WffNode *tmp1, *w, *ww; MixedOperator *tmp2; if ( curr_var < o->num_vars ) { if ( o->removed[curr_var] ) { /* param doesn't matter -- select any appropriate type constant * at least one there; otherwise, op would not have been translated. */ linst_table[curr_var] = gtype_consts[o->var_types[curr_var]][0]; create_hard_mixed_operators( o, curr_var + 1 ); linst_table[curr_var] = -1; return; } t = o->var_types[curr_var]; for ( i = 0; i < gtype_size[t]; i++ ) { linst_table[curr_var] = gtype_consts[t][i]; create_hard_mixed_operators( o, curr_var + 1 ); linst_table[curr_var] = -1; } return; } tmp1 = instantiate_wff( o->preconds ); if ( tmp1->connective == FAL ) { free_WffNode( tmp1 ); return; } dnf( &tmp1 ); cleanup_wff( &tmp1 ); if ( tmp1->connective == FAL ) { free_WffNode( tmp1 ); return; } /* only debugging, REMOVE LATER */ if ( is_dnf( tmp1 ) == -1 ) { printf("\n\nILLEGAL DNF %s AFTER INSTANTIATION\n\n", o->name); print_Wff( tmp1, 0 ); exit( 1 ); } switch ( tmp1->connective ) { case OR: for ( w = tmp1->sons; w; w = w->next ) { tmp2 = new_MixedOperator( o ); for ( i = 0; i < o->num_vars; i++ ) { tmp2->inst_table[i] = linst_table[i]; } if ( w->connective == AND ) { m = 0; for ( ww = w->sons; ww; ww = ww->next ) m++; tmp2->preconds = ( Fact * ) calloc( m, sizeof( Fact ) ); tmp2->num_preconds = m; m = 0; for ( ww = w->sons; ww; ww = ww->next ) { tmp2->preconds[m].predicate = ww->fact->predicate; for ( i = 0; i < garity[ww->fact->predicate]; i++ ) { tmp2->preconds[m].args[i] = ww->fact->args[i]; } m++; } } else { tmp2->preconds = ( Fact * ) calloc( 1, sizeof( Fact ) ); tmp2->num_preconds = 1; tmp2->preconds[0].predicate = w->fact->predicate; for ( i = 0; i < garity[w->fact->predicate]; i++ ) { tmp2->preconds[0].args[i] = w->fact->args[i]; } } tmp2->effects = instantiate_Effect( o->effects ); tmp2->next = ghard_mixed_operators; ghard_mixed_operators = tmp2; gnum_hard_mixed_operators++; } break; case AND: tmp2 = new_MixedOperator( o ); for ( i = 0; i < o->num_vars; i++ ) { tmp2->inst_table[i] = linst_table[i]; } m = 0; for ( w = tmp1->sons; w; w = w->next ) m++; tmp2->preconds = ( Fact * ) calloc( m, sizeof( Fact ) ); tmp2->num_preconds = m; m = 0; for ( w = tmp1->sons; w; w = w->next ) { tmp2->preconds[m].predicate = w->fact->predicate; for ( i = 0; i < garity[w->fact->predicate]; i++ ) { tmp2->preconds[m].args[i] = w->fact->args[i]; } m++; } tmp2->effects = instantiate_Effect( o->effects ); tmp2->next = ghard_mixed_operators; ghard_mixed_operators = tmp2; gnum_hard_mixed_operators++; break; case ATOM: tmp2 = new_MixedOperator( o ); for ( i = 0; i < o->num_vars; i++ ) { tmp2->inst_table[i] = linst_table[i]; } tmp2->preconds = ( Fact * ) calloc( 1, sizeof( Fact ) ); tmp2->num_preconds = 1; tmp2->preconds[0].predicate = tmp1->fact->predicate; for ( i = 0; i < garity[tmp1->fact->predicate]; i++ ) { tmp2->preconds[0].args[i] = tmp1->fact->args[i]; } tmp2->effects = instantiate_Effect( o->effects ); tmp2->next = ghard_mixed_operators; ghard_mixed_operators = tmp2; gnum_hard_mixed_operators++; break; case TRU: tmp2 = new_MixedOperator( o ); for ( i = 0; i < o->num_vars; i++ ) { tmp2->inst_table[i] = linst_table[i]; } tmp2->effects = instantiate_Effect( o->effects ); tmp2->next = ghard_mixed_operators; ghard_mixed_operators = tmp2; gnum_hard_mixed_operators++; break; default: printf("\n\nillegal connective %d in parsing DNF precond.\n\n", tmp1->connective); exit( 1 ); } free_WffNode( tmp1 ); }
WffNode *instantiate_wff( WffNode *w ) { WffNode *res = NULL, *tmp, *i; int j, m, h; Bool ok, ct; switch ( w->connective ) { case AND: m = 0; i = w->sons; while ( i ) { tmp = instantiate_wff( i ); if ( tmp->connective == FAL ) { free_WffNode( res ); return tmp; } if ( tmp->connective == TRU ) { free( tmp ); i = i->next; continue; } tmp->next = res; if ( res ) { res->prev = tmp; } res = tmp; i = i->next; m++; } if ( m == 0 ) { res = new_WffNode( TRU ); break; } if ( m == 1 ) { break; } tmp = new_WffNode( AND ); tmp->sons = res; res = tmp; break; case OR: m = 0; i = w->sons; while ( i ) { tmp = instantiate_wff( i ); if ( tmp->connective == TRU ) { free_WffNode( res ); return tmp; } if ( tmp->connective == FAL ) { free( tmp ); i = i->next; continue; } tmp->next = res; if ( res ) { res->prev = tmp; } res = tmp; i = i->next; m++; } if ( m == 0 ) { res = new_WffNode( FAL ); break; } if ( m == 1 ) { break; } tmp = new_WffNode( OR ); tmp->sons = res; res = tmp; break; case ATOM: res = new_WffNode( ATOM ); res->fact = new_Fact(); res->fact->predicate = w->fact->predicate; ok = TRUE; for ( j = 0; j < garity[res->fact->predicate]; j++ ) { h = ( w->fact->args[j] < 0 ) ? linst_table[DECODE_VAR( w->fact->args[j] )] : w->fact->args[j]; if ( h < 0 ) { ok = FALSE; res->fact->args[j] = w->fact->args[j]; } else { res->fact->args[j] = h; } } if ( !ok ) {/* contains ef params */ break; } if ( !full_possibly_negative( res->fact ) ) { free( res->fact ); res->fact = NULL; res->connective = TRU; break; } if ( !full_possibly_positive( res->fact ) ) { free( res->fact ); res->fact = NULL; res->connective = FAL; break; } break; case COMP: res = new_WffNode( COMP ); res->comp = w->comp; res->lh = copy_Exp( w->lh ); res->rh = copy_Exp( w->rh ); instantiate_exp( &(res->lh) ); instantiate_exp( &(res->rh) ); if ( res->lh->connective != NUMBER || res->rh->connective != NUMBER ) { /* logical simplification only possible if both parts are numbers */ break; } ct = number_comparison_holds( res->comp, res->lh->value, res->rh->value ); if ( ct ) { res->connective = TRU; free_ExpNode( res->lh ); res->lh = NULL; free_ExpNode( res->rh ); res->rh = NULL; res->comp = -1; } else { res->connective = FAL; free_ExpNode( res->lh ); res->lh = NULL; free_ExpNode( res->rh ); res->rh = NULL; res->comp = -1; } break; case TRU: case FAL: res = new_WffNode( w->connective ); break; default: printf("\n\nillegal connective %d in instantiate formula\n\n", w->connective); exit( 1 ); } return res; }