htncode

% Provides a prolog database env % % % % Logicmoo Project PrologMUD: A MUD server written in Prolog % Maintainer: Douglas Miles % Denton, TX 2005, 2010, 2014 % Dec 13, 2035 % */

   12:-module(htncode,[]).   13
   14
   15:-multifile(user:push_env_ctx/0).   16:-dynamic(user:push_env_ctx/0).   17
   18/* ***********************************/
   19/* Douglas Miles 2005, 2010, 2014 */
   20/* Denton, TX */
   21/* ***********************************/
   22
   23
   24
   25% [Required] Load the Logicmoo Library Utils
   26:- ensure_loaded(library(logicmoo_common)).   27:- ensure_loaded(library(planner_api)).   28:- use_module(library(logicmoo_planner)).   29
   30:- kb_shared(baseKB:mpred_prop/3).   31
   32:- ensure_loaded(library(logicmoo/util_structs)).   33:- ensure_loaded(library(logicmoo/util_bb_env)).   34:- prolog_load_context(file,File),ain(user:env_source_file(File)).   35
   36:-op(500,fx,env_call).
   37/* htncode.pl */

   38
   39/* ***********************************/

   40/* Donghong Liu */

   41/* University of Huddersfield */

   42/* September 2002 */

   43/* **********************************/

   44
   45:- style_check(-singleton).
   46:-use_module(library(system)).
   47
   48/*********************** initialisation**************/

   49:- dynamic op_num/1. 
   50:- dynamic sum/1.
   51%:- dynamic initial_node/1, final_node/1.
   52:- dynamic methodC/7, operatorC/5, my_stats/1.
   53%%:- dynamic node/7,,solved_node/5.
   54:- dynamic tp_node/6,tn/6.
   55:- dynamic solved_node/2.
   56:- dynamic closed_node/6.
   57:- dynamic gsubstate_classes/3.
   58:- dynamic current_num/2.
   59:- dynamic produce/4,goal_related/1. %%,achieved_goal/1.
   60%:- unknown(error,fail).
   61:- dynamic gsstates/3,gpred/2.
   62% for boot..
   63:- dynamic kill_file/1,solution_file/1.
   64%
   65:- op(100,xfy,'=>').
   66
   67:- dynamic htn_task/3.
   68:- multifile htn_task/3.
   69:- dynamic planner_task/3.
   70htn_task(Id,Goal,Init):-planner_task(Id,Goal,Init).
   71
   72% The following elements are expected in the sort engineered domain model
   73% 
   74:-dynamic  sorts/2,
   75  objects/2,
   76  predicates/1,
   77  atomic_invariants/1,
   78  substate_classes/3,
   79  method/6,
   80  operator/4,
   81  inconsistent_constraint/1.
   82  
   83%  ss class expressions
   84%  invariants:
   85%   atomic invariants
   86%   -ve invariants
   87%   +ve invariants
   88%  operators
   89%  foo.
   90
   91op_num(0).
   92my_stats(0).
   93solution_file(fred).
   94
   95%:-set_prolog_flag(unknown,fail).
   96solve(Id) :-
   97	htn_task(Id,Goal,Init),
   98	once(planner_interface(Goal,Init,Sol,_)),
   99%	solution_file(F),
  100%	gensym_num(F,Id,TRF),
  101%	tell(user),
  102%	write('TASK '),write(Id),nl,
  103%	write('SOLUTION'),nl,
  104%	display_sol(Sol),
  105%	write('END FILE'),nl,
  106%	told,
  107	clean.
  108
  109display_sol([]).
  110display_sol([H|T]) :-
  111	write(H),
  112	nl,
  113	display_sol(T).
  114
  115clean:-
  116	retractall(op_num(_)),
  117	retractall(current_num(_,_)),
  118	retractall(node(_,_,_,_,_)),
  119	retractall(tn(_,_,_,_,_,_)),
  120	retractall(gpredicates(_,_)),
  121	retractall(gsubstate_classes(_,_,_)),
  122	retractall(methodC(_,_,_,_,_,_,_)),
  123	retractall(operatorC(_,_,_,_,_)),
  124	retractall(goal_related(_)),
  125	retractall(produce(_,_,_,_)),
  126	retractall(solved_node(_,_)),
  127	retractall(tp_node(_,_,_,_,_,_)),
  128	retractall(closed_node(_,_,_,_,_,_)),
  129	retractall(score_list(_)),
  130	assert(op_num(0)),
  131	assert(my_stats(0)).
  132
  133planner_interface(G,I, SOLN,NODES):-
  134	time(ground_hierarchy),
  135	change_op_representation,
  136	find_relate_state,
  137        retract(op_num(_)),
  138        assert(op_num(0)),
  139        statistics(runtime,[_,Time]),
  140        (retract(my_stats(_)) ; true),
  141        assert(my_stats(Time)),
  142        make_problem_into_node(I, G, Node),
  143        assert(Node),
  144	start_solve(SOLN,NODES).
  145planner_interface(G,I, SOLN,NODES):-
  146	tell(user),nl,write('failure in initial node'),!.
  147
  148
  149	
  150/******************** HP ADT *********************/

  151
  152% node(Name, Precond ,Decomps, Temp, Statics)
  153getN_name(node(Name, _, _, _,_),  Name).
  154getN_pre(node(_,Pre, _, _, _),  Pre).
  155getN_decomp(node(_, _, Decomp,_,_),  Decomp).
  156getH_temp(node(_, _, _,Temps, _),  Temps).
  157getN_statics(node(_,_,_,_,Statics),  Statics).
  158
  159%Ron  21/9/01 - Try to give a closedown method
  160start_solve(SOLN,NODES):-
  161	kill_file(Kill),
  162	file_exists(Kill).
  163%	write('Found kill file'),nl.
  164
  165start_solve(Sol,OPNUM):-
  166   retract(final_node(Node)),
  167   retractall(current_num(_,_)),
  168   getN_statics(Node,Statics),
  169   statics_consist(Statics),
  170   extract_solution(Node,Sol,SIZE),
  171   statistics(runtime,[_,CP]),
  172   TIM is CP/1000,
  173   retract(op_num(OPNUM)),
  174   assert(op_num(0)),
  175
  176   nl, nl, write('CPU Time = '),write(CP),nl,
  177   write('TIME TAKEN = '),write(TIM),
  178   write(' SECONDS'),nl,
  179   write('Solution SIZE = '),write(SIZE),nl,
  180   write('Operator Used = '),write(OPNUM),nl,
  181   write('Solution:\n '),writeq(Sol),nl,
  182   write('***************************************'),
  183   assert(time_taken(CP)),  
  184   assert(soln_size(SIZE)),
  185   assert(plan_used(OPNUM)),
  186   retractall(tn(_,_,_,_,_,_)),!.
  187
  188start_solve(Sol,OPNUM):-
  189   select_node(Node),
  190%   nl,write('processing '),write(Node),nl,
  191            % expand/prove its hps
  192   process_node(Node),
  193   start_solve(Sol,OPNUM),!.
  194start_solve(Sol,OPNUM):-
  195    tell(user), write('+++ task FAILED +++'),
  196    clean.
  197
  198/******************************** MAIN LOOP **********/

  199
  200% expand a node..
  201process_node(Node) :-
  202   getN_name(Node,  Name),
  203   getN_pre(Node, Pre),
  204   getN_decomp(Node, Dec),
  205   getH_temp(Node, Temps),
  206   getN_statics(Node, Statics),
  207   expand_decomp(Dec,Pre,Post,Temps,Temp1,Statics,Statics1,Dec1),
  208   statics_consist(Statics),
  209   assert_node(Name,Pre,Dec1,Temp1,Statics1).
  210
  211assert_node(Name,Pre,Decomp,Temp,Statics):-
  212   all_HP_expanded(Decomp),
  213   assert(final_node(node(Name,Pre,Decomp,Temp,Statics))),!.
  214assert_node(Name,Pre,Dec,Temp,Statics):-
  215   gensym_special(root,SYM),
  216   assert(node(SYM,Pre,Dec,Temp,Statics)),!.
  217
  218all_HP_expanded([]):-!.
  219all_HP_expanded([step(HPid,Name,_,_,exp(TN))|THPS]):-
  220   all_HP_expanded(THPS),!.
  221/************ expand every step *********************/

  222expand_decomp([],Post,Post,Temp,Temp,Statics,Statics,[]):-!.
  223%0-1 if the step has expand already, get the state change, go to next
  224expand_decomp([step(HPid,Name,Pre0,Post0,exp(TN))|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,State1,Post1,exp(TN))|Decomp1]):-
  225   state_achieved(Pre0,Pre,State1),
  226   state_change(State1,Pre0,Post0,Post1),
  227   statics_consist(Statics),
  228   expand_decomp(Decomp,Post1,Post,Temp,Temp1,Statics,Statics1,Decomp1),!.
  229
  230%0-2. if it is an achieve action
  231expand_decomp([step(HPid,ACH,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
  232   ACH=..[achieve|_],
  233   state_achieved(Pre0,Pre,State),
  234   statics_consist(Statics),
  235   expand_decomp_ach([step(HPid,ACH,State,Post0,unexp)|Decomp],State,Post,Temp,Temp1,Statics,Statics1,Decomp1),!.
  236/*
  237% 1. if the step's Name is match a exist TN also Pre meet, return TN
  238% tn(Tn0,Name,Pre0,Post0,Temp0,Decomp)is a task network
  239% which was generated by previous process,
  240% it has been decomposed to ordered primitive operators
  241% and has all states changes defiend
  242expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(Tn0))|Decomp1]):-
  243   tn(Tn0,Name,Pre0,Post0,Temp0,Decomp0),
  244   state_achieved(Pre0,Pre,State1),
  245   statics_consist(Statics),
  246   state_change(State1,Pre0,Post0,State),
  247%   nl,write('step '),write(HPid),
  248   retract(op_num(N)),
  249   N1 is N+1,
  250   assert(op_num(N1)),
  251%   write('can be expand by tn '),write(Tn0),nl,
  252   expand_decomp(Decomp,State,Post,Temp,Temp1,Statics,Statics1,Decomp1),!.
  253*/

  254% 2. if HP's name meet an method,
  255% and it's precondition and achieve goals are achieved
  256% expand it and make it to that TNs
because the conditions are not ground at upper level so it have to be push down to the ground level to find all the state changes
  262expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(TN))|Decomp1]):-
  263   methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH),Dec0),
  264   append_cut(Statics0,Statics,Statics2),
  265   statics_consist(Statics2),
  266   remove_unneed(Statics2,[],Statics3),
  267   state_achieved(Pre0,Pre,State1),
  268%   state_achieved(ACH,State1,State11),
  269   statics_consist(Statics3),
  270   apply_method1(HPid,TN,Name,State1,Pre0,ACH,Post0,State,Statics3,Temp0,Dec0),
  271%   apply_method(HPid,TN,Name,State11,Pre0,Post0,State,Statics21,Temp0,Dec0),
  272% changed here for if there are too many unordered tasks
  273% apply_method1 is directly apply method even it's achieve goal not achieved
  274%   nl,write('step '),write(HPid),
  275   retract(op_num(N)),
  276   N1 is N+1,
  277   assert(op_num(N1)),
  278%   write('can be expand by method '),write(Name),nl,
  279   expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
  280
  281
  282
  283
  284
  285
  286
  287
  288% 3. if HP's name and it's Pre meet an operator, return operator's name
  289expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(Name))|Decomp1]):-
  290   operatorC(Name,Pre0,Post0,Cond,Statics0),
  291   append_cut(Statics0,Statics,Statics2),
  292   statics_consist(Statics2),
  293   remove_unneed(Statics2,[],Statics21),
  294   state_achieved(Pre0,Pre,State1),
  295   state_change(State1,Pre0,Post0,State2),
  296   cond_state_change(State2,Cond,State),
  297%   nl,write('step '),write(HPid),
  298   statics_consist_instance(Statics0),
  299%   write('can be expand by operator '),write(Name),nl,
  300   retract(op_num(N)),
  301   N1 is N+1,
  302   assert(op_num(N1)),
  303   expand_decomp(Decomp,State,Post,Temp,Temp1,Statics21,Statics1,Decomp1),!.
  304
  305% 4. get another step which matchs and not after it before it to give a try
  306expand_decomp([step(HP,N,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-  
  307   get_another_step(step(HP2,N2,Pre2,Post2,Exp),Pre,Statics,
  308                                  HP,Temp,Temp2,Decomp,Decomp2),
  309   expand_decomp([step(HP2,N2,Pre2,Post2,Exp),step(HP,N,Pre0,Post0,unexp)|Decomp2],Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1).
  310
  311/*
  312% 5. if HP's name meet an method,
  313% and it's precondition  achieved
  314% but failed to match achieve_goals
  315% expand it and make it to that TNs
  316expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(TN))|Decomp1]):-
  317   methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH),Dec0),
  318   append_cut(Statics0,Statics,Statics2),
  319   statics_consist(Statics2),
  320   remove_unneed(Statics2,[],Statics3),
  321   state_achieved(Pre0,Pre,State1),
  322   statics_consist(Statics3),
  323   apply_method1(HPid,TN,Name,State1,Pre0,ACH,Post0,State,Statics3,Temp0,Dec0),
  324   % need to expand achieve goals first
  325   expand_decomp(Decomp,State,Post,Temp,Temp1,Statics3,Statics1,Decomp1),!.
  326
  327% 6. if all above failed
  328% get another step which matchs and not after it before it to give a try
  329expand_decomp([step(HP,N,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-  
  330   get_another_step(step(HP2,N2,Pre2,Post2,Exp),Pre,Statics,
  331                                  HP,Temp,Temp2,Decomp,Decomp2),
  332   expand_decomp([step(HP2,N2,Pre2,Post2,Exp),step(HP,N,Pre0,Post0,unexp)|Decomp2],Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1).
  333*/

  334% Else fail expand
  335
  336% get another step which is not after it before it
  337get_another_step(A,Pre,Statics,HP,Temp,Temp1,[],Dec2):-fail.
  338get_another_step(step(HP2,Name2,Pre2,Post2,Exp),Pre,Statics,HP,Temp,[before(HP2,HP)|Temp],Dec,Dec2):-
  339   member(step(HP2,Name2,Pre2,Post2,Exp),Dec),
  340   not(necessarily_before(HP,HP2, Temp)),
  341   state_achieved(Pre2,Pre,_),
  342   statics_consist(Statics),
  343   list_take(Dec,[step(HP2,Name2,Pre2,Post2,Exp)],Dec2).
  344
  345% expand the achieve goal
  346% 1.if the ACH is achieved already
  347%   remove it from decomposion and do the next
  348expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
  349   state_achieved(Post0,Pre,State),
  350   statics_consist(Statics),
  351%   nl,write('step '),write(HPid),
  352%   write('is already achieved'),nl,
  353   remove_temp(Temp,HPid,Temp,Temp2),
  354   expand_decomp(Decomp,State,Post,Temp2,Temp1,Statics,Statics1,Decomp1),!.
  355
  356% 2.take out the already achieved states before expanding
  357expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,ACH,Pre,Post0,exp(TN))|Decomp1]):-
  358   expand_hp(HPid,TN,ACH,Pre,Post0,State,Statics,Statics2),
  359   expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
  360
  361% directly achieve HP's Pre and Post by operator,method or tn
  362expand_hp(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
  363   direct_expand_hp(HPid,TN,ACH,Pre,Post,State,Statics,Statics1),!.
  364% else, nothing directly can achieve HP's Pre and Post
  365% assert a temporely node for forward search
  366% tp_node(Name, Precond, Postcond, Statics,Score,Temp,Decomps)
  367expand_hp(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
  368   make_to_steps1(Pre,Post,Steps),
  369   gensym_special(tp,TP),
  370   get_all_related_goal(Post),
  371   get_score(100,[],Steps,Score),
  372   assert(tp_node(TP,Pre,Post,Statics,Score,Steps)),
  373   fwsearch(TN,Statics1,State),
  374   clean_temp_nodes.
  375
  376% make the achieve goal states [se(..),se(..),..] to separate steps
  377% remove states that have been achieved by precondition
  378% by same them for declobber
  379make_to_steps1(Pre,[],[]):-!.
  380make_to_steps1(Pre,[se(Sort,Obj,SE)|Post],Steps):-
  381   state_achieved([se(Sort,Obj,SE)],Pre,_),
  382   make_to_steps1(Pre,Post,Steps),!.
  383make_to_steps1(Pre,[se(Sort,Obj,SE)|Post],[step(HP,achieve(se(Sort,Obj,SE)),undefd,[se(Sort,Obj,SE)],unexp)|Steps]):-
  384   objects(Sort,Objls),%Sort is primitive 
  385   gensym_special(hp,HP),
  386   make_to_steps1(Pre,Post,Steps),!.
  387make_to_steps1(Pre,[se(Sort,Obj,SE)|Post],[step(HP,achieve(se(Sort,Obj,SE)),undefd,[se(Sort,Obj,SE)],unexp)|Steps]):-
  388   var(Obj),
  389   gensym_special(hp,HP),
  390   make_to_steps1(Pre,Post,Steps),!.
  391make_to_steps1(Pre,[se(SortN,Obj,SE)|Post],[step(HP,achieve(se(PSort,Obj,SE)),undefd,[se(PSort,Obj,SE)],unexp)|Steps]):-
  392   not(var(Obj)),
  393   find_prim_sort(SortN,PSortls),
  394   member(PSort,PSortls),
  395   objects(PSort,Objls),
  396   member(Obj,Objls),
  397   gensym_special(hp,HP),
  398   make_to_steps1(Pre,Post,Steps),!.
  399
  400
  401% get all the states that can achieve the goal state
  402get_all_related_goal(Goal):-
  403    retractall(goal_related(_)),
  404    assert(goal_related(Goal)),
  405    get_all_related_goal1(Goal,States),
  406    retractall(goal_related(_)),
  407    assert(goal_related(States)),!.
  408%    tell(user),write(States),nl,told,!.
  409
  410get_all_related_goal1([],States):-
  411    setof(State,goal_related(State),States1),
  412    set_flatten(States1,[],States),!.
  413get_all_related_goal1([se(Sort,Obj,SE)|Rest],States):-
  414    produce(se(Sort,Obj,SE1),A,OPre,ST),
  415    state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
  416    retract(goal_related(List)),
  417    set_append_e(List,OPre,List1),
  418    assert(goal_related(List1)),
  419    fail.
  420get_all_related_goal1([se(SortN,Obj,SE)|Rest],States):-
  421    nonvar(Obj),
  422    objects(Sort,Objls),
  423    not(not(member(Obj,Objls))),
  424    produce(se(Sort,Obj,SE1),A,OPre,ST),
  425    state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
  426    retract(goal_related(List)),
  427    set_append_e(List,OPre,List1),
  428    assert(goal_related(List1)),
  429    fail.
  430get_all_related_goal1([se(SortN,Obj,SE)|Rest],States):-
  431    var(Obj),
  432    find_prim_sort(SortN,Sortls),
  433    member(Sort,Sortls),
  434    produce(se(Sort,Obj,SE1),A,OPre,ST),
  435    state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
  436    retract(goal_related(List)),
  437    set_append_e(List,OPre,List1),
  438    assert(goal_related(List1)),
  439    fail.
  440get_all_related_goal1([se(Sort,Obj,SE)|Rest],States):-
  441    get_all_related_goal1(Rest,States).
  442
  443%   find out the difference of Pre and Post0 to expand
  444% if only one Obj in Post, don't do check.
  445take_out_achieved(Pre,[se(Sort,Obj,SE)],[],[se(Sort,Obj,SE)]):-!.
  446take_out_achieved(Pre,[],Post,Post):-!.
  447% Pre conditions are all instantiated states
  448% only Post conditions have variables
  449take_out_achieved(Pre,[se(Sort,Obj,ST)|Post],Post0,Post1):-
  450    var(Obj),
  451    append(Post0,[se(Sort,Obj,ST)],Post2),
  452    take_out_achieved(Pre,Post,Post2,Post1),!.
  453take_out_achieved(Pre,[se(Sort,Obj,ST)|Post],Post0,Post1):-
  454    member(se(Sort,Obj,ST1),Pre),
  455    append_diff(Sort,Obj,ST,ST1,Post0,Post2),
  456    list_take(Pre,[se(Sort,Obj,ST1)],Pre2),
  457    take_out_achieved(Pre2,Post,Post2,Post1),!.
  458 
  459% append only the different one
  460append_diff(Sort,Obj,ST,ST1,Post0,Post0):-
  461    not_conflict(Sort,Obj,ST,ST1,STN),!.
  462append_diff(Sort,Obj,ST,ST1,Post0,Post1):-
  463    append(Post0,[se(Sort,Obj,ST)],Post1),!.
  464% ---------------------------------------------------
  465 
  466%1. if an achieve action meets an TN Pre and post meet
  467direct_expand_hp(HPid,Tn0,ACH,Pre,Post,Pre1,Statics,Statics):-
  468   tn(Tn0,Name,Pre0,Post0,Temp0,Decomp0),
  469   state_achieved(Pre0,Pre,State1),
  470   state_achieved(Post,Post0,_),
  471   state_change(State1,Pre0,Post0,Pre1),
  472   statics_consist(Statics),
  473%   nl,write('step '),write(HPid),
  474    retract(op_num(N)),
  475    N1 is N+1,
  476    assert(op_num(N1)),!.
  477%    write('can be expand by tn '),write(Tn0),nl,!.
  478
  479%2. if an action's name meets is an operator's,
  480%    return 
  481direct_expand_hp(HPid,Name,Name,Pre,Post,State,Statics,Statics1):-
  482   operatorC(Name,Pre0,Post0,Cond,Statics0),
  483   append_cut(Statics0,Statics,Statics1),
  484   statics_consist(Statics11),
  485   remove_unneed(Statics11,[],Statics1),
  486   post_instant(Post0,Cond,Statics1,Post),
  487   state_achieved(Pre0,Pre,State1),
  488   state_change(State1,Pre0,Post0,State2),
  489   cond_state_change(State2,Cond,State3),
  490   state_achieved(Post,State3,State),
  491   statics_consist_instance(Statics0),
  492%   nl,write('step '),write(HPid),
  493%   write('can be expand by operator '),write(Name),nl,
  494   retract(op_num(N)),
  495   N1 is N+1,
  496   assert(op_num(N1)),!.
  497
  498%2. if an achieve action meets an operator's Pre and post meet,
  499%    return 
  500direct_expand_hp(HPid,Name,ACH,Pre,Post,State,Statics,Statics1):-
  501   operatorC(Name,Pre0,Post0,Cond,Statics0),
  502   append_cut(Statics0,Statics,Statics2),
  503   statics_consist(Statics2),
  504   remove_unneed(Statics2,[],Statics1),
  505   post_instant(Post0,Cond,Statics1,Post),
  506   state_achieved(Pre0,Pre,State1),
  507   state_change(State1,Pre0,Post0,State2),
  508   cond_state_change(State2,Cond,State3),
  509   state_achieved(Post,State3,State),
  510   statics_consist(Statics1),
  511%   nl,write('step '),write(HPid),
  512%   write('can be expand by operator '),write(Name),nl,
  513   retract(op_num(N)),
  514   N1 is N+1,
  515   assert(op_num(N1)),!.
  516
  517%3. if an achieve action meets a method's pre and post,
  518%    expand it and make it to that TNs
  519direct_expand_hp(HPid,TN,ACH,Pre,Post,State1,Statics,Statics1):-
  520   methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH0),Dec0),
  521   append_cut(Statics0,Statics,Statics2),
  522   statics_consist(Statics2),
  523   remove_unneed(Statics2,[],Statics1),
  524   post_instant(Post0,[],Statics1,Post),
  525   state_achieved(Pre0,Pre,Pre1),
  526   state_achieved(Post,Post0,Post01),
  527   statics_consist(Statics1),
  528   apply_method1(HPid,TN,Name,Pre1,Pre0,ACH0,Post01,State1,Statics1,Temp0,Dec0),!.
  529
  530% apply_method/10: apply method in the HP
  531% when the method name's precondition and achieve goals are achieved
  532% no extra operators are bring in while expansion
  533apply_method(HPid,TN,Name,Pre,Pre0,Post0,State,Statics,Temp,Dec):-
  534   make_dec0(HPid,Dec,Temp,Dec0,Temp0),
  535   expand_method(Dec0,Pre,Post,Temp0,Temp1,Statics,Statics1,Dec1),
  536   state_achieved(Post0,Post,State),
  537   statics_consist(Statics),
  538%   nl,write('step '),write(HPid),
  539%   write('can be expand by method '),write(Name),nl,
  540   retract(op_num(N)),
  541   N1 is N+1,
  542   assert(op_num(N1)),
  543   make_tn(TN,Name,Pre,State,Temp1,Dec1),!.
  544
  545make_dec0(HPid,Dec,Temp,Dec0,Temp0):-
  546    make_dec01(HPid,1,Dec,Dec0),
  547    change_temp(HPid,Temp,[],Temp0),!.
  548make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp1,Dec1):-
  549   state_achieved(ACH,Pre,_),
  550   make_dec01(HPid,1,Dec,Dec1),
  551   change_temp(HPid,Temp,[],Temp1).
  552make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,[before(STID0,STID1)|Temp1],[step(STID0,achieve(ACH),Pre,ACH,unexp)|Dec1]):-
  553   gensym_num(HPid,0,STID0),
  554   gensym_num(HPid,1,STID1),
  555   make_dec01(HPid,1,Dec,Dec1),
  556   change_temp(HPid,Temp,[],Temp1),!.
  557   
  558make_dec01(HPid,_,[],[]):-!.
  559make_dec01(HPid,Num,[HDec|TDec],[step(STID,HDec,undefd,undefd,unexp)|TDec0]):-
  560   operatorC(HDec,_,_,_,_),
  561   gensym_num(HPid,Num,STID),
  562   Num1 is Num + 1,
  563   make_dec01(HPid,Num1,TDec,TDec0).
  564make_dec01(HPid,Num,[HDec|TDec],[step(STID,HDec,undefd,undefd,unexp)|TDec0]):-
  565   methodC(HDec,_,_,_,_,_,_),
  566   gensym_num(HPid,Num,STID),
  567   Num1 is Num + 1,
  568   make_dec01(HPid,Num1,TDec,TDec0).
  569
  570change_temp(HPid,[],Temp2,Temp2):-!.
  571change_temp(HPid,[before(N1,N2)|Temp],Temp2,[before(ST1,ST2)|Temp0]):-
  572   gensym_num(HPid,N1,ST1),
  573   gensym_num(HPid,N2,ST2),
  574   change_temp(HPid,Temp,Temp2,Temp0),!.
  575
  576% expand_method/8 expand method without bring in any other operators
  577% that is: all achieve goals are achieved
  578expand_method([],Post,Post,Temp,Temp,Statics,Statics,[]):-!.
  579expand_method([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(TN))|Decomp1]):-
  580   tn(TN,Name,Pre0,Post0,Temp0,Decomp0),
  581   state_achieved(Pre0,Pre,State1),
  582   state_change(State1,Pre0,Post0,State),
  583   statics_consist(Statics),
  584%   nl,write('step '),write(HPid),
  585    retract(op_num(N)),
  586    N1 is N+1,
  587    assert(op_num(N1)),
  588%    write('can be expand by tn '),write(TN),
  589   expand_method(Decomp,State,Post,Temp,Temp1,Statics,Statics1,Decomp1).
  590expand_method([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(Name))|Decomp1]):-
  591   operatorC(Name,Pre0,Post0,Cond,Statics0),
  592   append_cut(Statics0,Statics,Statics2),
  593   statics_consist(Statics2),
  594   remove_unneed(Statics2,[],Statics21),
  595   post_instant(Post0,Cond,Statics21,Post0),
  596   state_achieved(Pre0,Pre,State1),
  597   state_change(State1,Pre0,Post0,State2),
  598   cond_state_change(State2,Cond,State3),
  599   state_achieved(Post0,State3,State), 
  600   statics_consist_instance(Statics0),
  601%   nl,write('step '),write(HPid),
  602%   write('can be expand by operator '),write(Name),nl,
  603    retract(op_num(N)),
  604    N1 is N+1,
  605    assert(op_num(N1)),
  606   expand_method(Decomp,State,Post,Temp,Temp1,Statics21,Statics1,Decomp1).
  607expand_method([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(TN))|Decomp1]):-
  608   methodC(Name,Pre0,Post0,Statics0,Temp0,ACH,Dec0),
  609   append_cut(Statics0,Statics,Statics2),
  610   statics_consist(Statics2),
  611   remove_unneed(Statics2,[],Statics21),
  612   post_instant(Post0,[],Statics21,Post0),
  613   state_achieved(Pre0,Pre,State1),
  614   statics_consist(Statics21),
  615   apply_method(HPid,TN,Name,State1,Pre0,Post0,State,Statics21,Temp0,Dec0),
  616   expand_method(Decomp,State,Post,Temp,Temp1,Statics21,Statics1,Decomp1).
  617% --------------------end of apply_method/10---------------------
  618
  619% apply_method1/11: apply method in the HP
  620% when the method name's precondition achieved
  621% but need bring in operators while expansion
  622apply_method1(HPid,TN,Name,Pre,Pre0,ACH,Post0,State,Statics,Temp,Dec):-
  623   make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp2,Dec2),
  624   expand_decomp(Dec2,Pre,Post,Temp2,Temp1,Statics,Statics1,Dec1),
  625   state_achieved(Post0,Post,State),
  626%   nl,write('step '),write(HPid),
  627%   write('can be expand by method '),write(Name),nl,
  628   retract(op_num(N)),
  629   N1 is N+1,
  630   assert(op_num(N1)),
  631   make_tn(TN,Name,Pre,State,Temp1,Dec1),!.
  632
  633
  634% --------------------end of apply_method1/11---------------------
  635
  636% forward search for operators can't directly solved
  637fwsearch(TN,ST,State):-
  638   retract(solved_node(ST,step(HP,Name,Pre,State,exp(TN)))).
  639fwsearch(TN,ST,State):-
  640   select_tnode(tp_node(TP,Pre,Post,Statics,Score,Dec)),
  641   assert(closed_node(TP,Pre,Post,Statics,Score,Dec)),
  642   expand_node(Statics,Statics1,Pre,Post,Dec,[],Dec1),
  643   assert_tnode(TP,Pre,Post,Statics1,Score,Dec1),
  644   solved_node(_,_),%expand every possible way until find solution
  645   fwsearch(TN,ST,State).
  646fwsearch(TN,ST,State):-
  647   tp_node(TP,Pre,Post,Statics,Score,Dec),
  648   fwsearch(TN,ST,State).
  649
  650clean_temp_nodes:-
  651   retractall(current_num(tp,_)),
  652   retractall(tp_node(_,_,_,_,_,_)),
  653   retractall(closed_node(_,_,_,_,_,_)),!.
  654
  655% expand all way possible to achieve the Post
  656expand_node(Statics,Statics,Pre,Post,[],List,List):-
  657   state_achieved(Post,Pre,_),!.
  658expand_node(Statics,Statics1,Pre,Post,[step(HP,Name,_,Post0,unexp)|Rest],List,Dec1):-
  659   state_achieved(Post0,Pre,_),
  660   expand_node(Statics,Statics1,Pre,Post,Rest,List,Dec1),!.
  661expand_node(Statics,Statics1,Pre,Post,[step(HP,Name,_,Post0,unexp)|Rest],List,Dec1):-
  662   direct_expand_hp(HP,TN,Name,Pre,Post0,State,Statics,Statics2),
  663   append(List,[step(HP,Name,Pre,State,exp(TN))],List2),
  664   remove_achieved_rest(State,Statics2,Rest,Rest1),
  665   make_to_steps(State,Post,Steps,Rest1),
  666   append(Rest1,Steps,Rest2),
  667   expand_node(Statics2,Statics1,State,Post,Rest2,List2,Dec1).
  668expand_node(Statics,Statics1,Pre,Post,[step(HP,Name,_,Post0,unexp)|Rest],List,Dec1):-
  669   apply_op(Statics,Statics1,[step(HP,Name,Pre,Post0,unexp)|Rest],List,Dec1).
  670expand_node(Statics,Statics1,Pre,Post,[step(HP,Name,Pre0,Post0,exp(TN))|TDec],List,Dec1):-
  671   append(List,[step(HP,Name,Pre0,Post0,exp(TN))],List2),
  672   expand_node(Statics,Statics1,Post0,Post,TDec,List2,Dec1),!.
  673
  674remove_achieved_rest(State,Statics,[],[]):-!.
  675remove_achieved_rest(State,Statics,[step(HP,Name,_,Post0,unexp)|Rest],Rest1):-
  676   state_achieved(Post0,State,_),
  677   remove_achieved_rest(State,Statics,Rest,Rest1),!.
  678remove_achieved_rest(State,Statics,[HP|Rest],[HP|Rest1]):-
  679   remove_achieved_rest(State,Statics,Rest,Rest1),!.
  680
  681/* add an operator that can achieve the goal related states*/

  682% This is only applied when it can't be directly achieved
  683apply_op(Statics,Statics1,[step(HP,Name,Pre,[se(Sort,Obj,SE)],unexp)|Rest],List,Dec):-
  684    produce(se(Sort,Obj,SE1),OP,OPre,ST),
  685    append_cut(Statics,ST,Statics2),
  686    statics_consist(Statics2),
  687    remove_unneed(Statics2,[],Statics1),
  688    state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
  689    instantiate(OPre,Statics1),
  690    not(member(step(_,OP,_,_,_),Rest)),
  691    make_to_steps(Pre,OPre,Steps,Rest),
  692    change_head_state(Pre,Steps,Steps1),
  693    append(List,Steps1,List2),
  694    append(List2,[step(HP,OP,undefd,[se(Sort,Obj,SE)],unexp)|Rest],Dec).
  695%    tell(user),write('+'),told.
  696apply_op(Statics,Statics1,[step(HP,Name,Pre,[se(SortN,Obj,SE)],unexp)|Rest],List,Dec):-
  697    find_prim_sort(SortN,PSortls),
  698    member(Sort,PSortls),
  699    produce(se(Sort,Obj,SE1),OP,OPre,ST),
  700    append_cut(Statics,ST,Statics2),
  701    statics_consist(Statics2),
  702    remove_unneed(Statics2,[],Statics1),
  703    state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
  704    instantiate(OPre,Statics1),
  705    not(member(step(_,OP,_,_,_),Rest)),
  706    make_to_steps(Pre,OPre,Steps,Rest),
  707    change_head_state(Pre,Steps,Steps1),
  708    append(List,Steps1,List2),
  709    append(List2,[step(HP,OP,undefd,[se(Sort,Obj,SE)],unexp)|Rest],Dec).
  710%    tell(user),write('+'),told.
  711
  712% make the achieve goal states [se(..),se(..),..] to separate steps
  713% remove the states that have been achieved by precondition
  714make_to_steps(Pre,[],[],Rest):-!.
  715make_to_steps(Pre,[se(Sort,Obj,SE)|Post],Steps,Rest):-
  716   state_achieved([se(Sort,Obj,SE)],Pre,_),
  717   make_to_steps(Pre,Post,Steps,Rest),!.
  718make_to_steps(Pre,[se(Sort,Obj,SE)|Post],Steps,Rest):-
  719   member(step(_,achieve(se(Sort,Obj,SE)),_,_),Rest),
  720   make_to_steps(Pre,Post,Steps,Rest),!.
  721make_to_steps(Pre,[se(Sort,Obj,SE)|Post],[step(HP,achieve(se(Sort,Obj,SE)),undefd,[se(Sort,Obj,SE)],unexp)|Steps],Rest):-
  722   gensym_special(hp,HP),
  723   make_to_steps(Pre,Post,Steps,Rest),!.
  724
  725change_head_state(Pre,[step(HP,A,undefd,B,unexp)|Steps],[step(HP,A,Pre,B,unexp)|Steps]):-!.
  726
  727% instantiate the object in lhs states
  728instantiate([],Statics).
  729instantiate([se(Sort,Obj,SE)|States],Statics):-
  730   nonvar(Obj),
  731   instantiate(States,Statics),!.
  732instantiate([se(Sort,Obj,SE)|States],Statics):-
  733   ground(SE),
  734   instantiate(States,Statics),!.
  735instantiate([se(Sort,Obj,SE)|States],Statics):-
  736   var(Obj),
  737   get_sort_objects(Sort,Objls),
  738   member(Obj,Objls),
  739   statics_consist_instance(Statics),
  740   instantiate(States,Statics).
  741
  742
  743% select a tp_node with lowest score
  744select_tnode(tp_node(HPid,Pre,Post,Statics,Score,Dec)) :-
  745   retractall(score_list(LS)),
  746   assert(score_list([])),
  747   lowest_score(Score),
  748   retract(tp_node(HPid,Pre,Post,Statics,Score,Dec)),!.
  749%   tell(user),nl,write('new level'),nl,told,!.
  750
  751% find the lowest_score of tp_node
  752lowest_score(LScore):-
  753     tp_node(HPid,Pre,Post,Statics,Score,Dec),
  754     retract(score_list(LS)),
  755     assert(score_list([Score|LS])),
  756     fail.
  757lowest_score(LScore):-
  758     retract(score_list(D)),
  759     sort(D,[LScore|SD]).
  760
  761:- style_check(-discontiguous).
  762% assert tp_node if all HP expand, assert solved_node
  763assert_tnode(TP,Pre,Post,Statics,Score1,Dec):-
  764   all_HP_expanded(Dec),
  765   combine_exp_steps2(Dec,OneDec),
  766   assert(solved_node(Statics,OneDec)),!.
  767
  768combine_exp_steps2([OneDec],OneDec):-!.
  769combine_exp_steps2([Dec1,Dec2|Rest],OneDec):-
  770   combine_steps1(Dec1,Dec2,OneDec1),
  771   combine_exp_steps2([OneDec1|Rest],OneDec),!.
  772% else combine the steps of expanded steps
  773% make it to [step(_,...,exp()),step(_,...,unexp)]
  774% that makes possible to find the repeated nodes
  775% also makes possible to make operator chains for later use
  776assert_tnode(TP,Pre,Post,Statics,Score,Dec):-
  777   combine_exp_steps(Dec,ExpDec,UnexpDec),
  778   assert_tnode1(TP,Pre,Post,Statics,Score,ExpDec,UnexpDec),!.
  779
  780:- style_check(+discontiguous).
  781
  782assert_tnode1(TP,Pre,Post,Statics,Score,ExpDec,UnexpDec):-
  783   existing_node(ExpDec,UnexpDec,Statics).
  784assert_tnode1(TP,Pre,Post,Statics,Score,ExpDec,UnexpDec):-
  785   get_score(Score,ExpDec,UnexpDec,Score1),
  786   gensym_special(tp,TP1),
  787   append(ExpDec,UnexpDec,Dec),
  788   assert(tp_node(TP1,Pre,Post,Statics,Score1,Dec)),!.
  789
  790% combine the expanded steps to one
  791combine_exp_steps([step(A,B,C,D,unexp)|Rest],[],[step(A,B,C,D,unexp)|Rest]):-!.
  792combine_exp_steps([step(A,B,C,D,exp(TN)),step(A1,B1,C1,D1,unexp)|Rest],[step(A,B,C,D,exp(TN))],[step(A1,B1,C1,D1,unexp)|Rest]):-!.
  793combine_exp_steps([step(A1,B1,C1,D1,exp(TN1)),step(A2,B2,C2,D2,exp(TN2))|Rest],ExpDec,UnexpDec):-
  794    combine_steps1(step(A1,B1,C1,D1,exp(TN1)),step(A2,B2,C2,D2,exp(TN2)),ExpDec1),
  795    combine_exp_steps([ExpDec1|Rest],ExpDec,UnexpDec),!.
  796
  797combine_steps1(step(HP1,Name1,Pre1,Post1,exp(TN1)),step(HP2,Name2,Pre2,Post2,exp(TN2)),step(HP,achieve(Post2),Pre1,Post2,exp(TN))):-
  798    find_only_changed(Pre1,Post2,[],Pre0,[],Post0),
  799    not(isemptylist(Post0)),
  800    gensym_special(hp,HP),
  801    gensym_special(tn,TN),
  802    assert(tn(TN,achieve(Post0),Pre0,Post0,[before(TN1,TN2)],[step(HP1,Name1,Pre1,Post1,exp(TN1)),step(HP2,Name2,Pre2,Post2,exp(TN2))])),!.
  803
  804% already been.
  805existing_node([step(_,_,Pre,Post,exp(TN))],UnexpDec,Statics):-
  806    tp_node(_,_,_,Statics0,_,[step(_,_,Pre0,Post0,exp(TN0))|Rest]),
  807    append_st(Statics,Statics0,Statics1),
  808    all_equal(Pre,Pre0,Statics1),
  809    all_equal(Post,Post0,Statics1),
  810    existing_node([],UnexpDec,Statics1).
  811existing_node([step(_,_,Pre,Post,exp(TN))],UnexpDec,Statics):-
  812    closed_node(_,_,_,Statics0,_,[step(_,_,Pre0,Post0,exp(TN0))|Rest]),
  813    append_st(Statics,Statics0,Statics1),
  814    all_equal(Pre,Pre0,Statics1),
  815    all_equal(Post,Post0,Statics1),
  816    existing_node([],UnexpDec,Statics1).
  817existing_node([],[step(_,Name,Pre,_,unexp)|Rest],Statics):-
  818    tp_node(_,_,_,Statics0,_,[step(_,Name,Pre0,_,_,unexp)|Rest]),
  819    append_st(Statics,Statics0,Statics1),
  820    all_equal(Pre,Pre0,Statics1).
  821existing_node([],[step(_,Name,Pre,_,unexp)|Rest],Statics):-
  822    closed_node(_,_,_,Statics0,_,[step(_,Name,Pre0,_,_,unexp)|Rest]),
  823    append_st(Statics,Statics0,Statics1),
  824    all_equal(Pre,Pre0,Statics1).
  825
  826
  827
  828
  829
  830
  831
  832all_equal([],[],Statics):-
  833    statics_consist(Statics),!.
  834all_equal([se(Sort,Obj,SE)|Pre],[se(Sort,Obj,SE1)|Pre1],Statics):-
  835    is_achieved(SE,SE1),
  836    all_equal(Pre,Pre1,Statics),!.
  837all_equal([se(Sort,Obj,SE)|Pre],Pre1,Statics):-
  838    member(se(Sort,Obj,SE1),Pre1),
  839    list_take(Pre1,[se(Sort1,Obj1,SE1)],Pre2),
  840    all_equal(Pre,Pre1,Statics),!.
  841
  842get_score(Score,[],UnExpDec,Score1):-
  843    length(UnExpDec,Len),
  844    Score1 is Score+Len,!.
  845get_score(Score,[step(HP,Name,Pre0,Post0,exp(TN))],UnExpDec,Score1):-
  846    goal_related(States),
  847    state_achieved_Num(Post0,States,0,Num),
  848    length(UnExpDec,Len),
  849    Score1 is Score-Num,!.
  850
  851%-------------------------------------------
  852% the number of postcondition achieved 
  853% the more the better
  854state_achieved_Num([],Pre0,Num,Num):-!.
  855state_achieved_Num([se(Sort,Obj,SE)|Post],Pre0,N,Num):-
  856    member(se(Sort,Obj,SE0),Pre0),
  857    state_achieved([se(Sort,Obj,SE0)],[se(Sort,Obj,SE)],_),
  858    N1 is N+1,
  859    state_achieved_Num(Post,Pre0,N1,Num),!.
  860state_achieved_Num([se(Sort,Obj,SE)|Post],Pre0,N,Num):-
  861    state_achieved_Num(Post,Pre0,N,Num),!.
  862
  863% the states that can achieve a state
  864% that is:
  865% for a state in the rhs of operator
  866% all the states in the lhs
  867find_relate_state:-
  868   operatorC(A,Pre,Post,Cond,ST),
  869   assert_related_states(A,Pre,Post,Cond,ST),
  870   fail.
  871find_relate_state.
  872
  873assert_related_states(A,Pre,Post,Cond,ST):-
  874   assert_related_states1(A,Pre,Post,ST),
  875   assert_related_states2(A,Pre,Cond,ST).
  876
  877% find relate in nec
  878% the sorts here are primitive
  879assert_related_states1(A,Pre,[],ST):-!.
  880%when prev
  881assert_related_states1(A,Pre,[se(Sort,Obj,SE)|Post],ST):-
  882   member_e(se(Sort,Obj,SE),Pre),
  883   assert_related_states1(A,Pre,Post,ST),!.
  884%when nec
  885assert_related_states1(A,Pre,[se(Sort,Obj,SE)|Post],ST):-
  886   assert(produce(se(Sort,Obj,SE),A,Pre,ST)),
  887   assert_related_states1(A,Pre,Post,ST),!.
  888
  889% find relate in conditional
  890% the sorts here are not primitive
  891assert_related_states2(A,Pre,SC,ST):-
  892   make_sc_primitive(SC,PSC),
  893   assert_related_states21(A,Pre,PSC,ST).
  894
  895assert_related_states21(A,Pre,[],ST):-!.
  896assert_related_states21(A,Pre,[sc(Sort,Obj,SE=>SS)|Trans],ST):-
  897   rem_statics([se(Sort,Obj,SE)],[se(Sort,Obj,SER)],St1),
  898   rem_statics([se(Sort,Obj,SS)],[se(Sort,Obj,SSR)],St2),
  899   append_cut(ST,St1,ST1),
  900   append_cut(ST1,St2,ST21),
  901   remove_unneed(ST21,[],ST2),
  902   append_cut(Pre,[se(Sort,Obj,SER)],Pre1),
  903   assert(produce(se(Sort,Obj,SSR),A,Pre1,ST2)),
  904   assert_related_states21(A,Pre,Trans,ST),!.
  905
  906%-------------------------------------------
  907% the number of state change compare with precondition
  908% the less the better
  909pre_changed_Num([],Pre0,Num,Num):-!.
  910pre_changed_Num([se(Sort,Obj,SE)|Pre],Pre0,N,Num):-
  911    state_achieved([se(Sort,Obj,SE)],Pre0,_),
  912    N1 is N+1,
  913    pre_changed_Num(Pre,Pre0,N1,Num),!.
  914pre_changed_Num([se(Sort,Obj,SE)|Pre],Pre0,N,Num):-
  915    pre_changed_Num(Pre,Pre0,N,Num),!.
  916
  917
  918
  919%-------------------------------------------
  920% remove HP1 from the temp list
  921% if  HP1<HP2, then all HP3<HP1 => HP3<HP2
  922% if  HP2<HP1, then all HP1<HP3 => HP2<HP3
  923remove_temp([],HP1,List,List):-!.
  924remove_temp([before(HP1,HP2)|Temp],HP1,List,Temp1):-
  925    remove_temp_before(List,before(HP1,HP2),List2),
  926    remove_temp(Temp,HP1,List2,Temp1),!.
  927remove_temp([before(HP2,HP1)|Temp],HP1,List,Temp1):-
  928    remove_temp_after(List,before(HP2,HP1),List2),
  929    remove_temp(Temp,HP1,List2,Temp1),!.
  930remove_temp([before(HPX,HPY)|Temp],HP1,List,Temp1):-
  931    remove_temp(Temp,HP1,List,Temp1),!.
  932
  933% if  HP1<HP2, remove HP1<HP2, and change all HP3<HP1 => HP3<HP2
  934remove_temp_before([],before(HP1,HP2),[]):-!.
  935remove_temp_before([before(HP1,HP2)|T],before(HP1,HP2),T1):-
  936   remove_temp_before(T,before(HP1,HP2),T1),!.
  937remove_temp_before([before(HP3,HP1)|T],before(HP1,HP2),[before(HP3,HP2)|T1]):-
  938   remove_temp_before(T,before(HP1,HP2),T1),!.
  939remove_temp_before([before(HPX,HPY)|T],before(HP1,HP2),[before(HPX,HPY)|T1]):-
  940   remove_temp_before(T,before(HP1,HP2),T1),!.
  941% if  HP2<HP1, remove HP2<HP1, and change all HP1<HP3 => HP2<HP3
  942remove_temp_after([],before(HP1,HP2),[]):-!.
  943remove_temp_after([before(HP2,HP1)|T],before(HP2,HP1),T1):-
  944   remove_temp_after(T,before(HP2,HP1),T1),!.
  945remove_temp_after([before(HP1,HP3)|T],before(HP2,HP1),[before(HP2,HP3)|T1]):-
  946   remove_temp_after(T,before(HP2,HP1),T1),!.
  947remove_temp_after([before(HPX,HPY)|T],before(HP2,HP1),[before(HPX,HPY)|T1]):-
  948   remove_temp_after(T,before(HP2,HP1),T1),!.
  949
  950remove_dec(HPid,[],[]):-!.
  951remove_dec(HPid,[step(HPid,_,_,_,_)|Dec],Dec1):-
  952   remove_dec(HPid,Dec,Dec1),!.
  953remove_dec(HPid,[step(A,B,C,D,F)|Dec],[step(A,B,C,D,F)|Dec1]):-
  954   remove_dec(HPid,Dec,Dec1),!.
  955   
  956/******************************************************/

  957% state2 is achieved by state1(or they are not conflict)
  958% the output state is the combination of the two states
  959state_achieved(undefd,State,State):-!.
  960state_achieved(State1,State2,State3):-
  961    state_achieved1(State1,State2,State3).
  962
  963state_achieved1([],State2,State2).
  964state_achieved1([se(Sort,Obj,ST1)|State1],State2,[se(Sort,Obj,STN)|State]):-
  965    get_sort_objects(Sort,Objls),
  966    member(Obj,Objls),
  967    member(se(Sort,Obj,ST2),State2),
  968    not_conflict(Sort,Obj,ST1,ST2,STN),
  969    list_take(State2,[se(Sort,Obj,ST2)],State21),
  970    state_achieved1(State1,State21,State).
  971state_achieved1([se(Sort1,Obj,ST1)|State1],State2,[se(Sort,Obj,STN)|State]):-
  972    not(objects(Sort1,Objls)),
  973    find_prim_sort(Sort1,Sortls),
  974    member(se(Sort,Obj,ST2),State2),
  975    member(Sort,Sortls),
  976    not_conflict(Sort,Obj,ST1,ST2,STN),
  977    list_take(State2,[se(Sort,Obj,ST2)],State21),
  978    state_achieved1(State1,State21,State).
  979
  980% two states not conflict
  981not_conflict(Sort,Obj,ST,ST1,NewST):-
  982    gsubstate_classes(Sort,Obj,Substateclasses),
  983    member(State0,Substateclasses),
  984    is_achieved(ST,State0),
  985    is_achieved(ST1,State0),
  986    set_append_e(ST,ST1,NewST),!.
  987
  988% all the element in list1 are static or in list2
  989is_achieved([],_):-!.
  990is_achieved([H|T], State) :-
  991    is_statics(H),
  992    is_achieved(T,State),!.
  993is_achieved([H|T], State) :-
  994    member(H,State),
  995    is_achieved(T,State),!.
  996
  997% check if a predicate is statics or not
  998is_statics(ne(A,B)):-!.
  999is_statics(is_of_sort(A,B)):-!.
 1000is_statics(is_of_primitive_sort(A,B)):-!.
 1001is_statics(Pred):-
 1002    functor(Pred,FF,NN),
 1003    functor(Pred1,FF,NN),
 1004    atomic_invariants(Atom),
 1005    member(Pred1,Atom),!.
 1006    
 1007    
 1008%-------------------------------------------
 1009
 1010/************ state changes by actions ********/

 1011% if an object's state meet the precondition
 1012% it change to the postcondition
 1013state_change([],Pre0,Post0,[]):-!.
 1014state_change(Pre,[],[],Pre):-!.
 1015% if a obj pre achieves action's pre
 1016% change the obj's post state with action's post
 1017state_change([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,NewSS)|State]):-
 1018    member(se(Sort,Obj,SE0),Pre0),
 1019    member(se(Sort,Obj,SS0),Post0),
 1020    not_conflict(Sort,Obj,SE,SE0,_),
 1021    state_change1(SE,SE0,SS0,NewSS),
 1022    list_take(Pre0,[se(Sort,Obj,SE0)],Pre1),
 1023    list_take(Post0,[se(Sort,Obj,SS0)],Post1),
 1024    state_change(Pre,Pre1,Post1,State),!.
 1025% if a obj state not defined in  action's pre
 1026% move it to its post
 1027state_change([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,SE)|State]):-
 1028    state_change(Pre,Pre0,Post0,State),!.
 1029
 1030state_change1([],SE0,SS0,SS0):-!.
 1031state_change1([HSE|TSE],SE0,SS0,SS1):-
 1032    member(HSE,SE0),
 1033    state_change1(TSE,SE0,SS0,SS1),!.
 1034state_change1([HSE|TSE],SE0,SS0,[HSE|SS1]):-
 1035    state_change1(TSE,SE0,SS0,SS1),!.
 1036%-------------------------------------------
 1037%-------------------------------------------
 1038/************ state changes by conditions ********/

 1039% for all the object's state meet the precondition
 1040% it change to the postcondition
 1041cond_state_change([],Cond,[]):-!.
 1042cond_state_change(State,[],State):-!.
 1043cond_state_change([se(Sort,Obj,SE)|Pre],Cond,[se(Sort,Obj,NewSS)|State]):-
 1044    member(sc(Sort1,Obj,SE0=>SS0),Cond),
 1045    statics_consist([is_of_sort(Obj,Sort1),is_of_sort(Obj,Sort)]),
 1046    not_conflict(Sort,Obj,SE,SE0,_),
 1047    state_change1(SE,SE0,SS0,NewSS),
 1048    cond_state_change(Pre,Cond,State),!.
 1049cond_state_change([se(Sort,Obj,SE)|Pre],Cond,[se(Sort,Obj,SE)|State]):-
 1050    cond_state_change(Pre,Cond,State),!.
 1051
 1052%-------------------------------------------
 1053% check that an action's state change
 1054% included changes of the goal states need to be achieved
 1055post_changed(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
 1056    member(se(Sort,Obj,SE0),Post0),
 1057    state_achieved([se(Sort,Obj,SE0)],[se(Sort,Obj,SE)],_),
 1058    statics_consist(Statics).
 1059post_changed(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
 1060    member(sc(Sort,Obj,SE1=>SS),Cond),
 1061    state_achieved([se(Sort,Obj,SS)],[se(Sort,Obj,SE)],_),
 1062    statics_consist(Statics).
 1063post_changed(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
 1064    post_changed(Post0,Cond,Statics,Post).
 1065
 1066all_achieved([],Statics,Post).
 1067all_achieved([se(Sort,Obj,SL)|Pre],Statics,Post):-
 1068    not(member(se(Sort,Obj,SR),Post)),
 1069    all_achieved(Pre,Statics,Post).
 1070all_achieved([se(Sort,Obj,SL)|Pre],Statics,Post):-
 1071    member(se(Sort,Obj,SR),Post),
 1072    is_achieved(SL,SR),
 1073    statics_consist_instance(Statics),
 1074    all_achieved(Pre,Statics,Post).
 1075
 1076
 1077%-------------------------------------------
 1078% instantiate a bit
 1079% use action's state change include the postcondition
 1080post_instant(Post0,Cond,Statics,[]):-!.
 1081post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
 1082    member(se(Sort,Obj,SE0),Post0),
 1083    statics_consist(Statics).
 1084post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
 1085    member(sc(Sort,Obj,SE1=>SS),Cond),
 1086    statics_consist(Statics).
 1087post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
 1088    member(sc(Sort0,Obj,SE1=>SS),Cond),
 1089    not(objects(Sort0,_)),
 1090    subsorts(Sort0,Sortls),
 1091    not(not(member(Sort,Sortls))),
 1092    statics_consist(Statics).
 1093post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
 1094    post_instant(Post0,Cond,Statics,Post),!.
 1095/******************************* NODE ADT *********************/

 1096
 1097%node(Name, Precond, Decomps, Temp, Statics)
 1098% When inputting new methods etc filter all statics into
 1099% static slot
 1100
 1101make_problem_into_node(I,goal(L,TM,STATS),  NN) :-
 1102     make_problem_up(L, STEPS),
 1103     make_num_hp(TM,Temp),
 1104     sort_steps(STEPS,Temp,STEPS1),
 1105     make_ss_to_se(I,I_Pre),
 1106     NN = node(root,I_Pre,STEPS1 ,Temp, STATS),!.
 1107
 1108% make problem to steps
 1109make_problem_up([],[]):-!.
 1110make_problem_up([achieve(L)|R],[step(HP,achieve(L1),undefd,[L1],unexp)|RS]):- 
 1111                             %preconditon here is undefd
 1112    make_ss_to_se([L],[L1]),
 1113    gensym_special(hp,HP),
 1114    make_problem_up(R, RS),!.
 1115make_problem_up([achieve(L)|R],[step(HP,achieve(L1),undefd,L1,unexp)|RS]):- 
 1116                             %preconditon here is undefd
 1117    make_ss_to_se(L,L1),
 1118    gensym_special(hp,HP),
 1119    make_problem_up(R, RS),!.
 1120make_problem_up([O|R],[step(HP,O,undefd,undefd,unexp)|RS]):-
 1121    methodC(O,Pre,Post,Statics1,Temp,ACH,Dec),
 1122    gensym_special(hp,HP),
 1123    make_problem_up(R, RS),!.
 1124make_problem_up([O|R],     
 1125           [step(HP,O,undefd,undefd,unexp)|RS]):-
 1126    operatorC(O,Pre,Post,Cond,Statics1),
 1127    gensym_special(hp,HP),
 1128    make_problem_up(R, RS),!.
 1129
 1130make_num_hp([],[]):-!.
 1131make_num_hp([before(N1,N2)|TM],[before(H1,H2)|Temp]):-
 1132    gensym_num(hp,N1,H1),
 1133    gensym_num(hp,N2,H2),
 1134    make_num_hp(TM,Temp),!.
 1135
 1136%**************sort steps*********************************
 1137% sort steps by temporal constraints.
 1138sort_steps(Steps,[],Steps):-!.
 1139sort_steps([Steps|[]],[],[Steps]):-!.
 1140sort_steps(Steps,Temp,OrderedST):-
 1141   steps_in_temp(Temp,[],ST),
 1142   sort_steps1(Temp,ST,OrderedSTID),
 1143   sort_steps2(Steps,OrderedSTID,[],OrderedST),!.
 1144
 1145% find out the steps in temporal constraints.
 1146steps_in_temp([],ST,ST):-!.
 1147steps_in_temp([before(H1,H2)|TT],List,ST):-
 1148   set_append_e(List,[H1,H2],List1),
 1149   steps_in_temp(TT,List1,ST),!.
 1150
 1151% sort the steps_id(hps) by temporal constraints.
 1152sort_steps1(Temp,[],[]):-!.
 1153sort_steps1(Temp,[HP1|TST],[HPF|OST]):-
 1154   earliest_step(HP1,HPF,Temp,TST,TST1),
 1155   sort_steps1(Temp,TST1,OST),!.
 1156   
 1157earliest_step(HPF,HPF,Temp,[],[]):-!.
 1158earliest_step(HP1,HPF,Temp,[HP2|TST],[HP1|TST1]):-
 1159   member(before(HP2,HP1),Temp),
 1160   earliest_step(HP2,HPF,Temp,TST,TST1),!.
 1161earliest_step(HP1,HPF,Temp,[HP2|TST],[HP2|TST1]):-
 1162   earliest_step(HP1,HPF,Temp,TST,TST1),!.
 1163
 1164% sort the steps, put the unordered steps in the front
 1165sort_steps2(OtherST,[],OrderedST1,OrderedST):-
 1166   append(OrderedST1,OtherST,OrderedST),!.
 1167sort_steps2(Steps,[HP|THPS],List,OrderedST):-
 1168   member(step(HP,N,Pre,Post,F),Steps),
 1169   append(List,[step(HP,N,Pre,Post,F)],List1),
 1170   list_take(Steps,[step(HP,N,Pre,Post,F)],Steps1),
 1171   sort_steps2(Steps1,THPS,List1,OrderedST),!.
 1172sort_steps2(Steps,[HP|THPS],List,OrderedST):-
 1173   sort_steps2(Steps,THPS,List,OrderedST),!.
 1174%*******************************************************
 1175
 1176% replace ss to se
 1177make_ss_to_se([],[]):-!.
 1178make_ss_to_se([ss(Sort,Obj,Post)|TPost],[se(Sort,Obj,Post)|TPre]):-
 1179     make_ss_to_se(TPost,TPre),!.
 1180make_ss_to_se([se(Sort,Obj,Post)|TPost],[se(Sort,Obj,Post)|TPre]):-
 1181     make_ss_to_se(TPost,TPre),!.
 1182
 1183%*******************************************************
 1184% extract_solution(Node,..
 1185% recurvise routine to work down tree and
 1186% print out a linearisation of it
 1187extract_solution(Node,PHPs,SIZE1) :-
 1188       % its the name of a hierarchical op......
 1189   getN_decomp(Node, HPs),
 1190   push_to_primitive(HPs,[],PHPs),
 1191   pprint(PHPs,1,SIZE),
 1192   SIZE1 is SIZE -1,!.
 1193
 1194
 1195
 1196
 1197
 1198
 1199
 1200
 1201
 1202
 1203
 1204
 1205
 1206
 1207
 1208
 1209
 1210
 1211
 1212
 1213/************ change_op_representation ***********/

 1214% make pre and post explicit
 1215% filter out statics and put in a new slot
 1216change_op_representation :-    
 1217    method(A,B,C,Stat,T,Dec),
 1218    make_ss_to_se(B,B0),
 1219    make_se_primitive(B0,B1),
 1220    make_sc_primitive(C,C1),
 1221    get_preconditions(C1,B1,Pre,Post),
 1222    rem_statics(Post, PostR,St1),
 1223    rem_statics(Pre, PreR,St2),
 1224    append_cut(St1,St2,Statics),
 1225    append_cut(Stat,Statics,Statics1),
 1226    remove_unneed(Statics1,[],Statics2),
 1227    get_achieval(A,Dec,T,Dec1,T1,ACH),
 1228    assert(methodC(A,PreR,PostR,Statics2,T1,achieve(ACH),Dec1)),
 1229    fail.
 1230change_op_representation :-
 1231    operator(A,B,C,D),
 1232    make_ss_to_se(B,B0),
 1233    make_se_primitive(B0,B1),
 1234    make_sc_primitive(C,C1),
 1235%    make_sc_primitive(D,D1),
 1236	%can't do that because it narrow the conditional change 
 1237    get_preconditions(C1,B1,Pre,Post),
 1238    rem_statics(Post, PostR,St1),
 1239    rem_statics(Pre, PreR,St2),
 1240    append_cut(St1,St2,Statics1),
 1241    remove_unneed(Statics1,[],Statics),
 1242    statics_consist(Statics),
 1243    assert(operatorC(A,PreR,PostR,D,Statics)),
 1244    fail.
 1245change_op_representation:-
 1246    retractall(current_num(sm,_)),!.
 1247
 1248get_preconditions([],Prev,Prev,Prev) :-!.
 1249get_preconditions([sc(S,X,From =>To)|Rest],Prev,[se(S,X,From1)|Pre],[se(S,X,To1)|Post]):-
 1250     member_e(se(S,X,PSE),Prev),
 1251     append(PSE,From,From1),
 1252     append(PSE,To,To1),
 1253     list_take(Prev,[se(S,X,PSE)],Prev1),
 1254     get_preconditions(Rest,Prev1, Pre,Post),!.
 1255get_preconditions([sc(S,X,From =>To)|Rest],Prev,[se(S,X,From)|Pre],[se(S,X,To)|Post]):-
 1256     get_preconditions(Rest,Prev, Pre,Post),!.
 1257get_preconditions([],Prev,Prev,Prev) :-!.
 1258
 1259% get all achieve goals out
 1260get_achieval(A,Dec,T,Dec1,T1,Achieval):-
 1261     retractall(current_num(sm,_)),
 1262     make_dec(A,Dec,Dec1,T,T1,[],Achieval),!.
 1263make_dec(A,[],[],Temp,Temp,Achieval,Achieval):-!.
 1264make_dec(A,[HD|TD],TD1,Temp,Temp1,Achieval,Achieval1):-
 1265     HD=..[achieve|Goal],
 1266     current_num(sm,Num),
 1267     replace_achieval_temp(Temp,Temp0,Num),
 1268     make_ss_to_se(Goal,Goal0),
 1269     append(Achieval,Goal0,Achieval0),
 1270     make_dec(A,TD,TD1,Temp0,Temp1,Achieval0,Achieval1),!.
 1271make_dec(A,[HD|TD],TD1,Temp,Temp1,Achieval,Achieval1):-
 1272     HD=..[achieve|Goal],
 1273     not(current_num(sm,Num)),
 1274     replace_achieval_temp(Temp,Temp0,1),
 1275     make_ss_to_se(Goal,Goal0),
 1276     append(Achieval,Goal0,Achieval0),
 1277     make_dec(A,TD,TD1,Temp0,Temp1,Achieval0,Achieval1).
 1278make_dec(A,[HD|TD],[HD|TD1],Temp,Temp1,Achieval,Achieval1):-
 1279     HD=..[DecName|Goal],
 1280     DecName\==achieve,
 1281     gensym_special(sm,SM),
 1282     current_num(sm,Num),
 1283     make_dec(A,TD,TD1,Temp,Temp1,Achieval,Achieval1),!.
 1284
 1285% get rid of the achievals in temp orders
 1286replace_achieval_temp(Temp,Temp1,Num):-
 1287     change_all_numbers(Temp,Num,Temp00),
 1288     tidy_temp(Temp00,Temp1).
 1289
 1290change_all_numbers([],Num,[]):-!.
 1291change_all_numbers([HTemp|TTemp],Num,[HTemp00|TTemp00]):-
 1292     HTemp=..[before|Nums],
 1293     change_nums(Nums,Num,Nums1),
 1294     HTemp00=..[before|Nums1],
 1295     change_all_numbers(TTemp,Num,TTemp00).
 1296
 1297change_nums([],Num,[]):-!.
 1298change_nums([Num1|TN],Num,[Num1|TN1]):-
 1299    Num1<Num,
 1300    change_nums(TN,Num,TN1),!.
 1301change_nums([Num1|TN],Num,[Num2|TN1]):-
 1302    Num1>Num,
 1303    Num2 is Num1-1,
 1304    change_nums(TN,Num,TN1),!.
 1305change_nums([Num|TN],Num,[0|TN1]):-
 1306    change_nums(TN,Num,TN1),!.
 1307
 1308% since assumed achieval only happen at first, so only change the after ones
 1309tidy_temp(Temp,Temp1):-
 1310     member(before(Num,0),Temp),
 1311     list_take(Temp,[before(Num,0)],Temp0),
 1312     change_laters(Temp0,Num,Temp01),
 1313     tidy_temp(Temp01,Temp1).
 1314tidy_temp([],[]):-!.
 1315tidy_temp([before(0,Num)|Temp],Temp0):-
 1316     tidy_temp(Temp,Temp0),!.
 1317tidy_temp([before(Num1,Num2)|Temp],[before(Num1,Num2)|Temp0]):-
 1318     tidy_temp(Temp,Temp0),!.
 1319
 1320change_laters([before(0,Num2)|Temp],Num,[before(Num,Num2)|Temp0]):-
 1321     change_laters(Temp,Num,Temp0).
 1322change_laters([before(Num1,0)|Temp],Num,[before(Num1,0)|Temp0]):-
 1323     change_laters(Temp,Num,Temp0).
 1324change_laters([before(Num1,Num2)|Temp],Num,[before(Num1,Num2)|Temp0]):-
 1325     change_laters(Temp,Num,Temp0).
 1326
 1327% change the states to primitive states
 1328make_se_primitive([],[]).
 1329make_se_primitive([se(Sort,Obj,ST)|SE],[se(Sort,Obj,ST)|SE0]):-
 1330    find_prim_sort(Sort,[Sort]),!,
 1331    make_se_primitive(SE,SE0).
 1332make_se_primitive([se(Sort,Obj,ST)|SE],[se(PSort,Obj,ST)|SE0]):-
 1333    find_prim_sort(Sort,PSorts),
 1334    member(PSort,PSorts),
 1335    make_se_primitive(SE,SE0).
 1336
 1337% change the state changes to primitive states
 1338make_sc_primitive([],[]).
 1339make_sc_primitive([sc(Sort,Obj,SE1=>SE2)|ST],[sc(Sort,Obj,SE1=>SE2)|ST0]):-
 1340    find_prim_sort(Sort,[Sort]),!,
 1341    make_sc_primitive(ST,ST0).
 1342make_sc_primitive([sc(Sort,Obj,SE1=>SE2)|ST],[sc(PSort,Obj,SE1=>SE2)|ST0]):-
 1343    find_prim_sort(Sort,PSorts),
 1344    member(PSort,PSorts),
 1345    make_sc_primitive(ST,ST0).
 1346% ------------ end of change operator ----------------------
 1347make_tn(TN,Name,Pre,Post,Temp,Dec):-
 1348    gensym_special(tn,TN),
 1349    find_only_changed(Pre,Post,[],Pre1,[],Post1),
 1350%    tell(user),nl,write(tn(TN,Name,Pre1,Post1,Temp,Dec)),nl,told,
 1351    assert(tn(TN,Name,Pre1,Post1,Temp,Dec)),!.
 1352
 1353find_only_changed([],[],Pre,Pre,Post,Post):-!.
 1354% just a lazy check if they are in exactly same sequence
 1355find_only_changed([se(Sort,Obj,ST)|Pre],[se(Sort,Obj,ST)|Post],Pre0,Pre1,Post0,Post1):-
 1356    find_only_changed(Pre,Post,Pre0,Pre1,Post0,Post1),!.
 1357find_only_changed([se(Sort,Obj,ST)|Pre],Post,Pre0,Pre1,Post0,Post1):-
 1358    member(se(Sort,Obj,ST1),Post),
 1359    list_take(Post,[se(Sort,Obj,ST1)],Post2),
 1360    append_changed(se(Sort,Obj,ST),se(Sort,Obj,ST1),Pre0,Pre3,Post0,Post3),
 1361    find_only_changed(Pre,Post2,Pre3,Pre1,Post3,Post1),!.
 1362find_only_changed([se(Sort,Obj,ST)|Pre],Post,Pre0,Pre1,Post0,Post1):-
 1363    member(se(SortN,Obj,ST1),Post),
 1364    list_take(Post,[se(SortN,Obj,ST1)],Post2),
 1365    append_changed(se(Sort,Obj,ST),se(SortN,Obj,ST1),Pre0,Pre3,Post0,Post3),
 1366    find_only_changed(Pre,Post2,Pre3,Pre1,Post3,Post1),!.
 1367% other fail. 
 1368
 1369% append  only changed states
 1370% not_conflict here means not changed
 1371append_changed(se(Sort,Obj,ST),se(Sort1,Obj,ST1),Pre0,Pre0,Post0,Post0):-
 1372    not_conflict(Sort,Obj,ST,ST1,_),!.
 1373append_changed(se(Sort,Obj,ST),se(Sort1,Obj,ST1),Pre0,Pre3,Post0,Post3):-
 1374    append(Pre0,[se(Sort,Obj,ST)],Pre3),
 1375    append(Post0,[se(Sort,Obj,ST1)],Post3),!.
 1376
 1377
 1378
 1379%***********print out solution**************************   
 1380push_to_primitive([],PHPs,PHPs) :-!.
 1381push_to_primitive([step(HPID,_,_,_,exp(TN))|HPs],List,PHPs) :-
 1382   tn(TN,Name,Pre,Post,Temp,Dec),
 1383   push_to_primitive(Dec,List,Dec1),
 1384   push_to_primitive(HPs,Dec1,PHPs),!.
 1385push_to_primitive([step(HPID,_,_,_,exp(Name))|HPs],List,PHPs):-
 1386   append(List,[Name],List1),
 1387   push_to_primitive(HPs,List1,PHPs),!.
 1388
 1389
 1390/*********** TEMPORAL AND DECLOBBERING ************/

 1391
 1392possibly_before(I,J,Temps) :-
 1393    \+ necessarily_before(J,I,Temps), !.
 1394
 1395necessarily_before(J,I,Temps) :-
 1396    member(before(J,I),Temps),!.
 1397necessarily_before(J,I,Temps) :-
 1398    member(before(J,Z),Temps),
 1399    necessarily_before(Z,I,Temps),!.
 1400
 1401/********** retracting, asserting and reporting nodes *****************/

 1402
 1403/***** SOLUTION ******/

 1404select_node(node(Name,Pre,Temp,Decomp,Statics)) :-
 1405   retract(node(Name,Pre,Temp,Decomp,Statics)),
 1406%   nl,nl,write(Name),write(' RETRACTED'),nl,nl,
 1407%   tell(user),
 1408%   nl,nl,write(Name),write(' RETRACTED'),nl,nl,
 1409%   tell(FF),
 1410    !.
 1411
 1412is_of_primitive_sort(X,Y) :-
 1413    objects(Y,L),member(X,L).
 1414is_of_sort(X,Y) :-
 1415    is_of_primitive_sort(X,Y).
 1416is_of_sort(X,Y) :-
 1417    sorts(Y,SL),member(Z,SL),is_of_sort(X,Z).
 1418
 1419find_all_upper([],[]).
 1420find_all_upper([HVars|TV],[HSorts|TS]):-
 1421     uppersorts(HSorts,Upsorts),
 1422     member(HVars,Upsorts),
 1423     find_all_upper(TV,TS).
 1424     
 1425% find out primitive sorts of a sort.
 1426find_prim_sort(Sort,PS):-
 1427  subsorts(Sort,Subsorts),
 1428  split_prim_noprim(Subsorts,PS,NP),!.
 1429
 1430% find out the objects of a sort
 1431get_sort_objects(Sort,Objs):-
 1432   find_prim_sort(Sort,PSorts),
 1433   get_objects1(PSorts,Objls),
 1434   flatten(Objls,[],Objs),!.
 1435
 1436get_objects1([],[]):-!.
 1437get_objects1([PS1|RS],[Objls1|Objls]):-
 1438   objects(PS1,Objls1),
 1439   get_objects1(RS,Objls),!.
 1440
 1441% find subsorts of a sort(include).
 1442subsorts(Sort,Subsorts):-
 1443  sort_down([Sort],[Sort],Subsorts),!.
 1444
 1445sort_down([],Subsorts,Subsorts):-!.
 1446sort_down([HOpen|TOpen],List,Sortslist):-
 1447  sorts(HOpen,Sorts),
 1448  append(List,Sorts,List1),
 1449  append(TOpen,Sorts,Open1),
 1450  sort_down(Open1,List1,Sortslist),!.
 1451sort_down([HOpen|TOpen],List,Sortslist):-
 1452  sort_down(TOpen,List,Sortslist),!.
 1453  
 1454% find uppersorts of a sort or object(include).
 1455uppersorts(Sort,Uppersorts):-
 1456  objects(Sort,Objls),
 1457  sort_up(Sort,[Sort],Uppersorts),!.
 1458uppersorts(Sort,Uppersorts):-
 1459  sorts(Sort,Sortls),
 1460  sort_up(Sort,[Sort],Uppersorts),!.
 1461uppersorts(Obj,Sortls):-
 1462  objects(Sort,Objls),
 1463  member(Obj, Objls),
 1464  sort_up(Sort,[Sort],Sortls),!.
 1465
 1466sort_up(Sort, List,Sortslist):-
 1467  sorts(non_primitive_sorts,NPSorts),
 1468  sort_up1(Sort,NPSorts,NPSorts,List,Sortslist),!.
 1469
 1470sort_up1(Sort,[],NPSorts,Sortslist,Sortslist):-!.
 1471sort_up1(Sort,[HNPSorts|TNPSorts],NPSorts,List,Sortslist):-
 1472  sorts(HNPSorts,Sorts),
 1473  member(Sort,Sorts),
 1474  append(List, [HNPSorts], List1),
 1475  sort_up(HNPSorts,List1,Sortslist),!. 
 1476sort_up1(Sort,[HNPSorts|TNPSorts],NPSorts,List,Sortslist):-
 1477  sort_up1(Sort,TNPSorts,NPSorts,List,Sortslist),!.
 1478
 1479% find out primitive sorts from a sorts list.
 1480split_prim_noprim([],[],[]):-!.
 1481split_prim_noprim([HS|TS],[HS|TP],NP):-
 1482     objects(HS,Obj),
 1483     split_prim_noprim(TS,TP,NP),!.		
 1484split_prim_noprim([HS|TS],PS,[HS|NP]):-
 1485     split_prim_noprim(TS,PS,NP),!.
 1486
 1487
 1488
 1489
 1490
 1491% check for statics consist and instanciate them
 1492statics_consist_instance([]):-!.
 1493statics_consist_instance(Statics) :-
 1494   get_invariants(Invs),
 1495   statics_consist_instance0(Invs,Statics).
 1496
 1497statics_consist_instance0(Invs,[]):-!.
 1498statics_consist_instance0(Invs,[ne_back(A,B)|TStatics]):-
 1499   not(A==B),
 1500   statics_consist_instance0(Invs,TStatics).
 1501statics_consist_instance0(Invs,[ne(A,B)|TStatics]):-
 1502   append(TStatics,[ne_back(A,B)],TStatics1),
 1503   statics_consist_instance0(Invs,TStatics1),!.
 1504statics_consist_instance0(Invs,[is_of_sort(Obj,Sort)|TStatics]):-
 1505   is_of_sort(Obj,Sort),
 1506   statics_consist_instance0(Invs,TStatics).
 1507statics_consist_instance0(Invs,[is_of_primitive_sort(Obj,Sort)|TStatics]):-
 1508   is_of_primitive_sort(Obj,Sort),
 1509   statics_consist_instance0(Invs,TStatics).
 1510statics_consist_instance0(Invs,[Pred|TStatics]):-
 1511   member(Pred,Invs),
 1512   statics_consist_instance0(Invs,TStatics).
 1513
 1514% check for statics consist without instanciate them
 1515% only instance the variable when there is one choice of from the ground lists
 1516statics_consist([]):-!.
 1517statics_consist(Statics):-
 1518   get_invariants(Invs),
 1519   statics_consist1(Invs,Statics),!.
 1520   
 1521statics_consist1(Invs,[]):-!.
 1522statics_consist1(Invs,[ne_back(A,B)|TStatics]):-
 1523   not(A==B),
 1524   statics_consist1(Invs,TStatics),!.
 1525statics_consist1(Invs,[ne(A,B)|TStatics]):-
 1526   append(TStatics,[ne_back(A,B)],TStatics1),
 1527   statics_consist1(Invs,TStatics1),!.
 1528statics_consist1(Invs,[is_of_sort(Obj,Sort)|TStatics]):-
 1529   get_sort_objects(Sort,Objs),
 1530   obj_member(Obj,Objs),
 1531   statics_consist1(Invs,TStatics),!.
 1532statics_consist1(Invs,[is_of_primitive_sort(Obj,Sort)|TStatics]):-
 1533   objects(Sort,Objs),
 1534   obj_member(Obj,Objs),
 1535   statics_consist1(Invs,TStatics),!.
 1536statics_consist1(Invs,[Pred|TStatics]):-
 1537   pred_member(Pred,Invs),
 1538   statics_consist1(Invs,TStatics),!.
 1539
 1540/***************** local utils *****************/

 1541
 1542/*********** DOMAIN MODEL FUNCTIONS *****************/

 1543get_invariants(Invs) :-
 1544    atomic_invariants(Invs),!.
 1545
 1546rem_statics([ss(S,X,Preds)|Post], [ss(S,X,PredR)|PostR],Rt1) :-
 1547    filter_list(Preds, is_a_dynamic_pred,PredR),
 1548    filter_list(Preds, is_a_static_pred, R),
 1549    rem_statics(Post, PostR,Rt),
 1550    append(Rt,[is_of_sort(X,S)|R],Rt1),!.
 1551rem_statics([se(S,X,Preds)|Post], [se(S,X,PredR)|PostR],Rt1) :-
 1552    filter_list(Preds, is_a_dynamic_pred,PredR),
 1553    filter_list(Preds, is_a_static_pred, R),
 1554    rem_statics(Post, PostR,Rt),
 1555    append(Rt,[is_of_sort(X,S)|R],Rt1),!.
 1556rem_statics([], [],[]) :-!.
 1557
 1558
 1559
 1560/***************** STATICS ************************/

 1561
 1562isemptylist([]):-!.
 1563
 1564
 1565member_cut(X,[X|_]) :- !.
 1566member_cut(X,[_|Y]) :- member_cut(X,Y),!.
 1567
 1568member_e(X,[Y|_]):-
 1569     X==Y,!.
 1570member_e(X,[Y|L]):-
 1571     var(Y),
 1572     member_e(X,L),!.
 1573member_e(ss(Sort,Obj,SE),[ss(Sort,Obj1,SE)|_]):-
 1574     Obj==Obj1,!.
 1575member_e(se(Sort,Obj,SE),[se(Sort,Obj1,SE)|_]):-
 1576     Obj==Obj1,!.
 1577member_e(sc(Sort,Obj,SE1=>SE2),[sc(Sort,Obj1,SE1=>SE2)|_]):-
 1578     Obj==Obj1,!.
 1579member_e(X,[Y|L]):- member_e(X,L),!.
 1580% u_mem in ob_utils is SLOW!?.
 1581% this is a fast impl.
 1582u_mem_cut(_,[]):-!,fail.
 1583u_mem_cut(X,[Y|_]) :- X == Y,!.
 1584u_mem_cut(X,[_|L]) :- u_mem_cut(X,L),!.
 1585
 1586
 1587% check if object X is a member of a objects list
 1588% 1. if it is not a variable, check if it is in the list
 1589% 2. X is a variable, and the list only has one objects, make X as that obj
 1590% 3. X is a variable, but the list has more than one objects, leave X unchange
 1591obj_member(X,[X|[]]):-!. 
 1592obj_member(X,List):-     
 1593    obj_member0(X,List),!.
 1594obj_member0(X,[Y|_]):-
 1595    var(X),!.%if X is var, but Y not, the leave X as that variable
 1596obj_member0(X,[Y|_]):-
 1597    X==Y,!.
 1598obj_member0(X,[_|Y]) :- obj_member0(X,Y),!.
 1599
 1600% check if a predicate is a member of a ground predicate list,
 1601% 1. when only one predicates found
 1602% just used in binding the predicates to a sort without instantiate it
 1603% for efficiency, instantiate the variable if the list only have one atom
 1604pred_member(ne(A,B),List):-
 1605    A\==B,!.
 1606pred_member(is_of_sort(A,B),List):-
 1607    get_sort_objects(B,Objls),
 1608    obj_member(A,Objls),!.
 1609pred_member(is_of_primitive_sort(A,B),List):-
 1610    objects(B,Objls),
 1611    obj_member(A,Objls),!.
 1612pred_member(X,List):-
 1613    setof(X,member(X,List),Refined),
 1614    pred_member0(X,Refined),!.
 1615pred_member0(X,[X|[]]):-!.
 1616pred_member0(X,Y):-
 1617    pred_member1(X,Y),!.
 1618pred_member1(X,[Y|_]):-
 1619    X=..[H|XLs],
 1620    Y=..[H|YLs],
 1621    vequal(XLs,YLs),!.
 1622pred_member1(X,[_|Y]):- pred_member1(X,Y),!.
 1623	
 1624
 1625
 1626append_cut([],L,L) :- !.
 1627append_cut([H|T],L,[H|Z]) :- append_cut(T,L,Z),!.
 1628
 1629% remove the constants that no need
 1630% instanciate the viables that all ready been bind
 1631% ------------------------------------------
 1632append_st(ST1,ST2,ST):-
 1633    append_cut(ST1,ST2,ST0),
 1634    remove_unneed(ST0,[],ST),!.
 1635
 1636% remove the constants that no need
 1637% instanciate the variables that all ready been bind
 1638remove_unneed([],C,C):-!.
 1639remove_unneed([A|B], Z, C):-
 1640    var(A),
 1641    member_e(A,Z),
 1642    remove_unneed(B, Z, C),! .
 1643remove_unneed([A|B], Z, C):-
 1644    var(A),
 1645    append(Z,[A],D),
 1646    remove_unneed(B, D, C),!.
 1647remove_unneed([A|B], Z, C):-
 1648    ground(A),
 1649    remove_unneed(B, Z, C),!.
 1650remove_unneed([A|B], Z, C):-
 1651    A=..[ne|Paras],
 1652    append(Z,[A],D),
 1653    remove_unneed(B, D, C),!.
 1654remove_unneed([A|B], Z, C):-
 1655    A=..[Pred|Paras],
 1656    same_var_member(A,Z),
 1657    remove_unneed(B, Z, C),!.
 1658remove_unneed([A|B], Z, C):-
 1659    append(Z,[A],D),
 1660    remove_unneed(B, D, C),!.
 1661
 1662same_var_member(Pred,[Pred1|List]):-
 1663     var(Pred1),
 1664     same_var_member(Pred,List),!.
 1665same_var_member(Pred,[Pred1|List]):-
 1666     Pred==Pred1,!.
 1667same_var_member(Pred,[Pred1|List]):-
 1668     Pred=..[H|T],
 1669     Pred1=..[H|T1],
 1670     same_var_member1(T,T1),!.
 1671same_var_member(Pred,[Pred1|List]):-
 1672     same_var_member(Pred,List),!.
 1673
 1674same_var_member1([],[]):-!.
 1675same_var_member1([H1|T],[H2|T]):-
 1676     var(H1),
 1677     H1==H2,!.
 1678same_var_member1([H|T1],[H|T2]):-
 1679     var(T1),
 1680     T1==T2,!.
 1681same_var_member1([H1|T1],[H2|T2]):-
 1682     H1==H2,
 1683     same_var_member1(T1,T2),!.
 1684
 1685	
 1686% set_append_e: list1 + list2 -> list
 1687% no duplicate, no instanciation
 1688% ------------------------------------------
 1689set_append_e(A,B,C):-
 1690    append_cut(A,B,D),
 1691    remove_dup(D,[],C),!.
 1692
 1693% remove duplicate
 1694remove_dup([],C,C):-!.
 1695remove_dup([A|B],Z,C) :-
 1696    member_e(A, Z),
 1697    remove_dup(B, Z, C),! .
 1698remove_dup([A|B], Z, C):-
 1699    append(Z,[A],D),
 1700    remove_dup(B, D, C),!.
 1701
 1702vequal([],[]):-!.
 1703vequal([X|XLs],[Y|YLs]):-
 1704    var(X),
 1705    vequal(XLs,YLs),!.    
 1706vequal([X|XLs],[Y|YLs]):-
 1707    var(Y),
 1708    vequal(XLs,YLs),!.
 1709vequal([X|XLs],[Y|YLs]):-
 1710    X==Y,	
 1711    vequal(XLs,YLs),!.
 1712
 1713
 1714% append_st: append two statics
 1715
 1716/* is X is in the atomic_invariants then by defn its a static. */

 1717is_a_static_pred(X) :-
 1718        atomic_invariants( A ),
 1719        not( not( member(X,A) )),!.
 1720is_a_static_pred(ne(_,_)) :-!.
 1721is_a_static_pred(is_of_sort(_,_)) :-!.
 1722 
 1723is_a_dynamic_pred(X) :-
 1724        not( is_a_static_pred(X) ),!.
 1725 
 1726/* filter_list(X,condition(args),XO)
 1727 
 1728 XO is reduced list */

 1729
 1730filter_list([X|Rest],Op,[X|Rest1]) :-
 1731        Op =.. OL,
 1732        append(OL,[X],OL1),
 1733        Pred =.. OL1,
 1734        call(Pred),
 1735        filter_list(Rest,Op,Rest1),!.
 1736filter_list([_|Rest],Op,Rest1) :-
 1737        filter_list(Rest,Op,Rest1),!.
 1738filter_list([],_,[]).
 1739
 1740
 1741
 1742% ----------------------utilities---------------------
 1743/*
 1744not(X):- \+X.
 1745member(X,[X|_]).
 1746member(X,[_|L]) :- member(X,L).
 1747append([],L,L):-!.
 1748append([H|T],L,[H|Z]) :- append(T,L,Z),!.
 1749 */

 1750
 1751file_exists(Filename):-exists_file(Filename).
 1752
 1753% subtract(A,B,C): subtract B from A
 1754% -------------------------------------
 1755subtract([],_,[]):-!.
 1756subtract([A|B],C,D) :-
 1757        member(A,C),
 1758        subtract(B,C,D),!.
 1759subtract([A|B],C,[A|D]) :-
 1760        subtract(B,C,D),!.
 1761
 1762/* arg1 - arg2 = arg3 */

 1763
 1764list_take(R,[E|R1],R2):-
 1765        remove_el(R,E,RR),
 1766        list_take(RR,R1,R2),!.
 1767list_take(R,[_|R1],R2):-
 1768        list_take(R,R1,R2),!.
 1769list_take(A,[],A) :- !.
 1770
 1771				% remove_el: list * el -> list-el 
 1772% ----------------------------------
 1773remove_el([],_,[]) :- ! .
 1774remove_el([A|B],A,B) :- ! .
 1775remove_el([A|B],C,[A|D]) :-
 1776        remove_el(B,C,D) .
 1777
 1778/* generate symbol predicate  (from file futile)*/

 1779
 1780gensym_special(Root,Atom) :-
 1781                        getnum(Root,Num),
 1782                        name(Root,Name1),
 1783                        name(Num,Name2),
 1784                        append(Name1,Name2,Name),
 1785                        name(Atom,Name).
 1786
 1787getnum(Root,Num) :-
 1788                        retract(current_num(Root,Num1)),!,
 1789                        Num is Num1+1,
 1790                        asserta(current_num(Root,Num)).
 1791
 1792getnum(Root,1) :- asserta(current_num(Root,1)).
 1793
 1794
 1795/************ end of change_op_representation ***********/

 1796
 1797gensym_num(Root,Num,Atom):-
 1798     name(Root,Name),
 1799     name(Num,Name1),
 1800     append(Name,Name1,Name2),
 1801     name(Atom,Name2),!.
 1802
 1803
 1804%*******************************************************   
 1805pprint([],SIZE,SIZE):-!.
 1806pprint([HS|TS],Size0,SIZE):-
 1807    list(HS),
 1808    pprint(HS,Size0,Size1),
 1809    pprint(TS,Size1,SIZE),!.
 1810pprint([HS|TS],Size0,SIZE):-
 1811%    write('step '),write(Size0),write(': '),
 1812%    write(HS),nl,
 1813    Size1 is Size0+1,
 1814    pprint(TS,Size1,SIZE),!.
 1815
 1816
 1817% list of lists -> list
 1818
 1819flatten([HO|TO], List, O_List):-
 1820	append(HO, List, List_tmp),
 1821	flatten(TO, List_tmp, O_List),!.
 1822flatten([H|TO], List,O_List):-
 1823	append([H], List, List_tmp),
 1824	flatten(TO, List_tmp, O_List).
 1825flatten([], [HList|T], O_List):-
 1826	HList = [],
 1827	flatten(T, [], O_List).
 1828flatten([], [HList|T], O_List):-
 1829	list(HList),
 1830	flatten([HList|T],[], O_List),!.
 1831flatten([], L,L):-!.
 1832
 1833% flatten with no duplicate
 1834set_flatten([HO|TO], List, O_List):-
 1835	set_append_e(HO, List, List_tmp),
 1836	set_flatten(TO, List_tmp, O_List),!.
 1837set_flatten([H|TO], List,O_List):-
 1838	set_append_e([H], List, List_tmp),
 1839	set_flatten(TO, List_tmp, O_List).
 1840set_flatten([], [HList|T], O_List):-
 1841	HList = [],
 1842	set_flatten(T, [], O_List).
 1843set_flatten([], [HList|T], O_List):-
 1844	list(HList),
 1845	set_flatten([HList|T],[], O_List),!.
 1846set_flatten([], L,L):-!.
 1847
 1848% list: [el1,el2, ...] --> bool
 1849% -----------------------------
 1850list(A) :-
 1851        var(A) ,
 1852        ! ,
 1853        fail .
 1854list(A) :-
 1855        functor(A,'.',_).
 1856% ***********************for multy tasks*****************
 1857:- assert(time_taken(0)).
 1858:- assert(soln_size(0)).
 1859:- assert(plan_used(0)).
 1860
 1861solve(N,FN):-
 1862   N < FN,
 1863   tell(user),
 1864   nl,write('task '), write(N),write(': '),nl,
 1865   solution_file(F),
 1866   tell(F),
 1867   nl,write('task '), write(N),write(': '),nl,
 1868   solve(N),
 1869   Ni is N+1,
 1870   solve(Ni,FN).
 1871solve(FN,FN):-
 1872   tell(user),nl,write('task '),
 1873   write(FN),write(': '),nl,
 1874   solution_file(F),
 1875   tell(F),
 1876   nl,write('task '),
 1877   write(FN),write(': '),nl,
 1878   solve(FN),
 1879   retractall(sum(_)),
 1880   assert(sum(0)),
 1881   sum_time(CP),
 1882   retractall(sum(_)),
 1883   assert(sum(0)),
 1884   sum_size(SIZE),
 1885   TIM is CP /1000,
 1886   retractall(sum(_)),
 1887   assert(sum(0)),
 1888   sum_plan(PLAN),
 1889   retractall(time_taken(_)),
 1890   retractall(soln_size(_)),
 1891   retractall(plan_used(_)),
 1892   nl,write('total time '),write(TIM),write(' seconds'),
 1893   nl,write('total size '),write(SIZE),
 1894   nl,write('plan used '),write(PLAN),
 1895   nl,
 1896   told.
 1897
 1898sum_time(TIM):-
 1899   time_taken(CP),
 1900   retract(sum(N)),
 1901   N1 is N +CP,
 1902   assert(sum(N1)),
 1903   fail.
 1904sum_time(TIM):-
 1905   sum(TIM).
 1906sum_size(SIZE):-
 1907   soln_size(S),
 1908   retract(sum(N)),
 1909   N1 is N +S,
 1910   assert(sum(N1)),
 1911   fail.
 1912sum_size(SIZE):-
 1913   sum(SIZE).
 1914   
 1915   
 1916sum_plan(Plan):-
 1917   plan_used(PL),
 1918   retract(sum(N)),
 1919   N1 is N +PL,
 1920   assert(sum(N1)),
 1921   fail.
 1922sum_plan(Plan):-
 1923   sum(Plan).
 1924   
 1925   
 1926%ground the hierarchy structure of HTN planning domain
 1927ground_hierarchy:-
 1928     ground_predicates,
 1929     ground_substate_class,!.
 1930
 1931% grounding predicates to object level
 1932% assert in prolog database as gpredicates(Pred,GPredls)
 1933ground_predicates:-
 1934     predicates(Preds),
 1935     grounding_preds(Preds),
 1936     collect_grounded_pred,!.
 1937
 1938grounding_preds([]).
 1939% if it is statics, only choose within atomic_invariants
 1940grounding_preds([HPred|TP]):-
 1941     functor(HPred,FF,NN),
 1942     functor(APred,FF,NN),
 1943     atomic_invariants(Atom),
 1944     member(APred,Atom),
 1945     ground_all_var_atom(HPred),
 1946     grounding_preds(TP).
 1947% else, combine all the objects
 1948grounding_preds([HPred|TP]):-
 1949     functor(HPred,FF,NN),
 1950     functor(GPred,FF,NN),
 1951     ground_all_var(GPred,HPred),
 1952     grounding_preds(TP).
 1953
 1954% collect all the grounded predicates together
 1955collect_grounded_pred:-
 1956     gpred(Pred,_),
 1957     setof(GPred,gpred(Pred,GPred),GPredls),
 1958     retractall(gpred(Pred,_)),
 1959     assert(gpredicates(Pred,GPredls)),
 1960     fail.
 1961collect_grounded_pred.
 1962
 1963% grounding substate_class to primary sort level
 1964% assert in prolog database as gsubstate_class(Sort,Obj,States)
 1965ground_substate_class:-
 1966     substate_classes(Sort,Obj,Substate),
 1967     find_prim_sort(Sort,PS),
 1968     assert_subclass(PS,Obj,Substate),
 1969     fail.
 1970ground_substate_class:-
 1971     collect_grounded_substates.
 1972
 1973assert_subclass([],Obj,Substate).
 1974assert_subclass([HS|TS],Obj,Substate):-
 1975     assert(gsstates(HS,Obj,Substate)),
 1976     assert_subclass(TS,Obj,Substate).
 1977
 1978collect_grounded_substates:-
 1979     gsstates(Sort,Obj,_),
 1980     setof(SStates,gsstates(Sort,Obj,SStates),GSStates),
 1981     retractall(gsstates(Sort,Obj,_)),
 1982     all_combined(GSStates,GSStates0),
 1983     assert(gsubstate_classes(Sort,Obj,GSStates0)),
 1984     fail.
 1985collect_grounded_substates.
 1986
 1987all_combined(SStates,CSStates):-
 1988     xprod(SStates,CSStates1),
 1989     flat_interal(CSStates1,CSStates),!.
 1990
 1991flat_interal([],[]):-!.
 1992flat_interal([HSS1|TSS1],[HSS|TSS]):-
 1993     flatten(HSS1,[],HSS),
 1994     flat_interal(TSS1,TSS),!.
 1995
 1996ground_all_var_atom(HPred):-
 1997     functor(HPred,FF,NN),
 1998     functor(GPred,FF,NN),
 1999     functor(APred,FF,NN),
 2000     atomic_invariants(Atom),
 2001     member(APred,Atom),
 2002     GPred=..[Name|Vars],
 2003     APred=..[Name|Sorts],
 2004     ground_all_var_atom0(Vars,Sorts),
 2005     assert_gpred(GPred),
 2006     fail.
 2007ground_all_var_atom(HPred).
 2008
 2009ground_all_var_atom0([],[]).
 2010ground_all_var_atom0([HVars|TV],[HSorts|TS]):-
 2011     subsorts(HSorts,Subsorts),
 2012     split_prim_noprim(Subsorts,PSorts,NPSorts),
 2013     member(LS,PSorts),
 2014     objects(LS,Objls),
 2015     member(HVars,Objls),
 2016     ground_all_var_atom0(TV,TS).
 2017ground_all_var_atom0([HVar|TV],[HASorts|TS]):-
 2018     objects(Sorts,Objls),
 2019     member(HASorts,Objls),
 2020     HVar=HASorts,
 2021     ground_all_var_atom0(TV,TS).
 2022
 2023ground_all_var(GPred,HPred):-
 2024     GPred=..[Name|Vars],
 2025     HPred=..[Name|Sorts],
 2026     ground_all_var0(Vars,Sorts),
 2027     not(inconsistent_constraint([GPred])),
 2028     assert_gpred(GPred),
 2029     fail.
 2030ground_all_var(GPred,HPred).
 2031
 2032ground_all_var0([],[]).
 2033ground_all_var0([HVars|TV],[HSorts|TS]):-
 2034     objects(HSorts,Objls),
 2035     member(HVars,Objls),
 2036     ground_all_var0(TV,TS).
 2037ground_all_var0([HVars|TV],[HSorts|TS]):-
 2038     subsorts(HSorts,Subsorts),
 2039     split_prim_noprim(Subsorts,PSorts,NPSorts),
 2040     member(LS,PSorts),
 2041     objects(LS,Objls),
 2042     member(HVars,Objls),
 2043     ground_all_var0(TV,TS).
 2044
 2045% assert grounded predicates with related upper level predicates
 2046
 2047assert_gpred(GPred):-
 2048     functor(GPred,FF,NN),
 2049     functor(UPred,FF,NN),
 2050     assert_gpred0(GPred,UPred),!.
 2051
 2052assert_gpred0(GPred,UPred):-
 2053     GPred=..[Name|PSorts],
 2054     UPred=..[Name|Vars],
 2055     get_obj_sort(Vars,PSorts),
 2056     assert(gpred(UPred,GPred)),
 2057     fail.
 2058assert_gpred0(GPred,UPred).
 2059
 2060get_obj_sort([],[]):-!.
 2061get_obj_sort([HVars|TV],[HObj|TS]):-
 2062     objects(HVars,Objls),
 2063     member(HObj,Objls),
 2064     get_obj_sort(TV,TS),!.
 2065
 2066delete_all_nodes :-
 2067	retractall(node(_,_,_,_,_)),
 2068	retractall(final_node(_)),
 2069	retractall(tp_node(_,_,_,_,_,_)),
 2070	retractall(closed_node(_,_,_,_,_,_)),
 2071	retractall(solved_node(_,_,_)).
 2072delete_all_nodes :- !.
 2073
 2074
 2075% xprod: list * list --> (list X list)
 2076% -----------------------------------
 2077xprod(A,B,C) :-
 2078        xprod([A,B],C) .
 2079 
 2080xprod([],[]).
 2081xprod(A,E) :-
 2082        xprod(A,B,C,D) ,
 2083        F =..[^,C,D] ,
 2084        call(setof(B,F,E)) .
 2085 
 2086xprod([X],[A],A,member(A,X)) .
 2087xprod([X,Y],[A,B],C,(D,E)) :-
 2088        C =..[^,A,B] ,
 2089        D =..[member,A,X] ,
 2090        E =..[member,B,Y] .
 2091xprod([X|Y],[A|E],D,(F,G)) :-
 2092        D =..[^,A,C] ,
 2093        F =..[member,A,X] ,
 2094        xprod(Y,E,C,G).
 2095
 2096
 2097:- include(translog4_domain).
 2098:- include(translog4_ops).
 2099%:- include(translog4_ops2).
 2100%:- include(translog4_ops3).

 2101:- include(translog4_problems).
 2102
 2103:- fixup_exports.