12:-module(htncode,[]). 13
14
15:-multifile(user:push_env_ctx/0). 16:-dynamic(user:push_env_ctx/0). 17
22
23
24
26:- ensure_loaded(library(logicmoo_common)). 27:- ensure_loaded(library(planner_api)). 28:- use_module(library(logicmoo_planner)). 29
30:- kb_shared(baseKB:mpred_prop/3). 31
32:- ensure_loaded(library(logicmoo/util_structs)). 33:- ensure_loaded(library(logicmoo/util_bb_env)). 34:- prolog_load_context(file,File),ain(user:env_source_file(File)). 35
36:-op(500,fx,env_call).
38
44
45:- style_check(-singleton).
46:-use_module(library(system)).
47
49:- dynamic op_num/1.
50:- dynamic sum/1.
52:- dynamic methodC/7, operatorC/5, my_stats/1.
54:- dynamic tp_node/6,tn/6.
55:- dynamic solved_node/2.
56:- dynamic closed_node/6.
57:- dynamic gsubstate_classes/3.
58:- dynamic current_num/2.
59:- dynamic produce/4,goal_related/1. 61:- dynamic gsstates/3,gpred/2.
63:- dynamic kill_file/1,solution_file/1.
65:- op(100,xfy,'=>').
66
67:- dynamic htn_task/3.
68:- multifile htn_task/3.
69:- dynamic planner_task/3.
70htn_task(Id,Goal,Init):-planner_task(Id,Goal,Init).
71
74:-dynamic sorts/2,
75 objects/2,
76 predicates/1,
77 atomic_invariants/1,
78 substate_classes/3,
79 method/6,
80 operator/4,
81 inconsistent_constraint/1.
82
90
91op_num(0).
92my_stats(0).
93solution_file(fred).
94
96solve(Id) :-
97 htn_task(Id,Goal,Init),
98 once(planner_interface(Goal,Init,Sol,_)),
107 clean.
108
109display_sol([]).
110display_sol([H|T]) :-
111 write(H),
112 nl,
113 display_sol(T).
114
115clean:-
116 retractall(op_num(_)),
117 retractall(current_num(_,_)),
118 retractall(node(_,_,_,_,_)),
119 retractall(tn(_,_,_,_,_,_)),
120 retractall(gpredicates(_,_)),
121 retractall(gsubstate_classes(_,_,_)),
122 retractall(methodC(_,_,_,_,_,_,_)),
123 retractall(operatorC(_,_,_,_,_)),
124 retractall(goal_related(_)),
125 retractall(produce(_,_,_,_)),
126 retractall(solved_node(_,_)),
127 retractall(tp_node(_,_,_,_,_,_)),
128 retractall(closed_node(_,_,_,_,_,_)),
129 retractall(score_list(_)),
130 assert(op_num(0)),
131 assert(my_stats(0)).
132
133planner_interface(G,I, SOLN,NODES):-
134 time(ground_hierarchy),
135 change_op_representation,
136 find_relate_state,
137 retract(op_num(_)),
138 assert(op_num(0)),
139 statistics(runtime,[_,Time]),
140 (retract(my_stats(_)) ; true),
141 assert(my_stats(Time)),
142 make_problem_into_node(I, G, Node),
143 assert(Node),
144 start_solve(SOLN,NODES).
145planner_interface(G,I, SOLN,NODES):-
146 tell(user),nl,write('failure in initial node'),!.
147
148
149
151
153getN_name(node(Name, _, _, _,_), Name).
154getN_pre(node(_,Pre, _, _, _), Pre).
155getN_decomp(node(_, _, Decomp,_,_), Decomp).
156getH_temp(node(_, _, _,Temps, _), Temps).
157getN_statics(node(_,_,_,_,Statics), Statics).
158
160start_solve(SOLN,NODES):-
161 kill_file(Kill),
162 file_exists(Kill).
164
165start_solve(Sol,OPNUM):-
166 retract(final_node(Node)),
167 retractall(current_num(_,_)),
168 getN_statics(Node,Statics),
169 statics_consist(Statics),
170 extract_solution(Node,Sol,SIZE),
171 statistics(runtime,[_,CP]),
172 TIM is CP/1000,
173 retract(op_num(OPNUM)),
174 assert(op_num(0)),
175
176 nl, nl, write('CPU Time = '),write(CP),nl,
177 write('TIME TAKEN = '),write(TIM),
178 write(' SECONDS'),nl,
179 write('Solution SIZE = '),write(SIZE),nl,
180 write('Operator Used = '),write(OPNUM),nl,
181 write('Solution:\n '),writeq(Sol),nl,
182 write('***************************************'),
183 assert(time_taken(CP)),
184 assert(soln_size(SIZE)),
185 assert(plan_used(OPNUM)),
186 retractall(tn(_,_,_,_,_,_)),!.
187
188start_solve(Sol,OPNUM):-
189 select_node(Node),
191 192 process_node(Node),
193 start_solve(Sol,OPNUM),!.
194start_solve(Sol,OPNUM):-
195 tell(user), write('+++ task FAILED +++'),
196 clean.
197
199
201process_node(Node) :-
202 getN_name(Node, Name),
203 getN_pre(Node, Pre),
204 getN_decomp(Node, Dec),
205 getH_temp(Node, Temps),
206 getN_statics(Node, Statics),
207 expand_decomp(Dec,Pre,Post,Temps,Temp1,Statics,Statics1,Dec1),
208 statics_consist(Statics),
209 assert_node(Name,Pre,Dec1,Temp1,Statics1).
210
211assert_node(Name,Pre,Decomp,Temp,Statics):-
212 all_HP_expanded(Decomp),
213 assert(final_node(node(Name,Pre,Decomp,Temp,Statics))),!.
214assert_node(Name,Pre,Dec,Temp,Statics):-
215 gensym_special(root,SYM),
216 assert(node(SYM,Pre,Dec,Temp,Statics)),!.
217
218all_HP_expanded([]):-!.
219all_HP_expanded([step(HPid,Name,_,_,exp(TN))|THPS]):-
220 all_HP_expanded(THPS),!.
222expand_decomp([],Post,Post,Temp,Temp,Statics,Statics,[]):-!.
224expand_decomp([step(HPid,Name,Pre0,Post0,exp(TN))|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,State1,Post1,exp(TN))|Decomp1]):-
225 state_achieved(Pre0,Pre,State1),
226 state_change(State1,Pre0,Post0,Post1),
227 statics_consist(Statics),
228 expand_decomp(Decomp,Post1,Post,Temp,Temp1,Statics,Statics1,Decomp1),!.
229
231expand_decomp([step(HPid,ACH,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
232 ACH=..[achieve|_],
233 state_achieved(Pre0,Pre,State),
234 statics_consist(Statics),
235 expand_decomp_ach([step(HPid,ACH,State,Post0,unexp)|Decomp],State,Post,Temp,Temp1,Statics,Statics1,Decomp1),!.
262expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(TN))|Decomp1]):-
263 methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH),Dec0),
264 append_cut(Statics0,Statics,Statics2),
265 statics_consist(Statics2),
266 remove_unneed(Statics2,[],Statics3),
267 state_achieved(Pre0,Pre,State1),
269 statics_consist(Statics3),
270 apply_method1(HPid,TN,Name,State1,Pre0,ACH,Post0,State,Statics3,Temp0,Dec0),
275 retract(op_num(N)),
276 N1 is N+1,
277 assert(op_num(N1)),
279 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
280
281
282
283
284
285
286
287
289expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(Name))|Decomp1]):-
290 operatorC(Name,Pre0,Post0,Cond,Statics0),
291 append_cut(Statics0,Statics,Statics2),
292 statics_consist(Statics2),
293 remove_unneed(Statics2,[],Statics21),
294 state_achieved(Pre0,Pre,State1),
295 state_change(State1,Pre0,Post0,State2),
296 cond_state_change(State2,Cond,State),
298 statics_consist_instance(Statics0),
300 retract(op_num(N)),
301 N1 is N+1,
302 assert(op_num(N1)),
303 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics21,Statics1,Decomp1),!.
304
306expand_decomp([step(HP,N,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
307 get_another_step(step(HP2,N2,Pre2,Post2,Exp),Pre,Statics,
308 HP,Temp,Temp2,Decomp,Decomp2),
309 expand_decomp([step(HP2,N2,Pre2,Post2,Exp),step(HP,N,Pre0,Post0,unexp)|Decomp2],Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1).
310
335
337get_another_step(A,Pre,Statics,HP,Temp,Temp1,[],Dec2):-fail.
338get_another_step(step(HP2,Name2,Pre2,Post2,Exp),Pre,Statics,HP,Temp,[before(HP2,HP)|Temp],Dec,Dec2):-
339 member(step(HP2,Name2,Pre2,Post2,Exp),Dec),
340 not(necessarily_before(HP,HP2, Temp)),
341 state_achieved(Pre2,Pre,_),
342 statics_consist(Statics),
343 list_take(Dec,[step(HP2,Name2,Pre2,Post2,Exp)],Dec2).
344
348expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
349 state_achieved(Post0,Pre,State),
350 statics_consist(Statics),
353 remove_temp(Temp,HPid,Temp,Temp2),
354 expand_decomp(Decomp,State,Post,Temp2,Temp1,Statics,Statics1,Decomp1),!.
355
357expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,ACH,Pre,Post0,exp(TN))|Decomp1]):-
358 expand_hp(HPid,TN,ACH,Pre,Post0,State,Statics,Statics2),
359 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
360
362expand_hp(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
363 direct_expand_hp(HPid,TN,ACH,Pre,Post,State,Statics,Statics1),!.
367expand_hp(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
368 make_to_steps1(Pre,Post,Steps),
369 gensym_special(tp,TP),
370 get_all_related_goal(Post),
371 get_score(100,[],Steps,Score),
372 assert(tp_node(TP,Pre,Post,Statics,Score,Steps)),
373 fwsearch(TN,Statics1,State),
374 clean_temp_nodes.
375
379make_to_steps1(Pre,[],[]):-!.
380make_to_steps1(Pre,[se(Sort,Obj,SE)|Post],Steps):-
381 state_achieved([se(Sort,Obj,SE)],Pre,_),
382 make_to_steps1(Pre,Post,Steps),!.
383make_to_steps1(Pre,[se(Sort,Obj,SE)|Post],[step(HP,achieve(se(Sort,Obj,SE)),undefd,[se(Sort,Obj,SE)],unexp)|Steps]):-
384 objects(Sort,Objls), 385 gensym_special(hp,HP),
386 make_to_steps1(Pre,Post,Steps),!.
387make_to_steps1(Pre,[se(Sort,Obj,SE)|Post],[step(HP,achieve(se(Sort,Obj,SE)),undefd,[se(Sort,Obj,SE)],unexp)|Steps]):-
388 var(Obj),
389 gensym_special(hp,HP),
390 make_to_steps1(Pre,Post,Steps),!.
391make_to_steps1(Pre,[se(SortN,Obj,SE)|Post],[step(HP,achieve(se(PSort,Obj,SE)),undefd,[se(PSort,Obj,SE)],unexp)|Steps]):-
392 not(var(Obj)),
393 find_prim_sort(SortN,PSortls),
394 member(PSort,PSortls),
395 objects(PSort,Objls),
396 member(Obj,Objls),
397 gensym_special(hp,HP),
398 make_to_steps1(Pre,Post,Steps),!.
399
400
402get_all_related_goal(Goal):-
403 retractall(goal_related(_)),
404 assert(goal_related(Goal)),
405 get_all_related_goal1(Goal,States),
406 retractall(goal_related(_)),
407 assert(goal_related(States)),!.
409
410get_all_related_goal1([],States):-
411 setof(State,goal_related(State),States1),
412 set_flatten(States1,[],States),!.
413get_all_related_goal1([se(Sort,Obj,SE)|Rest],States):-
414 produce(se(Sort,Obj,SE1),A,OPre,ST),
415 state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
416 retract(goal_related(List)),
417 set_append_e(List,OPre,List1),
418 assert(goal_related(List1)),
419 fail.
420get_all_related_goal1([se(SortN,Obj,SE)|Rest],States):-
421 nonvar(Obj),
422 objects(Sort,Objls),
423 not(not(member(Obj,Objls))),
424 produce(se(Sort,Obj,SE1),A,OPre,ST),
425 state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
426 retract(goal_related(List)),
427 set_append_e(List,OPre,List1),
428 assert(goal_related(List1)),
429 fail.
430get_all_related_goal1([se(SortN,Obj,SE)|Rest],States):-
431 var(Obj),
432 find_prim_sort(SortN,Sortls),
433 member(Sort,Sortls),
434 produce(se(Sort,Obj,SE1),A,OPre,ST),
435 state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
436 retract(goal_related(List)),
437 set_append_e(List,OPre,List1),
438 assert(goal_related(List1)),
439 fail.
440get_all_related_goal1([se(Sort,Obj,SE)|Rest],States):-
441 get_all_related_goal1(Rest,States).
442
445take_out_achieved(Pre,[se(Sort,Obj,SE)],[],[se(Sort,Obj,SE)]):-!.
446take_out_achieved(Pre,[],Post,Post):-!.
449take_out_achieved(Pre,[se(Sort,Obj,ST)|Post],Post0,Post1):-
450 var(Obj),
451 append(Post0,[se(Sort,Obj,ST)],Post2),
452 take_out_achieved(Pre,Post,Post2,Post1),!.
453take_out_achieved(Pre,[se(Sort,Obj,ST)|Post],Post0,Post1):-
454 member(se(Sort,Obj,ST1),Pre),
455 append_diff(Sort,Obj,ST,ST1,Post0,Post2),
456 list_take(Pre,[se(Sort,Obj,ST1)],Pre2),
457 take_out_achieved(Pre2,Post,Post2,Post1),!.
458
460append_diff(Sort,Obj,ST,ST1,Post0,Post0):-
461 not_conflict(Sort,Obj,ST,ST1,STN),!.
462append_diff(Sort,Obj,ST,ST1,Post0,Post1):-
463 append(Post0,[se(Sort,Obj,ST)],Post1),!.
465
467direct_expand_hp(HPid,Tn0,ACH,Pre,Post,Pre1,Statics,Statics):-
468 tn(Tn0,Name,Pre0,Post0,Temp0,Decomp0),
469 state_achieved(Pre0,Pre,State1),
470 state_achieved(Post,Post0,_),
471 state_change(State1,Pre0,Post0,Pre1),
472 statics_consist(Statics),
474 retract(op_num(N)),
475 N1 is N+1,
476 assert(op_num(N1)),!.
478
481direct_expand_hp(HPid,Name,Name,Pre,Post,State,Statics,Statics1):-
482 operatorC(Name,Pre0,Post0,Cond,Statics0),
483 append_cut(Statics0,Statics,Statics1),
484 statics_consist(Statics11),
485 remove_unneed(Statics11,[],Statics1),
486 post_instant(Post0,Cond,Statics1,Post),
487 state_achieved(Pre0,Pre,State1),
488 state_change(State1,Pre0,Post0,State2),
489 cond_state_change(State2,Cond,State3),
490 state_achieved(Post,State3,State),
491 statics_consist_instance(Statics0),
494 retract(op_num(N)),
495 N1 is N+1,
496 assert(op_num(N1)),!.
497
500direct_expand_hp(HPid,Name,ACH,Pre,Post,State,Statics,Statics1):-
501 operatorC(Name,Pre0,Post0,Cond,Statics0),
502 append_cut(Statics0,Statics,Statics2),
503 statics_consist(Statics2),
504 remove_unneed(Statics2,[],Statics1),
505 post_instant(Post0,Cond,Statics1,Post),
506 state_achieved(Pre0,Pre,State1),
507 state_change(State1,Pre0,Post0,State2),
508 cond_state_change(State2,Cond,State3),
509 state_achieved(Post,State3,State),
510 statics_consist(Statics1),
513 retract(op_num(N)),
514 N1 is N+1,
515 assert(op_num(N1)),!.
516
519direct_expand_hp(HPid,TN,ACH,Pre,Post,State1,Statics,Statics1):-
520 methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH0),Dec0),
521 append_cut(Statics0,Statics,Statics2),
522 statics_consist(Statics2),
523 remove_unneed(Statics2,[],Statics1),
524 post_instant(Post0,[],Statics1,Post),
525 state_achieved(Pre0,Pre,Pre1),
526 state_achieved(Post,Post0,Post01),
527 statics_consist(Statics1),
528 apply_method1(HPid,TN,Name,Pre1,Pre0,ACH0,Post01,State1,Statics1,Temp0,Dec0),!.
529
533apply_method(HPid,TN,Name,Pre,Pre0,Post0,State,Statics,Temp,Dec):-
534 make_dec0(HPid,Dec,Temp,Dec0,Temp0),
535 expand_method(Dec0,Pre,Post,Temp0,Temp1,Statics,Statics1,Dec1),
536 state_achieved(Post0,Post,State),
537 statics_consist(Statics),
540 retract(op_num(N)),
541 N1 is N+1,
542 assert(op_num(N1)),
543 make_tn(TN,Name,Pre,State,Temp1,Dec1),!.
544
545make_dec0(HPid,Dec,Temp,Dec0,Temp0):-
546 make_dec01(HPid,1,Dec,Dec0),
547 change_temp(HPid,Temp,[],Temp0),!.
548make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp1,Dec1):-
549 state_achieved(ACH,Pre,_),
550 make_dec01(HPid,1,Dec,Dec1),
551 change_temp(HPid,Temp,[],Temp1).
552make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,[before(STID0,STID1)|Temp1],[step(STID0,achieve(ACH),Pre,ACH,unexp)|Dec1]):-
553 gensym_num(HPid,0,STID0),
554 gensym_num(HPid,1,STID1),
555 make_dec01(HPid,1,Dec,Dec1),
556 change_temp(HPid,Temp,[],Temp1),!.
557
558make_dec01(HPid,_,[],[]):-!.
559make_dec01(HPid,Num,[HDec|TDec],[step(STID,HDec,undefd,undefd,unexp)|TDec0]):-
560 operatorC(HDec,_,_,_,_),
561 gensym_num(HPid,Num,STID),
562 Num1 is Num + 1,
563 make_dec01(HPid,Num1,TDec,TDec0).
564make_dec01(HPid,Num,[HDec|TDec],[step(STID,HDec,undefd,undefd,unexp)|TDec0]):-
565 methodC(HDec,_,_,_,_,_,_),
566 gensym_num(HPid,Num,STID),
567 Num1 is Num + 1,
568 make_dec01(HPid,Num1,TDec,TDec0).
569
570change_temp(HPid,[],Temp2,Temp2):-!.
571change_temp(HPid,[before(N1,N2)|Temp],Temp2,[before(ST1,ST2)|Temp0]):-
572 gensym_num(HPid,N1,ST1),
573 gensym_num(HPid,N2,ST2),
574 change_temp(HPid,Temp,Temp2,Temp0),!.
575
578expand_method([],Post,Post,Temp,Temp,Statics,Statics,[]):-!.
579expand_method([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(TN))|Decomp1]):-
580 tn(TN,Name,Pre0,Post0,Temp0,Decomp0),
581 state_achieved(Pre0,Pre,State1),
582 state_change(State1,Pre0,Post0,State),
583 statics_consist(Statics),
585 retract(op_num(N)),
586 N1 is N+1,
587 assert(op_num(N1)),
589 expand_method(Decomp,State,Post,Temp,Temp1,Statics,Statics1,Decomp1).
590expand_method([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(Name))|Decomp1]):-
591 operatorC(Name,Pre0,Post0,Cond,Statics0),
592 append_cut(Statics0,Statics,Statics2),
593 statics_consist(Statics2),
594 remove_unneed(Statics2,[],Statics21),
595 post_instant(Post0,Cond,Statics21,Post0),
596 state_achieved(Pre0,Pre,State1),
597 state_change(State1,Pre0,Post0,State2),
598 cond_state_change(State2,Cond,State3),
599 state_achieved(Post0,State3,State),
600 statics_consist_instance(Statics0),
603 retract(op_num(N)),
604 N1 is N+1,
605 assert(op_num(N1)),
606 expand_method(Decomp,State,Post,Temp,Temp1,Statics21,Statics1,Decomp1).
607expand_method([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre0,Post0,exp(TN))|Decomp1]):-
608 methodC(Name,Pre0,Post0,Statics0,Temp0,ACH,Dec0),
609 append_cut(Statics0,Statics,Statics2),
610 statics_consist(Statics2),
611 remove_unneed(Statics2,[],Statics21),
612 post_instant(Post0,[],Statics21,Post0),
613 state_achieved(Pre0,Pre,State1),
614 statics_consist(Statics21),
615 apply_method(HPid,TN,Name,State1,Pre0,Post0,State,Statics21,Temp0,Dec0),
616 expand_method(Decomp,State,Post,Temp,Temp1,Statics21,Statics1,Decomp1).
618
622apply_method1(HPid,TN,Name,Pre,Pre0,ACH,Post0,State,Statics,Temp,Dec):-
623 make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp2,Dec2),
624 expand_decomp(Dec2,Pre,Post,Temp2,Temp1,Statics,Statics1,Dec1),
625 state_achieved(Post0,Post,State),
628 retract(op_num(N)),
629 N1 is N+1,
630 assert(op_num(N1)),
631 make_tn(TN,Name,Pre,State,Temp1,Dec1),!.
632
633
635
637fwsearch(TN,ST,State):-
638 retract(solved_node(ST,step(HP,Name,Pre,State,exp(TN)))).
639fwsearch(TN,ST,State):-
640 select_tnode(tp_node(TP,Pre,Post,Statics,Score,Dec)),
641 assert(closed_node(TP,Pre,Post,Statics,Score,Dec)),
642 expand_node(Statics,Statics1,Pre,Post,Dec,[],Dec1),
643 assert_tnode(TP,Pre,Post,Statics1,Score,Dec1),
644 solved_node(_,_), 645 fwsearch(TN,ST,State).
646fwsearch(TN,ST,State):-
647 tp_node(TP,Pre,Post,Statics,Score,Dec),
648 fwsearch(TN,ST,State).
649
650clean_temp_nodes:-
651 retractall(current_num(tp,_)),
652 retractall(tp_node(_,_,_,_,_,_)),
653 retractall(closed_node(_,_,_,_,_,_)),!.
654
656expand_node(Statics,Statics,Pre,Post,[],List,List):-
657 state_achieved(Post,Pre,_),!.
658expand_node(Statics,Statics1,Pre,Post,[step(HP,Name,_,Post0,unexp)|Rest],List,Dec1):-
659 state_achieved(Post0,Pre,_),
660 expand_node(Statics,Statics1,Pre,Post,Rest,List,Dec1),!.
661expand_node(Statics,Statics1,Pre,Post,[step(HP,Name,_,Post0,unexp)|Rest],List,Dec1):-
662 direct_expand_hp(HP,TN,Name,Pre,Post0,State,Statics,Statics2),
663 append(List,[step(HP,Name,Pre,State,exp(TN))],List2),
664 remove_achieved_rest(State,Statics2,Rest,Rest1),
665 make_to_steps(State,Post,Steps,Rest1),
666 append(Rest1,Steps,Rest2),
667 expand_node(Statics2,Statics1,State,Post,Rest2,List2,Dec1).
668expand_node(Statics,Statics1,Pre,Post,[step(HP,Name,_,Post0,unexp)|Rest],List,Dec1):-
669 apply_op(Statics,Statics1,[step(HP,Name,Pre,Post0,unexp)|Rest],List,Dec1).
670expand_node(Statics,Statics1,Pre,Post,[step(HP,Name,Pre0,Post0,exp(TN))|TDec],List,Dec1):-
671 append(List,[step(HP,Name,Pre0,Post0,exp(TN))],List2),
672 expand_node(Statics,Statics1,Post0,Post,TDec,List2,Dec1),!.
673
674remove_achieved_rest(State,Statics,[],[]):-!.
675remove_achieved_rest(State,Statics,[step(HP,Name,_,Post0,unexp)|Rest],Rest1):-
676 state_achieved(Post0,State,_),
677 remove_achieved_rest(State,Statics,Rest,Rest1),!.
678remove_achieved_rest(State,Statics,[HP|Rest],[HP|Rest1]):-
679 remove_achieved_rest(State,Statics,Rest,Rest1),!.
680
683apply_op(Statics,Statics1,[step(HP,Name,Pre,[se(Sort,Obj,SE)],unexp)|Rest],List,Dec):-
684 produce(se(Sort,Obj,SE1),OP,OPre,ST),
685 append_cut(Statics,ST,Statics2),
686 statics_consist(Statics2),
687 remove_unneed(Statics2,[],Statics1),
688 state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
689 instantiate(OPre,Statics1),
690 not(member(step(_,OP,_,_,_),Rest)),
691 make_to_steps(Pre,OPre,Steps,Rest),
692 change_head_state(Pre,Steps,Steps1),
693 append(List,Steps1,List2),
694 append(List2,[step(HP,OP,undefd,[se(Sort,Obj,SE)],unexp)|Rest],Dec).
696apply_op(Statics,Statics1,[step(HP,Name,Pre,[se(SortN,Obj,SE)],unexp)|Rest],List,Dec):-
697 find_prim_sort(SortN,PSortls),
698 member(Sort,PSortls),
699 produce(se(Sort,Obj,SE1),OP,OPre,ST),
700 append_cut(Statics,ST,Statics2),
701 statics_consist(Statics2),
702 remove_unneed(Statics2,[],Statics1),
703 state_achieved([se(Sort,Obj,SE)],[se(Sort,Obj,SE1)],_),
704 instantiate(OPre,Statics1),
705 not(member(step(_,OP,_,_,_),Rest)),
706 make_to_steps(Pre,OPre,Steps,Rest),
707 change_head_state(Pre,Steps,Steps1),
708 append(List,Steps1,List2),
709 append(List2,[step(HP,OP,undefd,[se(Sort,Obj,SE)],unexp)|Rest],Dec).
711
714make_to_steps(Pre,[],[],Rest):-!.
715make_to_steps(Pre,[se(Sort,Obj,SE)|Post],Steps,Rest):-
716 state_achieved([se(Sort,Obj,SE)],Pre,_),
717 make_to_steps(Pre,Post,Steps,Rest),!.
718make_to_steps(Pre,[se(Sort,Obj,SE)|Post],Steps,Rest):-
719 member(step(_,achieve(se(Sort,Obj,SE)),_,_),Rest),
720 make_to_steps(Pre,Post,Steps,Rest),!.
721make_to_steps(Pre,[se(Sort,Obj,SE)|Post],[step(HP,achieve(se(Sort,Obj,SE)),undefd,[se(Sort,Obj,SE)],unexp)|Steps],Rest):-
722 gensym_special(hp,HP),
723 make_to_steps(Pre,Post,Steps,Rest),!.
724
725change_head_state(Pre,[step(HP,A,undefd,B,unexp)|Steps],[step(HP,A,Pre,B,unexp)|Steps]):-!.
726
728instantiate([],Statics).
729instantiate([se(Sort,Obj,SE)|States],Statics):-
730 nonvar(Obj),
731 instantiate(States,Statics),!.
732instantiate([se(Sort,Obj,SE)|States],Statics):-
733 ground(SE),
734 instantiate(States,Statics),!.
735instantiate([se(Sort,Obj,SE)|States],Statics):-
736 var(Obj),
737 get_sort_objects(Sort,Objls),
738 member(Obj,Objls),
739 statics_consist_instance(Statics),
740 instantiate(States,Statics).
741
742
744select_tnode(tp_node(HPid,Pre,Post,Statics,Score,Dec)) :-
745 retractall(score_list(LS)),
746 assert(score_list([])),
747 lowest_score(Score),
748 retract(tp_node(HPid,Pre,Post,Statics,Score,Dec)),!.
750
752lowest_score(LScore):-
753 tp_node(HPid,Pre,Post,Statics,Score,Dec),
754 retract(score_list(LS)),
755 assert(score_list([Score|LS])),
756 fail.
757lowest_score(LScore):-
758 retract(score_list(D)),
759 sort(D,[LScore|SD]).
760
761:- style_check(-discontiguous).
763assert_tnode(TP,Pre,Post,Statics,Score1,Dec):-
764 all_HP_expanded(Dec),
765 combine_exp_steps2(Dec,OneDec),
766 assert(solved_node(Statics,OneDec)),!.
767
768combine_exp_steps2([OneDec],OneDec):-!.
769combine_exp_steps2([Dec1,Dec2|Rest],OneDec):-
770 combine_steps1(Dec1,Dec2,OneDec1),
771 combine_exp_steps2([OneDec1|Rest],OneDec),!.
776assert_tnode(TP,Pre,Post,Statics,Score,Dec):-
777 combine_exp_steps(Dec,ExpDec,UnexpDec),
778 assert_tnode1(TP,Pre,Post,Statics,Score,ExpDec,UnexpDec),!.
779
780:- style_check(+discontiguous).
781
782assert_tnode1(TP,Pre,Post,Statics,Score,ExpDec,UnexpDec):-
783 existing_node(ExpDec,UnexpDec,Statics).
784assert_tnode1(TP,Pre,Post,Statics,Score,ExpDec,UnexpDec):-
785 get_score(Score,ExpDec,UnexpDec,Score1),
786 gensym_special(tp,TP1),
787 append(ExpDec,UnexpDec,Dec),
788 assert(tp_node(TP1,Pre,Post,Statics,Score1,Dec)),!.
789
791combine_exp_steps([step(A,B,C,D,unexp)|Rest],[],[step(A,B,C,D,unexp)|Rest]):-!.
792combine_exp_steps([step(A,B,C,D,exp(TN)),step(A1,B1,C1,D1,unexp)|Rest],[step(A,B,C,D,exp(TN))],[step(A1,B1,C1,D1,unexp)|Rest]):-!.
793combine_exp_steps([step(A1,B1,C1,D1,exp(TN1)),step(A2,B2,C2,D2,exp(TN2))|Rest],ExpDec,UnexpDec):-
794 combine_steps1(step(A1,B1,C1,D1,exp(TN1)),step(A2,B2,C2,D2,exp(TN2)),ExpDec1),
795 combine_exp_steps([ExpDec1|Rest],ExpDec,UnexpDec),!.
796
797combine_steps1(step(HP1,Name1,Pre1,Post1,exp(TN1)),step(HP2,Name2,Pre2,Post2,exp(TN2)),step(HP,achieve(Post2),Pre1,Post2,exp(TN))):-
798 find_only_changed(Pre1,Post2,[],Pre0,[],Post0),
799 not(isemptylist(Post0)),
800 gensym_special(hp,HP),
801 gensym_special(tn,TN),
802 assert(tn(TN,achieve(Post0),Pre0,Post0,[before(TN1,TN2)],[step(HP1,Name1,Pre1,Post1,exp(TN1)),step(HP2,Name2,Pre2,Post2,exp(TN2))])),!.
803
805existing_node([step(_,_,Pre,Post,exp(TN))],UnexpDec,Statics):-
806 tp_node(_,_,_,Statics0,_,[step(_,_,Pre0,Post0,exp(TN0))|Rest]),
807 append_st(Statics,Statics0,Statics1),
808 all_equal(Pre,Pre0,Statics1),
809 all_equal(Post,Post0,Statics1),
810 existing_node([],UnexpDec,Statics1).
811existing_node([step(_,_,Pre,Post,exp(TN))],UnexpDec,Statics):-
812 closed_node(_,_,_,Statics0,_,[step(_,_,Pre0,Post0,exp(TN0))|Rest]),
813 append_st(Statics,Statics0,Statics1),
814 all_equal(Pre,Pre0,Statics1),
815 all_equal(Post,Post0,Statics1),
816 existing_node([],UnexpDec,Statics1).
817existing_node([],[step(_,Name,Pre,_,unexp)|Rest],Statics):-
818 tp_node(_,_,_,Statics0,_,[step(_,Name,Pre0,_,_,unexp)|Rest]),
819 append_st(Statics,Statics0,Statics1),
820 all_equal(Pre,Pre0,Statics1).
821existing_node([],[step(_,Name,Pre,_,unexp)|Rest],Statics):-
822 closed_node(_,_,_,Statics0,_,[step(_,Name,Pre0,_,_,unexp)|Rest]),
823 append_st(Statics,Statics0,Statics1),
824 all_equal(Pre,Pre0,Statics1).
825
826
827
828
829
830
831
832all_equal([],[],Statics):-
833 statics_consist(Statics),!.
834all_equal([se(Sort,Obj,SE)|Pre],[se(Sort,Obj,SE1)|Pre1],Statics):-
835 is_achieved(SE,SE1),
836 all_equal(Pre,Pre1,Statics),!.
837all_equal([se(Sort,Obj,SE)|Pre],Pre1,Statics):-
838 member(se(Sort,Obj,SE1),Pre1),
839 list_take(Pre1,[se(Sort1,Obj1,SE1)],Pre2),
840 all_equal(Pre,Pre1,Statics),!.
841
842get_score(Score,[],UnExpDec,Score1):-
843 length(UnExpDec,Len),
844 Score1 is Score+Len,!.
845get_score(Score,[step(HP,Name,Pre0,Post0,exp(TN))],UnExpDec,Score1):-
846 goal_related(States),
847 state_achieved_Num(Post0,States,0,Num),
848 length(UnExpDec,Len),
849 Score1 is Score-Num,!.
850
854state_achieved_Num([],Pre0,Num,Num):-!.
855state_achieved_Num([se(Sort,Obj,SE)|Post],Pre0,N,Num):-
856 member(se(Sort,Obj,SE0),Pre0),
857 state_achieved([se(Sort,Obj,SE0)],[se(Sort,Obj,SE)],_),
858 N1 is N+1,
859 state_achieved_Num(Post,Pre0,N1,Num),!.
860state_achieved_Num([se(Sort,Obj,SE)|Post],Pre0,N,Num):-
861 state_achieved_Num(Post,Pre0,N,Num),!.
862
867find_relate_state:-
868 operatorC(A,Pre,Post,Cond,ST),
869 assert_related_states(A,Pre,Post,Cond,ST),
870 fail.
871find_relate_state.
872
873assert_related_states(A,Pre,Post,Cond,ST):-
874 assert_related_states1(A,Pre,Post,ST),
875 assert_related_states2(A,Pre,Cond,ST).
876
879assert_related_states1(A,Pre,[],ST):-!.
881assert_related_states1(A,Pre,[se(Sort,Obj,SE)|Post],ST):-
882 member_e(se(Sort,Obj,SE),Pre),
883 assert_related_states1(A,Pre,Post,ST),!.
885assert_related_states1(A,Pre,[se(Sort,Obj,SE)|Post],ST):-
886 assert(produce(se(Sort,Obj,SE),A,Pre,ST)),
887 assert_related_states1(A,Pre,Post,ST),!.
888
891assert_related_states2(A,Pre,SC,ST):-
892 make_sc_primitive(SC,PSC),
893 assert_related_states21(A,Pre,PSC,ST).
894
895assert_related_states21(A,Pre,[],ST):-!.
896assert_related_states21(A,Pre,[sc(Sort,Obj,SE=>SS)|Trans],ST):-
897 rem_statics([se(Sort,Obj,SE)],[se(Sort,Obj,SER)],St1),
898 rem_statics([se(Sort,Obj,SS)],[se(Sort,Obj,SSR)],St2),
899 append_cut(ST,St1,ST1),
900 append_cut(ST1,St2,ST21),
901 remove_unneed(ST21,[],ST2),
902 append_cut(Pre,[se(Sort,Obj,SER)],Pre1),
903 assert(produce(se(Sort,Obj,SSR),A,Pre1,ST2)),
904 assert_related_states21(A,Pre,Trans,ST),!.
905
909pre_changed_Num([],Pre0,Num,Num):-!.
910pre_changed_Num([se(Sort,Obj,SE)|Pre],Pre0,N,Num):-
911 state_achieved([se(Sort,Obj,SE)],Pre0,_),
912 N1 is N+1,
913 pre_changed_Num(Pre,Pre0,N1,Num),!.
914pre_changed_Num([se(Sort,Obj,SE)|Pre],Pre0,N,Num):-
915 pre_changed_Num(Pre,Pre0,N,Num),!.
916
917
918
923remove_temp([],HP1,List,List):-!.
924remove_temp([before(HP1,HP2)|Temp],HP1,List,Temp1):-
925 remove_temp_before(List,before(HP1,HP2),List2),
926 remove_temp(Temp,HP1,List2,Temp1),!.
927remove_temp([before(HP2,HP1)|Temp],HP1,List,Temp1):-
928 remove_temp_after(List,before(HP2,HP1),List2),
929 remove_temp(Temp,HP1,List2,Temp1),!.
930remove_temp([before(HPX,HPY)|Temp],HP1,List,Temp1):-
931 remove_temp(Temp,HP1,List,Temp1),!.
932
934remove_temp_before([],before(HP1,HP2),[]):-!.
935remove_temp_before([before(HP1,HP2)|T],before(HP1,HP2),T1):-
936 remove_temp_before(T,before(HP1,HP2),T1),!.
937remove_temp_before([before(HP3,HP1)|T],before(HP1,HP2),[before(HP3,HP2)|T1]):-
938 remove_temp_before(T,before(HP1,HP2),T1),!.
939remove_temp_before([before(HPX,HPY)|T],before(HP1,HP2),[before(HPX,HPY)|T1]):-
940 remove_temp_before(T,before(HP1,HP2),T1),!.
942remove_temp_after([],before(HP1,HP2),[]):-!.
943remove_temp_after([before(HP2,HP1)|T],before(HP2,HP1),T1):-
944 remove_temp_after(T,before(HP2,HP1),T1),!.
945remove_temp_after([before(HP1,HP3)|T],before(HP2,HP1),[before(HP2,HP3)|T1]):-
946 remove_temp_after(T,before(HP2,HP1),T1),!.
947remove_temp_after([before(HPX,HPY)|T],before(HP2,HP1),[before(HPX,HPY)|T1]):-
948 remove_temp_after(T,before(HP2,HP1),T1),!.
949
950remove_dec(HPid,[],[]):-!.
951remove_dec(HPid,[step(HPid,_,_,_,_)|Dec],Dec1):-
952 remove_dec(HPid,Dec,Dec1),!.
953remove_dec(HPid,[step(A,B,C,D,F)|Dec],[step(A,B,C,D,F)|Dec1]):-
954 remove_dec(HPid,Dec,Dec1),!.
955
959state_achieved(undefd,State,State):-!.
960state_achieved(State1,State2,State3):-
961 state_achieved1(State1,State2,State3).
962
963state_achieved1([],State2,State2).
964state_achieved1([se(Sort,Obj,ST1)|State1],State2,[se(Sort,Obj,STN)|State]):-
965 get_sort_objects(Sort,Objls),
966 member(Obj,Objls),
967 member(se(Sort,Obj,ST2),State2),
968 not_conflict(Sort,Obj,ST1,ST2,STN),
969 list_take(State2,[se(Sort,Obj,ST2)],State21),
970 state_achieved1(State1,State21,State).
971state_achieved1([se(Sort1,Obj,ST1)|State1],State2,[se(Sort,Obj,STN)|State]):-
972 not(objects(Sort1,Objls)),
973 find_prim_sort(Sort1,Sortls),
974 member(se(Sort,Obj,ST2),State2),
975 member(Sort,Sortls),
976 not_conflict(Sort,Obj,ST1,ST2,STN),
977 list_take(State2,[se(Sort,Obj,ST2)],State21),
978 state_achieved1(State1,State21,State).
979
981not_conflict(Sort,Obj,ST,ST1,NewST):-
982 gsubstate_classes(Sort,Obj,Substateclasses),
983 member(State0,Substateclasses),
984 is_achieved(ST,State0),
985 is_achieved(ST1,State0),
986 set_append_e(ST,ST1,NewST),!.
987
989is_achieved([],_):-!.
990is_achieved([H|T], State) :-
991 is_statics(H),
992 is_achieved(T,State),!.
993is_achieved([H|T], State) :-
994 member(H,State),
995 is_achieved(T,State),!.
996
998is_statics(ne(A,B)):-!.
999is_statics(is_of_sort(A,B)):-!.
1000is_statics(is_of_primitive_sort(A,B)):-!.
1001is_statics(Pred):-
1002 functor(Pred,FF,NN),
1003 functor(Pred1,FF,NN),
1004 atomic_invariants(Atom),
1005 member(Pred1,Atom),!.
1006
1007
1009
1013state_change([],Pre0,Post0,[]):-!.
1014state_change(Pre,[],[],Pre):-!.
1017state_change([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,NewSS)|State]):-
1018 member(se(Sort,Obj,SE0),Pre0),
1019 member(se(Sort,Obj,SS0),Post0),
1020 not_conflict(Sort,Obj,SE,SE0,_),
1021 state_change1(SE,SE0,SS0,NewSS),
1022 list_take(Pre0,[se(Sort,Obj,SE0)],Pre1),
1023 list_take(Post0,[se(Sort,Obj,SS0)],Post1),
1024 state_change(Pre,Pre1,Post1,State),!.
1027state_change([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,SE)|State]):-
1028 state_change(Pre,Pre0,Post0,State),!.
1029
1030state_change1([],SE0,SS0,SS0):-!.
1031state_change1([HSE|TSE],SE0,SS0,SS1):-
1032 member(HSE,SE0),
1033 state_change1(TSE,SE0,SS0,SS1),!.
1034state_change1([HSE|TSE],SE0,SS0,[HSE|SS1]):-
1035 state_change1(TSE,SE0,SS0,SS1),!.
1041cond_state_change([],Cond,[]):-!.
1042cond_state_change(State,[],State):-!.
1043cond_state_change([se(Sort,Obj,SE)|Pre],Cond,[se(Sort,Obj,NewSS)|State]):-
1044 member(sc(Sort1,Obj,SE0=>SS0),Cond),
1045 statics_consist([is_of_sort(Obj,Sort1),is_of_sort(Obj,Sort)]),
1046 not_conflict(Sort,Obj,SE,SE0,_),
1047 state_change1(SE,SE0,SS0,NewSS),
1048 cond_state_change(Pre,Cond,State),!.
1049cond_state_change([se(Sort,Obj,SE)|Pre],Cond,[se(Sort,Obj,SE)|State]):-
1050 cond_state_change(Pre,Cond,State),!.
1051
1055post_changed(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1056 member(se(Sort,Obj,SE0),Post0),
1057 state_achieved([se(Sort,Obj,SE0)],[se(Sort,Obj,SE)],_),
1058 statics_consist(Statics).
1059post_changed(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1060 member(sc(Sort,Obj,SE1=>SS),Cond),
1061 state_achieved([se(Sort,Obj,SS)],[se(Sort,Obj,SE)],_),
1062 statics_consist(Statics).
1063post_changed(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1064 post_changed(Post0,Cond,Statics,Post).
1065
1066all_achieved([],Statics,Post).
1067all_achieved([se(Sort,Obj,SL)|Pre],Statics,Post):-
1068 not(member(se(Sort,Obj,SR),Post)),
1069 all_achieved(Pre,Statics,Post).
1070all_achieved([se(Sort,Obj,SL)|Pre],Statics,Post):-
1071 member(se(Sort,Obj,SR),Post),
1072 is_achieved(SL,SR),
1073 statics_consist_instance(Statics),
1074 all_achieved(Pre,Statics,Post).
1075
1076
1080post_instant(Post0,Cond,Statics,[]):-!.
1081post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1082 member(se(Sort,Obj,SE0),Post0),
1083 statics_consist(Statics).
1084post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1085 member(sc(Sort,Obj,SE1=>SS),Cond),
1086 statics_consist(Statics).
1087post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1088 member(sc(Sort0,Obj,SE1=>SS),Cond),
1089 not(objects(Sort0,_)),
1090 subsorts(Sort0,Sortls),
1091 not(not(member(Sort,Sortls))),
1092 statics_consist(Statics).
1093post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1094 post_instant(Post0,Cond,Statics,Post),!.
1096
1100
1101make_problem_into_node(I,goal(L,TM,STATS), NN) :-
1102 make_problem_up(L, STEPS),
1103 make_num_hp(TM,Temp),
1104 sort_steps(STEPS,Temp,STEPS1),
1105 make_ss_to_se(I,I_Pre),
1106 NN = node(root,I_Pre,STEPS1 ,Temp, STATS),!.
1107
1109make_problem_up([],[]):-!.
1110make_problem_up([achieve(L)|R],[step(HP,achieve(L1),undefd,[L1],unexp)|RS]):-
1111 1112 make_ss_to_se([L],[L1]),
1113 gensym_special(hp,HP),
1114 make_problem_up(R, RS),!.
1115make_problem_up([achieve(L)|R],[step(HP,achieve(L1),undefd,L1,unexp)|RS]):-
1116 1117 make_ss_to_se(L,L1),
1118 gensym_special(hp,HP),
1119 make_problem_up(R, RS),!.
1120make_problem_up([O|R],[step(HP,O,undefd,undefd,unexp)|RS]):-
1121 methodC(O,Pre,Post,Statics1,Temp,ACH,Dec),
1122 gensym_special(hp,HP),
1123 make_problem_up(R, RS),!.
1124make_problem_up([O|R],
1125 [step(HP,O,undefd,undefd,unexp)|RS]):-
1126 operatorC(O,Pre,Post,Cond,Statics1),
1127 gensym_special(hp,HP),
1128 make_problem_up(R, RS),!.
1129
1130make_num_hp([],[]):-!.
1131make_num_hp([before(N1,N2)|TM],[before(H1,H2)|Temp]):-
1132 gensym_num(hp,N1,H1),
1133 gensym_num(hp,N2,H2),
1134 make_num_hp(TM,Temp),!.
1135
1138sort_steps(Steps,[],Steps):-!.
1139sort_steps([Steps|[]],[],[Steps]):-!.
1140sort_steps(Steps,Temp,OrderedST):-
1141 steps_in_temp(Temp,[],ST),
1142 sort_steps1(Temp,ST,OrderedSTID),
1143 sort_steps2(Steps,OrderedSTID,[],OrderedST),!.
1144
1146steps_in_temp([],ST,ST):-!.
1147steps_in_temp([before(H1,H2)|TT],List,ST):-
1148 set_append_e(List,[H1,H2],List1),
1149 steps_in_temp(TT,List1,ST),!.
1150
1152sort_steps1(Temp,[],[]):-!.
1153sort_steps1(Temp,[HP1|TST],[HPF|OST]):-
1154 earliest_step(HP1,HPF,Temp,TST,TST1),
1155 sort_steps1(Temp,TST1,OST),!.
1156
1157earliest_step(HPF,HPF,Temp,[],[]):-!.
1158earliest_step(HP1,HPF,Temp,[HP2|TST],[HP1|TST1]):-
1159 member(before(HP2,HP1),Temp),
1160 earliest_step(HP2,HPF,Temp,TST,TST1),!.
1161earliest_step(HP1,HPF,Temp,[HP2|TST],[HP2|TST1]):-
1162 earliest_step(HP1,HPF,Temp,TST,TST1),!.
1163
1165sort_steps2(OtherST,[],OrderedST1,OrderedST):-
1166 append(OrderedST1,OtherST,OrderedST),!.
1167sort_steps2(Steps,[HP|THPS],List,OrderedST):-
1168 member(step(HP,N,Pre,Post,F),Steps),
1169 append(List,[step(HP,N,Pre,Post,F)],List1),
1170 list_take(Steps,[step(HP,N,Pre,Post,F)],Steps1),
1171 sort_steps2(Steps1,THPS,List1,OrderedST),!.
1172sort_steps2(Steps,[HP|THPS],List,OrderedST):-
1173 sort_steps2(Steps,THPS,List,OrderedST),!.
1175
1177make_ss_to_se([],[]):-!.
1178make_ss_to_se([ss(Sort,Obj,Post)|TPost],[se(Sort,Obj,Post)|TPre]):-
1179 make_ss_to_se(TPost,TPre),!.
1180make_ss_to_se([se(Sort,Obj,Post)|TPost],[se(Sort,Obj,Post)|TPre]):-
1181 make_ss_to_se(TPost,TPre),!.
1182
(Node,PHPs,SIZE1) :-
1188 1189 getN_decomp(Node, HPs),
1190 push_to_primitive(HPs,[],PHPs),
1191 pprint(PHPs,1,SIZE),
1192 SIZE1 is SIZE -1,!.
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1216change_op_representation :-
1217 method(A,B,C,Stat,T,Dec),
1218 make_ss_to_se(B,B0),
1219 make_se_primitive(B0,B1),
1220 make_sc_primitive(C,C1),
1221 get_preconditions(C1,B1,Pre,Post),
1222 rem_statics(Post, PostR,St1),
1223 rem_statics(Pre, PreR,St2),
1224 append_cut(St1,St2,Statics),
1225 append_cut(Stat,Statics,Statics1),
1226 remove_unneed(Statics1,[],Statics2),
1227 get_achieval(A,Dec,T,Dec1,T1,ACH),
1228 assert(methodC(A,PreR,PostR,Statics2,T1,achieve(ACH),Dec1)),
1229 fail.
1230change_op_representation :-
1231 operator(A,B,C,D),
1232 make_ss_to_se(B,B0),
1233 make_se_primitive(B0,B1),
1234 make_sc_primitive(C,C1),
1236 1237 get_preconditions(C1,B1,Pre,Post),
1238 rem_statics(Post, PostR,St1),
1239 rem_statics(Pre, PreR,St2),
1240 append_cut(St1,St2,Statics1),
1241 remove_unneed(Statics1,[],Statics),
1242 statics_consist(Statics),
1243 assert(operatorC(A,PreR,PostR,D,Statics)),
1244 fail.
1245change_op_representation:-
1246 retractall(current_num(sm,_)),!.
1247
1248get_preconditions([],Prev,Prev,Prev) :-!.
1249get_preconditions([sc(S,X,From =>To)|Rest],Prev,[se(S,X,From1)|Pre],[se(S,X,To1)|Post]):-
1250 member_e(se(S,X,PSE),Prev),
1251 append(PSE,From,From1),
1252 append(PSE,To,To1),
1253 list_take(Prev,[se(S,X,PSE)],Prev1),
1254 get_preconditions(Rest,Prev1, Pre,Post),!.
1255get_preconditions([sc(S,X,From =>To)|Rest],Prev,[se(S,X,From)|Pre],[se(S,X,To)|Post]):-
1256 get_preconditions(Rest,Prev, Pre,Post),!.
1257get_preconditions([],Prev,Prev,Prev) :-!.
1258
1260get_achieval(A,Dec,T,Dec1,T1,Achieval):-
1261 retractall(current_num(sm,_)),
1262 make_dec(A,Dec,Dec1,T,T1,[],Achieval),!.
1263make_dec(A,[],[],Temp,Temp,Achieval,Achieval):-!.
1264make_dec(A,[HD|TD],TD1,Temp,Temp1,Achieval,Achieval1):-
1265 HD=..[achieve|Goal],
1266 current_num(sm,Num),
1267 replace_achieval_temp(Temp,Temp0,Num),
1268 make_ss_to_se(Goal,Goal0),
1269 append(Achieval,Goal0,Achieval0),
1270 make_dec(A,TD,TD1,Temp0,Temp1,Achieval0,Achieval1),!.
1271make_dec(A,[HD|TD],TD1,Temp,Temp1,Achieval,Achieval1):-
1272 HD=..[achieve|Goal],
1273 not(current_num(sm,Num)),
1274 replace_achieval_temp(Temp,Temp0,1),
1275 make_ss_to_se(Goal,Goal0),
1276 append(Achieval,Goal0,Achieval0),
1277 make_dec(A,TD,TD1,Temp0,Temp1,Achieval0,Achieval1).
1278make_dec(A,[HD|TD],[HD|TD1],Temp,Temp1,Achieval,Achieval1):-
1279 HD=..[DecName|Goal],
1280 DecName\==achieve,
1281 gensym_special(sm,SM),
1282 current_num(sm,Num),
1283 make_dec(A,TD,TD1,Temp,Temp1,Achieval,Achieval1),!.
1284
1286replace_achieval_temp(Temp,Temp1,Num):-
1287 change_all_numbers(Temp,Num,Temp00),
1288 tidy_temp(Temp00,Temp1).
1289
1290change_all_numbers([],Num,[]):-!.
1291change_all_numbers([HTemp|TTemp],Num,[HTemp00|TTemp00]):-
1292 HTemp=..[before|Nums],
1293 change_nums(Nums,Num,Nums1),
1294 HTemp00=..[before|Nums1],
1295 change_all_numbers(TTemp,Num,TTemp00).
1296
1297change_nums([],Num,[]):-!.
1298change_nums([Num1|TN],Num,[Num1|TN1]):-
1299 Num1<Num,
1300 change_nums(TN,Num,TN1),!.
1301change_nums([Num1|TN],Num,[Num2|TN1]):-
1302 Num1>Num,
1303 Num2 is Num1-1,
1304 change_nums(TN,Num,TN1),!.
1305change_nums([Num|TN],Num,[0|TN1]):-
1306 change_nums(TN,Num,TN1),!.
1307
1309tidy_temp(Temp,Temp1):-
1310 member(before(Num,0),Temp),
1311 list_take(Temp,[before(Num,0)],Temp0),
1312 change_laters(Temp0,Num,Temp01),
1313 tidy_temp(Temp01,Temp1).
1314tidy_temp([],[]):-!.
1315tidy_temp([before(0,Num)|Temp],Temp0):-
1316 tidy_temp(Temp,Temp0),!.
1317tidy_temp([before(Num1,Num2)|Temp],[before(Num1,Num2)|Temp0]):-
1318 tidy_temp(Temp,Temp0),!.
1319
1320change_laters([before(0,Num2)|Temp],Num,[before(Num,Num2)|Temp0]):-
1321 change_laters(Temp,Num,Temp0).
1322change_laters([before(Num1,0)|Temp],Num,[before(Num1,0)|Temp0]):-
1323 change_laters(Temp,Num,Temp0).
1324change_laters([before(Num1,Num2)|Temp],Num,[before(Num1,Num2)|Temp0]):-
1325 change_laters(Temp,Num,Temp0).
1326
1328make_se_primitive([],[]).
1329make_se_primitive([se(Sort,Obj,ST)|SE],[se(Sort,Obj,ST)|SE0]):-
1330 find_prim_sort(Sort,[Sort]),!,
1331 make_se_primitive(SE,SE0).
1332make_se_primitive([se(Sort,Obj,ST)|SE],[se(PSort,Obj,ST)|SE0]):-
1333 find_prim_sort(Sort,PSorts),
1334 member(PSort,PSorts),
1335 make_se_primitive(SE,SE0).
1336
1338make_sc_primitive([],[]).
1339make_sc_primitive([sc(Sort,Obj,SE1=>SE2)|ST],[sc(Sort,Obj,SE1=>SE2)|ST0]):-
1340 find_prim_sort(Sort,[Sort]),!,
1341 make_sc_primitive(ST,ST0).
1342make_sc_primitive([sc(Sort,Obj,SE1=>SE2)|ST],[sc(PSort,Obj,SE1=>SE2)|ST0]):-
1343 find_prim_sort(Sort,PSorts),
1344 member(PSort,PSorts),
1345 make_sc_primitive(ST,ST0).
1347make_tn(TN,Name,Pre,Post,Temp,Dec):-
1348 gensym_special(tn,TN),
1349 find_only_changed(Pre,Post,[],Pre1,[],Post1),
1351 assert(tn(TN,Name,Pre1,Post1,Temp,Dec)),!.
1352
1353find_only_changed([],[],Pre,Pre,Post,Post):-!.
1355find_only_changed([se(Sort,Obj,ST)|Pre],[se(Sort,Obj,ST)|Post],Pre0,Pre1,Post0,Post1):-
1356 find_only_changed(Pre,Post,Pre0,Pre1,Post0,Post1),!.
1357find_only_changed([se(Sort,Obj,ST)|Pre],Post,Pre0,Pre1,Post0,Post1):-
1358 member(se(Sort,Obj,ST1),Post),
1359 list_take(Post,[se(Sort,Obj,ST1)],Post2),
1360 append_changed(se(Sort,Obj,ST),se(Sort,Obj,ST1),Pre0,Pre3,Post0,Post3),
1361 find_only_changed(Pre,Post2,Pre3,Pre1,Post3,Post1),!.
1362find_only_changed([se(Sort,Obj,ST)|Pre],Post,Pre0,Pre1,Post0,Post1):-
1363 member(se(SortN,Obj,ST1),Post),
1364 list_take(Post,[se(SortN,Obj,ST1)],Post2),
1365 append_changed(se(Sort,Obj,ST),se(SortN,Obj,ST1),Pre0,Pre3,Post0,Post3),
1366 find_only_changed(Pre,Post2,Pre3,Pre1,Post3,Post1),!.
1368
1371append_changed(se(Sort,Obj,ST),se(Sort1,Obj,ST1),Pre0,Pre0,Post0,Post0):-
1372 not_conflict(Sort,Obj,ST,ST1,_),!.
1373append_changed(se(Sort,Obj,ST),se(Sort1,Obj,ST1),Pre0,Pre3,Post0,Post3):-
1374 append(Pre0,[se(Sort,Obj,ST)],Pre3),
1375 append(Post0,[se(Sort,Obj,ST1)],Post3),!.
1376
1377
1378
1380push_to_primitive([],PHPs,PHPs) :-!.
1381push_to_primitive([step(HPID,_,_,_,exp(TN))|HPs],List,PHPs) :-
1382 tn(TN,Name,Pre,Post,Temp,Dec),
1383 push_to_primitive(Dec,List,Dec1),
1384 push_to_primitive(HPs,Dec1,PHPs),!.
1385push_to_primitive([step(HPID,_,_,_,exp(Name))|HPs],List,PHPs):-
1386 append(List,[Name],List1),
1387 push_to_primitive(HPs,List1,PHPs),!.
1388
1389
1391
1392possibly_before(I,J,Temps) :-
1393 \+ necessarily_before(J,I,Temps), !.
1394
1395necessarily_before(J,I,Temps) :-
1396 member(before(J,I),Temps),!.
1397necessarily_before(J,I,Temps) :-
1398 member(before(J,Z),Temps),
1399 necessarily_before(Z,I,Temps),!.
1400
1402
1404select_node(node(Name,Pre,Temp,Decomp,Statics)) :-
1405 retract(node(Name,Pre,Temp,Decomp,Statics)),
1410 !.
1411
1412is_of_primitive_sort(X,Y) :-
1413 objects(Y,L),member(X,L).
1414is_of_sort(X,Y) :-
1415 is_of_primitive_sort(X,Y).
1416is_of_sort(X,Y) :-
1417 sorts(Y,SL),member(Z,SL),is_of_sort(X,Z).
1418
1419find_all_upper([],[]).
1420find_all_upper([HVars|TV],[HSorts|TS]):-
1421 uppersorts(HSorts,Upsorts),
1422 member(HVars,Upsorts),
1423 find_all_upper(TV,TS).
1424
1426find_prim_sort(Sort,PS):-
1427 subsorts(Sort,Subsorts),
1428 split_prim_noprim(Subsorts,PS,NP),!.
1429
1431get_sort_objects(Sort,Objs):-
1432 find_prim_sort(Sort,PSorts),
1433 get_objects1(PSorts,Objls),
1434 flatten(Objls,[],Objs),!.
1435
1436get_objects1([],[]):-!.
1437get_objects1([PS1|RS],[Objls1|Objls]):-
1438 objects(PS1,Objls1),
1439 get_objects1(RS,Objls),!.
1440
1442subsorts(Sort,Subsorts):-
1443 sort_down([Sort],[Sort],Subsorts),!.
1444
1445sort_down([],Subsorts,Subsorts):-!.
1446sort_down([HOpen|TOpen],List,Sortslist):-
1447 sorts(HOpen,Sorts),
1448 append(List,Sorts,List1),
1449 append(TOpen,Sorts,Open1),
1450 sort_down(Open1,List1,Sortslist),!.
1451sort_down([HOpen|TOpen],List,Sortslist):-
1452 sort_down(TOpen,List,Sortslist),!.
1453
1455uppersorts(Sort,Uppersorts):-
1456 objects(Sort,Objls),
1457 sort_up(Sort,[Sort],Uppersorts),!.
1458uppersorts(Sort,Uppersorts):-
1459 sorts(Sort,Sortls),
1460 sort_up(Sort,[Sort],Uppersorts),!.
1461uppersorts(Obj,Sortls):-
1462 objects(Sort,Objls),
1463 member(Obj, Objls),
1464 sort_up(Sort,[Sort],Sortls),!.
1465
1466sort_up(Sort, List,Sortslist):-
1467 sorts(non_primitive_sorts,NPSorts),
1468 sort_up1(Sort,NPSorts,NPSorts,List,Sortslist),!.
1469
1470sort_up1(Sort,[],NPSorts,Sortslist,Sortslist):-!.
1471sort_up1(Sort,[HNPSorts|TNPSorts],NPSorts,List,Sortslist):-
1472 sorts(HNPSorts,Sorts),
1473 member(Sort,Sorts),
1474 append(List, [HNPSorts], List1),
1475 sort_up(HNPSorts,List1,Sortslist),!.
1476sort_up1(Sort,[HNPSorts|TNPSorts],NPSorts,List,Sortslist):-
1477 sort_up1(Sort,TNPSorts,NPSorts,List,Sortslist),!.
1478
1480split_prim_noprim([],[],[]):-!.
1481split_prim_noprim([HS|TS],[HS|TP],NP):-
1482 objects(HS,Obj),
1483 split_prim_noprim(TS,TP,NP),!.
1484split_prim_noprim([HS|TS],PS,[HS|NP]):-
1485 split_prim_noprim(TS,PS,NP),!.
1486
1487
1488
1489
1490
1492statics_consist_instance([]):-!.
1493statics_consist_instance(Statics) :-
1494 get_invariants(Invs),
1495 statics_consist_instance0(Invs,Statics).
1496
1497statics_consist_instance0(Invs,[]):-!.
1498statics_consist_instance0(Invs,[ne_back(A,B)|TStatics]):-
1499 not(A==B),
1500 statics_consist_instance0(Invs,TStatics).
1501statics_consist_instance0(Invs,[ne(A,B)|TStatics]):-
1502 append(TStatics,[ne_back(A,B)],TStatics1),
1503 statics_consist_instance0(Invs,TStatics1),!.
1504statics_consist_instance0(Invs,[is_of_sort(Obj,Sort)|TStatics]):-
1505 is_of_sort(Obj,Sort),
1506 statics_consist_instance0(Invs,TStatics).
1507statics_consist_instance0(Invs,[is_of_primitive_sort(Obj,Sort)|TStatics]):-
1508 is_of_primitive_sort(Obj,Sort),
1509 statics_consist_instance0(Invs,TStatics).
1510statics_consist_instance0(Invs,[Pred|TStatics]):-
1511 member(Pred,Invs),
1512 statics_consist_instance0(Invs,TStatics).
1513
1516statics_consist([]):-!.
1517statics_consist(Statics):-
1518 get_invariants(Invs),
1519 statics_consist1(Invs,Statics),!.
1520
1521statics_consist1(Invs,[]):-!.
1522statics_consist1(Invs,[ne_back(A,B)|TStatics]):-
1523 not(A==B),
1524 statics_consist1(Invs,TStatics),!.
1525statics_consist1(Invs,[ne(A,B)|TStatics]):-
1526 append(TStatics,[ne_back(A,B)],TStatics1),
1527 statics_consist1(Invs,TStatics1),!.
1528statics_consist1(Invs,[is_of_sort(Obj,Sort)|TStatics]):-
1529 get_sort_objects(Sort,Objs),
1530 obj_member(Obj,Objs),
1531 statics_consist1(Invs,TStatics),!.
1532statics_consist1(Invs,[is_of_primitive_sort(Obj,Sort)|TStatics]):-
1533 objects(Sort,Objs),
1534 obj_member(Obj,Objs),
1535 statics_consist1(Invs,TStatics),!.
1536statics_consist1(Invs,[Pred|TStatics]):-
1537 pred_member(Pred,Invs),
1538 statics_consist1(Invs,TStatics),!.
1539
1541
1543get_invariants(Invs) :-
1544 atomic_invariants(Invs),!.
1545
1546rem_statics([ss(S,X,Preds)|Post], [ss(S,X,PredR)|PostR],Rt1) :-
1547 filter_list(Preds, is_a_dynamic_pred,PredR),
1548 filter_list(Preds, is_a_static_pred, R),
1549 rem_statics(Post, PostR,Rt),
1550 append(Rt,[is_of_sort(X,S)|R],Rt1),!.
1551rem_statics([se(S,X,Preds)|Post], [se(S,X,PredR)|PostR],Rt1) :-
1552 filter_list(Preds, is_a_dynamic_pred,PredR),
1553 filter_list(Preds, is_a_static_pred, R),
1554 rem_statics(Post, PostR,Rt),
1555 append(Rt,[is_of_sort(X,S)|R],Rt1),!.
1556rem_statics([], [],[]) :-!.
1557
1558
1559
1561
1562isemptylist([]):-!.
1563
1564
1565member_cut(X,[X|_]) :- !.
1566member_cut(X,[_|Y]) :- member_cut(X,Y),!.
1567
1568member_e(X,[Y|_]):-
1569 X==Y,!.
1570member_e(X,[Y|L]):-
1571 var(Y),
1572 member_e(X,L),!.
1573member_e(ss(Sort,Obj,SE),[ss(Sort,Obj1,SE)|_]):-
1574 Obj==Obj1,!.
1575member_e(se(Sort,Obj,SE),[se(Sort,Obj1,SE)|_]):-
1576 Obj==Obj1,!.
1577member_e(sc(Sort,Obj,SE1=>SE2),[sc(Sort,Obj1,SE1=>SE2)|_]):-
1578 Obj==Obj1,!.
1579member_e(X,[Y|L]):- member_e(X,L),!.
1582u_mem_cut(_,[]):-!,fail.
1583u_mem_cut(X,[Y|_]) :- X == Y,!.
1584u_mem_cut(X,[_|L]) :- u_mem_cut(X,L),!.
1585
1586
1591obj_member(X,[X|[]]):-!.
1592obj_member(X,List):-
1593 obj_member0(X,List),!.
1594obj_member0(X,[Y|_]):-
1595 var(X),!. 1596obj_member0(X,[Y|_]):-
1597 X==Y,!.
1598obj_member0(X,[_|Y]) :- obj_member0(X,Y),!.
1599
1604pred_member(ne(A,B),List):-
1605 A\==B,!.
1606pred_member(is_of_sort(A,B),List):-
1607 get_sort_objects(B,Objls),
1608 obj_member(A,Objls),!.
1609pred_member(is_of_primitive_sort(A,B),List):-
1610 objects(B,Objls),
1611 obj_member(A,Objls),!.
1612pred_member(X,List):-
1613 setof(X,member(X,List),Refined),
1614 pred_member0(X,Refined),!.
1615pred_member0(X,[X|[]]):-!.
1616pred_member0(X,Y):-
1617 pred_member1(X,Y),!.
1618pred_member1(X,[Y|_]):-
1619 X=..[H|XLs],
1620 Y=..[H|YLs],
1621 vequal(XLs,YLs),!.
1622pred_member1(X,[_|Y]):- pred_member1(X,Y),!.
1623
1624
1625
1626append_cut([],L,L) :- !.
1627append_cut([H|T],L,[H|Z]) :- append_cut(T,L,Z),!.
1628
1632append_st(ST1,ST2,ST):-
1633 append_cut(ST1,ST2,ST0),
1634 remove_unneed(ST0,[],ST),!.
1635
1638remove_unneed([],C,C):-!.
1639remove_unneed([A|B], Z, C):-
1640 var(A),
1641 member_e(A,Z),
1642 remove_unneed(B, Z, C),! .
1643remove_unneed([A|B], Z, C):-
1644 var(A),
1645 append(Z,[A],D),
1646 remove_unneed(B, D, C),!.
1647remove_unneed([A|B], Z, C):-
1648 ground(A),
1649 remove_unneed(B, Z, C),!.
1650remove_unneed([A|B], Z, C):-
1651 A=..[ne|Paras],
1652 append(Z,[A],D),
1653 remove_unneed(B, D, C),!.
1654remove_unneed([A|B], Z, C):-
1655 A=..[Pred|Paras],
1656 same_var_member(A,Z),
1657 remove_unneed(B, Z, C),!.
1658remove_unneed([A|B], Z, C):-
1659 append(Z,[A],D),
1660 remove_unneed(B, D, C),!.
1661
1662same_var_member(Pred,[Pred1|List]):-
1663 var(Pred1),
1664 same_var_member(Pred,List),!.
1665same_var_member(Pred,[Pred1|List]):-
1666 Pred==Pred1,!.
1667same_var_member(Pred,[Pred1|List]):-
1668 Pred=..[H|T],
1669 Pred1=..[H|T1],
1670 same_var_member1(T,T1),!.
1671same_var_member(Pred,[Pred1|List]):-
1672 same_var_member(Pred,List),!.
1673
1674same_var_member1([],[]):-!.
1675same_var_member1([H1|T],[H2|T]):-
1676 var(H1),
1677 H1==H2,!.
1678same_var_member1([H|T1],[H|T2]):-
1679 var(T1),
1680 T1==T2,!.
1681same_var_member1([H1|T1],[H2|T2]):-
1682 H1==H2,
1683 same_var_member1(T1,T2),!.
1684
1685
1689set_append_e(A,B,C):-
1690 append_cut(A,B,D),
1691 remove_dup(D,[],C),!.
1692
1694remove_dup([],C,C):-!.
1695remove_dup([A|B],Z,C) :-
1696 member_e(A, Z),
1697 remove_dup(B, Z, C),! .
1698remove_dup([A|B], Z, C):-
1699 append(Z,[A],D),
1700 remove_dup(B, D, C),!.
1701
1702vequal([],[]):-!.
1703vequal([X|XLs],[Y|YLs]):-
1704 var(X),
1705 vequal(XLs,YLs),!.
1706vequal([X|XLs],[Y|YLs]):-
1707 var(Y),
1708 vequal(XLs,YLs),!.
1709vequal([X|XLs],[Y|YLs]):-
1710 X==Y,
1711 vequal(XLs,YLs),!.
1712
1713
1715
1717is_a_static_pred(X) :-
1718 atomic_invariants( A ),
1719 not( not( member(X,A) )),!.
1720is_a_static_pred(ne(_,_)) :-!.
1721is_a_static_pred(is_of_sort(_,_)) :-!.
1722
1723is_a_dynamic_pred(X) :-
1724 not( is_a_static_pred(X) ),!.
1725
1729
1730filter_list([X|Rest],Op,[X|Rest1]) :-
1731 Op =.. OL,
1732 append(OL,[X],OL1),
1733 Pred =.. OL1,
1734 call(Pred),
1735 filter_list(Rest,Op,Rest1),!.
1736filter_list([_|Rest],Op,Rest1) :-
1737 filter_list(Rest,Op,Rest1),!.
1738filter_list([],_,[]).
1739
1740
1741
1750
1751file_exists(Filename):-exists_file(Filename).
1752
1755subtract([],_,[]):-!.
1756subtract([A|B],C,D) :-
1757 member(A,C),
1758 subtract(B,C,D),!.
1759subtract([A|B],C,[A|D]) :-
1760 subtract(B,C,D),!.
1761
1763
1764list_take(R,[E|R1],R2):-
1765 remove_el(R,E,RR),
1766 list_take(RR,R1,R2),!.
1767list_take(R,[_|R1],R2):-
1768 list_take(R,R1,R2),!.
1769list_take(A,[],A) :- !.
1770
1771 1773remove_el([],_,[]) :- ! .
1774remove_el([A|B],A,B) :- ! .
1775remove_el([A|B],C,[A|D]) :-
1776 remove_el(B,C,D) .
1777
1779
1780gensym_special(Root,Atom) :-
1781 getnum(Root,Num),
1782 name(Root,Name1),
1783 name(Num,Name2),
1784 append(Name1,Name2,Name),
1785 name(Atom,Name).
1786
1787getnum(Root,Num) :-
1788 retract(current_num(Root,Num1)),!,
1789 Num is Num1+1,
1790 asserta(current_num(Root,Num)).
1791
1792getnum(Root,1) :- asserta(current_num(Root,1)).
1793
1794
1796
1797gensym_num(Root,Num,Atom):-
1798 name(Root,Name),
1799 name(Num,Name1),
1800 append(Name,Name1,Name2),
1801 name(Atom,Name2),!.
1802
1803
1805pprint([],SIZE,SIZE):-!.
1806pprint([HS|TS],Size0,SIZE):-
1807 list(HS),
1808 pprint(HS,Size0,Size1),
1809 pprint(TS,Size1,SIZE),!.
1810pprint([HS|TS],Size0,SIZE):-
1813 Size1 is Size0+1,
1814 pprint(TS,Size1,SIZE),!.
1815
1816
1818
1819flatten([HO|TO], List, O_List):-
1820 append(HO, List, List_tmp),
1821 flatten(TO, List_tmp, O_List),!.
1822flatten([H|TO], List,O_List):-
1823 append([H], List, List_tmp),
1824 flatten(TO, List_tmp, O_List).
1825flatten([], [HList|T], O_List):-
1826 HList = [],
1827 flatten(T, [], O_List).
1828flatten([], [HList|T], O_List):-
1829 list(HList),
1830 flatten([HList|T],[], O_List),!.
1831flatten([], L,L):-!.
1832
1834set_flatten([HO|TO], List, O_List):-
1835 set_append_e(HO, List, List_tmp),
1836 set_flatten(TO, List_tmp, O_List),!.
1837set_flatten([H|TO], List,O_List):-
1838 set_append_e([H], List, List_tmp),
1839 set_flatten(TO, List_tmp, O_List).
1840set_flatten([], [HList|T], O_List):-
1841 HList = [],
1842 set_flatten(T, [], O_List).
1843set_flatten([], [HList|T], O_List):-
1844 list(HList),
1845 set_flatten([HList|T],[], O_List),!.
1846set_flatten([], L,L):-!.
1847
1850list(A) :-
1851 var(A) ,
1852 ! ,
1853 fail .
1854list(A) :-
1855 functor(A,'.',_).
1857:- assert(time_taken(0)).
1858:- assert(soln_size(0)).
1859:- assert(plan_used(0)).
1860
1861solve(N,FN):-
1862 N < FN,
1863 tell(user),
1864 nl,write('task '), write(N),write(': '),nl,
1865 solution_file(F),
1866 tell(F),
1867 nl,write('task '), write(N),write(': '),nl,
1868 solve(N),
1869 Ni is N+1,
1870 solve(Ni,FN).
1871solve(FN,FN):-
1872 tell(user),nl,write('task '),
1873 write(FN),write(': '),nl,
1874 solution_file(F),
1875 tell(F),
1876 nl,write('task '),
1877 write(FN),write(': '),nl,
1878 solve(FN),
1879 retractall(sum(_)),
1880 assert(sum(0)),
1881 sum_time(CP),
1882 retractall(sum(_)),
1883 assert(sum(0)),
1884 sum_size(SIZE),
1885 TIM is CP /1000,
1886 retractall(sum(_)),
1887 assert(sum(0)),
1888 sum_plan(PLAN),
1889 retractall(time_taken(_)),
1890 retractall(soln_size(_)),
1891 retractall(plan_used(_)),
1892 nl,write('total time '),write(TIM),write(' seconds'),
1893 nl,write('total size '),write(SIZE),
1894 nl,write('plan used '),write(PLAN),
1895 nl,
1896 told.
1897
1898sum_time(TIM):-
1899 time_taken(CP),
1900 retract(sum(N)),
1901 N1 is N +CP,
1902 assert(sum(N1)),
1903 fail.
1904sum_time(TIM):-
1905 sum(TIM).
1906sum_size(SIZE):-
1907 soln_size(S),
1908 retract(sum(N)),
1909 N1 is N +S,
1910 assert(sum(N1)),
1911 fail.
1912sum_size(SIZE):-
1913 sum(SIZE).
1914
1915
1916sum_plan(Plan):-
1917 plan_used(PL),
1918 retract(sum(N)),
1919 N1 is N +PL,
1920 assert(sum(N1)),
1921 fail.
1922sum_plan(Plan):-
1923 sum(Plan).
1924
1925
1927ground_hierarchy:-
1928 ground_predicates,
1929 ground_substate_class,!.
1930
1933ground_predicates:-
1934 predicates(Preds),
1935 grounding_preds(Preds),
1936 collect_grounded_pred,!.
1937
1938grounding_preds([]).
1940grounding_preds([HPred|TP]):-
1941 functor(HPred,FF,NN),
1942 functor(APred,FF,NN),
1943 atomic_invariants(Atom),
1944 member(APred,Atom),
1945 ground_all_var_atom(HPred),
1946 grounding_preds(TP).
1948grounding_preds([HPred|TP]):-
1949 functor(HPred,FF,NN),
1950 functor(GPred,FF,NN),
1951 ground_all_var(GPred,HPred),
1952 grounding_preds(TP).
1953
1955collect_grounded_pred:-
1956 gpred(Pred,_),
1957 setof(GPred,gpred(Pred,GPred),GPredls),
1958 retractall(gpred(Pred,_)),
1959 assert(gpredicates(Pred,GPredls)),
1960 fail.
1961collect_grounded_pred.
1962
1965ground_substate_class:-
1966 substate_classes(Sort,Obj,Substate),
1967 find_prim_sort(Sort,PS),
1968 assert_subclass(PS,Obj,Substate),
1969 fail.
1970ground_substate_class:-
1971 collect_grounded_substates.
1972
1973assert_subclass([],Obj,Substate).
1974assert_subclass([HS|TS],Obj,Substate):-
1975 assert(gsstates(HS,Obj,Substate)),
1976 assert_subclass(TS,Obj,Substate).
1977
1978collect_grounded_substates:-
1979 gsstates(Sort,Obj,_),
1980 setof(SStates,gsstates(Sort,Obj,SStates),GSStates),
1981 retractall(gsstates(Sort,Obj,_)),
1982 all_combined(GSStates,GSStates0),
1983 assert(gsubstate_classes(Sort,Obj,GSStates0)),
1984 fail.
1985collect_grounded_substates.
1986
1987all_combined(SStates,CSStates):-
1988 xprod(SStates,CSStates1),
1989 flat_interal(CSStates1,CSStates),!.
1990
1991flat_interal([],[]):-!.
1992flat_interal([HSS1|TSS1],[HSS|TSS]):-
1993 flatten(HSS1,[],HSS),
1994 flat_interal(TSS1,TSS),!.
1995
1996ground_all_var_atom(HPred):-
1997 functor(HPred,FF,NN),
1998 functor(GPred,FF,NN),
1999 functor(APred,FF,NN),
2000 atomic_invariants(Atom),
2001 member(APred,Atom),
2002 GPred=..[Name|Vars],
2003 APred=..[Name|Sorts],
2004 ground_all_var_atom0(Vars,Sorts),
2005 assert_gpred(GPred),
2006 fail.
2007ground_all_var_atom(HPred).
2008
2009ground_all_var_atom0([],[]).
2010ground_all_var_atom0([HVars|TV],[HSorts|TS]):-
2011 subsorts(HSorts,Subsorts),
2012 split_prim_noprim(Subsorts,PSorts,NPSorts),
2013 member(LS,PSorts),
2014 objects(LS,Objls),
2015 member(HVars,Objls),
2016 ground_all_var_atom0(TV,TS).
2017ground_all_var_atom0([HVar|TV],[HASorts|TS]):-
2018 objects(Sorts,Objls),
2019 member(HASorts,Objls),
2020 HVar=HASorts,
2021 ground_all_var_atom0(TV,TS).
2022
2023ground_all_var(GPred,HPred):-
2024 GPred=..[Name|Vars],
2025 HPred=..[Name|Sorts],
2026 ground_all_var0(Vars,Sorts),
2027 not(inconsistent_constraint([GPred])),
2028 assert_gpred(GPred),
2029 fail.
2030ground_all_var(GPred,HPred).
2031
2032ground_all_var0([],[]).
2033ground_all_var0([HVars|TV],[HSorts|TS]):-
2034 objects(HSorts,Objls),
2035 member(HVars,Objls),
2036 ground_all_var0(TV,TS).
2037ground_all_var0([HVars|TV],[HSorts|TS]):-
2038 subsorts(HSorts,Subsorts),
2039 split_prim_noprim(Subsorts,PSorts,NPSorts),
2040 member(LS,PSorts),
2041 objects(LS,Objls),
2042 member(HVars,Objls),
2043 ground_all_var0(TV,TS).
2044
2046
2047assert_gpred(GPred):-
2048 functor(GPred,FF,NN),
2049 functor(UPred,FF,NN),
2050 assert_gpred0(GPred,UPred),!.
2051
2052assert_gpred0(GPred,UPred):-
2053 GPred=..[Name|PSorts],
2054 UPred=..[Name|Vars],
2055 get_obj_sort(Vars,PSorts),
2056 assert(gpred(UPred,GPred)),
2057 fail.
2058assert_gpred0(GPred,UPred).
2059
2060get_obj_sort([],[]):-!.
2061get_obj_sort([HVars|TV],[HObj|TS]):-
2062 objects(HVars,Objls),
2063 member(HObj,Objls),
2064 get_obj_sort(TV,TS),!.
2065
2066delete_all_nodes :-
2067 retractall(node(_,_,_,_,_)),
2068 retractall(final_node(_)),
2069 retractall(tp_node(_,_,_,_,_,_)),
2070 retractall(closed_node(_,_,_,_,_,_)),
2071 retractall(solved_node(_,_,_)).
2072delete_all_nodes :- !.
2073
2074
2077xprod(A,B,C) :-
2078 xprod([A,B],C) .
2079
2080xprod([],[]).
2081xprod(A,E) :-
2082 xprod(A,B,C,D) ,
2083 F =..[^,C,D] ,
2084 call(setof(B,F,E)) .
2085
2086xprod([X],[A],A,member(A,X)) .
2087xprod([X,Y],[A,B],C,(D,E)) :-
2088 C =..[^,A,B] ,
2089 D =..[member,A,X] ,
2090 E =..[member,B,Y] .
2091xprod([X|Y],[A|E],D,(F,G)) :-
2092 D =..[^,A,C] ,
2093 F =..[member,A,X] ,
2094 xprod(Y,E,C,G).
2095
2096
2097:- include(translog4_domain).
2098:- include(translog4_ops).
2101:- include(translog4_problems).
2102
2103:- fixup_exports.
htncode
% Provides a prolog database env % % % % Logicmoo Project PrologMUD: A MUD server written in Prolog % Maintainer: Douglas Miles % Denton, TX 2005, 2010, 2014 % Dec 13, 2035 % */