4:- module(ec_lps_convert,[ 5 6 assert_lps/1,
7 test_logicmoo_ec_lps_reader/0,
8 test_lps_ereader/0,
9 test_logicmoo_ec_sanity/0,
10 test_logicmoo_ec_reader_2/0,
11 test_logicmoo_ec_lps_reader/2,test_logicmoo_ec_lps_reader/1]). 12
13skip_tests.
14
15test_logicmoo_ec_sanity:- skip_tests,!.
16test_logicmoo_ec_sanity:- test_lps_ereader.
17
18:- use_module(library(logicmoo_common)). 19
20:- use_module(library(ec_planner/ec_loader)). 21
26:- use_module(library(logicmoo_lps)). 28:- user:use_module(library('ec_planner/ec_planner_dmiles')). 29:- use_module(library(ec_planner/ec_reader)). 30
31:- use_module(library(lps_corner)). 32
33:- set_prolog_flag(lps_translation_only_HIDE,false). 34:- set_prolog_flag(lps_translation_only,false). 35
36
37assert_lps(user:ec_current_domain_db(Stuff,_)):-!, assert_lps(Stuff).
38assert_lps(axiom(Stuff,Nil)):- Nil==[],!,assert_lps(Stuff).
39assert_lps(axiom(Stuff,List)):- !,must_or_rtrace(list_to_conjuncts(List,Body)),!,assert_lps(->(Body,Stuff)).
40assert_lps(Stuff):- assert_ep(lps_test_mod,Stuff).
41
42print_tree_cmt(C,P):-
43 mention_o_s_l,
44 notrace((echo_format('~N'),
45 with_output_to(string(S), in_cmt((
46 format('~NFrom E: \n\n',[]),
47 print_tree(P)))),
48 to_ansi(C, C0),
49 real_ansi_format(C0, '~s', [S]))).
50
51already_lps(Form):- var(Form),!,throw(var_already_lps(Form)).
52already_lps(:- _):-!.
53already_lps(option(_,_)):-!.
54already_lps(false(_)):-!.
55already_lps(mpred_prop(_,_)):-!.
56already_lps(sort(_)):-!.
57already_lps(subsort(_,_)):-!.
58
59
60assert_ep( _M,Form):- print_tree_cmt(white,Form),fail.
61assert_ep(Mod,Form):- already_lps(Form),assert_post(Mod,Form,Form).
62assert_ep(Mod,Form):-
63 format('~N',[]),
64 must_or_rtrace(ep_to_lps(Form,Lps)),
65 major_debug(ignore((Form\==Lps->pprint_ecp_cmt(hfg(green),Form)))),
66 must_or_rtrace(assert_post(Mod,Form,Lps)),!.
67
68
73assert_post(Mod,Form,Lps):- nortrace, is_list(Lps),!, maplist(assert_post(Mod,Form),Lps).
74assert_post(Mod,t(Type,Inst),_):- atom(Type), M=..[Type,Inst],!,assert_post(Mod,M,M),!.
75assert_post(Mod,_Form,Lps):-
76 lps_xform(Mod,Lps,Prolog),!,
77 must_or_rtrace((Lps\==Prolog->(ignore(( 78 print_lps_syntax(yellow,Lps),
79 nop(pprint_ecp(yellow,Lps)))),
80 pprint_ecp_cmt(cyan,Prolog),pprint_ecp_cmt(white,"% ================================="))
81 ;assert_lps_try_harder(Prolog))),
82 must_or_rtrace(assert_prolog(Mod,Prolog)),!.
83
84print_lps_syntax(Color,Lps):-
85 with_lps_operators2(user,
86 ec_lps_convert:with_lps_operators2(pretty_clauses,pretty_clauses:clause_to_string(Lps,S))),
87 real_ansi_format(hfg(Color), '~N~s.~N', [S]),!.
88
89lps_xform(Mod,Lps,Prolog):-
90 locally(current_prolog_flag(lps_translation_only_HIDE,true),
91 locally(t_l:lps_program_module(Mod),
92 must_or_rtrace(lps_f_term_expansion_now(Mod,Lps,Prolog)))),!.
93
94:- export_transparent(with_lps_operators2/2). 95with_lps_operators2(M,Goal):-
96 setup_call_cleanup(push_operators(M:[op(900, fy, M:not), op(1200, xfx, M:then), op(1185, fx, M:if), op(1190, xfx, M:if), op(1100, xfy, M:else), op(1050, xfx, M:terminates), op(1050, xfx, M:initiates), op(1050, xfx, M:updates), op(1050, fx, M:observe), op(1050, fx, M:false), op(1050, fx, M:initially), op(1050, fx, M:fluents), op(1050, fx, M:events), op(1050, fx, M:prolog_events), op(1050, fx, M:actions), op(1050, fx, M:unserializable), op(999, fx, M:update), op(999, fx, M:initiate), op(999, fx, M:terminate), op(997, xfx, M:in), op(995, xfx, M:at), op(995, xfx, M:during), op(995, xfx, M:from), op(994, xfx, M:to), op(1050, xfy, M: ::), op(1200, xfx, M:(<-)), op(1050, fx, M:(<-)), op(700, xfx, M: <=)],Undo),
97 M:call(Goal),pop_operators(Undo)).
98
99:- export_transparent(with_lps_operators/1). 100with_lps_operators(MGoal):-
101 strip_module(MGoal,M,Goal),
102 with_lps_operators2(user,ec_lps_convert:with_lps_operators2(M,M:Goal)).
103
104
105assert_prolog(Mod,Prolog):- is_list(Prolog),!, maplist(assert_prolog(Mod),Prolog).
106assert_prolog(_Mod,Prolog):- 107 major_debug(pprint_ecp_pl(yellow,Prolog)).
108
109include_e_lps_file_now(Type,MFile):- strip_module(MFile,M,File), include_e_lps_file_now(Type,M,File).
110include_e_lps_file_now(Type,M,File):- absolute_file_name(File,AbsFile),File\==AbsFile,exists_file(AbsFile), !,include_e_lps_file_now(Type,M,AbsFile).
111
112include_e_lps_file_now(Type,Mod,File):-
113 translate_e_to_filetype(Mod:Type,File).
114
115load_e_lps_file(Type,File):- retractall(etmp:ec_option(load(_), _)), include_e_lps_file(Type,File).
116load_e_lps_file(Type,File):- retractall(etmp:ec_option(load(_), _)), include_e_lps_file(Type,File).
117
118include_e_lps_file(Type,File):- is_list(File), !, maplist(include_e_lps_file(Type),File).
119include_e_lps_file(Type,File):- wdmsg(include_e_lps_file(Type,File)),fail.
120include_e_lps_file(Type,File):- needs_resolve_local_files(File,Resolved),!,include_e_lps_file(Type,Resolved).
121include_e_lps_file(Type,File):- absolute_file_name(File,DB), exists_file(DB),!,
122 update_changed_files,
123 strip_module(_,M,_), prolog_statistics:time(M:include_e_lps_file_now(Type,File)),!.
124include_e_lps_file(Type,File):- throw(with_abs_paths(include_e_lps_file(Type),File)).
125
126
127test_logicmoo_ec_lps_reader(File):- test_logicmoo_ec_lps_reader(lps, File).
128test_logicmoo_ec_lps_reader(Proc1,File):- load_e_lps_file(Proc1,File).
129
130test_logicmoo_ec_lps_reader:- skip_tests, !.
131test_logicmoo_ec_lps_reader:-
132 test_logicmoo_ec_lps_reader([ec('ecnet/Diving.e'), ec('foundations/*.e'), ec('ecnet/*.e')]).
133
134test_logicmoo_ec_reader_2:-
135 test_logicmoo_ec_lps_reader(library('../test/ec_planner/*/*/*/*.e')).
136
137test_lps_ereader:- skip_tests, !.
138test_lps_ereader:-
139 convert_e(assert_ep(test_lps_mod),user_error,
140 [ec('ecnet/Diving.e'), ec('foundations/*.e'), ec('ecnet/*.e'),
141 library('../test/ec_planner/*/*/*/*.e')]).
142
143get_time_arg(Holds1,T1):- compound_gt(Holds1,1),
144 functor(Holds1,F,_),
145 time_arg(F,N),
146 arg(N,Holds1,T1),can_be_time_arg(T1),( var(T1); compound(T1)),!.
147
148ep_to_lps_remove_time(FormI,Form):- bagof(Time,Sub^(sub_term(Sub,FormI),get_time_arg(Sub,Time)),TArgs),
149 sort(TArgs,STArgs),
150 pprint_ecp_cmt(green,STArgs),
151 ((STArgs=[Time],var(Time)) -> (remove_time_arg(Time,FormI,Form), \+ sub_var(Time,Form)) ; FormI=Form),!.
152ep_to_lps_remove_time(FormI,FormI).
153
154ep_to_lps(Form,Lps):- \+ compound(Form),!,Lps=Form.
155ep_to_lps(Form,Lps):- already_lps(Form),!,Lps=Form.
156ep_to_lps(t(Type,Inst),Lps):- atom(Type), M=..[Type,Inst],!,ep_to_lps(M,Lps).
157ep_to_lps(FormI,LpsO):-
158 ep_to_lps_remove_time(FormI,Form),
159 ep_to_lps_arg(1,[],Form,Lps), ep_to_lps_arg(2,[],Lps,LpsO),!.
160
162ep_to_lps_pass2(Lps,LpsO):- subst(Lps,holds_at,at,LpsO).
163ep_to_lps_pass2(Lps,LpsO):- must_or_rtrace((fix_axiom_head(_Global,Lps,LpsM),over_pass(2,[],LpsM,LpsO))),!.
164
165ep_to_lps_arg(_Pass,_Top,Form,Lps):- \+ compound(Form),!,Lps=Form.
166ep_to_lps_arg(_Pass,_Top,Form,Lps):- already_lps(Form),!,Lps=Form.
168ep_to_lps_arg(Pass, Top,Form,Lps):-
169 compound_name_arguments(Form,F,Args),
170 maplist(ep_to_lps_arg(Pass,[F|Top]),Args,ArgsO),
171 compound_name_arguments_maybe_zero(LpsM,F,ArgsO),
172 over_pass(Pass,Top,LpsM,Lps),!.
173
174compound_name_arguments_maybe_zero(F,F,[]):- !.
175compound_name_arguments_maybe_zero(LpsM,F,ArgsO):- compound_name_arguments(LpsM,F,ArgsO).
176
177:- use_module(library(lps_syntax)). 178
179over_pass_ex(_Top, X, _):- \+ compound(X),!,fail.
180over_pass_ex(_Top,'->'(at(F1,T1),initiates(E,F2,T2)),Becomes):- T1==T2,
181 Becomes = (F1->initiates(E,F2)).
182over_pass_ex(_Top,'->'(at(F1,T1),terminates(E,F2,T2)),Becomes):- T1==T2,
183 Becomes = (F1->terminates(E,F2)).
184over_pass_ex(_Top,'->'(holds_at(F1,T1),initiates(E,F2,T2)),Becomes):- T1==T2,
185 Becomes = (F1->initiates(E,F2)).
186over_pass_ex(_Top,'->'(holds_at(F1,T1),terminates(E,F2,T2)),Becomes):- T1==T2,
187 Becomes = (F1->terminates(E,F2)).
188
189over_pass(_Pass,_Top, X, X):- \+ compound(X),!.
190over_pass(_Pass,_Top, X, X):- functor(X,_,1), arg(1,X,Var), is_ftVar(Var),!.
191over_pass(_Pass,_Top,at(X,Y),loc_at(X,Y)).
192over_pass(_Pass,_Top,metreqs(X),X).
193
194over_pass(2,Top, X, Y):- over_pass_ex(Top, X, Y),!.
195
201
202
203over_pass(Pass,Top,neg(X),Rest):- ep_to_lps_arg(Pass,Top,not(X),Rest).
204over_pass(2,_Top,holds_at(Fluent, Time),initially(Fluent)):- Time==start, !.
205over_pass(2,_Top,holds_at(Fluent, Time),initially(Fluent)):- Time==0, !.
206%over_pass(_Pass,_Top,holds_at(Fluent, Time),at(Fluent, Time)):- !.
207over_pass(2,[],happens_at(Event,Time),(observe Event at Time)):- !.
208over_pass(2,_Top,happens(Event,Time),(Event at Time)):- !.
210over_pass(2, [],initiates_at(Event,Fluent,Time),initiates(Event,Fluent)):- is_ftVar(Time), !.
211over_pass(2, [],terminates_at(Event,Fluent,Time),terminates(Event,Fluent)):- is_ftVar(Time), !.
212
213over_pass(2,_Top,initiates_at(Event,Fluent,Time),(Event initiates Fluent at Time)):- !.
214over_pass(2,_Top,terminates_at(Event,Fluent,Time),(Event terminates Fluent at Time)):- !.
215
216over_pass(1,_Top, not(exists(_,X)), not(X)):-!.
219
220over_pass(2,_Top, holds_at(Fluent, From, To),holds(Fluent, From, To)):- !.
221
222
223
225over_pass(_Pass,_Top,Form,LpsO):- Form=..[EFP,X], argtype_pred(EFP,_), protify(EFP,X,Lps),!,flatten([Lps],LpsO).
226over_pass(_Pass,_Top,X=Y,Lps):- callable(X),append_term(X,Y,Lps).
227over_pass(Pass,Top,','(X1,X2),(Lps1,Lps2)):- over_pass(Pass,Top,X1,Lps1),over_pass(Pass,Top,X2,Lps2).
228over_pass(Pass,Top,'<->'(X1,X2),[Lps1,Lps2]):- simply_atomic_or_conj(X1),simply_atomic_or_conj(X2), over_pass(Pass,Top,'->'(X1,X2),Lps1),over_pass(Pass,Top,'->'(X2,X1),Lps2).
229over_pass(_Pass,_Top,'->'(X1,X2),(X2 if X1)):- simply_atomic_or_conj(X1),simply_atomic_or_conj(X2),!.
230over_pass(_Pass,_Top,X1,X1):- simply_atomic(X1),!.
231over_pass(_Pass,_Top,X1,false(Lps)):- \+ (X1 = false(_)), into_false_conj(X1,Lps),Lps\=not(_),!.
232over_pass(_Pass,_Top,X,X):-!.
233
234into_false_conj(X1,Lps):- \+ (X1 = false(_)), into_pnf_conj(X1,Lps) -> Lps\=not(_),simply_atomic_or_conj(Lps).
235into_pnf_conj(X1,Lps):- pnf(X1,X2),nnf(X2,X3),conjuncts_to_list(X3,X3L),list_to_conjuncts(X3L,X4), Lps = X4.
236
237removes_at(F,_):- sent_op_f(F),!,fail.
238removes_at(F,F1):- atom_concat(F1,'_at',F),!.
240remove_time_arg(_Time,Holds,Holds):- \+ compound_gt(Holds,0),!.
241remove_time_arg(Time,Holds,HoldsMT):- \+ sub_var(Time,Holds),!,Holds=HoldsMT.
242remove_time_arg(Time,not(Holds),not(HoldsMT)):-!, remove_time_arg(Time,Holds,HoldsMT).
243remove_time_arg(Time,happens_at(Holds,T1),Holds):- T1==Time.
244remove_time_arg(Time,holds_at(Holds,T1),Holds):- T1==Time.
245remove_time_arg(Time,at(Holds,T1),Holds):- T1==Time.
246remove_time_arg(_Time,Holds,Holds):- \+ compound_gt(Holds,1),!.
247remove_time_arg(Time,Holds,HoldsMT):- Holds=..[F|Args],append(Left,[T1],Args),T1==Time,removes_at(F,F1),HoldsMT=..[F1|Left],!.
248remove_time_arg(Time,Holds,HoldsMT):- Holds=..[F|Args],maplist(remove_time_arg(Time),Args,Left),HoldsMT=..[F|Left],!.
249
250simply_atomic_or_conj(X1):- var(X1),!,fail.
251simply_atomic_or_conj((X1,X2)):- !, simply_atomic_or_conj(X1),simply_atomic_or_conj(X2).
252simply_atomic_or_conj(X1):- simply_atomic(X1).
253
254simply_atomic(X1):- var(X1),!,fail.
255simply_atomic(X1):- \+ compound_gt(X1,0),!.
256simply_atomic(not(X1)):-!, simply_atomic(X1).
257simply_atomic(at(X1,_)):-!, simply_atomic(X1).
258simply_atomic((_;_)):- !, fail.
259simply_atomic(X1):- compound_name_arguments(X1,F,Args), simply_atomic_f(F), maplist(simply_atomic_arg,Args).
260simply_atomic_f(F):- \+ sent_op_f(F).
261
262sent_op_f(F):- upcase_atom(F,FU),FU=F.
263
264simply_atomic_arg(A):- var(A);simply_atomic(A).
265
266assert_lps_try_harder_now((X2 if X1),(if X1 then X2)):- simply_atomic_or_conj(X1), simply_atomic_or_conj(X2).
267assert_lps_try_harder(Prolog):- assert_lps_try_harder_now(Prolog,Again),
268 lps_xform(lps_test_mod,Again,PrologAgain),Again\==PrologAgain,!,
269 print_lps_syntax(yellow,Again),
270 pprint_ecp_cmt(cyan,PrologAgain),
271 pprint_ecp_cmt(white,"% ================================="),
272 !.
273assert_lps_try_harder(Prolog):- pprint_ecp(red,Prolog).
274
275argtype_pred(event,events).
276argtype_pred(fluent,fluents).
277argtype_pred(action,actions).
278argtype_pred(predicate,predicates).
279argtype_pred(Action,Actions):- arg_info(domain,Action,arginfo),atom_concat(Action,"s",Actions).
280
281protify(both,Form,[Lps1,Lps2]):- protify(events,Form,Lps1),protify(action,Form,Lps2).
282protify(Type,Form,Lps):- is_list(Form),!,maplist(protify(Type),Form,Lps).
283protify(Type,Form,Lps):- argtype_pred(Type,LPSType), \+ callable(Form),!,Lps=..[LPSType,[Form]].
284protify(Type,Form,Lps):- argtype_pred(Type,LPSType), \+ compound(Form),!,Lps=..[LPSType,[Form/0]].
285protify(Type,F/A, Lps):- argtype_pred(Type,LPSType), integer(A),!,Lps=..[LPSType,[F/A]].
286protify(Type,(X1,X2),[Lps1,Lps2]):- !, protify(Type,X1,Lps1),protify(Type,X2,Lps2).
288protify(Event,X,LPS):- ((event) == Event), compound(X), arg(1,X,Agent),
289 is_agent(Agent),
290 !,protify(both,X,LPS).
291protify(Type,X,[mpred_prop(X,Type),LPS]):- argtype_pred(Type,LPSType),protify(LPSType,X,LPS).
292protify(LPSType,X,LPS):- cfunctor(X,F,A),cfunctor(_Lps,F,A),!,Pred=..[LPSType,[F/A]],LPS=[Pred].
293
294is_agent(Agent):- \+ atom(Agent),!,fail.
295is_agent(diver).
296is_agent(agent).
297is_agent(Agent):- call_u(subsort(Agent,agent)),!