?? instantiatei.c
字號:
/********************************************************************* * File: instantiateI.c * Description: code for instantiating operators, first part. * - transforms PlNodes to CodeNodes * - performs syntax check * - builds gobal tuple tables * - encodes unary inertia as types * * Author: Joerg Hoffmann 1999 * *********************************************************************/ /********************************************************************* * (C) Copyright 1998 Albert Ludwigs University Freiburg * Institute of Computer Science * * All rights reserved. Use of this software is permitted for * non-commercial research purposes, and it may be copied only * for that use. All copies must include this copyright message. * This software is made available AS IS, and neither the authors * nor the Albert Ludwigs University Freiburg make any warranty * about the software or its performance. *********************************************************************//********************************************************************* * * one thing that I'd like to say about the instantiation code is this: * * it might look terrible to have four big files of C code just * to instantiate the operators, and in fact, it is. * * on the other hand, this is only the result of the terrible * things that the domain designer is allowed to define in * full scale PDDL. * * though the code produces a very tight domain representation * in all cases, see Technical Report 122 * "Handling of Inertia in a Planning System" on the IPP homepage, * there are quite some cases where it could be made far more * efficient. At the very least, if one is facing a simple STRIPS * domain, it's better to skip the whole thing and use a specialised * algorithm which will be faster than this code by factors in the order * of hundreds... * **********************************************************************/#include "ipp.h"#include "output.h"#include "utilities.h"#include "memory.h"#include "instantiateI.h" /* * ----------------------------- SET UP GLOBAL STRING ARRAYS ---------------------------- */void collect_all_strings( void ){ FactList *f; TokenList *t; int p_num, type_num, c_num, i, j, ar; Integers *tmp, *g; for ( f = gorig_constant_list; f; f = f->next ) { if ( (type_num = position_in_types_table( f->item->next->item )) == -1 ) { if ( gtypes_table_size == MAX_TYPES_TABLE ) { printf("\ntoo many types! increase MAX_TYPES_TABLE (currently %d)\n\n", MAX_TYPES_TABLE); exit( 1 ); } gtypes_table[gtypes_table_size].name = copy_string( f->item->next->item ); gtypes_table[gtypes_table_size].integers = NULL; type_num = gtypes_table_size++; } if ( (c_num = position_in_constants_table( f->item->item )) == -1 ) { if ( gconstants_table_size == MAX_CONSTANTS_TABLE ) { printf("\ntoo many constants! increase MAX_CONSTANTS_TABLE (currently %d)\n\n", MAX_CONSTANTS_TABLE); exit( 1 ); } gconstants_table[gconstants_table_size] = copy_string( f->item->item ); c_num = gconstants_table_size++; } tmp = new_integers( c_num ); for ( g = gtypes_table[type_num].integers; g; g = g->next ) { if ( g->index == c_num ) break; } if ( !g ) { tmp->next = gtypes_table[type_num].integers; gtypes_table[type_num].integers = tmp; } } for ( f = gpredicates_and_types; f; f = f->next ) { if ( (p_num = position_in_predicates_table( f->item->item )) != -1 ) { printf("\npredicate %s declared twice!\n\n", f->item->item); exit(BADDOMAIN_ERROR_CODE); } if ( gpredicates_table_size == MAX_PREDICATES_TABLE ) { printf("\ntoo many predicates! increase MAX_PREDICATES_TABLE (currently %d)\n\n", MAX_PREDICATES_TABLE); exit( 1 ); } gpredicates_table[gpredicates_table_size] = copy_string( f->item->item ); ar = 0; for ( t = f->item->next; t; t = t->next ) { if ( (type_num = position_in_types_table( t->item )) == -1 ) { printf("\nwarning: predicate %s uses unknown or empty type %s\n\n", f->item->item, t->item); } gpredicates_args_type[gpredicates_table_size][ar++] = type_num; } garity[gpredicates_table_size++] = ar; } free_complete_fact_list( gorig_constant_list ); free_complete_fact_list( gpredicates_and_types ); if ( gcmd_line.display_info == 2 || gcmd_line.display_info == 3 ) { printf("\nconstant table:"); for ( i = 0; i < gconstants_table_size; i++ ) { printf("\n%d --> %s", i, gconstants_table[i]); } printf("\n\ntypes table:"); for ( i = 0; i < gtypes_table_size; i++ ) { printf("\n%d --> %s: ", i, gtypes_table[i].name); for ( tmp=gtypes_table[i].integers; tmp; tmp=tmp->next ) { printf("%d ", tmp->index); } } printf("\n\npredicates table:"); for ( i = 0; i < gpredicates_table_size; i++ ) { printf("\n%d --> %s: ", i, gpredicates_table[i]); for ( j = 0; j < garity[i]; j++ ) { printf("%s ", gtypes_table[gpredicates_args_type[i][j]].name); } } printf("\n\n"); }}int position_in_types_table( char *str ){ int i; for ( i=0; i<gtypes_table_size; i++ ) { if ( str == gtypes_table[i].name || (strcmp( str, gtypes_table[i].name ) == SAME) ) { break; } } return ( i == gtypes_table_size ) ? -1 : i;}int position_in_constants_table( char *str ){ int i; for ( i=0; i<gconstants_table_size; i++ ) { if ( str == gconstants_table[i] || strcmp( str, gconstants_table[i] ) == SAME ) { break; } } return ( i == gconstants_table_size ) ? -1 : i;}int position_in_predicates_table( char *str ){ int i; for ( i=0; i<gpredicates_table_size; i++ ) { if ( str == gpredicates_table[i] || strcmp( str, gpredicates_table[i] ) == SAME ) { break; } } return ( i == gpredicates_table_size ) ? -1 : i;} /* * ----------------------- TRANSLATE TO CODE NODES + SYNTAX CHECK ------------------------ */char *lvar_str[MAX_VARS];int lvar_types[MAX_VARS];void transform_PlNodes_to_CodeNodes( void ){ PlOperator *op; CodeOperator *tmp, *j; if ( !normalize_initial( &gorig_initial_facts ) ) { printf("\nillegal initial state!\n\n"); exit(BADDOMAIN_ERROR_CODE); } if ( !is_legal_condition( gorig_goal_facts ) ) { printf("\nillegal goal state!\n\n"); exit(BADDOMAIN_ERROR_CODE); } for ( op = gloaded_ops; op; op = op->next ) { if ( !is_legal_condition( op->preconds ) ) { printf("\nop %s has illegal precondition\n\n", op->name->item); exit(BADDOMAIN_ERROR_CODE); } if ( !normalize_effects( &op->effects ) ) { printf("\nop %s has illegal effects\n\n", op->name->item); exit(BADDOMAIN_ERROR_CODE); } } gcode_initial_state = transform_Pl_to_Code( gorig_initial_facts, 0 ); gcode_goal_state = transform_Pl_to_Code( gorig_goal_facts, 0 ); for ( op = gloaded_ops; op; op = op->next ) { tmp = transform_PlOp_to_CodeOp( op ); if ( tmp != NULL ) { tmp->next = gcode_operators; gcode_operators = tmp; } } if ( gcmd_line.display_info == 3 ) { printf("\ntranslation to CodeNodes complete."); printf("\ncoded initial state is:\n"); print_CodeNode( gcode_initial_state, 0 ); printf("\ncoded goal state is:\n"); print_CodeNode( gcode_goal_state, 0 ); printf("\ncoded operators are:\n"); for ( j = gcode_operators; j; j = j->next ) { print_CodeOperator( j ); } }} Bool normalize_initial( PlNode **n ){ PlNode *i, *tmp; TokenList *t; if ( !(*n) ) { return TRUE; } if ( (*n)->connective != AND ) { tmp = copy_pl_node( *n ); tmp->sons = (*n)->sons; tmp->next = (*n)->next; (*n)->connective = AND; (*n)->sons = tmp; (*n)->next = NULL; return normalize_initial( n ); } for ( i = (*n)->sons; i; i=i->next ) { if ( i->connective != ATOM ) { printf("\nnon ATOM in initial state"); return FALSE; } if ( i->sons != NULL ) { printf("\ninitial atom has sons"); return FALSE; } if ( (strcmp( i->atom->item, "EQ" ) == SAME) || (strcmp( i->atom->item, "eq" ) == SAME) ) { printf("\neq predicate in initial state"); return FALSE; } for ( t = i->atom->next; t; t = t->next ) { if ( t->item[0] == '?' ) { printf("\nvariable in initial state"); return FALSE; } } } return TRUE;}Bool is_legal_condition( PlNode *n ){ PlNode *i; if ( !n ) { return TRUE; } switch ( n->connective ) { case ATOM: if ( n->sons != NULL ) { printf("\nATOM has son"); return FALSE; } return TRUE; case ALL: case EX: case NOT: if ( !(n->sons) ) { printf("\n%s without sons", gconnectives[n->connective]); } if ( !is_legal_condition( n->sons ) ) { return FALSE; } return TRUE; case AND: case OR: if ( !(n->sons) ) { printf("\n%s without sons", gconnectives[n->connective]); return FALSE; } for ( i=n->sons; i; i = i->next ) { if ( !is_legal_condition( i ) ) { return FALSE; } } return TRUE; case TRU: case FAL: if ( n->sons ) { printf("\n%s with sons", gconnectives[n->connective]); return FALSE; } return TRUE; default: printf("\nillegal connective in condition: %s", gconnectives[n->connective]); return FALSE; } return TRUE;}Bool normalize_effects( PlNode **n ){ PlNode *i, *j, *tmp; if ( !(*n) ) { return TRUE; } if ( (*n)->connective != AND ) { tmp = copy_pl_node( *n ); tmp->sons = (*n)->sons; tmp->next = (*n)->next; (*n)->connective = AND; (*n)->sons = tmp; (*n)->next = NULL; return normalize_effects( n ); } for ( i = (*n)->sons; i; i = i->next ) { for ( j = i; j; j = j->sons ) { if ( j->connective == WHEN ) { break; } if ( j->connective != ALL ) { printf("\nnon ALL before WHEN"); return FALSE; } } if ( !j ) { printf("\neffect without WHEN"); return FALSE; } if ( !j->sons ) { printf("\neffect has neither condition nor postconds"); return FALSE; } if ( !j->sons->next ) { printf("\neffect has empty postconds"); return FALSE; } if ( !is_legal_condition( j->sons ) ) { printf("\neffect condition illegal"); return FALSE; } if ( !normalize_effect( &(j->sons->next) ) ) { printf("\neffect illegal"); return FALSE; } } return TRUE;}Bool normalize_effect( PlNode **n ){ PlNode *i, *tmp; if ( !(*n) ) { return FALSE; } if ( (*n)->connective != AND ) { tmp = copy_pl_node( *n ); tmp->sons = (*n)->sons; tmp->next = (*n)->next; (*n)->connective = AND; (*n)->sons = tmp; (*n)->next = NULL; return normalize_effect( n ); } for ( i = (*n)->sons; i; i = i->next ) { if ( i->connective != ATOM ) { if ( i->connective != NOT ) { printf("\nnon literal in effect"); return FALSE; } if ( !i->sons ) { printf("NOT without son"); return FALSE; } if ( i->sons->connective != ATOM ) { printf("\nnon literal in effect"); return FALSE; } if ( i->sons->sons || i->sons->next ) { printf("\nnegated atom with sons/next"); return FALSE; } if ( (strcmp( i->sons->atom->item, "EQ" ) == SAME) || (strcmp( i->sons->atom->item, "eq" ) == SAME) ) { printf("\neq predicate in effect"); return FALSE; } } else { if ( (strcmp( i->atom->item, "EQ" ) == SAME) || (strcmp( i->atom->item, "eq" ) == SAME) ) { printf("\neq predicate in effect"); return FALSE; } if ( i->sons ) { printf("\natom with sons"); return FALSE; } } } return TRUE;}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -