12:-module(logicmoo_hyhtn_code,[]). 13
14:-multifile(user:push_env_ctx/0). 15:-dynamic(user:push_env_ctx/0). 16
21
23:- ensure_loaded(library(logicmoo_common)). 24:- ensure_loaded(library(planner_api)). 25:- use_module(library(logicmoo_planner)). 26
27:- kb_shared(baseKB:mpred_prop/3). 28
29:- ensure_loaded(library(logicmoo/util_structs)). 30:- ensure_loaded(library(logicmoo/util_bb_env)). 31:- prolog_load_context(file,File),ain(user:env_source_file(File)). 32
33:-op(500,fx,env_call). 35
43
44
45planner_failure(Why,Info):-dmsg(error,Why-Info),banner_party(error,'FAILURE_PLANNER'),print_message(error,'FAILURE_PLANNER'(Why,Info)),!. 46
47:-thread_local t_l:doing/1. 48
49statistics_runtime(CP):-statistics(runtime,[_,CP0]), (CP0==0 -> CP= 0.0000000000001 ; CP is (CP0/1000)) . 50statistics_walltime(CP):-statistics(walltime,[_,CP0]), (CP0==0 -> CP= 0.0000000000001 ; CP is (CP0/1000)) . 51
52
53
76 81:-dynamic my_stats/1.
82
83
84:-multifile(on_call_decl_hyhtn/0). 85:-export(on_call_decl_hyhtn/0). 87on_call_decl_hyhtn :- decl_env_mpred_dom([kb(dom,tasks),stubType(dyn)], ( htn_task/3, planner_task/3, planner_task_slow/3 )).
88
89on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(temp_assertIndivConds/1)). 90on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(is_of_primitive_sort/2, is_of_sort/2)).
91on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(methodC/7, opParent/6,operatorC/5,gOperator/3)).
92on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(objectsC/2,objectsD/2,atomic_invariantsC/1)). 93on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(objectsOfSort/2)). 94on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache) ,(related_op/2, gsubstate_classes/3, gsstates/3)).
95
96on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(initial_state/1)).
97on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache) ,(op_score/2)).
98on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache),(node/5,final_node/1)).
99on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(tp_goal/3,closed_node/6,solved_node/2, goal_related_search/1)).
101on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(goal_related/3)).
102on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(current_num/2)).
103on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(tn/6)). 104on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(tp_node/6)).
107
109on_call_decl_hyhtn :- decl_env_mpred_dom([kb(dom,tasks),stubType(dyn)], ( htn_task/3, planner_task/3, planner_task_slow/3 )).
110
112on_call_decl_hyhtn :-
113 decl_env_mpred_dom([kb(dom,file),stubType(dyn)],[domain_name/1,sorts/2,substate_classes/3,objects/2,predicates/1,inconsistent_constraint/1,atomic_invariants/1,
114 implied_invariant/2,operator/4,
115 116 method/6]).
117
118:-export(call_decl_hyhtn/0). 119
121
122call_decl_hyhtn:-must(doall(on_call_decl_hyhtn)).
123
125 126
127
128
132
133
135tryff(Call):- predicate_property(Call,_),!,once(tryf((Call,assert(passed_test_try(Call))))),fail.
136tryf(Call):- predicate_property(Call,_),!,catch(Call,E,dmsg(E = Call)).
137trye(Call):- catch(Call,E,((dmsg(error(E , Call)),throw(trace),Call))).
138
139:-dynamic(passed_test_try/1). 140:-dynamic(testing_already/0). 141
142check_passed_any:-not(not(passed_test_try(_))),nl,listing(passed_test_try/1).
143
144ttm:-retractall(passed_test_try(_)),fail.
145ttm:-testing_already,!.
146ttm:-asserta(testing_already), make, retractall(testing_already),fail.
147
148
149:-export(banner_party/2). 150banner_party(E,BANNER):-
151 ansicall(yellow,(
152 format("% xxxxxxxxxxxxxxx ~w xxxxxxxxxxxxxxxxxxx~n",[E]),
153 forall(t_l:doing(X),dmsg(E,doing(X))),
154 dmsg5(E,BANNER),
155 format("% xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx~n",[]))).
156
157
158subst_eq_h(A,B,C,D):- nd_subst(A,B,C,D),!.
159subst_eq_h(A,B,C,D):- throw(trace), nd_subst(A,B,C,D),!.
160
161
162:-export(term_expansion_alias/2). 163term_expansion_alias(In,Out):-term_expansion_alias([],In,Out).
164term_expansion_alias(Not,In,Out):-term_alias(I,O),not(member(I,Not)),subst_eq_h(In,I,O,M), In\=@=M,!, term_expansion_alias([I|Not],M,Out).
165term_expansion_alias(_Not,InOut,InOut).
166term_alias(cond,se).
167term_alias(se,se).
168term_alias(state,ss).
169term_alias(trans,sc).
170term_alias(ne,dif).
171term_alias(neq,dif).
173term_alias(startOcl,start).
174term_alias(startOCL,start).
175
176
179
180
181tasks:-
182 Call = get_tasks(N,Goal,State),
183 setof(Call,Call,List),list_to_set(List,Set),!,
184 env_info(kb(dom,file)),!,
185 once((ignore(forall(member(Call,Set),
186 locally(t_l:doing(tasks(N,Goal)),
187 ((
188 must(nonvar(Goal)),must(nonvar(State)),
189 ignore(N=Goal),
190 must((term_expansion_alias((Goal:-State),(GGoal:-GState)))),
191 must(nonvar(GGoal)),must(nonvar(GState)),
192 banner_party(informational,('GOAL'(N):-GGoal)),
193 must(((once( once(startOCL(GGoal,GState))*->banner_party(informational,'SUCCESSS');banner_party(error,'FAILUR!!!!!!!!!!!!!!!!!!!!!!!!!!!E')))))))))))).
194
195t:- once(run_header_tests).
196
197tt:-catch(ttm,E,dmsg(E)),!.
198
199tt:-tryff((tasks)).
200tt:-tryff((task1)).
201tt:-tryff((task2)).
202tt:-tryff((task3)).
203tt:-tryff((task4)).
204tt:-tryff((task5)).
205tt:-tryff((task6)).
206tt:-tryff((task7)).
207tt:-tryff((task8)).
208tt:-tryff((task9)).
209tt:-tryff((task10)).
210tt:-tryff((task11)).
211tt:-tryff((task22)).
212tt:-tryff((task33)).
213tt:-tryff((task44)).
214tt:-check_passed_any,!.
215
216
217tdom:-tdom([htn1,ty,ocl1,r3]).
218tdom2:-tdom([ocl2,htn2,htn3,htn4]).
219
220tdomfile(F):-tdomcall(load_data_file(F)).
221tdomcall(Call):- trye(once((env_clear_doms_and_tasks,trye(Call),tt))).
222
223tdom([]):-!.
224tdom([H|T]):- !, tdom(H),tdom(T).
225tdom(H):- predicate_property(H,_),!, throw(trace),call(H).
226tdom(F):- tdom1(F),!.
227tdom(F):- expand_file_name(F,List),List\=[],!,tdom1(List).
228tdom(F):- throw(tdom(F)).
229
230tdom1([]):-!.
231tdom1([H|T]):- !, tdom(H),tdom(T).
232tdom1(F):- tdom2(F),!.
233tdom1(FIn):- atom(FIn),tdom2(FIn).
234
235tdom2(FIn):- tdom3(FIn),!.
236tdom2(FIn):- atom_concat('domains_ocl/',FIn,F), tdom3(F),!.
237
238tdom3(FIn):- tdom4(FIn).
239tdom3(FIn):- atom_concat(FIn,'htn',F),tdom4(F).
240tdom3(FIn):- atom_concat(FIn,'ocl',F),tdom4(F).
241
242tdom4(F):- exists_file(F),!,tdomfile(F).
243
244
245:- discontiguous(post_header_hook/0).:-retractall(canDoTermExp).
249post_header_hook:-asserta(canDoTermExp).
250
251:-export(env_clear_doms_and_tasks/0). 252env_clear_doms_and_tasks:- env_clear(kb(dom,file)),env_clear(kb(dom,tasks)),env_clear(kb(dom,cache)),!.
253
254:- op(100,xfy,( cl:('=>'))). 255
257post_header_hook:-set_prolog_flag(verbose_load,full).
258post_header_hook:-use_module(library(lists)).
259
261:- style_check(-singleton). 262:- style_check(+discontiguous). 263
264
266:-if(exists_source(library(gui_tracer))). 267post_header_hook:- user:use_module(library(gui_tracer)),catch(guitracer,_,true).
268:-endif.:- !.
270post_header:- dmsg(post_header),fail, forall(clause(post_header_hook,G),G).
271
272
273:- discontiguous(header_tests/0). 274
275run_tests(Call) :-
276 statistics_runtime(InTime),
277 locally(doing(run_tests(Call)),
278 call_cleanup(Call,
279 ((
280 statistics_runtime(OutTime),
281 Time is OutTime - InTime,
282 banner_party(informational,runtests(Call) = time(Time)))))).
283
:- run_tests(forall(clause(header_tests,G),run_tests(G))).
285
286
287:-export(test_ocl/1). 288test_ocl(File):- forall(filematch(File,FM),test_ocl0(FM)).
289test_ocl0(File):- time(locally(t_l:doing(test_ocl(File)),
290 once((env_clear_doms_and_tasks,clean_problem,l_file(File),tasks)))).
291
:-test_ocl('domains_ocl/*.ocl').
293
294
295:- style_check(-singleton). 296:- style_check(-discontiguous). 298
300
312
313
315:-dynamic kill_file/1,solution_file/1. 318:- style_check(-singleton). 322:- op(100,xfy,'=>'). 335
336incr_op_num:- flag(op_num,X,X+1).
337set_op_num(X):-flag(op_num,_,X).
338current_op_num(X):- flag(op_num,X,X).
339
340
341
342startOCL(Goal,Init):-
343 clean_problem,
344 dmsg('OCL-PLANNER-TASK'(Goal)),
345 must(planner_interface(Goal,Init,Sol,_,TNLst)),
346 show_result_and_clean(F,Id,Sol,TNLst).
347
348
349get_tasks(N,Goal,State):- htn_task(N,Goal,State).
351get_tasks(N,Goal,State):- env_call planner_task(N,Goal,State).
352
353:- set_op_num(0). 354:-asserta(my_stats(0)). 355
356l_file(File):- \+ exists_file(File),!,forall(filematch(File,FM),l_file(FM)).
357l_file(F):- env_consult(ocl:F).
358l_file(F):-
359 if_defined(cl:force_reload_mpred_file(F),
360 if_defined(cl:with_mpred_consult(cl:consult(F)),cl:consult(F))).
361
362solve_file(F):-with_filematch(l_file(wfm(F))), doall(solve(_)).
363solve:- solve_file(test_hyhtn).
364
365
366
367solve(Id) :-
368 no_repeats(get_tasks(Id,Goal,Init)),
369 planner_interface(Goal,Init,Sol,_,TNLst),
370 show_result_and_clean(user,Id,Sol,TNLst).
371
372show_result_and_clean(F,Id,Sol,TNLst):-
373 must(solution_file(F)),
374 tell(F),
375 format('~NSOLUTION for TASK ~w~n',[Id]),
376 display_sol(Sol),
377 write_out_test_data('.preClean',Id),
378 write('END FILE'),nl,nl,
379 nop((reverse(TNLst,TNForward),
380 display_details(TNForward),
381 write('END PLANNER RESULT'))),
382 told,
383 clean_problem.
384
385write_out_test_data(_MoreID,_Id):-!.
386write_out_test_data(MoreID,Id):-
387 must((
388
389 (var(Id)->statistics(walltime,[Id,_]);true),
390 (push_env_ctx-> A= pushed_ ; A=nonpushed_ ),
391 atom_concat(A,Id,TN),atom_concat(TN,MoreID,TTN),
392 lws(TTN))),!.
393
394display_sol([]).
395display_sol([H|T]) :-
396 write(H),
397 nl,
398 display_sol(T).
399
400display_details([]):-!.
401display_details([H|T]):-!,display_details(H),display_details(T),!.
402display_details(tn(TN,Name,Pre,Post,Temp,Dec)):-
404 nl,write('BEGIN METHOD'),nl,write(TN),write(';'),
405 nl,write('Name:'),write(Name),write(';'),
406 nl,write('Pre-condition:'),write(Pre),write(';'),
408 nl,write('Index Transitions:'),write('=>'),write(Post),write(';'),
410 nl,write('Temporal Constraints:'),write(Temp),write(';'),
411 nl,write('Decomposition:'),write(Dec),write(';'),
412 nl.
413
414display_details(H):-dmsg((display_details:-H)).
415
416clean_problem:-
417 env_retractall(temp_assertIndivConds(_)),
418 env_retractall(opParent(_,_,_,_,_,_)),
419 env_retractall(operatorC(_,_,_,_,_)),
420 env_retractall(initial_state(_)),
421 env_retractall(objectsOfSort(_,_)),
422 env_retractall(objectsD(_,_)),
423 env_retractall(objectsC(_,_)),
424 env_retractall(methodC(_,_,_,_,_,_,_)),
425 env_retractall(is_of_sort(_,_)),
426 env_retractall(is_of_primitive_sort(_,_)),
427 env_retractall(gsubstate_classes(_,_,_)),
428 env_retractall(gOperator(_,_,_)),
429 env_retractall(current_num(_,_)),
430 env_retractall(atomic_invariantsC(_)),
431
432 433 env_retractall(final_node(_)),
434 435 nb_setval(tnodeSORTED,[]),
436 env_retractall(tp_node(_,_,_,_,_,_)),
437 env_retractall(current_num(tp,_)),
438 env_retractall(node(_,_,_,_,_)),
439 env_retractall(solved_node(_,_)),
440 env_retractall(tn(_,_,_,_,_,_)),
441 env_retractall(closed_node(_,_,_,_,_,_)),
442 443 444 env_retractall(op_score(_,_)),
445 env_retractall(goal_related(_,_,_)),
446 env_retractall(goal_related_search(_)),
447
448
449 450 set_op_num(0).
451
452
453
454get_instance(A1,Obj1):- \+compound(A1),!,Obj1=A1.
455get_instance(A1,Obj1):-functor(A1,_,N),N<3,!,arg(1,A1,Obj1).
456get_instance(A1,Obj1):-arg(2,A1,Obj1).
457
458compare_on_instance(Comp,A1,A2):- get_instance(A1,Obj1),get_instance(A2,Obj2),compare(Comp1,Obj1,Obj2),
459 (Comp1== (=) -> compare(Comp,A1,A2) ; Comp=Comp1).
460
461to_ssify(_,_,goal(S,X,L),goal(S,X,L)):-!.
462to_ssify(Hints,SS,G,GGG):-must_det_l((is_list(G),must(to_ssify0(Hints,SS,G,GG)),predsort(compare_on_instance,GG,GGS),must(merge_ss2s(GGS,GGG)))).
463
464to_ssify0(Hints,SS,G,GG):-must(to_ssify1(Hints,SS,G,GG)).
465to_ssify1(Hints,SS,[],[]):- !.
466to_ssify1(Hints,SS,G,[GG]):- atom(G),!,G1=..[G,self],GG=..[SS,top,self,[G1]].
467to_ssify1(Hints,SS,G,[GG]):- ( \+ compound(G)),!,GG=..[SS,top,self,[beliefsTrue(self,G)]].
468to_ssify1(Hints,_,goal(S,X,L),[goal(S,X,L)]):-!.
469to_ssify1(Hints,SS,[GH|GT],GG):- !,to_ssify0(Hints,SS,GH,L1),to_ssify0(Hints,SS,GT,L2),append(L1,L2,GG).
470to_ssify1(Hints,SS,ss(S,X,L),GG):- !, (length(L,1)->GG=[ss(S,X,L)];(append(Hints,L,NewHints),to_ssify0(NewHints,SS,L,GG))).
471to_ssify1(Hints,SS,se(S,X,L),GG):- !, (length(L,1)->GG=[se(S,X,L)];(append(Hints,L,NewHints),to_ssify0(NewHints,SS,L,GG))).
472to_ssify1(Hints,SS,scL(S,X,L),GG):- !, append(Hints,[is_of_sort(X,S)|L],NewHints),to_ssify0(NewHints,scL,L,GG).
473to_ssify1(Hints,SS,scR(S,X,L),GG):- !, append(Hints,[is_of_sort(X,S)|L],NewHints),to_ssify0(NewHints,scR,L,GG).
474to_ssify1(Hints,SS,sc(S,X,Neg=>Pos),GG):-!,
475 must_det_l(( to_ssify0(Hints,SS,scL(S,X,Neg),L1),to_ssify0(Hints,SS,scR(S,X,Pos),L2),
476 append(L1,L2,GG))).
477to_ssify1(Hints,SS,G,[GG]):-get_instance(G,X),must_det_l((get_one_isa(S,X,Hints),GG=..[SS,S,X,[G]])).
478
479merge_ss2s([],[]).
480merge_ss2s([G1,G2|GG],GGG):- must(\+ is_list(G1)),
481 once(must_det_l((G1=..[SS1,S1,Obj1,L1],G2=..[SS2,S2,Obj2,L2]))),Obj1==Obj2,
482 merge_ss(SS1,S1,Obj1,L1,SS2,S2,Obj2,L2,G),merge_ss2s([G|GG],GGG),!.
483merge_ss2s([G|GG],[G|GGG]):-
484 merge_ss2s(GG,GGG).
485
486
487need_ss_swap(scR,scL).
488need_ss_swap(scR,sc).
489need_ss_swap(scL,sc).
490merge_ss(SS1,S1,Obj1,L1,SS2,S2,Obj2,L2,G):- need_ss_swap(SS1,SS2),!,merge_ss(SS2,S2,Obj2,L2,SS1,S1,Obj1,L1,G).
491
492merge_ss(scL,S1,Obj1,L1,scR,S2,_Obj2,L2,sc(S1,Obj1,L1=>L2)):-!.
493merge_ss(sc,S1,Obj1,[L=>R],scL,S2,_Obj2,[L2],sc(S1,Obj1,LL=>R)):-append(L,L2,LL).
494merge_ss(sc,S1,Obj1,[L=>R],scR,S2,_Obj2,[L2],sc(S1,Obj1,L=>RR)):-append(R,L2,RR).
495merge_ss(sc,S1,Obj1,[L1=>R1],sc,S2,_Obj2,[L2=>R2],sc(S1,Obj1,LL=>RR)):-append(L1,L2,LL),append(R1,R2,RR).
497merge_ss(SS,S1,Obj1,L1,SS,S1,Obj2,L2,G):-append(L1,L2,LL),G=..[SS,S1,Obj1,LL].
498
499
500init_locol_planner_interface(G,I,Node):-
501 locally(t_l:db_spy,
502 init_locol_planner_interface0(G,I,Node)).
503
504assert_itital_state(I0):- is_list(I0),!, must_maplist(assert_itital_state,I0),!.
505assert_itital_state(sc(S,X,List)):- !,must(assert_itital_state(List)),must(assert_itital_state(is_of_sort(X,S))),!.
506assert_itital_state(ss(S,X,List)):- !,must(assert_itital_state(List)),must(assert_itital_state(is_of_sort(X,S))),!.
507assert_itital_state(is_of_sort(X,S)):- ground(X:S),env_assert(is_of_sort(X,S)).
508assert_itital_state(I0):- env_assert(initial_state(I0)).
509
510get_inital_state_preds0(G,[]):- (\+compound(G)),!.
511get_inital_state_preds0([],[]):-!.
512get_inital_state_preds0([G1|G2],L):- !,get_inital_state_preds0(G1,L1),get_inital_state_preds0(G2,L2),append(L1,L2,L).
513get_inital_state_preds0(ss(_,_,G),L):- !,get_inital_state_preds0(G,L).
514get_inital_state_preds0(sc(_,_,G),L):- !,get_inital_state_preds0(G,L).
515get_inital_state_preds0(se(_,_,G),L):- !,get_inital_state_preds0(G,L).
516get_inital_state_preds0(G,L):- G=..[F,_,_|GL], arg(_,v(ss,sc,se),F),!,get_inital_state_preds0(GL,L).
517get_inital_state_preds0(G,L):- G=..[F,_|GL], arg(_,v(operator,method,substate_classes,se),F),!,get_inital_state_preds0(GL,L).
518get_inital_state_preds0(G,L):- G=..[F|GL], arg(_,v(goal,initial_state,implied_invariant,inconsistent_constraint,(=>),achieve,predicates),F),!,get_inital_state_preds0(GL,L).
519get_inital_state_preds0(G,[F/A]):-functor(G,F,A).
520
521get_inital_state_preds(Hints,L):-
522 findall(G1,
523 (member(F/A,[initial_state/1,operator/4,method/6,substate_classes/3,
524 predicates/1,implied_invariant/2,inconsistent_constraint/1]),
525 functor(G1,F,A),
526 env_call(G1)),LS),
527 get_inital_state_preds0([Hints|LS],UL),list_to_set(UL,L).
528
529
530env_call_if_defined(P):-catch((env_call P),_,fail).
531
532get_inital_state(G,I0):-
533 get_inital_state_preds(G,SP),
534 findall(S,(member(F/A,SP),functor(S,F,A),(if_defined(S,if_defined(ocl:S));env_call(initial_state(S)))),EL),
535 must(to_ssify(EL,ss,EL,I0)).
536
537
538:-export(init_locol_planner_interface0/3). 539
541init_locol_planner_interface0(G0,I0,Node):-
542 must_maplist(assert_itital_state,I0),
543 must(init_locol_planner_interface1(G0,Node)).
544
545init_locol_planner_interface1(G0,Node):-
546 must_det_l((get_inital_state(G0,I0),init_locol_planner_interface2(G0,I0,Node))).
547
548init_locol_planner_interface2(G0,I0,Node):-
549 must_det_l((
550 flatten([G0,I0],Hints),
551 to_ssify(Hints,se,G0,G),
552 to_ssify(Hints,ss,I0,I),
553 wdmsg((goals:-G)),
554 wdmsg((input:-I)),
555
556 change_obj_list(I),
557 ground_op,
558 assert_is_of_sort,
559 change_op_representation,
560 prim_substate_class,
561 set_op_num(0),
562 statistics_runtime(Time),
563 statistics_walltime(_),
564 write_out_test_data('.setup',Id),
565 ignore(retract(my_stats(_))),
566 assert(my_stats(Time)),
567 make_problem_into_node(I, G, Node))),!.
568
569
570show_time(must(Call)):-!, functor(Call,F,A), time(must(Call)),format('~N% ~q~n',[previous_statistics_for(F/A)]).
571show_time(Call):- functor(Call,F,A), time(Call),format('~N% ~q~n',[previous_statistics_for(F/A)]).
572
573planner_interface0(G,I, SOLN,OPNUM,TNList):-
574 gripe_time(2.0,show_time(must(init_locol_planner_interface(G,I,Node)))),
575 env_assert(Node),!,
576 dmsg(solving_for(G)),!,
577 start_solve(SOLN,OPNUM,TNList).
578
579
580planner_interface(G,I, SOLN,OPNUM,TNList):-
581 (planner_interface0(G,I, SOLN,OPNUM,TNList)
582 *-> true ;
583 (tell(user),nl,write('failure in initial node'),
584 planner_failure('failure in initial node',planner_interface(G,I, SOLN,NODES,TNList)),!)).
585
589
590
591getN_name(node(Name, _, _, _,_), Name).
592getN_pre(node(_,Pre, _, _, _), Pre).
593getN_decomp(node(_, _, Decomp,_,_), Decomp).
594getH_temp(node(_, _, _,Temps, _), Temps).
595getN_statics(node(_,_,_,_,Statics), Statics).
596
598start_solve(SOLN,OPNUM,_):-
599 kill_file(Kill),
600 exists_file(Kill),
602 !,fail.
603
604start_solve(Sol,OPNUM,TNList):-
605 env_retract(final_node(Node)),
606 env_retractall(current_num(_,_)),
607 getN_statics(Node,Statics),
608 statics_consist(Statics),
609 extract_solution(Node,Sol,SIZE,TNList),
610 statistics_runtime(CP),
611 statistics_walltime(TIM),
612 tell(user),
613 flag(op_num,OPNUM,0),
614 nl, nl,
615 write('CPU Time = '),write(CP),nl,
616 write('WALL Time = '),write(TIM),
617 write(' SECONDS'),nl,
618 write('Solution SIZE = '),write(SIZE),nl,
619 write('Operator Used = '),write(OPNUM),nl,
620 write('***************************************'),
621 assert(time_taken(CP)),
622 assert(soln_size(SIZE)),
623 env_retractall(tn(_,_,_,_,_,_)),
624 !.
625start_solve(Sol,OPNUM,TNList):-start_solve0(Sol,OPNUM,TNList)*->true;start_solve1(Sol,OPNUM,TNList).
626
627start_solve0(Sol,OPNUM,TNList):-
628 select_node(Node),
630 631 process_node(Node),
632 start_solve(Sol,OPNUM,TNList).
633
634start_solve1(Sol,OPNUM,TNList):- solve_failed(Sol,OPNUM,TNList).
635
636solve_failed(Sol,OPNUM,TNList):-
637 tell(user), write('+++ task FAILED +++'),
638 planner_failure('+++ task FAILED +++',start_solve(Sol,OPNUM,TNList)),
639 640 clean_problem.
641
642
643
645
647process_node(Node) :-
648 getN_name(Node, Name),
649 getN_pre(Node, Pre),
650 getN_decomp(Node, Dec),
651 getH_temp(Node, Temps),
652 getN_statics(Node, Statics),
653 expand_decomp(Dec,Pre,Post,Temps,Temp1,Statics,Statics1,Dec1),
654 statics_consist(Statics),
655 assert_node(Name,Pre,Dec1,Temp1,Statics1).
656
657assert_node(Name,Pre,Decomp,Temp,Statics):-
658 all_HP_expanded(Decomp),
659 env_assert(final_node(node(Name,Pre,Decomp,Temp,Statics))),!.
660assert_node(Name,Pre,Dec,Temp,Statics):-
661 gensym_special(root,SYM),
662 env_assert(node(SYM,Pre,Dec,Temp,Statics)),!.
663
664all_HP_expanded([]):-!.
665all_HP_expanded([step(HPid,Name,_,_,exp(TN))|THPS]):-
666 all_HP_expanded(THPS),!.
667
673expand_decomp([],Post,Post,Temp,Temp,Statics,Statics,[]):-!.
674
676expand_decomp([step(HPid,Name,Pre0,Post0,exp(TN))|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre,State,exp(TN))|Decomp1]):-
677 state_achieved(Pre0,Pre),
678 state_change(Pre,Pre0,Post0,State),
679 statics_consist(Statics),
680 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics,Statics1,Decomp1),!.
681
683expand_decomp([step(HPid,ACH,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
684 ACH=..[achieve|_],
685 statics_consist(Statics),
686 expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,
687 Temp,Temp1,Statics,Statics1,Decomp1),!.
688
690expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre,State,exp(Name))|Decomp1]):-
691 apply_op(Name,HPid,Name,Pre,undefd,State,Statics,Statics2),
692 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
693
695apply_op(Name,HPid,Name,Pre,Post,State,Statics,Statics1):-
696 env_call operatorC(Name,Pre0,Post0,Cond,Statics0),
697 statics_append(Statics0,Statics,Statics2),
698 state_achieved(Pre,Pre0,Statics2),
699 state_change(Pre,Pre0,Post0,State2),
700 cond_state_change(State2,Cond,State),
701 all_achieved(Post,Statics2,State),
702 remove_unneed(Statics2,[],Statics1),
703 statics_consist_instance(Statics1),
704 statics_consist_instance(Statics0),
705 incr_op_num,!.
706
709expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre,State,exp(TN))|Decomp1]):-
710 apply_method(TN,HPid,Name,Pre,undefd,State,Statics,Statics2),
711 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
712
714apply_method(TN,HPid,Name,Pre,Post,State,Statics,Statics1):-
715 env_call methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH0),Dec0),
716 statics_append(Statics0,Statics,Statics2),
717 all_achieved(Pre0,Statics2,Pre),
718 remove_unneed(Statics2,[],Statics21),
719 make_dec1(HPid,Pre,ACH0,Statics21,Temp0,Dec0,Temp2,Dec2),
720 expand_decomp(Dec2,Pre,State,Temp2,Temp1,Statics21,Statics1,Dec1),
721 all_achieved(Post,Statics1,State),
722 incr_op_num,
723 make_tn(TN,Name,Pre,State,Temp1,Dec1),!.
724
726expand_decomp([step(HP,N,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
727 get_another_step(step(HP2,N2,Pre2,Post2,Exp),Pre,Statics,
728 HP,Temp,Temp2,Decomp,Decomp2),
729 expand_decomp([step(HP2,N2,Pre2,Post2,Exp),step(HP,N,Pre0,Post0,unexp)|Decomp2],
730 Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1).
731
734
736get_another_step(A,Pre,Statics,HP,Temp,Temp1,[],Dec2):-fail.
737get_another_step(step(HP2,Name2,Pre2,Post2,Exp),Pre,Statics,HP,Temp,[before(HP2,HP)|Temp],Dec,Dec2):-
738 member(step(HP2,Name2,Pre2,Post2,Exp),Dec),
739 not(necessarily_before(HP,HP2, Temp)),
740 state_achieved(Pre2,Pre,Statics),
741 list_take(Dec,[step(HP2,Name2,Pre2,Post2,Exp)],Dec2).
742
746expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
747 state_achieved(Post0,Pre),
750 remove_temp(Temp,HPid,Temp,Temp2),
751 expand_decomp(Decomp,Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1),!.
752
754expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,ACH,Pre,Post0,exp(TN))|Decomp1]):-
755 expand_ach_goal(HPid,TN,ACH,Pre,Post0,State,Statics,Statics2),
756 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
757
759expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,ACH,Pre,Post0,exp(TN))|Decomp1]):-
760 get_another_step(step(HP2,N2,Pre2,Post2,Exp),Pre,Statics,
761 HP,Temp,Temp2,Decomp,Decomp2),
762 expand_decomp([step(HP2,N2,Pre2,Post2,Exp),step(HPid,ACH,Pre,Post0,unexp)|Decomp2], Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1).
763
766
769expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
770 direct_expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics1),!.
771
775expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
776 make_tpnodes(Pre,Post,Statics),
778 fwsearch(TN,State),
781 clean_temp_nodes.
782
785
788direct_expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics):-
789 apply_tn(TN,HPid,ACH,Pre,Post,State,Statics,Statics).
791direct_expand_ach_goal(HPid,OP,ACH,Pre,Post,State,Statics,Statics1):-
792 dir_apply_op(OP,HPid,ACH,Pre,Post,State,Statics,Statics1).
795direct_expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
796 dir_apply_method(TN,HPid,ACH,Pre,Post,State,Statics,Statics1),!.
798
800apply_tn(Tn0,HPid,ACH,Pre,Post,State,Statics,Statics):-
801 env_call(tn(Tn0,Name,Pre0,Post0,Temp0,Decomp0)),
802 state_achieved(Pre0,Pre),
803 state_change(Pre,Pre0,Post0,State),
804 all_achieved(Post,Statics,State),
806 incr_op_num,!.
808
810dir_apply_op(Name,HPid,ACH,Pre,Goal,State,Statics,Statics1):-
812 make_se_primitive(Goal,Post),
813 env_call operatorC(Name,Pre0,Post0,Cond,Statics0),
814 statics_append(Statics0,Statics,Statics2),
815 state_related(Post0,Cond,Post),
817 state_change(Pre,Pre0,Post0,State2),
818 cond_state_change(State2,Cond,State),
819 all_achieved(Post,Statics2,State),
820 remove_unneed(Statics2,[],Statics1),
821 statics_consist(Statics1),
824 825 826 incr_op_num,!.
827
829dir_apply_method(TN,HPid,ACH,Pre,Goal,State,Statics,Statics1):-
831 make_se_primitive(Goal,Post),
832 env_call methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH0),Dec0),
833 statics_append(Statics0,Statics,Statics2),
834 state_related(Post0,Post),
836 state_change(Pre,Pre0,Post0,State2),
838 may_achieved(Post,Statics2,State2),
840 make_dec1(HPid,Pre,ACH0,Statics2,Temp0,Dec0,Temp2,Dec2),
841 expand_decomp(Dec2,Pre,State,Temp2,Temp1,Statics2,Statics1,Dec1),
842 all_achieved(Post,Statics1,State),
845 incr_op_num,
846 make_tn(TN,Name,Pre,State,Temp1,Dec1),!.
847
849make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp1,Dec1):-
850 var(HPid),
851 gensym_special(hp,HPid),
852 make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp1,Dec1),!.
853make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp1,Dec1):-
854 all_achieved(ACH,Statics,Pre),
855 make_dec01(HPid,1,Dec,Dec1),
856 change_temp(HPid,Temp,[],Temp1),!.
857make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,[before(STID0,STID1)|Temp1],[step(STID0,achieve(ACH),Pre,ACH,unexp)|Dec1]):-
858 gensym_num(HPid,0,STID0),
859 gensym_num(HPid,1,STID1),
860 make_dec01(HPid,1,Dec,Dec1),
861 change_temp(HPid,Temp,[],Temp1),!.
862
863make_dec01(HPid,_,[],[]):-!.
864make_dec01(HPid,Num,[HDec|TDec],[step(STID,HDec,undefd,undefd,unexp)|TDec0]):-
865 env_call operatorC(HDec,_,_,_,_),
866 gensym_num(HPid,Num,STID),
867 Num1 is Num + 1,
868 make_dec01(HPid,Num1,TDec,TDec0).
869make_dec01(HPid,Num,[HDec|TDec],[step(STID,HDec,undefd,undefd,unexp)|TDec0]):-
870 env_call methodC(HDec,_,_,_,_,_,_),
871 gensym_num(HPid,Num,STID),
872 Num1 is Num + 1,
873 make_dec01(HPid,Num1,TDec,TDec0).
874
875change_temp(HPid,[],Temp2,Temp2):-!.
876change_temp(HPid,[before(N1,N2)|Temp],Temp2,[before(ST1,ST2)|Temp0]):-
877 gensym_num(HPid,N1,ST1),
878 gensym_num(HPid,N2,ST2),
879 change_temp(HPid,Temp,Temp2,Temp0),!.
882
885make_tpnodes(Pre,Post, Statics):-
886 flag(opCounter,Num,Num),
887 Num>=1000,
888 env_retractall(tp_goal(_,_,_)),
889 env_retractall(related_op(_,_)),
890 env_assert(tp_goal(Pre,Post,Statics)),
891 env_assert(tp_node(init,Pre,Statics,from(init),0,[])),!.
892
893make_tpnodes(Pre,Post, Statics):-
894 env_retractall(tp_goal(_,_,_)),
895 env_retractall(related_op(_,_)),
896 env_assert(tp_goal(Pre,Post,Statics)),
897 assert_goal_related_init(Pre,Post,Statics),
898 env_assert(op_score(goal,0)),
899 find_all_related_goals(Pre,Statics,1,N),
901 env_assert(tp_node(init,Pre,Statics,from(init),0,[])),!.
902
904
905
906
908
909fwsearch(TN,State):- fwsearch0(fwsearch,1000, TN,State).
911
912
913fwsearch0(Caller, D, TN,State):-fwsearch0_0(Caller, D, TN,State).
914
915fwsearch0_0(_,_, TN,State):-
916 env_retract(solved_node(_,step(HP,Name,Pre,State,exp(TN)))).
917fwsearch0_0(_Caller, D, TN,State):-
918 var(TN),var(State),!,
919 D > 4, D2 is D-4,
920 fwsearch1(var_fwsearch0, D2, TN,State).
921fwsearch0_0(_Caller, D, TN,State):-
922 D > 0, D2 is D-1,
923 fwsearch1(fwsearch0, D2, TN,State).
924
925fwsearch1(_Caller, D, TN,State):-
926 select_tnode(tp_node(TP,Pre,Statics,from(PR),Score,Steps)),
927 env_assert(closed_node(TP,Pre,Statics,from(PR),Score,Steps)),
928 expand_node(TP,OP,Statics,Statics1,Pre,Post,from(PR),Steps,Steps1),
929 assert_tnode(TP,OP,PR,Score1,Post,Statics1,Steps1),
930 env_call solved_node(_,_), 931 fwsearch0(fwsearch1, D, TN,State).
932fwsearch1(_Caller, D ,TN,State):-
933 env_call(tp_node(TP,Pre,Statics,from(PR),Score,Steps)),
934 fwsearch0(tp_node, D ,TN,State).
935
936
937
938clean_temp_nodes_was_wrong:-
939 940 env_retractall(related_op(_,_)),
941 942 env_retractall(tp_goal(_,_,_)),!.
943
944clean_temp_nodes:-
945 clean_temp_nodes_was_wrong,
946 env_retractall(goal_related(_,_,_)),
947 env_retractall(goal_related_search(_)),
948 env_retractall(op_score(_,_)),
949 env_retractall(solved_node(_,_)),
950 env_retractall(current_num(tp,_)),
951 env_retractall(tp_node(_,_,_,_,_,_)),
952
953
954 env_retractall(closed_node(_,_,_,_,_,_)),!.
955
958expand_node(TP,done,Statics,Statics,Pre,Pre,from(PR),List,List):-
959 env_call tp_goal(_,Goal,_),
960 state_achieved(Goal,Pre,Statics),!.
961expand_node(TP,TN,Statics,Statics1,Pre,State,from(PR),List,List1):-
962 expand_node1(TN,Statics,Statics1,Pre,State,from(PR),List,List1).
963
965expand_node1(TN,Statics,Statics1,Pre,State,from(PR),List,List1):-
966 env_call tp_goal(_,Goal,_),
967 direct_expand(HP,TN,achieve(Goal),Pre,Goal,State,Statics,Statics1),
969 append_dcut(List,[step(HP,achieve(Goal),Pre,State,exp(TN))],List1),!.
973direct_expand(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
974 dir_apply_method(TN,HPid,ACH,Pre,Post,State,Statics,Statics1),!.
975
977expand_node1(ID,Statics,Statics,Pre,State,from(PR),List,List1):-
978 find_related_op(Pre,[],OPls),
979 member(ID,OPls),
980 env_call gOperator(ID,_,OP),
981 apply_ground_op(OP,Pre,State,List,List1).
982expand_node1(OP,Statics,Statics1,Pre,State,from(PR),List,List1):-
983 not(env_call goal_related(_,_,_)),
984 env_call operatorC(OP,Pre0,Post0,Cond,ST),
985 apply_unground_op(OP,Pre0,Post0,Cond,ST,Statics,Statics1,Pre,State,List,List1).
986
987apply_ground_op(operator(OP,Prev,Nec,Cond),Pre,State,List,List1):-
988 state_achieved(Prev,Pre),
989 nec_state_change(Pre,Nec,State2),
990 cond_state_change(State2,Cond,State),
991 gensym_special(hp,HP),
992 append_dcut(List,[step(HP,OP,Pre,State,exp(OP))],List1),
993 incr_op_num,!.
994
995apply_unground_op(OP,Pre0,Post0,Cond,ST,Statics,Statics1,Pre,State,List,List1):-
996 statics_append(ST,Statics,Statics2),
997 state_achieved(Pre0,Pre,Statics2),
998 state_change(Pre,Pre0,Post0,State2),
999 cond_state_change(State2,Cond,State),
1000 statics_consist_instance(ST),
1001 remove_unneed(Statics2,[],Statics1),
1002 gensym_special(hp,HP),
1003 append_dcut(List,[step(HP,OP,Pre,State,exp(OP))],List1),
1004 incr_op_num.
1005
1006find_related_op([],Ops1,Ops):-
1007 remove_dup(Ops1,[],Ops),!.
1008find_related_op([Head|Pre],List,Ops):-
1009 setof(OPls,Head^Level^env_call(goal_related(Head,OPls,Level)),OPs0),
1010 flatten(OPs0,[],OPs1),
1011 append_dcut(List,OPs1,List1),
1012 find_related_op(Pre,List1,Ops),!.
1013find_related_op([Head|Pre],List,Ops):-
1014 find_related_op(Pre,List,Ops),!.
1015
1016
1017
1046
1047select_tnode(tp_node(TPid,Pre,Statics,Parents,Score,Dec)) :-
1048 lowest_tnode_FORCED,
1049 nb_getval(tnodeLOWEST,tp_node(TPid,Pre,Statics,Parents,Score,Dec)),
1050 env_retract(tp_node(TPid,Pre,Statics,Parents,Score,Dec)),!,
1051 nb_delete(tnodeLOWEST).
1053
1054random_tnode_FREE :- nb_current(tnodeLOWEST,_)->true;random_tnode_FORCED.
1055
1056random_tnode_FORCED:-
1057 env_call(tp_node(HPid,Pre,Statics,Parents,Score,Dec)),
1058 nb_setval(tnodeLOWEST,tp_node(HPid,Pre,Statics,Parents,Score,Dec)),!.
1059
1060lowest_tnode_FORCED:-
1061 random_tnode_FORCED,
1062 ignore(((
1063 nb_getval(tnodeLOWEST,tp_node(_,_,_,_,SScore,_)),
1064 env_call(tp_node(HPid,Pre,Statics,Parents,Score,Dec)),
1065 Score < SScore,
1066 nb_setval(tnodeLOWEST,tp_node(HPid,Pre,Statics,Parents,Score,Dec)),
1067 fail))).
1068
1069
1072assert_tnode(TP,OP,PR,Score,Post,Statics,[]):-!.
1073assert_tnode(TP,OP,PR,Score,Post,Statics,Steps):-
1074 env_call tp_goal(Pre,Goal,Statics1),
1075 state_achieved(Goal,Post,Statics),
1076 combine_exp_steps(Post,Steps,OneStep),
1077 env_assert(solved_node(Statics,OneStep)),!.
1078assert_tnode(TP,OP,PR,Score,Post,Statics,Steps):-
1079 existing_node(Post,Statics),!.
1080assert_tnode(TP,OP,PR,Score,Post,Statics,Steps):-
1081 get_score(PR,Post,Steps,Score),
1084 gensym_special(tp,TP1),
1087 env_assert(tp_node(TP1,Post,Statics,from(TP),Score,Steps)),!.
1088
1089
1090
1091assert_tnode(TP,OP,PR,Score,Post,Statics,Steps):-
1092 get_score(PR,Post,Steps,Score),
1095 gensym_special(tp,TP1),
1098 TNODEFULL = tp_node(TP1,Post,Statics,from(TP),Score,Steps),
1099 ((nb_current(tnodeLOWEST,tp_node(_,_,_,_,SScore,_)), Score < SScore)
1100 ->
1101 true;
1102 (nb_setval(tnodeLOWEST,TNODEFULL),
1103 env_shadow(asserta,TNODEFULL))),!,
1104 assert_tnode_into_sorted_list(Score,Post,TNODEFULL).
1105
1106assert_tnode_into_sorted_list(Score,Post,TNODEFULL):-
1107 (nb_getval(tnodeSORTED,NODES) ->
1108 (NODES = [] ->
1109 nb_setval(tnodeSORTED,[TNODEFULL]);
1110 ((
1111 [FrontNode|RightNodes] = NODES,
1112 must(FrontNode = tp_node(_,FPost,_,_,FScore,_)),
1113 (Post == FPost -> true ;
1114 ((FScore > Score)
1115 -> nb_setval(tnodeSORTED,[TNODEFULL|NODES]) ;
1116 insert_to_list(Score,Post,TNODEFULL,RightNodes,NODES))))));
1117
1118 nb_setval(tnodeSORTED,[TNODEFULL])).
1119
1120
1121insert_to_list(_,_,TNODEFULL,[],NODES):-nb_setarg(2,NODES,[TNODEFULL]),!.
1122insert_to_list(Score,Post,TNODEFULL,AllNodes,NODES):-
1123 AllNodes = [FrontNode|RightNodes],
1124 FrontNode = tp_node(_,FPost,_,_,FScore,_),!,
1125 (Post = FPost -> true ;
1126 ((FScore > Score) -> ( env_asserta(TNODEFULL),nb_setarg(2,NODES,[TNODEFULL|AllNodes])) ;
1127 (insert_to_list(Score,Post,TNODEFULL,RightNodes,AllNodes)))).
1128insert_to_list(Score,Post,TNODEFULL,AllNodes,NODES):-
1129 AllNodes = [_|RightNodes],
1130 insert_to_list(Score,Post,TNODEFULL,RightNodes,AllNodes).
1131
1132
1133combine_exp_steps(Post,Steps,step(HP,achieve(Goal),Pre,Post,exp(TN))):-
1134 env_call tp_goal(Pre,Goal,Statics),
1135 get_action_list(Steps,[],ACTls),
1136 make_temp(ACTls,[],Temp),
1137 gensym_special(hp,HP),
1138 make_tn(TN,achieve(Goal),Pre,Post,Temp,Steps),!.
1139
1141get_action_list([],ACTls,ACTls):-!.
1142get_action_list([step(HP,_,_,_,_)|Steps],List,ACTls):-
1143 append_dcut(List,[HP],List1),
1144 get_action_list(Steps,List1,ACTls),!.
1145
1146make_temp([HP|[]],Temp,Temp):-!.
1147make_temp([HP1|[HP2|Rest]],List,Temp):-
1148 append_dcut(List,[before(HP1,HP2)],List1),
1149 make_temp([HP2|Rest],List,Temp),!.
1150
1151existing_node(Post,_Statics):-
1152 env_call(tp_node(_,Post,_,_,_,_)).
1153existing_node(Post,_Statics):-
1154 env_call closed_node(_,Post,_,_,_,_).
1156
1157assert_goal_related_init(Pre,[],Statics):-!.
1160
1161
1162assert_goal_related_init(Pre,[se(Sort,Obj,SE)|Post],Statics):-
1163 ground(Obj),
1164 env_call is_of_primitive_sort(Obj,SortP),
1165 env_assert(goal_related(se(SortP,Obj,SE),[],0)),
1166 assert_goal_related_init(Pre,Post,Statics),!.
1167assert_goal_related_init(Pre,[se(Sort,Obj,SE)|Post],Statics):-
1168 assert_related_goals_varible(Sort,Obj,SE,goal,0),
1169 assert_goal_related_init(Pre,Post,Statics),!.
1170
1173find_all_related_goals(Pre,Statics,N,N):-
1174 get_all_state(States),
1175 all_found(States,Pre,Statics),
1176 env_assert(goal_related_search(succ)),
1177 find_all_related_goals_final(Statics,N),!.
1178find_all_related_goals(Pre,Statics,I,N):-
1179 I1 is I-1,
1180 env_call goal_related(_,_,I1),
1181 find_related_goal(Statics,I1,I),
1182 I2 is I+1,
1183 Key is random(10),
1184 find_all_related_goals(Pre,Statics,I2,N),!.
1185find_all_related_goals(Pre,Statics,N,N):-
1186 not(env_call goal_related(_,_,N)),
1187 env_assert(goal_related_search(fail)),
1188 write('related goal search failed'),
1189 env_retractall(goal_related(_,_,_)),!.
1190 1191 1192
1193
1196find_all_related_goals_final(Statics,N):-
1197 N1 is N-1,
1198 env_call goal_related(Pre,_,N1),
1199 find_related_goal(Statics,N1,N),!.
1200find_all_related_goals_final(Statics,N):-!.
1201
1203get_all_state(States):-
1204 setof(Goal, Statics^Level^OP^(env_call goal_related(Goal,OP,Level)),States11),
1205 put_one_obj_together(States11,[],States),!.
1206
1207put_one_obj_together([],States,States):-!.
1208put_one_obj_together([se(Sort,Obj,ST)|States1],List,States):-
1209 put_one_obj_together1(se(Sort,Obj,ST),List,List1),
1210 put_one_obj_together(States1,List1,States),!.
1211
1212put_one_obj_together1(se(Sort,Obj,ST),[],[se(Sort,Obj,ST)]):-!.
1213put_one_obj_together1(se(Sort,Obj,ST),[se(Sort,Obj,ST00)|List],[se(Sort,Obj,ST1)|List]):-
1214 set_append_e(ST,ST00,ST1),!.
1215put_one_obj_together1(se(Sort,Obj,ST),[se(Sort1,Obj1,ST1)|List],[se(Sort1,Obj1,ST1)|List1]):-
1216 Obj\==Obj1,
1217 put_one_obj_together1(se(Sort,Obj,ST),List,List1),!.
1218
1220all_found([],States,Statics):-!.
1221all_found([se(Sort,Obj,ST)|States],Pre,Statics):-
1222 member(se(Sort,Obj,SPre),Pre),
1223 subtract(SPre,ST,Diff),
1224 isemptylist(Diff),
1225 all_found(States,Pre,Statics),!.
1228find_related_goal(Statics,I1,I):-
1229 env_call gOperator(OPID,ID,operator(Name,Prev,Nec,Cond)),
1230 find_related_goal_nec(OPID,Name,Prev,Nec,Statics,I1,I),
1231 find_related_goal_cond(OPID,Name,Prev,Nec,Cond,Statics,I1,I),
1232 fail.
1233find_related_goal(Statics,I1,I).
1234
1235find_related_goal_nec(ID,Name,Prev,Nec,Statics,I1,I):-
1236 env_call goal_related(se(Sort,Obj,SE),Ops,I1),
1237 member(sc(Sort,Obj,Lhs=>Rhs),Nec),
1238 state_match(Sort,Obj,SE,Rhs),
1239 statics_consist(Statics),
1241 assert_goal_related(Prev,Nec,ID,I).
1242
1243find_related_goal_cond(ID,Name,Prev,Nec,[],Statics,I1,I):-
1244 !.
1245find_related_goal_cond(ID,Name,Prev,Nec,Cond,Statics,I1,I):-
1246 env_call goal_related(se(Sort,Obj,SE),Ops,I1),
1247 member(sc(Sort0,Obj,LHS=>RHS),Cond),
1248 env_call is_of_sort(Obj,Sort0),
1249 env_call is_of_sort(Obj,Sort), 1250 filterInvars(LHS,LInVars,LIsOfSorts,LNEs,FLHS),
1251 filterInvars(RHS,RInVars,RIsOfSorts,RNEs,FRHS),
1252 can_achieve_g([se(Sort,Obj,FRHS)],[se(Sort,Obj,SE)],Statics),
1253 statics_consist(Statics),
1254 checkInVars(LInVars),
1255 checkInVars(RInVars),
1256 checkIsOfSorts(LIsOfSorts),
1257 checkIsOfSorts(RIsOfSorts),
1258 obeysNEs(LNEs),
1259 obeysNEs(RNEs),
1261 assert_goal_related(Prev,[sc(Sort,Obj,FLHS=>FRHS)|Nec],ID,I).
1262
1263
1265filterInvars([],[],[],[],[]):-!.
1266filterInvars([is_of_sort(A,B)|State],InVars,[is_of_sort(A,B)|IsOfSorts],NEs,FState):-
1267 !,
1268 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1269filterInvars([ne(A,B)|State],InVars,IsOfSorts,[ne(A,B)|NEs],FState):- dif(A,B),
1270 !,
1271 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1272filterInvars([is_of_primitive_sort(A,B)|State],InVars,[is_of_sort(A,B)|IsOfSorts],NEs,FState):-
1273 !,
1274 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1275filterInvars([Pred|State],[Pred|InVars],IsOfSorts,NEs,FState):-
1276 functor(Pred,FF,NN),
1277 functor(Pred1,FF,NN),
1278 env_call(atomic_invariantsC(Atom)),
1279 member(Pred1,Atom),!,
1280 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1281filterInvars([Pred|State],InVars,IsOfSorts,NEs,[Pred|FState]):-
1282 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1283
1285filterNes([],[],[]):-!.
1286filterNes([ne(A,B)|State],[ne(A,B)|NEs],FState):- dif:dif(A,B),
1287 !,
1288 filterNes(State,NEs,FState).
1289filterNes([Pred|State],NEs,[Pred|FState]):-
1290 filterNes(State,NEs,FState).
1291
1292assert_related_op(OP,I):-
1293 env_call related_op(OP,_),!.
1294assert_related_op(OP,I):-
1295 env_asserta(related_op(OP,I)),!.
1296
1298can_achieve_g([],State2,Statics):-!.
1299can_achieve_g(State1,State2,Statics):-
1300 can_achieve_g(State1,State2),
1301 statics_consist(Statics).
1302
1303can_achieve_g([se(Sort,Obj,ST1)|State1],[se(Sort,Obj,ST2)]):-
1304 state_match(Sort,Obj,ST2,ST1).
1305can_achieve_g([Head|State1],State2):-
1306 can_achieve_g(State1,State2).
1307
1309assert_goal_related(Prev,Nec,OP,I):-
1310 assert_goal_related1(Prev,OP,I),!,
1311 assert_goal_related1(Nec,OP,I).
1312
1313assert_goal_related1([],Op,I):-!.
1314assert_goal_related1([se(Sort,Obj,SE)|Prev],Op,I):-
1315 assert_goal_related2(se(Sort,Obj,SE),Op,I),
1316 assert_goal_related1(Prev,Op,I),!.
1317assert_goal_related1([sc(Sort,Obj,LH=>RH)|Nec],Op,I):-
1318 ground(Obj), 1319 env_call is_of_primitive_sort(Obj,PSort),!,
1320 assert_goal_related2(se(PSort,Obj,LH),Op,I),
1321 assert_goal_related1(Nec,Op,I).
1322assert_goal_related1([sc(Sort,Obj,LH=>RH)|Nec],Op,I):-
1323 var(Obj),
1324 assert_related_goals_varible(Sort,Obj,LH,Op,I),
1325 assert_goal_related1(Nec,Op,I).
1326
1327assert_goal_related2(se(Sort,Obj,SE),goal,I):-
1328 env_assert(goal_related(se(Sort,Obj,SE),[],I)),!.
1329assert_goal_related2(se(Sort,Obj,SE),Op,I):-
1330 env_call goal_related(se(Sort,Obj,SE1),Ops,I),
1331 not(is_diff(SE,SE1)),
1332 env_retract(goal_related(se(Sort,Obj,SE),Ops,I)),
1333 env_assert(goal_related(se(Sort,Obj,SE),[Op|Ops],I)),!.
1334assert_goal_related2(se(Sort,Obj,SE),Op,I):-
1335 env_assert(goal_related(se(Sort,Obj,SE),[Op],I)),!.
1336
1337assert_related_goals_varible(Sort,Obj,SE,Op,I):-
1338 find_prim_sort(Sort,PSorts),
1339 member(Sort1,PSorts),
1340 assert_goal_related2(se(Sort1,Obj,SE),Op,I),
1341 fail.
1342assert_related_goals_varible(Sort,Obj,SE,Op,I).
1343
1345assert_op_score(OP,OPB):-
1346 env_call op_score(OP,_),!.
1347assert_op_score(OP,OPB):-
1348 env_call op_score(OPB,I),
1349 I1 is I+1,
1350 env_assert(op_score(OP,I1)),!.
1351
1352get_score(PR,Post,Steps,Score):-
1353 env_call tp_goal(Pre,Goal,Statics),
1354 get_distance(Pre,Post,Goal,0,Dis), 1356 get_tnode_length(PR,1,Len),
1360 Score is Dis+Len,!.
1361
1362get_distance(Pre,[],Goal,Dis,Dis):-!.
1363get_distance(Pre,[se(Sort,Obj,SE)|Post],Goal,Dis1,Dis):-
1364 member(se(Sort,Obj,SE0),Goal),
1365 state_match(Sort,Obj,SE0,SE), 1366 get_distance(Pre,Post,Goal,Dis1,Dis),!.
1367get_distance(Pre,[se(Sort,Obj,SE)|Post],Goal,Dis1,Dis):-
1368 env_call goal_related(se(Sort,Obj,SE0),_,Level),
1369 state_match(Sort,Obj,SE0,SE),
1370 Dis2 is Dis1+Level,
1371 get_distance(Pre,Post,Goal,Dis2,Dis),!.
1372get_distance(Pre,[se(Sort,Obj,SE)|Post],Goal,Dis1,Dis):-
1373 member(se(Sort,Obj,SE0),Pre),
1374 state_match(Sort,Obj,SE,SE0), 1375 Dis2 is Dis1+1,
1376 get_distance(Pre,Post,Goal,Dis2,Dis),!.
1377get_distance(Pre,[se(Sort,Obj,SE)|Post],Goal,Dis1,Dis):-
1378 Dis2 is Dis1+100,
1379 get_distance(Pre,Post,Goal,Dis2,Dis),!.
1380
1381get_tnode_length(init,Len,Len):-!.
1382get_tnode_length(TP,Len1,Len):-
1383 env_call closed_node(TP,_,_,from(PR),_,_),
1384 Len2 is Len1+1,
1385 get_tnode_length(PR,Len2,Len),!.
1386
1387
1392find_relate_state:-
1393 operatorC(A,Pre,Post,Cond,ST),
1394 assert_related_states(A,Pre,Post,Cond,ST),
1395 fail.
1396find_relate_state.
1397
1398assert_related_states(A,Pre,Post,Cond,ST):-
1399 assert_related_states1(A,Pre,Post,ST),
1400 assert_related_states2(A,Pre,Cond,ST).
1403assert_related_states1(A,Pre,[],ST):-!.
1405assert_related_states1(A,Pre,[se(Sort,Obj,SE)|Post],ST):-
1406 u_mem_cut(se(Sort,Obj,SE),Pre),
1407 assert_related_states1(A,Pre,Post,ST),!.
1409assert_related_states1(A,Pre,[se(Sort,Obj,SE)|Post],ST):-
1410 assert(produce(se(Sort,Obj,SE),A,Pre,ST)),
1411 assert_related_states1(A,Pre,Post,ST),!.
1412
1415assert_related_states2(A,Pre,SC,ST):-
1416 make_sc_primitive(SC,PSC),
1417 assert_related_states21(A,Pre,PSC,ST).
1418
1419assert_related_states21(A,Pre,[],ST):-!.
1420assert_related_states21(A,Pre,[sc(Sort,Obj,SE=>SS)|Trans],ST):-
1421 rem_statics([se(Sort,Obj,SE)],[se(Sort,Obj,SER)],St1),
1422 rem_statics([se(Sort,Obj,SS)],[se(Sort,Obj,SSR)],St2),
1423 append_dcut(ST,St1,ST1),
1424 append_dcut(ST1,St2,ST21),
1425 remove_unneed(ST21,[],ST2),
1426 append_dcut(Pre,[se(Sort,Obj,SER)],Pre1),
1427 env_assert(produce(se(Sort,Obj,SSR),A,Pre1,ST2)),
1428 must(produce(se(Sort,Obj,SSR),A,Pre1,ST2)),
1429 assert_related_states21(A,Pre,Trans,ST),!.
1430
1435remove_temp([],HP1,List,List):-!.
1436remove_temp([before(HP1,HP2)|Temp],HP1,List,Temp1):-
1437 remove_temp_before(List,before(HP1,HP2),List2),
1438 remove_temp(Temp,HP1,List2,Temp1),!.
1439remove_temp([before(HP2,HP1)|Temp],HP1,List,Temp1):-
1440 remove_temp_after(List,before(HP2,HP1),List2),
1441 remove_temp(Temp,HP1,List2,Temp1),!.
1442remove_temp([before(HPX,HPY)|Temp],HP1,List,Temp1):-
1443 remove_temp(Temp,HP1,List,Temp1),!.
1444
1446remove_temp_before([],before(HP1,HP2),[]):-!.
1447remove_temp_before([before(HP1,HP2)|T],before(HP1,HP2),T1):-
1448 remove_temp_before(T,before(HP1,HP2),T1),!.
1449remove_temp_before([before(HP3,HP1)|T],before(HP1,HP2),[before(HP3,HP2)|T1]):-
1450 remove_temp_before(T,before(HP1,HP2),T1),!.
1451remove_temp_before([before(HPX,HPY)|T],before(HP1,HP2),[before(HPX,HPY)|T1]):-
1452 remove_temp_before(T,before(HP1,HP2),T1),!.
1454remove_temp_after([],before(HP1,HP2),[]):-!.
1455remove_temp_after([before(HP2,HP1)|T],before(HP2,HP1),T1):-
1456 remove_temp_after(T,before(HP2,HP1),T1),!.
1457remove_temp_after([before(HP1,HP3)|T],before(HP2,HP1),[before(HP2,HP3)|T1]):-
1458 remove_temp_after(T,before(HP2,HP1),T1),!.
1459remove_temp_after([before(HPX,HPY)|T],before(HP2,HP1),[before(HPX,HPY)|T1]):-
1460 remove_temp_after(T,before(HP2,HP1),T1),!.
1461
1462remove_dec(HPid,[],[]):-!.
1463remove_dec(HPid,[step(HPid,_,_,_,_)|Dec],Dec1):-
1464 remove_dec(HPid,Dec,Dec1),!.
1465remove_dec(HPid,[step(A,B,C,D,F)|Dec],[step(A,B,C,D,F)|Dec1]):-
1466 remove_dec(HPid,Dec,Dec1),!.
1467
1471state_achieved(undefd,State,Statics):-!.
1472state_achieved([],State2,Statics):-!.
1473state_achieved(State1,State2,Statics):-
1474 state_achieved(State1,State2),
1475 statics_consist(Statics).
1476
1477state_achieved(undefd,State):-!.
1478state_achieved([],State2).
1479state_achieved([se(Sort,Obj,ST1)|State1],State2):-
1480 member(se(Sort,Obj,ST2),State2),
1481 env_call is_of_sort(Obj,Sort),
1482 state_match(Sort,Obj,ST1,ST2),
1483 list_take(State2,[se(Sort,Obj,ST2)],State21),
1484 state_achieved(State1,State21).
1485state_achieved([se(Sort,Obj,ST1)|State1],State2):-
1486 not(member(se(Sort,Obj,ST2),State2)),
1487 state_achieved(State1,State2),!.
1488
1492state_match(Sort,Obj,ST,ST1):-
1493 not(is_diff(ST,ST1)),!.
1498state_match(Sort,Obj,ST,ST1):-
1499 is_achieved(ST,ST1),
1500 env_call(gsubstate_classes(Sort,Obj,Substateclasses)),
1501 not(in_different_states(ST,ST1,Substateclasses)),!.
1504state_match(Sort,Obj,ST,ST1):-
1505 not(is_achieved(ST,ST1)),
1506 set_append(ST,ST1,ST0),
1507 env_call(gsubstate_classes(Sort,Obj,Substateclasses)),
1508 in_same_sub_states(ST0,Substateclasses),!.
1509
1511in_same_sub_states(ST0,[State|SCls]):-
1512 is_achieved(ST0,State),!.
1513in_same_sub_states(ST0, [State|SCls]):-
1514 in_same_sub_states(ST0,SCls),!.
1515
1517in_different_states(ST,ST1, [State|SCls]):-
1518 max_member(ST,Substateclasses,MSub, _),
1519 max_member(ST1,Substateclasses,MSub1, _),
1520 MSub\==MSub1,!.
1521
1522max_member(State, Stateclass, MSub, Others):-
1523 max_member1(State, Stateclass, 0, [],MSub),
1524 subtract(State,MSub,Others),!.
1525
1527max_member1(State, [], Num, MSub, MSub):-!.
1529max_member1(State, [State1|SCls], Num, MSub1, MSub):-
1530 same_items(State1,State,MSSt),
1531 length(MSSt,Len),
1532 Len > Num,
1533 max_member1(State, SCls, Len, MSSt, MSub),!.
1534max_member1(State, [State1|SCls], Num, MSub1,MSub):-
1535 max_member1(State, SCls, Num, MSub1,MSub),!.
1536
1538same_items([],List2,[]):-!.
1539same_items([X|List1],List2,[X|Same]):-
1540 member(X,List2),
1541 same_items(List1,List2,Same),!.
1542same_items([X|List1],List2,Same):-
1543 same_items(List1,List2,Same),!.
1544
1545
1547is_achieved([],_):-!.
1548is_achieved([H|T], State) :-
1549 is_statics(H),
1550 is_achieved(T,State),!.
1551is_achieved([H|T], State) :-
1552 member(H,State),
1553 is_achieved(T,State),!.
1554
1556is_statics(ne(A,B)):-!.
1557is_statics(is_of_sort(A,B)):-!.
1558is_statics(is_of_primitive_sort(A,B)):-!.
1559is_statics(Pred):-
1560 functor(Pred,FF,NN),
1561 functor(Pred1,FF,NN),
1562 env_call(atomic_invariantsC(Atom)),
1563 member(Pred1,Atom),!.
1564
1572state_change([],Pre0,Post0,[]):-!.
1573state_change(Pre,[],[],Pre):-!.
1574state_change([se(Sort,Obj,SPre)|Pre],Pre0,Post0,[se(Sort,Obj,STPost)|Post]):-
1575 state_achieved([se(Sort,Obj,SPre)],Pre0),
1576 state_change0(Sort,Obj,SPre,Pre0,Post0,Pre1,Post1,STPost),
1577 state_change(Pre,Pre1,Post1,Post).
1578state_change([se(Sort,Obj,SPre)|Pre],Pre0,Post0,[se(Sort,Obj,STPost)|Post]):-
1579 not(member(se(Sort,Obj,SPre0),Pre0)),
1580 state_change(Pre,Pre1,Post1,Post).
1581
1583state_change0(Sort,Obj,SPre,[],[],[],[],SPre):-!.
1584state_change0(Sort,Obj,SPre,[se(Sort,Obj,SPre0)|Pre0],[se(Sort,Obj,SPost0)|Post0],Pre0,Post0,STPost):-
1585 state_change1(SPre,SPre0,SPost0,STPost).
1586state_change0(Sort,Obj,SPre,[se(Sort1,Obj1,SPre0)|Pre0],[se(Sort1,Obj1,SPost0)|Post0],[se(Sort1,Obj1,SPre0)|Pre1],[se(Sort1,Obj1,SPost0)|Post1],STPost):-
1587 Obj\==Obj1,
1588 state_change0(Sort,Obj,SPre,Pre0,Post0,Pre1,Post1,STPost).
1589
1590state_change1([],SPre0,SPost0,SPost0):-!.
1591state_change1(Pre,[],[],Pre):-!.
1593state_change1([Head|SPre],SPre0,SPost0,STPost):-
1594 member(Head,SPre0),
1595 not(member(Head,SPost0)),
1596 state_change1(SPre,SPre0,SPost0,STPost).
1598state_change1([Head|SPre],SPre0,SPost0,[Head|STPost]):-
1599 not(member(Head,SPre0)),
1600 not(member(Head,SPost0)),
1601 state_change1(SPre,SPre0,SPost0,STPost),!.
1604state_change1([Head|SPre],SPre0,SPost0,STPost):-
1605 member(Head,SPost0),
1606 state_change1(SPre,SPre0,SPost0,STPost).
1607
1609rough_state_change(Pre,[],[],Pre):-!.
1610rough_state_change([],_,_,[]):-!.
1611rough_state_change([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,SS0)|Post]):-
1612 member(se(Sort,Obj,SE0),Pre0),
1613 member(se(Sort,Obj,SS0),Post0),
1614 env_call is_of_sort(Obj,Sort),
1615 state_achieved([se(Sort,Obj,SE0)],[se(Sort,Obj,SE)]),
1616 list_take(Pre0,[se(Sort,Obj,SE0)],Pre01),
1617 list_take(Post0,[se(Sort,Obj,SS0)],Post01),
1618 rough_state_change(Pre,Pre01,Post01,Post),!.
1619rough_state_change([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,SE)|Post]):-
1620 rough_state_change(Pre,Pre0,Post0,Post),!.
1621
1623state_changeG([],Pre0,Post0,[]):-!.
1624state_changeG([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,RHS)|State]):-
1625 member(se(Sort,Obj,LHS),Pre0),
1626 member(se(Sort,Obj,RHS),Post0),
1627 state_match(Sort,Obj,SE,LHS),
1628 state_changeG(Pre,Pre0,Post0,State),!.
1629state_changeG([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,SE)|State]):-
1630 not(member(se(Sort,Obj,LHS),Pre0)),
1631 state_changeG(Pre,Pre0,Post0,State),!.
1632
1633find_lower_sort(Sort,Sort,Sort):-!.
1634find_lower_sort(Sort,Sort1,Sort1):-
1635 subsorts(Sort,Sortls),
1636 member(Sort1,Sortls),!.
1637find_lower_sort(Sort,Sort1,Sort):-
1638 subsorts(Sort1,Sortls),
1639 member(Sort,Sortls),!.
1645nec_state_change([],Nec,[]):-!.
1646nec_state_change([se(Sort,Obj,SE)|Pre],Nec,[se(Sort,Obj,Post)|State]):-
1647 member(sc(Sort,Obj,Lhs=>Rhs),Nec),
1648 state_match(Sort,Obj,Lhs,SE),
1649 state_change1(SE,Lhs,Rhs,Post),
1650 nec_state_change(Pre,Nec,State),!.
1651nec_state_change([se(Sort,Obj,SE)|Pre],Nec,[se(Sort,Obj,SE)|State]):-
1652 not(member(sc(Sort,Obj,Lhs=>Rhs),Nec)),
1653 nec_state_change(Pre,Nec,State),!.
1658cond_state_change([],Cond,[]):-!.
1659cond_state_change(State,[],State):-!.
1660cond_state_change([se(Sort,Obj,SE)|Pre],Cond,[NewSS|State]):-
1661 member(sc(Sort1,Obj1,SE0=>SS0),Cond),
1663 subsorts(Sort1,Subsorts),
1664 member(Sort,Subsorts),
1665 copy_states(se(Sort1,Obj1,SE0),se(Sort,Obj,SE2)),
1666 copy_states(se(Sort1,Obj1,SS0),se(Sort,Obj,SS2)),
1668 filterInvars(SE2,LInVars,LIsOfSorts,LNEs,FSE),
1669 filterInvars(SS2,RInVars,RIsOfSorts,RNEs,FSS),
1670 state_match(Sort,Obj,SE,FSE),
1671 state_change([se(Sort,Obj,SE)],[se(Sort,Obj,FSE)],
1672 [se(Sort,Obj,FSS)],[NewSS]),
1673 checkInVars(LInVars),
1674 checkInVars(RInVars),
1675 checkIsOfSorts(LIsOfSorts),
1676 checkIsOfSorts(RIsOfSorts),
1677 obeysNEs(LNEs),
1678 obeysNEs(RNEs),
1679 cond_state_change(Pre,Cond,State),!.
1680cond_state_change([se(Sort,Obj,SE)|Pre],Cond,[se(Sort,Obj,SE)|State]):-
1681 cond_state_change(Pre,Cond,State),!.
1682
1684copy_states(se(Sort1,Obj1,SE0),se(Sort,Obj,SE2)):-
1685 copy_states1(Obj1,SE0,Obj,SE2),!.
1686copy_states1(Obj1,[],Obj,[]):-!.
1687copy_states1(Obj1,[Pred|SE0],Obj,[Pred2|SE2]):-
1688 functor(Pred,FF,NN),
1689 functor(Pred2,FF,NN),
1690 Pred=..[Name|Vars],
1691 Pred2=..[Name|Vars2],
1692 copy_pred(Obj1,Obj,Vars,Vars2),
1693 copy_states1(Obj1,SE0,Obj,SE2),!.
1694
1695copy_pred(Obj1,Obj,[],[]):-!.
1696copy_pred(Obj1,Obj,[Var|Vars],[Var2|Vars2]):-
1697 Obj1==Var,
1698 Var2=Obj,
1699 copy_pred(Obj1,Obj,Vars,Vars2),!.
1700copy_pred(Obj1,Obj,[Var|Vars],[Var|Vars2]):-
1701 copy_pred(Obj1,Obj,Vars,Vars2),!.
1704all_achieved(undefd,Statics,List2):-!.
1705all_achieved([],Statics,List2):-!.
1706all_achieved(List1,Statics,List2):-
1707 all_achieved(List1,List2),
1708 statics_consist(Statics).
1709
1710all_achieved([],List2).
1711all_achieved([se(Sort,Obj,SL)|List1],List2):-
1712 member(se(Sort1,Obj,SR),List2),
1713 env_call is_of_sort(Obj,Sort1),
1714 env_call is_of_sort(Obj,Sort),
1715 env_call is_of_primitive_sort(Obj,PSort),
1716 state_match(PSort,Obj,SL,SR),
1717 all_achieved(List1,List2).
1720may_achieved(undefd,Statics,Post):-!.
1721may_achieved([],Statics,Post):-!.
1722may_achieved(Pre,Statics,Post):-
1723 may_achieved(Pre,Post),
1724 statics_consist(Statics),!.
1725may_achieved([],Post).
1726may_achieved([se(Sort,Obj,SL)|Pre],Post):-
1727 member(se(Sort1,Obj,SR),Post),
1728 env_call is_of_sort(Obj,Sort1),
1729 env_call is_of_sort(Obj,Sort),
1730 env_call is_of_primitive_sort(Obj,PSort),
1731 state_may_achieved(PSort,Obj,SL,SR),
1732 may_achieved(Pre,Post),!.
1733
1735state_may_achieved(Sort,Obj,[],ST2):-!.
1736state_may_achieved(Sort,Obj,ST1,ST2):-
1737 is_achieved(ST1,ST2),!.
1741post_instant(Post0,Cond,Statics,undefd):-!.
1742post_instant(Post0,Cond,Statics,[]):-!.
1743post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1744 member(se(Sort,Obj,SE0),Post0),
1745 statics_consist(Statics).
1746post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1747 member(sc(Sort,Obj,SE1=>SS),Cond),
1748 statics_consist(Statics).
1749post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1750 member(sc(Sort0,Obj,SE1=>SS),Cond),
1751 not(env_call(objectsC(Sort0,_))),
1752 subsorts(Sort0,Sortls),
1753 not(not(member(Sort,Sortls))),
1754 statics_consist(Statics).
1755post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1756 post_instant(Post0,Cond,Statics,Post),!.
1757
1760statics_consist([]):-!.
1761statics_consist(Statics):-
1762 get_invariants(Invs),
1763 statics_consist(Invs,Statics),!.
1764statics_consist(Invs,[]):-!.
1765statics_consist(Invs,[ne(A,B)|Statics]):- dif(A,B),
1766 not(A==B),!,
1767 statics_consist(Invs,Statics).
1768statics_consist(Invs,[is_of_sort(Obj,Sort)|Statics]):-
1769 not(not(env_call is_of_sort(Obj,Sort))),!,
1770 statics_consist(Invs,Statics).
1771statics_consist(Invs,[is_of_primitive_sort(Obj,Sort)|Statics]):-
1772 not(not( env_call is_of_primitive_sort(Obj,Sort))),!,
1773 statics_consist(Invs,Statics).
1774statics_consist(Invs,[Pred|Statics]):-
1775 pred_member(Pred,Invs),!,
1776 statics_consist(Invs,Statics).
1777
1779statics_consist_instance([]):-!.
1780statics_consist_instance(Statics):-
1781 get_invariants(Invs),
1782 statics_consist_instance(Invs,Statics).
1783
1784statics_consist_instance(Invs,[]):-!.
1785statics_consist_instance(Invs,[is_of_sort(Obj,Sort)|Atom]):-
1786 ground(Obj),
1787 env_call is_of_sort(Obj,Sort),!,
1788 statics_consist_instance(Invs,Atom).
1789statics_consist_instance(Invs,[is_of_sort(Obj,Sort)|Atom]):-
1790 var(Obj),
1791 env_call is_of_sort(Obj,Sort),
1792 statics_consist_instance(Invs,Atom).
1793statics_consist_instance(Invs,[is_of_primitive_sort(Obj,Sort)|Atom]):-
1794 ground(Obj),
1795 env_call is_of_primitive_sort(Obj,Sort),!,
1796 statics_consist_instance(Invs,Atom).
1797statics_consist_instance(Invs,[is_of_primitive_sort(Obj,Sort)|Atom]):-
1798 var(Obj),
1799 env_call is_of_primitive_sort(Obj,Sort),
1800 statics_consist_instance(Invs,Atom).
1801statics_consist_instance(Invs,[ne_back(A,B)|Atom]):-
1802 A\==B, dif(A,B),
1803 statics_consist_instance(Invs,Atom).
1804statics_consist_instance(Invs,[ne(A,B)|Atom]):-
1805 append_dcut(Atom,[ne_back(A,B)],Atom1),!,
1806 statics_consist_instance(Invs,Atom1).
1807statics_consist_instance(Invs,[Pred|Atom]):-
1808 ground(Pred),
1809 member(Pred,Invs),!,
1810 statics_consist_instance(Invs,Atom).
1811statics_consist_instance(Invs,[Pred|Atom]):-
1812 not(ground(Pred)),
1813 member(Pred,Invs),
1814 statics_consist_instance(Invs,Atom).
1815
1816
1817
1822
1823make_problem_into_node(I,goal(L,TM,STATS), NN) :-
1824 must_det_l(( make_problem_up(L, STEPS),
1825 make_num_hp(TM,Temp),
1826 sort_steps(STEPS,Temp,STEPS1),
1827 make_ss_to_se(I,I_Pre),
1828 NN = node(root,I_Pre,STEPS1 ,Temp, STATS))),!.
1829make_problem_into_node(I,L, NN) :-
1830 must_det_l(( make_problem_up([achieve(L)], STEPS),
1831 make_num_hp(TM,Temp),
1832 sort_steps(STEPS,Temp,STEPS1),
1833 make_ss_to_se(I,I_Pre),
1834 NN = node(root,I_Pre,STEPS1 ,Temp, STATS))),!.
1835
1837make_problem_up([],[]):-!.
1838make_problem_up([achieve(L)|R],[step(HP,achieve(L1),undefd,[L1],unexp)|RS]):-
1839 1840 make_ss_to_se([L],[L1]),
1841 gensym_special(hp,HP),
1842 make_problem_up(R, RS),!.
1843make_problem_up([achieve(L)|R],[step(HP,achieve(L1),undefd,L1,unexp)|RS]):-
1844 1845 make_ss_to_se(L,L1),
1846 gensym_special(hp,HP),
1847 make_problem_up(R, RS),!.
1848make_problem_up([O|R],[step(HP,O,undefd,undefd,unexp)|RS]):-
1849 env_call methodC(O,Pre,Post,Statics1,Temp,ACH,Dec),
1850 gensym_special(hp,HP),
1851 make_problem_up(R, RS),!.
1852make_problem_up([O|R],
1853 [step(HP,O,undefd,undefd,unexp)|RS]):-
1854 env_call operatorC(O,Pre,Post,Cond,Statics1),
1855 gensym_special(hp,HP),
1856 make_problem_up(R, RS),!.
1857
1858make_num_hp([],[]):-!.
1859make_num_hp([before(N1,N2)|TM],[before(H1,H2)|Temp]):-
1860 gensym_num(hp,N1,H1),
1861 gensym_num(hp,N2,H2),
1862 make_num_hp(TM,Temp),!.
1863
1866sort_steps(Steps,[],Steps):-!.
1867sort_steps([Steps|[]],[],[Steps]):-!.
1868sort_steps(Steps,Temp,OrderedST):-
1869 steps_in_temp(Temp,[],ST),
1870 sort_steps1(Temp,ST,OrderedSTID),
1871 sort_steps2(Steps,OrderedSTID,[],OrderedST),!.
1872
1874steps_in_temp([],ST,ST):-!.
1875steps_in_temp([before(H1,H2)|TT],List,ST):-
1876 set_append_e(List,[H1,H2],List1),
1877 steps_in_temp(TT,List1,ST),!.
1878
1880sort_steps1(Temp,[],[]):-!.
1881sort_steps1(Temp,[HP1|TST],[HPF|OST]):-
1882 earliest_step(HP1,HPF,Temp,TST,TST1),
1883 sort_steps1(Temp,TST1,OST),!.
1884
1885earliest_step(HPF,HPF,Temp,[],[]):-!.
1886earliest_step(HP1,HPF,Temp,[HP2|TST],[HP1|TST1]):-
1887 member(before(HP2,HP1),Temp),
1888 earliest_step(HP2,HPF,Temp,TST,TST1),!.
1889earliest_step(HP1,HPF,Temp,[HP2|TST],[HP2|TST1]):-
1890 earliest_step(HP1,HPF,Temp,TST,TST1),!.
1891
1893sort_steps2(OtherST,[],OrderedST1,OrderedST):-
1894 append_dcut(OrderedST1,OtherST,OrderedST),!.
1895sort_steps2(Steps,[HP|THPS],List,OrderedST):-
1896 member(step(HP,N,Pre,Post,F),Steps),
1897 append_dcut(List,[step(HP,N,Pre,Post,F)],List1),
1898 list_take(Steps,[step(HP,N,Pre,Post,F)],Steps1),
1899 sort_steps2(Steps1,THPS,List1,OrderedST),!.
1900sort_steps2(Steps,[HP|THPS],List,OrderedST):-
1901 sort_steps2(Steps,THPS,List,OrderedST),!.
1903
1905make_ss_to_se([],[]):-!.
1906make_ss_to_se([ss(Sort,Obj,Post)|TPost],[se(Sort,Obj,Post)|TPre]):-
1907 make_ss_to_se(TPost,TPre),!.
1908make_ss_to_se([se(Sort,Obj,Post)|TPost],[se(Sort,Obj,Post)|TPre]):-
1909 make_ss_to_se(TPost,TPre),!.
1910
(Node,PHPs,SIZE1,TNList) :-
1916 1917 getN_decomp(Node, HPs),
1918 push_to_primitive(HPs,[],PHPs,[],TNList),
1919 pprint(PHPs,1,SIZE),
1920 SIZE1 is SIZE -1,!.
1921
1925change_op_representation :-
1926 env_call(method(A,B,C,Stat,T,Dec)),
1927 make_ss_to_se(B,B0),
1928 make_se_primitive(B0,B1),
1929 make_sc_primitive(C,C1),
1930 get_preconditions(C1,B1,Pre,Post),
1931 rem_statics(Post, PostR,St1),
1932 rem_statics(Pre, PreR,St2),
1933 append_dcut(St1,St2,Statics),
1934 append_dcut(Stat,Statics,Statics1),
1935 remove_unneed(Statics1,[],Statics2),
1936 get_achieval(A,Dec,T,Dec1,T1,ACH),
1937 env_assert(methodC(A,PreR,PostR,Statics2,T1,achieve(ACH),Dec1)),
1938 fail.
1939change_op_representation :-
1940 env_call(operator(A,B,C,D)),
1941 make_ss_to_se(B,B0),
1942 make_se_primitive(B0,B1),
1943 make_sc_primitive(C,C1),
1945 1946 get_preconditions(C1,B1,Pre,Post),
1947 rem_statics(Post, PostR,St1),
1948 rem_statics(Pre, PreR,St2),
1949 append_dcut(St1,St2,Statics1),
1950 remove_unneed(Statics1,[],Statics),
1951 statics_consist(Statics),
1952 env_assert(operatorC(A,PreR,PostR,D,Statics)),
1953 fail.
1954change_op_representation:-
1955 env_retractall(current_num(sm,_)),!.
1956
1957get_preconditions([],Prev,Prev,Prev) :-!.
1958get_preconditions([sc(S,X,From =>To)|Rest],Prev,[se(S,X,From1)|Pre],[se(S,X,To1)|Post]):-
1959 member_e(se(S,X,PSE),Prev),
1960 append_dcut(PSE,From,From1),
1961 append_dcut(PSE,To,To1),
1962 list_take(Prev,[se(S,X,PSE)],Prev1),
1963 get_preconditions(Rest,Prev1, Pre,Post),!.
1964get_preconditions([sc(S,X,From =>To)|Rest],Prev,[se(S,X,From)|Pre],[se(S,X,To)|Post]):-
1965 get_preconditions(Rest,Prev, Pre,Post),!.
1966get_preconditions([],Prev,Prev,Prev) :-!.
1967
1969get_achieval(A,Dec,T,Dec1,T1,Achieval):-
1970 env_retractall(current_num(sm,_)),
1971 make_dec(A,Dec,Dec1,T,T1,[],Achieval),!.
1972make_dec(A,[],[],Temp,Temp,Achieval,Achieval):-!.
1973make_dec(A,[HD|TD],TD1,Temp,Temp1,Achieval,Achieval1):-
1974 HD=..[achieve|Goal],
1975 env_call current_num(sm,Num),
1976 replace_achieval_temp(Temp,Temp0,Num),
1977 make_ss_to_se(Goal,Goal0),
1978 append_dcut(Achieval,Goal0,Achieval0),
1979 make_dec(A,TD,TD1,Temp0,Temp1,Achieval0,Achieval1),!.
1980make_dec(A,[HD|TD],TD1,Temp,Temp1,Achieval,Achieval1):-
1981 HD=..[achieve|Goal],
1982 not(env_call current_num(sm,_)),
1983 replace_achieval_temp(Temp,Temp0,1),
1984 make_ss_to_se(Goal,Goal0),
1985 append_dcut(Achieval,Goal0,Achieval0),
1986 make_dec(A,TD,TD1,Temp0,Temp1,Achieval0,Achieval1).
1987make_dec(A,[HD|TD],[HD|TD1],Temp,Temp1,Achieval,Achieval1):-
1988 HD=..[DecName|Goal],
1989 DecName\==achieve,
1990 gensym_special(sm,SM),
1991 env_call current_num(sm,Num),
1992 make_dec(A,TD,TD1,Temp,Temp1,Achieval,Achieval1),!.
1993
1995replace_achieval_temp(Temp,Temp1,Num):-
1996 change_all_numbers(Temp,Num,Temp00),
1997 tidy_temp(Temp00,Temp1).
1998
1999change_all_numbers([],Num,[]):-!.
2000change_all_numbers([HTemp|TTemp],Num,[HTemp00|TTemp00]):-
2001 HTemp=..[before|Nums],
2002 change_nums(Nums,Num,Nums1),
2003 HTemp00=..[before|Nums1],
2004 change_all_numbers(TTemp,Num,TTemp00).
2005
2006change_nums([],Num,[]):-!.
2007change_nums([Num1|TN],Num,[Num1|TN1]):-
2008 Num1<Num,
2009 change_nums(TN,Num,TN1),!.
2010change_nums([Num1|TN],Num,[Num2|TN1]):-
2011 Num1>Num,
2012 Num2 is Num1-1,
2013 change_nums(TN,Num,TN1),!.
2014change_nums([Num|TN],Num,[0|TN1]):-
2015 change_nums(TN,Num,TN1),!.
2016
2018tidy_temp(Temp,Temp1):-
2019 member(before(Num,0),Temp),
2020 list_take(Temp,[before(Num,0)],Temp0),
2021 change_laters(Temp0,Num,Temp01),
2022 tidy_temp(Temp01,Temp1).
2023tidy_temp([],[]):-!.
2024tidy_temp([before(0,Num)|Temp],Temp0):-
2025 tidy_temp(Temp,Temp0),!.
2026tidy_temp([before(Num1,Num2)|Temp],[before(Num1,Num2)|Temp0]):-
2027 tidy_temp(Temp,Temp0),!.
2028
2029change_laters([before(0,Num2)|Temp],Num,[before(Num,Num2)|Temp0]):-
2030 change_laters(Temp,Num,Temp0).
2031change_laters([before(Num1,0)|Temp],Num,[before(Num1,0)|Temp0]):-
2032 change_laters(Temp,Num,Temp0).
2033change_laters([before(Num1,Num2)|Temp],Num,[before(Num1,Num2)|Temp0]):-
2034 change_laters(Temp,Num,Temp0).
2035
2037make_se_primitive([],[]).
2038make_se_primitive([se(Sort,Obj,ST)|SE],[se(Sort,Obj,ST)|SE0]):-
2039 env_call objectsC(Sort,Objls),!,
2040 make_se_primitive(SE,SE0).
2041make_se_primitive([se(Sort,Obj,ST)|SE],[se(PSort,Obj,ST)|SE0]):-
2042 find_prim_sort(Sort,PSorts),
2043 member(PSort,PSorts),
2044 make_se_primitive(SE,SE0).
2045
2047make_sc_primitive([],[]).
2048make_sc_primitive([sc(Sort,Obj,SE1=>SE2)|ST],[sc(Sort,Obj,SE1=>SE2)|ST0]):-
2049 env_call objectsC(Sort,Objls),!,
2050 make_sc_primitive(ST,ST0).
2051make_sc_primitive([sc(Sort,Obj,SE1=>SE2)|ST],[sc(PSort,Obj,SE1=>SE2)|ST0]):-
2052 find_prim_sort(Sort,PSorts),
2053 member(PSort,PSorts),
2054 make_sc_primitive(ST,ST0).
2055
2056
2059make_tn(TN,Name,Pre,Post,Temp,Dec):-
2060 find_only_changed(Pre,Post,[],Pre1,[],Post1),
2061 not(isemptylist(Post1)),
2062 not(exist_tn(Pre,Post)),
2063 gensym_special(tn,TN),
2065 env_assert(tn(TN,Name,Pre1,Post1,Temp,Dec)),!.
2066
2067exist_tn(Pre,Post):-
2068 env_call(tn(_,_,Pre,Post1,_,_)),
2069 state_achieved(Post,Post1),!.
2070find_only_changed([],[],Pre,Pre,Post,Post):-!.
2072find_only_changed([se(Sort,Obj,ST)|Pre],[se(Sort,Obj,ST)|Post],Pre0,Pre1,Post0,Post1):-
2073 find_only_changed(Pre,Post,Pre0,Pre1,Post0,Post1),!.
2074find_only_changed([se(Sort,Obj,ST)|Pre],Post,Pre0,Pre1,Post0,Post1):-
2075 member(se(Sort,Obj,ST1),Post),
2076 list_take(Post,[se(Sort,Obj,ST1)],Post2),
2077 append_changed(se(Sort,Obj,ST),se(Sort,Obj,ST1),Pre0,Pre3,Post0,Post3),
2078 find_only_changed(Pre,Post2,Pre3,Pre1,Post3,Post1),!.
2079find_only_changed([se(Sort,Obj,ST)|Pre],Post,Pre0,Pre1,Post0,Post1):-
2080 member(se(SortN,Obj,ST1),Post),
2081 list_take(Post,[se(SortN,Obj,ST1)],Post2),
2082 append_changed(se(Sort,Obj,ST),se(SortN,Obj,ST1),Pre0,Pre3,Post0,Post3),
2083 find_only_changed(Pre,Post2,Pre3,Pre1,Post3,Post1),!.
2085
2088append_changed(se(Sort,Obj,ST),se(Sort1,Obj,ST1),Pre0,Pre0,Post0,Post0):-
2089 state_match(Sort,Obj,ST,ST1),!.
2090append_changed(se(Sort,Obj,ST),se(Sort1,Obj,ST1),Pre0,Pre3,Post0,Post3):-
2091 append_dcut(Pre0,[se(Sort,Obj,ST)],Pre3),
2092 append_dcut(Post0,[se(Sort,Obj,ST1)],Post3),!.
2093
2095push_to_primitive([],PHPs,PHPs,TNLst,TNLst) :-!.
2096push_to_primitive([step(HPID,_,_,_,exp(TN))|HPs],List,PHPs,TNSoFar,TNFinal) :-
2097 env_call(tn(TN,Name,Pre,Post,Temp,Dec)),
2098 push_to_primitive(Dec,List,Dec1,[tn(TN,Name,Pre,Post,Temp,Dec)|TNSoFar],TNNext),
2099 push_to_primitive(HPs,Dec1,PHPs,TNNext,TNFinal),!.
2100push_to_primitive([step(HPID,_,_,_,exp(Name))|HPs],List,PHPs,TNSoFar,TNFinal):-
2101 append_dcut(List,[Name],List1),
2102 push_to_primitive(HPs,List1,PHPs,TNSoFar,TNFinal),!.
2103
2105
2106possibly_before(I,J,Temps) :-
2107 \+ necessarily_before(J,I,Temps), !.
2108
2109necessarily_before(J,I,Temps) :-
2110 member(before(J,I),Temps),!.
2111necessarily_before(J,I,Temps) :-
2112 member(before(J,Z),Temps),
2113 necessarily_before(Z,I,Temps),!.
2114
2115select_node(node(Name,Pre,Temp,Decomp,Statics)) :-
2116 env_retract(node(Name,Pre,Temp,Decomp,Statics)),
2121 !.
2122
2123get_obj_prim_sort([],[]):-!.
2124get_obj_prim_sort([HSort|TV],[HObj|TS]):-
2125 env_call is_of_primitive_sort(HObj,HSort),
2126 get_obj_prim_sort(TV,TS),!.
2135find_all_upper([],[]).
2136find_all_upper([HVars|TV],[HSorts|TS]):-
2137 uppersorts(HSorts,Upsorts),
2138 member(HVars,Upsorts),
2139 find_all_upper(TV,TS).
2140
2142find_prim_sort(Sort,PS):-
2143 subsorts(Sort,Subsorts),
2144 split_prim_noprim(Subsorts,PS,NP),!.
2145
2147get_sort_objects(Sort,Objs):-
2148 find_prim_sort(Sort,PSorts),
2149 get_objects1(PSorts,Objls),
2150 flatten(Objls,[],Objs),!.
2151
2152get_objects1([],[]):-!.
2153get_objects1([PS1|RS],[Objls1|Objls]):-
2154 env_call objectsC(PS1,Objls1),
2155 get_objects1(RS,Objls),!.
2156
2159subsortse(Sort,Subsorts):-
2160 subsorts(Sort,Subsorts1),
2161 subtract(Subsorts1,[Sort],Subsorts),!.
2163subsorts(Sort,Subsorts):-
2164 sort_down([Sort],[],Subsorts),!.
2165
2166sort_down([],Subsorts,Subsorts):-!.
2167sort_down([HOpen|TOpen],List,Subsorts):-
2168 env_call objectsC(HOpen,Objls),
2169 append_dcut(List,[HOpen],List1),
2170 sort_down(TOpen,List1,Subsorts),!.
2171sort_down([HOpen|TOpen],List,Sortslist):-
2172 env_call sorts(HOpen,Sorts),
2173 sort_down(Sorts,List,List2),
2174 sort_down(TOpen,[HOpen|List2],Sortslist),!.
2175sort_down([HOpen|TOpen],List,Sortslist):-
2176 sort_down(TOpen,List,Sortslist),!.
2177
2179uppersortse(Sort,Uppersorts):-
2180 uppersorts(Sort,Uppersortsf),
2181 subtract(Uppersortsf,[Sort],Uppersorts),!.
2183uppersorts(Sort,Uppersorts):-
2184 env_call objectsC(Sort,Objls),
2185 sort_up(Sort,[Sort],Uppersorts),!.
2186uppersorts(Sort,Uppersorts):-
2187 env_call sorts(Sort,Sortls),
2188 sort_up(Sort,[Sort],Uppersorts),!.
2189uppersorts(Obj,Sortls):-
2190 env_call objectsC(Sort,Objls),
2191 member(Obj, Objls),
2192 sort_up(Sort,[Sort],Sortls),!.
2193
2194sort_up(Sort, List,Sortslist):-
2195 env_call sorts(NPSort, NPSortls),
2196 not((special_sorts(PS), NPSort == PS )),
2197 member(Sort,NPSortls),
2198 sort_up(NPSort,[NPSort|List],Sortslist).
2199sort_up(Sort, List,List):-!.
2200
2201special_sorts(primitive_sorts).
2202special_sorts(non_primitive_sorts).
2203
2204
2206sametree(Sort1,Sort2):-
2207 Sort1==Sort2,!.
2208sametree(Sort1,Sort2):-
2209 var(Sort1),!.
2210sametree(Sort1,Sort2):-
2211 var(Sort2),!.
2212sametree(Sort1,Sort2):-
2213 uppersorts(Sort2,Sortls),
2214 member(Sort1,Sortls),!.
2215sametree(Sort1,Sort2):-
2216 uppersorts(Sort1,Sortls),
2217 member(Sort2,Sortls),!.
2218
2220split_prim_noprim([],[],[]):-!.
2221split_prim_noprim([HS|TS],[HS|TP],NP):-
2222 env_call objectsC(HS,Obj),
2223 split_prim_noprim(TS,TP,NP),!.
2224split_prim_noprim([HS|TS],PS,[HS|NP]):-
2225 split_prim_noprim(TS,PS,NP),!.
2226
2228
2230get_invariants(Invs) :-
2231 env_call(atomic_invariantsC(Invs)),!.
2232
2233get_one_isa(S,X,Preds):- \+ is_list(Preds),!,get_one_isa(S,X,[Preds]).
2234get_one_isa(S,X,Preds):-member(is_of_sort(X,S),Preds).
2235get_one_isa(S,X,Preds):-member(ss(S,X,_),Preds).
2236get_one_isa(S,X,Preds):-member(se(S,X,_),Preds).
2237get_one_isa(S,X,Preds):- member(Pred,Preds),Pred=..[F|Args],functor(Pred,F,A),get_one_isa_fa(S,X,F,A,Args).
2238
2239get_one_isa_fa(S,X,S,1,[X]):- is_sort(S).
2240get_one_isa_fa(S,X,_,_,[X|_]):- nonvar(X), is_decl_of_sort(X,S),!.
2241get_one_isa_fa(S,X,F,N,[X|_]):- functor(P,F,N),(env_call predicates(SS)),member(P,SS),arg(1,P,S),S\== physical_obj, nonvar(S).
2242
2243is_decl_of_sort(X,S):- env_call is_of_sort(X,S).
2244is_decl_of_sort(X,S):- env_call objects(S,L),member(X,L).
2246is_decl_of_sort(X,S):- name(X,Codes),append(NameCodes,NumberCodes,Codes),catch(number_codes(Number,NumberCodes),error(syntax_error(_),_),fail),name(Thing,NameCodes),downcase_atom(Thing,S).
2247
2248is_sort(V):-var(V),!,fail.
2249is_sort(obj).
2250is_sort(driver).
2251is_sort(truck).
2252is_sort(ball).
2253is_sort(room).
2254is_sort(gripper).
2255is_sort(S):- (env_call sorts(_,SS)),member(S,SS).
2256is_sort(S):- (env_call sorts(S,_)).
2257
2258rem_statics([sc(S,X,Lhs=>Rhs)|ST], [sc(S,X,LhsR=>RhsR)|STR],Rt1) :-
2259 split_st_dy(Lhs,[],LR, [],LhsR),
2260 split_st_dy(Rhs,[],RR,[],RhsR),
2261 append_dcut(LR,RR,R),
2262 rem_statics(ST, STR,Rt),
2263 append_dcut(Rt,[is_of_sort(X,S)|R],Rt1),!.
2264rem_statics([ss(S,X,Preds)|Post], [ss(S,X,PredR)|PostR],Rt1) :-
2265 split_st_dy(Preds,[],R, [],PredR),
2266 rem_statics(Post, PostR,Rt),
2267 append_dcut(Rt,[is_of_sort(X,S)|R],Rt1),!.
2268rem_statics([se(S,X,Preds)|Post], [se(S,X,PredR)|PostR],Rt1) :-
2269 split_st_dy(Preds,[],R, [],PredR),
2270 rem_statics(Post, PostR,Rt),
2271 append_dcut(Rt,[is_of_sort(X,S)|R],Rt1),!.
2272rem_statics([], [],[]) :-!.
2273
2274
2276
2277
2278isemptylist([]):-!.
2279
2286
2287member_cut(X,[X|_]) :- !.
2288member_cut(X,[_|Y]) :- member_cut(X,Y),!.
2289
2291member_e(X,[Y|_]):-
2292 X==Y,!.
2293member_e(X,[Y|L]):-
2294 var(Y),
2295 member_e(X,L),!.
2296member_e(ss(Sort,Obj,SE),[ss(Sort,Obj1,SE)|_]):-
2297 Obj==Obj1,!.
2298member_e(se(Sort,Obj,SE),[se(Sort,Obj1,SE)|_]):-
2299 Obj==Obj1,!.
2300member_e(sc(Sort,Obj,SE1=>SE2),[sc(Sort,Obj1,SE1=>SE2)|_]):-
2301 Obj==Obj1,!.
2302member_e(X,[Y|L]):- member_e(X,L),!.
2303
2304
2307u_mem_cut(_,[]):-!,fail.
2308u_mem_cut(X,[Y|_]) :- X == Y,!.
2309u_mem_cut(X,[_|L]) :- u_mem_cut(X,L),!.
2314obj_member(X,[X|[]]):-!.
2315obj_member(X,List):-
2316 obj_member0(X,List),!.
2317obj_member0(X,[Y|_]):-
2318 var(X),!. 2319obj_member0(X,[Y|_]):-
2320 X==Y,!.
2321obj_member0(X,[_|Y]) :- obj_member0(X,Y),!.
2322
2327pred_member(X,List):-
2328 ground(X),
2329 member(X,List),!.
2330pred_member(X,List):-
2331 setof(X,member(X,List),Refined),
2332 pred_member0(X,Refined),!.
2333pred_member0(X,[X|[]]):-!.
2334pred_member0(X,Y):-
2335 pred_member1(X,Y),!.
2336pred_member1(X,[Y|_]):-
2337 X=..[H|XLs],
2338 Y=..[H|YLs],
2339 vequal(XLs,YLs),!.
2340pred_member1(X,[_|Y]):- pred_member1(X,Y),!.
2341
2342statics_append([],L,L):-
2343 statics_consist(L),!.
2344statics_append(L,[],L):-
2345 statics_consist(L),!.
2346statics_append(List1,List2,L):-
2347 statics_consist(List1),
2348 statics_consist(List2),
2349 statics_append1(List1,List2,[],L),
2350 statics_consist(L),!.
2351
2352statics_append1([],List2,L1,L):-
2353 append_dcut(List2,L1,L),!.
2354statics_append1([H|List1],List2,L,Z) :-
2355 statics_append0(H,List2,L,L1),
2356 statics_append1(List1,List2,L1,Z),!.
2357
2358statics_append0(H,[],L,[H|L]):-!.
2359statics_append0(H,[H|Z],L,L):-!.
2360statics_append0(H,[X|Z],L1,L):-
2361 statics_append0(H,Z,L1,L),!.
2362
2363append_dcut([],L,L):-!.
2364append_dcut([H|T],L,[H|Z]) :- append_dcut(T,L,Z),!.
2365
2366append_cut([],L,L) :- !.
2367append_cut([H|T],L,[H|Z]) :- append_cut(T,L,Z),!.
2368
2373append_st(ST1,ST2,ST):-
2374 append_dcut(ST1,ST2,ST0),
2375 remove_unneed(ST0,[],ST),!.
2376
2379remove_unneed([],C,C):-!.
2380remove_unneed([A|B], Z, C):-
2381 var(A),
2382 member_e(A,Z),
2383 remove_unneed(B, Z, C),! .
2384remove_unneed([A|B], Z, C):-
2385 var(A),
2386 append_dcut(Z,[A],D),
2387 remove_unneed(B, D, C),!.
2388remove_unneed([A|B], Z, C):-
2389 ground(A),
2390 remove_unneed(B, Z, C),!.
2391remove_unneed([A|B], Z, C):-
2392 A=..[ne|Paras],
2393 append_dcut(Z,[A],D),
2394 remove_unneed(B, D, C),!.
2395remove_unneed([A|B], Z, C):-
2396 A=..[Pred|Paras],
2397 same_var_member(A,Z),
2398 remove_unneed(B, Z, C),!.
2399remove_unneed([A|B], Z, C):-
2400 append_dcut(Z,[A],D),
2401 remove_unneed(B, D, C),!.
2402
2403same_var_member(Pred,[Pred1|List]):-
2404 var(Pred1),
2405 same_var_member(Pred,List),!.
2406same_var_member(Pred,[Pred1|List]):-
2407 Pred==Pred1,!.
2408same_var_member(Pred,[Pred1|List]):-
2409 Pred=..[H|T],
2410 Pred1=..[H|T1],
2411 same_var_member1(T,T1),!.
2412same_var_member(Pred,[Pred1|List]):-
2413 same_var_member(Pred,List),!.
2414
2415same_var_member1([],[]):-!.
2416same_var_member1([H1|T],[H2|T]):-
2417 var(H1),
2418 H1==H2,!.
2419same_var_member1([H|T1],[H|T2]):-
2420 var(T1),
2421 T1==T2,!.
2422same_var_member1([H1|T1],[H2|T2]):-
2423 H1==H2,
2424 same_var_member1(T1,T2),!.
2425
2427is_equal_list(List1,List2):-
2428 List1==List2,!.
2429is_equal_list([],[]):-!.
2430is_equal_list(List1,List2):-
2431 length(List1,L),
2432 length(List2,L),
2433 is_equal_list1(List1,List2),!.
2434is_equal_list1([],[]):-!.
2435is_equal_list1([Head1|List1],[Head2|List2]):-
2436 Head1==Head2,
2437 is_equal_list1(List1,List2),!.
2438is_equal_list1([se(Sort,Obj,Head1)|List1],[se(Sort,Obj,Head2)|List2]):-
2439 is_equal_list(Head1,Head2),
2440 is_equal_list1(List1,List2),!.
2441is_equal_list1([Head1|List1],[Head2|List2]):-
2442 Head1=..[FF|Var1],
2443 Head2=..[FF|Var2],
2444 FF\==se,
2445 vequal(Var1,Var2),
2446 is_equal_list1(List1,List2),!.
2447is_equal_list1([Head1|List1],List2):-
2448 member(Head1,List2),
2449 append_dcut(List1,[Head1],List10),
2450 is_equal_list1(List10,List2),!.
2451
2453is_diff(List1,List2):-
2454 length(List1,L1),
2455 length(List2,L2),
2456 L1\==L2,!.
2457is_diff([Head|List1],List2):-
2458 not_exist(Head,List2),!.
2459is_diff([Head|List1],List2):-
2460 list_take(List2,[Head],List21),
2461 is_diff(List1,List21),!.
2462
2463not_exist(Pred,List2):-
2464 not(member(Pred,List2)),!.
2465not_exist(se(Sort,Obj,Head1),List2):-
2466 not(member(se(Sort,Obj,Head),List2)),!.
2467not_exist(se(Sort,Obj,Head1),List2):-
2468 member(se(Sort,Obj,Head2),List2),
2469 is_diff(Head1,Head2),!.
2470
2474set_append([], Z, Z):-! .
2475set_append([A|B], Z, C) :-
2476 not(not(member(A, Z))) ,
2477 set_append(B, Z, C),! .
2478set_append([A|B], Z, [A|C]) :-
2479 set_append(B, Z, C) .
2480
2484set_append_e(A,B,C):-
2485 append_dcut(A,B,D),
2486 remove_dup(D,[],C),!.
2487
2489remove_dup([],C,C):-!.
2490remove_dup([A|B],Z,C) :-
2491 member_e(A, Z),
2492 remove_dup(B, Z, C),! .
2493remove_dup([A|B], Z, C):-
2494 append_dcut(Z,[A],D),
2495 remove_dup(B, D, C),!.
2496
2498vequal([],[]):-!.
2499vequal([X|XLs],[Y|YLs]):-
2500 X==Y,
2501 vequal(XLs,YLs),!.
2502vequal([X|XLs],[Y|YLs]):-
2503 var(X),
2504 vequal(XLs,YLs),!.
2505vequal([X|XLs],[Y|YLs]):-
2506 var(Y),
2507 vequal(XLs,YLs),!.
2508
2509
2520
2522
2523list_take(R,[E|R1],R2):-
2524 remove_el(R,E,RR),
2525 list_take(RR,R1,R2),!.
2526list_take(R,[_|R1],R2):-
2527 list_take(R,R1,R2),!.
2528list_take(A,[],A) :- !.
2529
2532remove_el([],_,[]) :- ! .
2533remove_el([A|B],A,B) :- ! .
2534remove_el([A|B],C,[A|D]) :-
2535 remove_el(B,C,D) .
2536
2538
2539
2540gensym_special(Root,Atom) :-
2541 getnum(Root,Num),
2542 name(Root,Name1),
2543 name(Num,Name2),
2544 append_dcut(Name1,Name2,Name),
2545 name(Atom,Name).
2546
2547getnum(Root,Num) :-
2548 env_retract(current_num(Root,Num1)),!,
2549 Num is Num1+1,
2550 env_asserta(current_num(Root,Num)).
2551
2552getnum(Root,1) :- env_asserta(current_num(Root,1)).
2553
2554gensym_num(Root,Num,Atom):-atom_concat(Root,Num,Atom),!.
2555gensym_num(Root,Num,Atom):-
2556 name(Root,Name),
2557 name(Num,Name1),
2558 append_dcut(Name,Name1,Name2),
2559 name(Atom,Name2),!.
2560
2561
2562pprint([],SIZE,SIZE):-!.
2563pprint([HS|TS],Size0,SIZE):-
2564 is_list(HS),
2565 pprint(HS,Size0,Size1),
2566 pprint(TS,Size1,SIZE),!.
2567pprint([HS|TS],Size0,SIZE):-
2570 Size1 is Size0+1,
2571 pprint(TS,Size1,SIZE),!.
2572
2574
2575split_st_dy([],ST,ST,DY,DY):-!.
2576split_st_dy([Pred|TStates],ST0,ST,DY0,DY):-
2577 is_statics(Pred),
2578 append_dcut(ST0,[Pred],ST1),
2579 split_st_dy(TStates,ST1,ST,DY0,DY),!.
2580split_st_dy([Pred|TStates],ST0,ST,DY0,DY):-
2581 append_dcut(DY0,[Pred],DY1),
2582 split_st_dy(TStates,ST0,ST,DY1,DY),!.
2583
2585
2586flatten([HO|TO], List, O_List):-
2587 append_dcut(HO, List, List_tmp),
2588 flatten(TO, List_tmp, O_List),!.
2589flatten([H|TO], List,O_List):-
2590 append_dcut([H], List, List_tmp),
2591 flatten(TO, List_tmp, O_List).
2592flatten([], [HList|T], O_List):-
2593 HList = [],
2594 flatten(T, [], O_List).
2595flatten([], [HList|T], O_List):-
2596 is_list(HList),
2597 flatten([HList|T],[], O_List),!.
2598flatten([], L,L):-!.
2599
2601set_flatten([HO|TO], List, O_List):-
2602 set_append_e(HO, List, List_tmp),
2603 set_flatten(TO, List_tmp, O_List),!.
2604set_flatten([H|TO], List,O_List):-
2605 set_append_e([H], List, List_tmp),
2606 set_flatten(TO, List_tmp, O_List).
2607set_flatten([], [HList|T], O_List):-
2608 HList = [],
2609 set_flatten(T, [], O_List).
2610set_flatten([], [HList|T], O_List):-
2611 is_list(HList),
2612 set_flatten([HList|T],[], O_List),!.
2613set_flatten([], L,L):-!.
2614
2615
2626
2627reverse_unused(L,RL) :-
2628 revSlave(L,[],RL).
2629
2630revSlave([],RL,RL).
2631revSlave([H|T],Sofar,Final) :-
2632 revSlave(T,[H|Sofar],Final).
2633
2635:- assert_if_new(time_taken(0)). 2636:- assert_if_new(sum(0)). 2637:- assert_if_new(soln_size(0)). 2638
2639solve(N,FN):-
2640 N < FN,
2641 nl,write('task '), write(N),write(': '),nl,
2642 solve(N),
2643 Ni is N+1,
2644 solve(Ni,FN).
2645solve(FN,FN):-
2646 nl,write('task '), write(FN),write(': '),nl,
2647 solve(FN),
2648 retractall(sum(_)),
2649 assert(sum(0)),
2650 sum_time(CP),
2651 retractall(sum(_)),
2652 assert(sum(0)),
2653 sum_size(SIZE),
2654 TIM is CP /1000,
2655 retractall(time_taken(_)),
2656 retractall(soln_size(_)),
2657 nl,write('total time '),write(TIM),write(' seconds'),
2658 nl,write('total size '),write(SIZE),nl.
2659solve(N,N).
2660
2661sum_time(TIM):-
2662 time_taken(CP),
2663 env_retract(sum(N)),
2664 N1 is N +CP,
2665 assert(sum(N1)),
2666 fail.
2667sum_time(TIM):-
2668 sum(TIM).
2669sum_size(SIZE):-
2670 soln_size(S),
2671 retract(sum(N)),
2672 N1 is N +S,
2673 assert(sum(N1)),
2674 fail.
2675sum_size(SIZE):-
2676 sum(SIZE).
2677
2678stoppoint.
2680state_related(Post,Cond,undefd):-!.
2681state_related(Post,Cond,[]):-!.
2682state_related(Post,Cond,State2):-
2683 append_dcut(Post,Cond,State1),
2684 state_related(State1,State2).
2685
2688state_related([se(Sort,Obj,SE1)|State1],State2):-
2689 member(se(Sort,Obj,SE2),State2),
2690 state_related0(SE1,SE2).
2692state_related([sc(Sort1,Obj,SE1=>SS1)|State1],State2):-
2693 member(se(Sort,Obj,SE2),State2),
2694 env_call is_of_sort(Obj,Sort1),
2695 env_call is_of_sort(Obj,Sort).
2696state_related([se(Sort,Obj,SE)|State1],State2):-
2697 state_related(State1,State2),!.
2698state_related([sc(Sort,Obj,SE=>SS)|State1],State2):-
2699 state_related(State1,State2),!.
2700
2702state_related0([],SE2):-!.
2703state_related0([Head|SE1],SE2):-
2704 member(Head,SE2),
2705 state_related0(SE1,SE2).
2706state_related0([Head|SE1],SE2):-
2707 state_related0(SE1,SE2).
2708
2711change_obj_list(I):-
2712 find_dynamic_objects(I),
2713 collect_dynamic_obj,
2714 change_obj_list1,
2715 change_atomic_inv,!.
2716
2717change_obj_list1:-
2718 forall(objects(Sort,_),change_obj_list2(Sort)).
2719
2721change_obj_list2(Sort):-
2722 env_call(objectsC(Sort,Objls)),!.
2724change_obj_list2(Sort):-
2725 findall(Obj1s,env_call(objects(Sort,Objls)),Lists),
2726 flatten(Lists,Objls),
2727 env_assert(objectsC(Sort,Objls)),!.
2728
2730change_atomic_inv:-
2731 findall(Atom1,(env_call(atomic_invariants(Atom)),change_atomic_inv1(Atom,Atom1)),Lists),
2732 flatten(Lists,List),
2733 env_assert(atomic_invariantsC(List)),!.
2734change_atomic_inv.
2735
2736change_atomic_inv1([],[]).
2737change_atomic_inv1([Pred|Atom],[Pred|Atom1]):-
2738 Pred=..[Name|Objs],
2739 just_dynamic_objects(Objs),
2740 change_atomic_inv1(Atom,Atom1).
2741change_atomic_inv1([Pred|Atom],Atom1):-
2742 change_atomic_inv1(Atom,Atom1).
2743
2744just_dynamic_objects([]).
2745just_dynamic_objects([Head|Objs]):-
2746 env_call(objectsC(Sort,Objls)),
2747 member(Head,Objls),!,
2748 just_dynamic_objects(Objs).
2749
2750find_dynamic_objects([]):-!.
2751find_dynamic_objects([SE|Rest]):-
2752 find_dynamic_objects(SE),
2753 find_dynamic_objects(Rest),!.
2754find_dynamic_objects(ss(Sort,Obj,List)):-
2755 env_assert(objectsD(Sort,Obj)),!,
2756 forall((member(is_of_sort(X,S),List),
2757 nonvar(X),nonvar(S)),
2758 env_assert(objectsD(S,X))),!.
2759
2760collect_dynamic_obj:-
2761 env_call(objectsD(Sort,_)),
2762 setof(Obj, env_call(objectsD(Sort,Obj)), Objls),
2763 env_retractall(objectsD(Sort,_)),
2764 env_assert(objectsC(Sort,Objls)),
2765 fail.
2766collect_dynamic_obj.
2767
2768get_preconditions_g([],Prev,Prev,Prev):-!.
2769get_preconditions_g([sc(S,X,(From =>To))|Rest],Prev,[se(S,X,From)|Pre],[se(S,X,To)|Post]):-
2770 !,
2771 get_preconditions_g(Rest,Prev, Pre,Post).
2772
2775
2776ground_op :-
2777 assert_sort_objects,
2778 enumerateOps,
2779 instOps,
2780 flag(opCounter,Top,Top),
2781 write(opCounter = Top),nl,!.
2782
2783enumerateOps :-
2784 flag(opCounter,_,1),
2785 enumOps.
2786
2787enumOps :-
2788 env_call operator(Name,Prev,Nec,Cond),
2789 flag(opCounter,Count,Count),
2790 containsInvars(operator(Name,Prev,Nec,Cond),InVars,IsOfSorts,FPrev,FNec),
2791 2792 findVarsAndTypes(operator(Name,Prev,Nec,Cond),VT,NEs),
2793 env_assert(opParent(Count,operator(Name,FPrev,FNec,Cond),VT,NEs,InVars,IsOfSorts)),
2794 Next is Count + 1,
2795 flag(opCounter,Count,Next),
2796 fail.
2797
2798enumOps.
2799
2800
2809
2810
2811findVarsAndTypes(operator(_,Pre,Nec,Cond),Vars,NEs) :-
2812 vtPrevail(Pre,PreVars,PreNEs),
2813 vtEffects(Nec,NecVars,NecNEs),
2814 append_dcut(PreVars,NecVars,Vars),
2815 append_dcut(PreNEs,NecNEs,NEs),
2816 !.
2817
2820
2821vtEffects([],[],[]).
2822
2823vtEffects([sc(Type,Obj1,Preds)|Rest],VT,NEs) :-
2824 vtPreds(Preds,Related,NEs1),
2825 append_dcut([Type,Obj1],Related,Obj1VT),
2826 vtEffects(Rest,RestVT,RestNEs),
2827 append_dcut(Obj1VT,RestVT,VT),
2828 append_dcut(NEs1,RestNEs,NEs).
2829
2832
2833vtPrevail([],[],[]).
2834
2835vtPrevail([se(Type,Obj1,Preds)|Rest],VT,NEs) :-
2836 vtPLst(Preds,Related,NEs1),
2837 append_dcut([Type,Obj1],Related,Obj1VT),
2838 vtPrevail(Rest,RestVT,RestNEs),
2839 append_dcut(Obj1VT,RestVT,VT),
2840 append_dcut(NEs1,RestNEs,NEs).
2841
2844
2845vtPreds((Pre => Add),Res,NEs) :-
2846 vtPLst(Pre,VTPre,NEs1),
2847 vtPLst(Add,VTAdd,NEs2),
2848 append_dcut(VTPre,VTAdd,Res),
2849 append_dcut(NEs1,NEs2,NEs).
2850
2853
2854vtPLst([],[],[]).
2855
2856vtPLst([ne(X,Y)|Rest],Res,[ne(X,Y)|RestNEs]) :-
2857 !,
2858 vtPLst(Rest,Res,RestNEs).
2859
2860vtPLst([Pred|Preds],Res,NEs) :-
2861 functor(Pred,_,1),
2862 !,
2863 vtPLst(Preds,Res,NEs).
2864
2865vtPLst([is_of_sort(_,_)|Preds],Res,NEs) :-
2866 !,
2867 vtPLst(Preds,Res,NEs).
2868
2872
2873vtPLst([Pred|Preds],Res,NEs) :-
2874 functor(Pred,Name,Arity),
2875 Pred =.. [Name,Obj1|Rest],
2876 VNeeded is Arity - 1,
2877 createVarList(VNeeded,VN),
2878 DummyPred =.. [Name,X|VN],
2879 env_call predicates(PList),
2880 member(DummyPred,PList),
2881 pair(VN,Rest,This),
2882 vtPLst(Preds,RestPre,NEs),
2883 append_dcut(This,RestPre,Res).
2884
2887
2888createVarList(1,[X]) :-
2889 !.
2890
2891createVarList(N,[X|Rest]) :-
2892 Next is N - 1,
2893 createVarList(Next,Rest).
2894
2897
2898pair([],[],[]).
2899
2900pair([Type|Types],[Var|Vars],[Type,Var|Rest]) :-
2901 pair(Types,Vars,Rest).
2902
2903
2904
2909
2910instOps :-
2911 flag(opCounter,_,1),
2912 env_call opParent(No,Operator,VT,NEs,InVars,IsOfSorts),
2913 checkIsOfSorts(IsOfSorts),
2914 checkInVars(InVars),
2915 chooseVals(VT,NEs,InVars,Vals),
2916 obeysNEs(NEs),
2917 flag(opCounter,Count,Count),
2918 (operator(Name,Prev,Nec,Cond) = Operator),
2919 filterSE(Prev,FPrev),
2920 filterSC(Nec,FNec),
2921 env_assert(gOperator(Count,No,operator(Name,FPrev,FNec,Cond))),
2922 Next is Count + 1,
2923 flag(opCounter,Count,Next),
2924 fail.
2925
2926instOps.
2927
2928
2929checkInVars([]):- !.
2930checkInVars(Preds):-
2931 env_call(atomic_invariantsC(Invars)),
2932 doCheckInvars(Preds,Invars).
2933
2934doCheckInvars([],_).
2935doCheckInvars([Pred|Rest],Invars) :-
2936 member(Pred,Invars),
2937 doCheckInvars(Rest,Invars).
2938
2939checkIsOfSorts([]).
2940checkIsOfSorts([is_of_sort(V,Sort)|Rest]) :-
2941 env_call objectsOfSort(Sort,Objs),
2942 member(V,Objs),
2943 checkIsOfSorts(Rest).
2944
2945
2947
2948filterSE([],[]) :- !.
2949filterSE([se(Sort,Id,Preds)|Rest],[se(Sort,Id,FPreds)|FRest]) :-
2950 filterPreds(Preds,FPreds),!,
2951 filterSE(Rest,FRest).
2952
2954
2955filterSC([],[]) :- !.
2956filterSC([sc(Sort,Id,(Pre => Post))|Rest],[sc(Sort,Id,(FPre => FPost))|FRest]) :-
2957 filterPreds(Pre,FPre),
2958 filterPreds(Post,FPost),
2959 !,
2960 filterSC(Rest,FRest).
2961
2963
2964filterPreds([],[]).
2965filterPreds([ne(_,_)|Rest],FRest) :-
2966 !,
2967 filterPreds(Rest,FRest).
2968filterPreds([is_of_sort(_,_)|Rest],FRest) :-
2969 !,
2970 filterPreds(Rest,FRest).
2976filterPreds([H|T],[H|FT]) :-
2977 filterPreds(T,FT).
2978
2979
2981
2982collectAllConds(_,_,_,_,[],[]) :- !.
2983
2984collectAllConds(CondVT,NEs,InVars,CondVals,Cond,_) :-
2985 env_retractall(temp_assertIndivConds(_)),
2986 chooseVals(CondVT,NEs,InVars,Vals),
2987 assertIndivConds(Cond),
2988 fail.
2989
2990collectAllConds(_,_,_,_,_,NewConds) :-
2991 setof(Cond,env_call(temp_assertIndivConds(Cond)),NewConds),
2992 dmsg(collectAllConds=NewConds),!.
2993
2994assertIndivConds([]) :- !.
2995
2996assertIndivConds([H|T]) :-
2997 env_assert(temp_assertIndivConds(H)),
2998 assertIndivConds(T).
2999
3001
3002containsInvars(operator(Name,Prev,Nec,Cond),InVars,IsOfSorts,FPrev,FNec) :-
3003 prevInvars(Prev,PInVars,PIsOfSorts,FPrev),
3004 necInvars(Nec,NecInVars,NIsOfSorts,FNec),
3005 append_dcut(NecInVars,PInVars,InVars),
3006 append_dcut(PIsOfSorts,NIsOfSorts,IsOfSorts),
3007 !.
3008
3009prevInvars([],[],[],[]).
3010prevInvars([se(Type,Obj,Props)|Rest],InVars,IsOfSorts,[se(Type,Obj,FProps)|RFPrev]) :-
3011 propsInvars(Props,PInvars,PIsOfSorts,FProps),
3012 prevInvars(Rest,RInVars,RIsOfSorts,RFPrev),
3013 append_dcut(PInVars,RInVars,InVars),
3014 append_dcut([is_of_sort(Obj,Type)|PIsOfSorts],RIsOfSorts,IsOfSorts).
3015
3016necInvars([],[],[],[]).
3017necInvars([sc(Type,Obj,(Props => Adds))|Rest],Invars,IsOfSorts,[sc(Type,Obj,(FProps => FAdds))|RFNec]) :-
3018 propsInvars(Props,PInvars,PIsOfSorts,FProps),
3019 propsInvars(Adds,AInvars,AIsOfSorts,FAdds),
3020 necInvars(Rest,RInvars,RIsOfSorts,RFNec),
3021 append_dcut(AInvars,PInvars,Temp),
3022 append_dcut(Temp,RInvars,Invars),
3023 append_dcut(PIsOfSorts,AIsOfSorts,SortsTemp),
3024 append_dcut([is_of_sort(Obj,Type)|SortsTemp],RIsOfSorts,IsOfSorts).
3025
3026propsInvars([],[],[],[]).
3027propsInvars([Prop|Props],[Prop|Rest],IsOfSorts,FProps) :-
3028 isInvariant(Prop),
3029 !,
3030 propsInvars(Props,Rest,IsOfSorts,FProps).
3031propsInvars([is_of_sort(X,Y)|Props],InVars,[is_of_sort(X,Y)|IsOfSorts],FProps):-
3032 !,
3033 propsInvars(Props,InVars,IsOfSorts,FProps).
3034
3035propsInvars([Pred|Props],Rest,IsOfSorts,[Pred|FProps]) :-
3036 propsInvars(Props,Rest,IsOfSorts,FProps).
3037
3038isInvariant(Prop) :-
3039 env_call atomic_invariantsC(Invars),
3040 functor(Prop,Name,Arity),
3041 createVarList(Arity,VN),
3042 Pred =.. [Name | VN],
3043 member(Pred,Invars).
3044
3048
3049chooseVals([],_,_,[]).
3050
3051chooseVals([Type,Var|TypeVars],NEs,InVars,Vals) :-
3052 ground(Var),
3053 !,
3054 chooseVals(TypeVars,NEs,InVars,Vals).
3055
3056chooseVals([Type,Var|TypeVars],NEs,InVars,[Var|Vals]) :-
3057 env_call objectsOfSort(Type,AllVals),
3058 member(Var,AllVals),
3059 chooseVals(TypeVars,NEs,InVars,Vals).
3066assert_sort_objects :-
3067 env_call objectsC(Type,Objects),
3068 env_assert(objectsOfSort(Type,Objects)),
3069 fail.
3070
3071assert_sort_objects :-
3072 env_call sorts(Type,SubTypes),
3073 not((special_sorts(PS), Type == PS )),
3074 all_objects(Type,Objs),
3075 env_assert(objectsOfSort(Type,Objs)),
3076 fail.
3077
3078assert_sort_objects.
3079
3080all_objects(Type,Objs) :-
3081 env_call objectsC(Type,Objs),
3082 !.
3083all_objects(Type,Objs) :-
3084 env_call sorts(Type,SubSorts),
3085 !,
3086 collect_subsort_objects(SubSorts,Objs).
3087
3088collect_subsort_objects([],[]).
3089collect_subsort_objects([Sort|Rest],Objs ) :-
3090 all_objects(Sort,SortObjs),
3091 !,
3092 collect_subsort_objects(Rest,RestObjs),
3093 append_dcut(SortObjs,RestObjs,Objs).
3094
3095obeysNEs([]).
3096
3097obeysNEs([ne(V1,V2)|Rest]) :-
3098 V1 \== V2, dif(V1,V2), 3099 obeysNEs(Rest),!.
3100
3101obeysInVars([]).
3102obeysInVars([Prop|Rest]) :-
3103 env_call(atomic_invariantsC(Invars)),
3104 member(Prop,Invars),
3105 !.
3106
3110
3111
3113
3114prettyPrintOp(gOperator(No,Par,Op)) :-
3115 write('gOperator('),
3116 write(No),write(','),
3117 write(Par),write(','),nl,
3118 writeOp(4,Op),
3119 !.
3120
3121writeOp(TabVal,operator(Name,Prev,Nec,Cond)) :-
3122 tab(TabVal),
3123 write('operator('),write(Name),write(','),nl,
3124 tab(8),write('% Prevail'),nl,
3125 tab(8),write('['),nl,
3126 writePrevailLists(8,Prev),
3127 tab(8),write('],'),nl,
3128 tab(8),write('% Necessary'),nl,
3129 tab(8),write('['),nl,
3130 writeChangeLists(10,Nec),
3131 tab(8),write('],'),nl,
3132 tab(8),write('% Conditional'),nl,
3133 tab(8),write('['),nl,
3134 writeChangeLists(10,Cond),
3135 tab(8),write('])).'),nl.
3136
3137writePropList(TabVal,[]) :-
3138 tab(TabVal),
3139 write('[]').
3140
3141writePropList(TabVal,[ne(_,_)|Props]) :-
3142 !,
3143 writePropList(Indent,Props).
3144
3145writePropList(TabVal,[Prop|Props]) :-
3146 env_call(atomic_invariantsC(Invars)),
3147 member(Prop,Invars),
3148 writePropList(TabVal,Props).
3149
3150writePropList(TabVal,[Prop|Props]) :-
3151 tab(TabVal),
3152 write('['),
3153 write(Prop),
3154 Indent is TabVal + 1,
3155 writePList(Indent,Props).
3156
3157writePList(TabVal,[]) :-
3158 nl,
3159 tab(TabVal),
3160 write(']').
3161
3162writePList(TabVal,[ne(_,_)]) :-
3163 !,
3164 nl,
3165 tab(TabVal),
3166 write(']').
3167
3168writePList(TabVal,[Prop]) :-
3169 env_call(atomic_invariantsC(Invars)),
3170 member(Prop,Invars),
3171 !,
3172 nl,
3173 tab(TabVal),
3174 write(']').
3175
3176writePList(TabVal,[Prop]) :-
3177 write(','),
3178 nl,
3179 tab(TabVal),
3180 write(Prop),
3181 write(']').
3182
3183writePList(TabVal,[ne(_,_),P2|Rest]) :-
3184 !,
3185 writePList(TabVal,[P2|Rest]).
3186
3187writePList(TabVal,[Prop,P2|Rest]) :-
3188 env_call(atomic_invariantsC(Invars)),
3189 member(Prop,Invars),
3190 !,
3191 writePList(TabVal,[P2|Rest]).
3192
3193writePList(TabVal,[P1,P2|Rest]) :-
3194 write(','),
3195 nl,
3196 tab(TabVal),
3197 write(P1),
3198 writePList(TabVal,[P2|Rest]).
3199
3200writeChangeLists(_,[]).
3201
3202writeChangeLists(TabVal,[sc(Type,Obj,(Req => Add))|Rest]) :-
3203 tab(TabVal),
3204 write('sc('),write(Type),write(','),write(Obj),write(',('),nl,
3205 Indent is TabVal + 12,
3206 writePropList(Indent,Req),
3207 nl,
3208 tab(Indent),
3209 write('=>'),
3210 nl,
3211 writePropList(Indent,Add),
3212 write('))'),writeComma(Rest),
3213 nl,
3214 writeChangeLists(TabVal,Rest).
3215
3216writeComma([]).
3217writeComma(_) :-
3218 write(',').
3219
3220writePrevailLists(_,[]).
3221
3222writePrevailLists(TabVal,[se(Type,Obj,Props)|Rest]) :-
3223 tab(TabVal),
3224 write('se('),write(Type),write(','),write(Obj),write(','),nl,
3225 Indent is TabVal + 12,
3226 writePropList(Indent,Props),
3227 write(')'),writeComma(Rest),
3228 nl,
3229 writePrevailLists(TabVal,Rest).
3230
3231
3232assert_is_of_sort :-
3233 env_call objectsOfSort(Type,Objects),
3234 member(Obj,Objects),
3235 assert_is_of_sort1(Type,Obj),
3236 fail.
3237assert_is_of_sort :-
3238 env_call objectsC(Type,Objects),
3239 member(Obj,Objects),
3240 assert_is_of_primitive_sort(Type,Obj),
3241 fail.
3242assert_is_of_sort.
3243
3244assert_is_of_sort1(Type,Obj):- env_assert(is_of_sort(Obj,Type)),!.
3245assert_is_of_primitive_sort(Type,Obj):-
3246 env_assert(is_of_primitive_sort(Obj,Type)),!.
3247
3250prim_substate_class:-
3251 env_call substate_classes(Sort,Obj,Substate),
3252 find_prim_sort(Sort,PS),
3253 assert_subclass(PS,Obj,Substate),
3254 fail.
3255prim_substate_class:-
3256 collect_prim_substates.
3257
3258assert_subclass([],Obj,Substate).
3259assert_subclass([HS|TS],Obj,Substate):-
3260 env_assert(gsstates(HS,Obj,Substate)),
3261 assert_subclass(TS,Obj,Substate).
3262
3263collect_prim_substates:-
3264 env_call gsstates(Sort,Obj,_),
3265 setof(SStates,(env_call gsstates(Sort,Obj,SStates)),GSStates),
3266 env_retractall(gsstates(Sort,Obj,_)),
3267 all_combined(GSStates,GSStates0),
3268 env_assert(gsubstate_classes(Sort,Obj,GSStates0)),
3269 fail.
3270collect_prim_substates.
3271
3272all_combined(SStates,CSStates):-
3273 xprod(SStates,CSStates1),
3274 flat_interal(CSStates1,CSStates),!.
3275
3276flat_interal([],[]):-!.
3277flat_interal([HSS1|TSS1],[HSS|TSS]):-
3278 flatten(HSS1,[],HSS),
3279 flat_interal(TSS1,TSS),!.
3280
3283xprod(A,B,C) :-
3284 xprod([A,B],C) .
3285
3286xprod([],[]).
3287xprod(A,E) :-
3288 xprod(A,B,C,D) ,
3289 F =..[^,C,env_call(D)] ,
3290 call(setof(B,F,E)) .
3291
3292xprod([X],[A],A,member(A,X)) .
3293xprod([X,Y],[A,B],C,(D,E)) :-
3294 C =..[^,A,B] ,
3295 D =..[member,A,X] ,
3296 E =..[member,B,Y] .
3297xprod([X|Y],[A|E],D,(F,G)) :-
3298 D =..[^,A,C] ,
3299 F =..[member,A,X] ,
3300 xprod(Y,E,C,G).
3301
3302
3303:-retractall(solution_file(_)). 3304:-asserta(solution_file(user)). 3305
3308
3309
3310
3311lws:- listing(ocl:[method,
3312operator,implied_invariant,atomic_invariants,inconsistent_constraint,predicates,objects,substate_classes,sorts,domain_name,planner_task_slow,planner_task,
3313htn_task,tp_node,tn,current_num,goal_related,goal_related_search,solved_node,closed_node,tp_goal,final_node,node,op_score,gsstates,gsubstate_classes,related_op,
3314objectsOfSort,atomic_invariantsC,objectsD,objectsC,gOperator,operatorC,opParent,methodC,is_of_sort,is_of_primitive_sort,temp_assertIndivConds]).
3315
3316lws(F):-tell(F),lws,told.
3317
3318:-export(rr/0). 3319:-export(rr1/0). 3320rr:- test_ocl('domains_ocl/chameleonWorld.ocl').
3321rr1:- test_ocl('domains_ocl/translog.ocl').
3322
3323:- fixup_exports. 3324
3325:- include(translog4). 3326
3327:-rr.
logicmoo_hyhtn_code
% 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 % */