1 | % (c) 2021-2025 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, | |
2 | % Heinrich Heine Universitaet Duesseldorf | |
3 | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) | |
4 | ||
5 | :- module(b_intelligent_trace_replay, [get_transition_details/6, | |
6 | perform_single_replay_step/5, | |
7 | replay_json_trace_file/2, replay_json_trace_file/5, | |
8 | read_json_trace_file/3, | |
9 | tcltk_replay_json_trace_file/3, | |
10 | replay_prolog_trace_file/1, | |
11 | ||
12 | % API for interactive JSON trace replay: | |
13 | load_json_trace_file_for_ireplay/1, | |
14 | tk_get_stored_json_trace_description/1, | |
15 | get_ireplay_status/3, | |
16 | replay_of_current_step_is_possible/5, | |
17 | replay_current_step/1, | |
18 | ireplay_fast_forward/1, | |
19 | skip_current_ireplay_step/1]). | |
20 | ||
21 | :- use_module(module_information,[module_info/2]). | |
22 | :- module_info(group,testing). | |
23 | :- module_info(description,'Replay saved (JSON) traces in a flexible way.'). | |
24 | % successor to b_trace_checking | |
25 | ||
26 | :- meta_predicate exclude_and_collect_errors(2,-,-,-,-). | |
27 | ||
28 | :- use_module(state_space,[current_state_id/1, transition/4, visited_expression/2]). % transition(CurID,Term,TransId,DestID) | |
29 | ||
30 | :- use_module(tools_strings,[ajoin/2,ajoin_with_sep/3]). | |
31 | :- use_module(specfile,[extract_variables_from_state/2, get_state_for_b_formula/3, b_or_z_mode/0, | |
32 | get_local_states_for_operation_transition/4, create_local_store_for_operation/4, | |
33 | get_operation_name/2]). | |
34 | :- use_module(error_manager). | |
35 | :- use_module(debug). | |
36 | ||
37 | % A single step is a list of informations about the step | |
38 | % name/OperationName | |
39 | % paras/List of =(Name,Value) | |
40 | ||
41 | % TO DO: for match_spec use mutable counter for number of matches instead of used parameter | |
42 | ||
43 | :- use_module(tcltk_interface,[compute_all_transitions_if_necessary/2]). | |
44 | ||
45 | :- use_module(probsrc(bmachine),[b_get_operation_non_det_modifies/2]). | |
46 | :- use_module(probsrc(bsyntaxtree), [def_get_texpr_id/2, get_texpr_ids/2, get_texpr_id/2, get_texpr_type/2]). | |
47 | :- use_module(probsrc(tools_matching), [get_possible_fuzzy_matches_and_completions_msg/3]). | |
48 | :- use_module(probsrc(b_interpreter),[b_test_boolean_expression_for_ground_state/5]). | |
49 | ||
50 | perform_single_replay_step(FromID,TransID,DestID,MatchSpec,TransSpec) :- | |
51 | %format('* FROM: ~w ',[FromID]),portray_match_spec(MatchSpec),nl, | |
52 | (match_spec_has_optimize_field(MatchSpec) | |
53 | -> findall(sol(MM,TID,DID), | |
54 | perform_single_replay_step_statespace(FromID,TID,DID,MatchSpec,TransSpec,MM), | |
55 | Sols), | |
56 | mark_match_spec_as_used(MatchSpec), | |
57 | length(Sols,NrSols), | |
58 | min_member(sol(Mismatches,TransID,DestID),Sols), | |
59 | format('Min. nr of mismatches found ~w among ~w candidate steps (starting from state ~w). ~n',[Mismatches,NrSols,FromID]) | |
60 | ; % just find first solution: | |
61 | perform_single_replay_step_statespace(FromID,TransID,DestID,MatchSpec,TransSpec,_) | |
62 | ). | |
63 | perform_single_replay_step(FromID,TransID,DestID,MatchSpec,TransSpec) :- | |
64 | perform_single_replay_step_by_pred(FromID,TransID,DestID,MatchSpec,TransSpec). | |
65 | ||
66 | ||
67 | ||
68 | ||
69 | % Assumption: ParaStore and ResultStore are sorted | |
70 | % returns the number of mismatches for those Keys marked as optimize | |
71 | perform_single_replay_step_statespace(FromID,TransID,DestID, | |
72 | MatchSpec, | |
73 | transition_spec(OpName, _, ParaStore, ResultStore, DestStore, UnchangedVars, Preds, _Postconditions), | |
74 | Mismatches) :- | |
75 | mark_match_spec_as_used(MatchSpec), | |
76 | get_opt_match_spec_val(opname,MatchSpec,OpMatch), | |
77 | compute_all_transitions_if_necessary(FromID,false), % could be made optional | |
78 | (OpMatch==match -> TOpName=OpName ; true), | |
79 | get_sorted_transition_details(FromID,TransID,DestID,TOpName,FullParaStore,FullResultStore), | |
80 | (TOpName=OpName -> MM0 = 0 ; MM0=1, assert_mismatch(OpMatch)), | |
81 | count_store_mismatches(FullParaStore,ParaStore,paras,MatchSpec,MM0,MM1), % check if parameters match | |
82 | count_store_mismatches(FullResultStore,ResultStore,results,MatchSpec,MM1,MM2), % check if operation return values match | |
83 | get_unchanged_store(FromID,UnchangedVars,UnchangedStore), | |
84 | (DestStore=[], UnchangedStore=[] | |
85 | -> MM6=MM2 | |
86 | ; % check if variables in destination state match | |
87 | visited_expression(DestID,DestState), | |
88 | get_dest_store(DestState,FullDestStore), | |
89 | count_store_mismatches(FullDestStore,DestStore,dest,MatchSpec,MM2,MM3), | |
90 | count_store_mismatches(FullDestStore,UnchangedStore,unchanged,MatchSpec,MM3,MM4), | |
91 | (get_match_spec_val(nondet_vars,MatchSpec,_) | |
92 | -> b_get_op_non_det_modifies(OpName,NonDetModifies), | |
93 | include(b_intelligent_trace_replay:bind_id_is_element_of(NonDetModifies),DestStore,D1), | |
94 | count_store_mismatches(FullDestStore,D1,nondet_vars,MatchSpec,MM4,MM5), | |
95 | include(b_intelligent_trace_replay:bind_id_is_element_of(NonDetModifies),UnchangedStore,D2), | |
96 | count_store_mismatches(FullDestStore,D2,nondet_vars,MatchSpec,MM5,MM6) | |
97 | ; MM6=MM4 | |
98 | ) | |
99 | ), | |
100 | Mismatches = MM6, | |
101 | (Preds=[] -> true | |
102 | ; get_match_spec_val(preds,MatchSpec,match) | |
103 | -> bsyntaxtree:conjunct_predicates(Preds,Pred), | |
104 | format('Testing preds in destination state ~w after ~w: ',[FromID,DestID]),translate:print_bexpr(Pred),nl, | |
105 | % TODO: insert_before_substitution_variables for $0 vars; check that it is consistent with Tcl/Tk and ProB2-UI | |
106 | % maybe use annotate_becomes_such_vars or find_used_primed_ids | |
107 | append(FullResultStore,FullParaStore,LocalStore), | |
108 | b_test_boolean_expression_for_ground_state(Pred,LocalStore,FullDestStore,'trace replay', OpName) | |
109 | ; true | |
110 | ). | |
111 | ||
112 | node_not_fully_explored(FromID,OpName) :- | |
113 | (max_reached_for_node(FromID) | |
114 | ; not_all_transitions_added(FromID) | |
115 | ; not_interesting(FromID) | |
116 | ; time_out_for_node(FromID,OpName,_)). | |
117 | ||
118 | :- use_module(probsrc(preferences),[get_time_out_preference_with_factor/2]). | |
119 | % lookup in state space failed; try perform by predicate | |
120 | % Note: does not use optimize keys (yet), only match keys | |
121 | perform_single_replay_step_by_pred(FromID,TransID,DestID, | |
122 | MatchSpec, | |
123 | transition_spec(OpName, _, ParaStore, ResultStore, DestStore, UnchangedVars, Preds, | |
124 | _Postconditions)) :- | |
125 | nonvar(OpName), % we currently cannot execute by predicate without knowing OpName | |
126 | (node_not_fully_explored(FromID,OpName) -> true | |
127 | ; debug_format(19,'Node ~w fully explored for ~w; no use in attempting execute by predicate~n',[FromID,OpName]), | |
128 | fail | |
129 | ), | |
130 | mark_match_spec_as_used(MatchSpec), | |
131 | !, | |
132 | (ParaStore \= [], % check parameters of operation | |
133 | get_match_spec_val(paras,MatchSpec,match) | |
134 | -> b_get_operation_typed_paras(OpName,Parameters), | |
135 | generate_predicates_from_store(operation_parameters,Parameters,ParaStore,ParaPreds) | |
136 | ; ParaPreds = []), | |
137 | (ResultStore \= [], % check return values of operation | |
138 | get_match_spec_val(results,MatchSpec,match) | |
139 | -> b_get_operation_typed_results(OpName,Results), | |
140 | generate_predicates_from_store(operation_results,Results,ResultStore,ResultPreds) | |
141 | ; ResultPreds = []), | |
142 | get_machine_identifiers(OpName,TVars), | |
143 | (DestStore = [] -> DestPreds=[] | |
144 | ; get_match_spec_val(dest,MatchSpec,match) | |
145 | -> generate_predicates_from_store(dest_variables,TVars,DestStore,DestPreds) | |
146 | ; get_match_spec_val(nondet_vars,MatchSpec,match) | |
147 | -> b_get_op_non_det_modifies(OpName,NonDetModifies), | |
148 | include(b_intelligent_trace_replay:id_is_element_of(NonDetModifies),TVars,NDVars), | |
149 | generate_predicates_from_store(non_det_vars,NDVars,DestStore,DestPreds) | |
150 | ; DestPreds=[] | |
151 | ), | |
152 | get_unchanged_store(FromID,UnchangedVars,UnchangedStore), | |
153 | (UnchangedStore = [] -> UnchangedPreds=[] | |
154 | ; get_match_spec_val(unchanged,MatchSpec,match) -> | |
155 | generate_predicates_from_store(unchanged_vars,TVars,UnchangedStore,UnchangedPreds) | |
156 | ; get_match_spec_val(nondet_vars,MatchSpec,match) -> | |
157 | include(b_intelligent_trace_replay:id_is_element_of(NonDetModifies),TVars,NDVars), | |
158 | generate_predicates_from_store(non_det_vars,NDVars,UnchangedStore,UnchangedPreds) | |
159 | ; UnchangedPreds=[] | |
160 | ), | |
161 | (get_match_spec_val(preds,MatchSpec,match) -> AddPreds=Preds ; AddPreds=[]), | |
162 | append([ParaPreds,ResultPreds,DestPreds,UnchangedPreds,AddPreds],AllPreds), | |
163 | conjunct_predicates(AllPreds,Pred), | |
164 | format('Trying to execute ~w in state ~w by predicate: ',[OpName,FromID]), translate:print_bexpr(Pred),nl,flush_output, | |
165 | get_time_out_preference_with_factor(5,TO), % TODO: store this in meta JSON info or options | |
166 | safe_time_out(tcltk_interface:tcltk_add_user_executed_operation_typed(OpName,FromID,_,Pred,TransID,DestID), | |
167 | TO, TimeOutRes), | |
168 | (TimeOutRes = time_out | |
169 | -> format_with_colour(user_output,[orange],'==> Timeout when executing ~w by predicate in state ~w~n',[OpName,FromID]),fail | |
170 | ; true). | |
171 | ||
172 | get_unchanged_store(_,[],UnchangedStore) :- !, UnchangedStore=[]. | |
173 | get_unchanged_store(FromID,UnchangedVars,UnchangedStore) :- | |
174 | % copy old values to UnchangedStore | |
175 | visited_expression(FromID,FromState), | |
176 | extract_variables_from_state(FromState,FullFromStore), | |
177 | sort(FullFromStore,SortedStore), | |
178 | % TODO: generate warning when unchanged variable does not exist? | |
179 | include(b_intelligent_trace_replay:bind_id_is_element_of(UnchangedVars),SortedStore,UnchangedStore). | |
180 | ||
181 | generate_predicate_from_bind(Kind,TypedIDs,json_bind(ID,Val,Type,Pos),b(Res,pred,[])) :- | |
182 | (member(b(identifier(ID),ExpectedType,_),TypedIDs) -> | |
183 | (unify_types_strict(ExpectedType,Type) | |
184 | -> TID = b(identifier(ID),ExpectedType,[]), TVal = b(value(Val),ExpectedType,[]), | |
185 | Res = equal(TID,TVal) | |
186 | ; pretty_type(ExpectedType,ETS), pretty_type(Type,TS), write(clash(Kind,ID,ETS,TS)),nl, | |
187 | % error should probably be caught earlier: | |
188 | add_warning(b_intelligent_trace_replay,'Ignoring value for stored identifier due to type clash: ',ID,Pos), | |
189 | Res = truth | |
190 | ) | |
191 | ; % the identifier is not in the list and should be ignored here; sanity checks are made somewhere else | |
192 | Res = truth | |
193 | ). | |
194 | generate_predicate_from_bind(Kind,TypedIDs,bind(ID,Val),R) :- % bind without type infos, e.g., from unchanged store | |
195 | generate_predicate_from_bind(Kind,TypedIDs,json_bind(ID,Val,any,unkown),R). | |
196 | ||
197 | generate_predicates_from_store(Kind,TVars,DestStore,DestPreds) :- | |
198 | maplist(b_intelligent_trace_replay:generate_predicate_from_bind(Kind,TVars),DestStore,DestPreds). | |
199 | ||
200 | get_machine_identifiers(Op,TConsts) :- is_setup_constants_op(Op), !, b_get_machine_constants(TConsts). | |
201 | get_machine_identifiers(_,TVars) :- b_get_machine_variables(TVars). | |
202 | ||
203 | is_setup_constants_op('$setup_constants'). | |
204 | is_setup_constants_op('$partial_setup_constants'). | |
205 | ||
206 | b_get_op_non_det_modifies(Op,NonDetModifies) :- is_setup_constants_op(Op), | |
207 | !,% return all constants as non-det modifies | |
208 | b_get_machine_constants(TConsts),get_texpr_ids(TConsts,ND), | |
209 | sort(ND,NonDetModifies). | |
210 | b_get_op_non_det_modifies(OpName,NonDetModifies) :- b_get_operation_non_det_modifies(OpName,NonDetModifies). | |
211 | ||
212 | % wrappers to deal with a few special transitions; TO DO: extend for CSP||B | |
213 | b_get_operation_typed_results('$setup_constants',Results) :- !, | |
214 | Results=[]. % for trace replay we assume setup_constants to have no result variables | |
215 | b_get_operation_typed_results('$initialise_machine',Results) :- !, Results=[]. % ditto | |
216 | b_get_operation_typed_results('$partial_setup_constants',Results) :- !, Results=[]. % ditto | |
217 | b_get_operation_typed_results(OpName,Results) :- b_or_z_mode, !, b_get_machine_operation_typed_results(OpName,Results). | |
218 | b_get_operation_typed_results(_,[]). | |
219 | ||
220 | b_get_operation_typed_paras('$setup_constants',Paras) :- !, | |
221 | Paras=[]. % for trace replay we assume setup_constants to have no parameters | |
222 | b_get_operation_typed_paras('$initialise_machine',Paras) :- !, Paras=[]. % ditto | |
223 | b_get_operation_typed_paras('$partial_setup_constants',Paras) :- !, Paras=[]. % ditto | |
224 | b_get_operation_typed_paras(OpName,Paras) :- b_or_z_mode,!, | |
225 | b_get_machine_operation_typed_parameters_for_animation(OpName,Paras). | |
226 | b_get_operation_typed_paras(OpName,[]) :- | |
227 | add_message(replay_json_trace_file,'Not in B mode, cannot obtain parameter info for: ',OpName). | |
228 | ||
229 | % ------------------- | |
230 | ||
231 | % now a version with multiple MatchSpecs to be tried in order | |
232 | % Flag can be used to see how many alternatives were tried | |
233 | flexible_perform_single_replay_step(FromID,TransID,DestID,[MatchSpec1|TMS],TransitionSpec,MName) :- | |
234 | skip_match_spec(FromID,TransitionSpec,MatchSpec1),!, | |
235 | debug_println(9,skipping_redundant_failing_check(MatchSpec1)), | |
236 | flexible_perform_single_replay_step(FromID,TransID,DestID,TMS,TransitionSpec,MName). | |
237 | flexible_perform_single_replay_step(FromID,TransID,DestID,[MatchSpec1|TMS],TransitionSpec,MName) :- | |
238 | if(perform_single_replay_step(FromID,TransID,DestID,MatchSpec1,TransitionSpec), | |
239 | get_match_spec_txt(MatchSpec1,MName), | |
240 | flexible_perform_single_replay_step(FromID,TransID,DestID,TMS,TransitionSpec,MName) | |
241 | ). | |
242 | ||
243 | % ------------------- | |
244 | ||
245 | ||
246 | % we only assert mismatches; if a variable remains untouched we matched perfectly | |
247 | assert_mismatch(Var) :- var(Var),!, Var=optimize. | |
248 | assert_mismatch(require_mismatch). % probably not useful; difficult to support by predicate | |
249 | assert_mismatch(optimize). | |
250 | ||
251 | precise_match_spec(match_spec(_,precise,KeyVals)) :- | |
252 | KeyVals = [dest/match,opname/match,paras/match,preds/match,results/match,unchanged/match]. | |
253 | ignore_dest_match_spec(match_spec(_,params_and_results,KeyVals)) :- | |
254 | KeyVals = [opname/match,paras/match,preds/match,results/match,nondet_vars/match,dest/optimize,unchanged/optimize]. | |
255 | %ignore_return_match_spec(match_spec(_,parameters_only,KeyVals)) :- | |
256 | % KeyVals = [opname/match,paras/match,results/optimize,nondet_vars/optimize]. | |
257 | opname_optimize_match_spec(match_spec(_,keep_name,KeyVals)) :- | |
258 | KeyVals = [opname/match,paras/optimize,results/optimize,nondet_vars/optimize,dest/optimize,unchanged/optimize]. | |
259 | ||
260 | % conditions on when to skip certain match_specs | |
261 | % (we assume that the precise_match_spec was tried before) | |
262 | skip_match_spec(root,TS,match_spec(_,params_and_results,_)) :- get_transition_spec_op(TS,'$setup_constants'). | |
263 | % for setup_constants: nondet_vars are all constants; so this is equivalent to precise | |
264 | % for initialise_machine: paras are all variables | |
265 | skip_match_spec(_,TS,match_spec(_,MS,_)) :- MS \= precise, | |
266 | get_transition_spec_meta(TS,Meta), | |
267 | % for an unknown operation we only try a precise replay (e.g., if operation just renamed), otherwise we skip it | |
268 | member(unknown_operation/_,Meta). | |
269 | % TODO: skip parameters_only if an operation has not results and no nondet_vars | |
270 | ||
271 | match_spec_was_used(match_spec(UsedFlag,_,_)) :- UsedFlag==used. | |
272 | mark_match_spec_as_used(match_spec(used,_,_)). | |
273 | ||
274 | get_match_spec_txt(match_spec(_,Name,_),Name). | |
275 | get_match_spec_val(Key,match_spec(_,_,List),Res) :- member(Key/Val,List),!,Res=Val. | |
276 | ||
277 | get_opt_match_spec_val(Key,MS,Res) :- get_match_spec_val(Key,MS,Val),!, Res=Val. | |
278 | get_opt_match_spec_val(_,_,optimize). | |
279 | ||
280 | % check if it is useful to optimize the mismatches | |
281 | match_spec_has_optimize_field(match_spec(_,_,KeyVals)) :- member(_/optimize,KeyVals). | |
282 | ||
283 | :- public valid_match_spec_key/1. | |
284 | valid_match_spec_key(dest). | |
285 | valid_match_spec_key(nondet_vars). | |
286 | valid_match_spec_key(opname). | |
287 | valid_match_spec_key(paras). | |
288 | valid_match_spec_key(results). | |
289 | valid_match_spec_key(unchanged). | |
290 | ||
291 | :- public portray_match_spec/1. | |
292 | portray_match_spec(match_spec(UsedFlag,Name,List)) :- | |
293 | (UsedFlag==used -> U=used ; U=not_yet_used), | |
294 | format('~w (~w): ~w~n',[Name,U,List]). | |
295 | ||
296 | % ------------------- | |
297 | ||
298 | get_dest_store(concrete_constants(C),SC) :- !, sort(C,SC). | |
299 | get_dest_store(Store,Vars) :- b_or_z_mode,!, extract_variables_from_state(Store,Vars). | |
300 | get_dest_store(_,[]). % CSP,... has no concept of variables | |
301 | ||
302 | ||
303 | % count the number of mismatches for a given key and MatchSpec | |
304 | % if MatchSpec requires match (perfect match) it will fail if Mismatches are 0 | |
305 | % it accumulates the global number of mismatches in a DCG style accumulator | |
306 | count_store_mismatches(FullStore,PartialStore,Key,MatchSpec,MismatchesIn,MismatchesOut) :- | |
307 | get_match_spec_val(Key,MatchSpec,MatchVal), !, | |
308 | (MatchVal=match | |
309 | -> MismatchesIn=MismatchesOut, | |
310 | count_mismatches(FullStore,Key,PartialStore,0) | |
311 | ; count_mismatches(FullStore,Key,PartialStore,Mismatches), | |
312 | MismatchesOut is MismatchesIn+Mismatches, | |
313 | %print(new_mm(MismatchesOut,Key,MatchVal,Mismatches)),nl, | |
314 | (Mismatches=0 -> true | |
315 | ; assert_mismatch(MatchVal)) | |
316 | ). | |
317 | count_store_mismatches(_,_,_Key,_,M,M). % key does not exist; no matching required | |
318 | ||
319 | % for maplist, include, exclude: | |
320 | bind_id_is_element_of(Vars,Bind) :- is_bind(Bind,ID,_), member(ID,Vars). % we could use ord_member | |
321 | id_is_element_of(Vars,TID) :- def_get_texpr_id(TID,ID), member(ID,Vars). | |
322 | ||
323 | is_bind(bind(ID,Val),ID,Val). | |
324 | is_bind(json_bind(ID,Val,_,_),ID,Val). | |
325 | ||
326 | %check_no_mismatches(FullStore,Key,PartialStore) :- count_mismatches(FullStore,Key,PartialStore,0). | |
327 | ||
328 | count_mismatches(FullStore,Key,PartialStore,Mismatches) :- | |
329 | count_mismatches_aux(FullStore,Key,PartialStore,0,Mismatches). | |
330 | :- use_module(probsrc(translate), [translate_bvalue_with_limit/3]). | |
331 | % count mismatches in FullStore compared to partial reference store | |
332 | % if result is set to 0, it will fail after first mismatch | |
333 | count_mismatches_aux(_,_,[],Acc,Res) :- !,Res=Acc. | |
334 | count_mismatches_aux([],Key,PartialStore,_,_) :- | |
335 | ajoin(['Saved trace step contains unknown bindings for ',Key,': '],Msg), | |
336 | add_error(b_intelligent_trace_replay,Msg,PartialStore), | |
337 | fail. | |
338 | count_mismatches_aux([Bind1|T],Key,[Bind2|T2],Acc,Res) :- | |
339 | is_bind(Bind1,ID,Val), is_bind(Bind2,ID,Val2),!, | |
340 | (check_value_equal(ID,Val,Val2) | |
341 | -> count_mismatches_aux(T,Key,T2,Acc,Res) | |
342 | ; (debug_mode(off) -> true | |
343 | ; translate_bvalue_with_limit(Val,200,V1), translate_bvalue_with_limit(Val2,200,V2), | |
344 | formatsilent_with_colour(user_output,[red],'==> Mismatch for ~w ~w:~n ~w~n (trace) ~w~n',[Key,ID,V1,V2]) | |
345 | %nl,print(Val),nl,nl,print(Val2),nl,nl, | |
346 | ), | |
347 | inc_mismatches(Acc,Acc1,Res), | |
348 | count_mismatches_aux(T,Key,T2,Acc1,Res) | |
349 | ). | |
350 | count_mismatches_aux([_ID|T],Key,PartialStore,Acc,Res) :- count_mismatches_aux(T,Key,PartialStore,Acc,Res). | |
351 | ||
352 | inc_mismatches(X,_,Res) :- number(Res),X>Res,!,fail. % we will never reach Res; could be 0 for perfect match | |
353 | inc_mismatches(Acc,Acc1,_) :- Acc1 is Acc+1. | |
354 | ||
355 | % check if saved value and actual value is identical | |
356 | :- use_module(kernel_objects,[equal_object/3]). | |
357 | check_value_equal(ID,Val1,Val2) :- | |
358 | temporary_set_preference(allow_enumeration_of_infinite_types,true,OldValueOfPref), | |
359 | call_cleanup(check_value_equal_aux(ID,Val1,Val2), | |
360 | reset_temporary_preference(allow_enumeration_of_infinite_types,OldValueOfPref)). | |
361 | ||
362 | % what if trace was saved with different SYMBOLIC pref value? | |
363 | :- use_module(b_ast_cleanup, [clean_up/3]). | |
364 | :- use_module(custom_explicit_sets, [same_closure/2]). | |
365 | :- use_module(debug, [debug_mode/1]). | |
366 | check_value_equal_aux(ID,closure(P1,T1,B1),C2) :- C2 = closure(P2,T2,B2), | |
367 | C1 = closure(P1,T1,B1), | |
368 | !, % we have two symbolic values | |
369 | (same_closure(C1,C2) | |
370 | -> true | |
371 | ; % simple comparison failed, now try and normalize the symbolic values and compare again | |
372 | temporary_set_preference(normalize_ast,true,CHANGE), | |
373 | % normalize_ast_sort_commutative should probably be false, unless we improve the sorting | |
374 | %print(compiling_cur_value(ID)),nl, | |
375 | clean_up(B1,[],B1C), | |
376 | %we could call: b_compiler:b_compile_closure(closure(P1,T1,B1C),closure(P12,T12,B12)), | |
377 | %print(compiling_trace_value(ID)),nl, | |
378 | clean_up(B2,[],B2C), | |
379 | reset_temporary_preference(normalize_ast,CHANGE), | |
380 | (same_closure(closure(P1,T1,B1C),closure(P2,T2,B2C)) | |
381 | -> true | |
382 | ; Val=closure(P1,T1,B1C), Val2 = closure(P2,T2,B2C), | |
383 | debug_mode(on), | |
384 | translate_bvalue_with_limit(Val,500,V1), translate_bvalue_with_limit(Val2,500,V2), | |
385 | formatsilent_with_colour(user_output,[red],'==> Symbolic Mismatch for ~w:~n ~w~n (trace) ~w~n',[ID,V1,V2]), | |
386 | % trace, same_closure(closure(P1,T1,B1C),closure(P2,T2,B2C)), | |
387 | fail | |
388 | ) | |
389 | ). | |
390 | %check_value_equal_aux(ID,Val1,Val2) :- !, equal_object(Val1,Val2,ID). | |
391 | check_value_equal_aux(ID,Val1,Val2) :- | |
392 | catch( | |
393 | equal_object_time_out(Val1,Val2,ID,2500), | |
394 | enumeration_warning(_A,_B,_C,_D,_E), | |
395 | (format_with_colour(user_output,[red],'==> Enumeration warning when comparing values for ~w~n',[ID]),fail) | |
396 | ). | |
397 | ||
398 | :- use_module(tools_meta,[safe_time_out/3]). | |
399 | equal_object_time_out(Val1,Val2,ID,TO) :- | |
400 | safe_time_out(equal_object(Val1,Val2,ID),TO,TimeOutRes), | |
401 | (TimeOutRes = time_out | |
402 | -> format_with_colour(user_output,[red],'==> Timeout when comparing values for ~w~n',[ID]),fail | |
403 | ; true). | |
404 | ||
405 | ||
406 | :- use_module(specfile,[get_operation_internal_name/2, | |
407 | state_corresponds_to_set_up_constants_only/2]). | |
408 | :- use_module(bmachine,[b_get_machine_operation_parameter_names_for_animation/2, | |
409 | b_get_machine_operation_typed_parameters_for_animation/2, | |
410 | b_get_machine_operation_result_names/2, | |
411 | b_get_machine_operation_typed_results/2, | |
412 | b_get_machine_variables/1, b_get_machine_constants/1, | |
413 | bmachine_is_precompiled/0, b_top_level_operation/1, | |
414 | b_machine_name/1, b_is_variable/1, b_is_variable/2, b_is_constant/1, b_is_constant/2]). | |
415 | :- use_module(probsrc(bsyntaxtree), [conjunct_predicates/2]). | |
416 | ||
417 | :- use_module(probsrc(state_space),[max_reached_for_node/1, not_all_transitions_added/1, | |
418 | time_out_for_node/3, not_interesting/1, | |
419 | try_set_trace_by_transition_ids/1]). | |
420 | ||
421 | :- use_module(library(lists)). | |
422 | ||
423 | % get the information of a B state_space transition in more detailed form | |
424 | % we get a store of parameter values and a store of result values and the operation name | |
425 | get_transition_details(FromID,TransID,DestID,OpName,ParaStore,ResultStore) :- | |
426 | transition(FromID,OperationTerm,TransID,DestID), | |
427 | get_operation_internal_name(OperationTerm,OpName), | |
428 | get_transition_details_aux(OpName,OperationTerm,DestID,ParaStore,ResultStore). | |
429 | ||
430 | get_transition_details_aux('$setup_constants',_,DestID,ParaStore,ResultStore) :- !, | |
431 | ResultStore=[], | |
432 | visited_expression(DestID,DestState), | |
433 | state_corresponds_to_set_up_constants_only(DestState,ParaStore). | |
434 | get_transition_details_aux('$initialise_machine',_,DestID,ParaStore,ResultStore) :- !, | |
435 | ResultStore=[], | |
436 | visited_expression(DestID,DestState), | |
437 | extract_variables_from_state(DestState,ParaStore). | |
438 | get_transition_details_aux(OpName,OperationTerm,_,ParaStore,ResultStore) :- | |
439 | get_local_states_for_operation_transition(OpName,OperationTerm,ParaStore,ResultStore). | |
440 | ||
441 | get_transition_name(FromID,TransID,OpName) :- | |
442 | transition(FromID,OperationTerm,TransID,_), | |
443 | get_operation_name(OperationTerm,OpName). | |
444 | ||
445 | ||
446 | ||
447 | % a variation where the two stores are sorted according to Prolog order: | |
448 | get_sorted_transition_details(FromID,TransID,DestID,TOpName,SortedPS,SortedRS) :- | |
449 | get_transition_details(FromID,TransID,DestID,TOpName,FullParaStore,FullResultStore), | |
450 | sort(FullParaStore,SortedPS), | |
451 | sort(FullResultStore,SortedRS). | |
452 | ||
453 | % ------------------------ | |
454 | ||
455 | get_transition_spec_op(transition_spec(OpName, _, _, _, _, _, _, _),OpName). | |
456 | get_transition_spec_meta(transition_spec(_, Meta, _, _, _, _, _, _),Meta). | |
457 | ||
458 | % get textual representation of transition spec (portray/translate) | |
459 | get_transition_spec_txt(transition_spec(OpName, Meta, ParaStore, ResultStore, | |
460 | _DestStore, _Unchanged, _Preds, _Postconditions),Txt) :- | |
461 | (member(description/Desc,Meta) -> ajoin([Desc,' :: '],DescTxt) ; DescTxt = ''), | |
462 | (nonvar(OpName) -> OpTxt=OpName | |
463 | ; member(unknown_operation/op(OldOpName,OldParas,OldResults),Meta) | |
464 | -> translate_unknown_operation(OldOpName,OldParas,OldResults,OpTxt) | |
465 | ; OpTxt='?'), | |
466 | (ParaStore = [] -> ParaText1='', ParaText2='' | |
467 | ; ParaText1=' paras: ', | |
468 | maplist(get_bind_txt,ParaStore,Paras), | |
469 | ajoin_with_sep(Paras,',',ParaText2) | |
470 | ), | |
471 | (ResultStore = [] -> ResultText1='', ResultText2='' | |
472 | ; ResultText1=' results: ', | |
473 | maplist(get_bind_txt,ResultStore,Results), | |
474 | ajoin_with_sep(Results,',',ResultText2) | |
475 | ),!, | |
476 | ajoin([DescTxt,OpTxt,ParaText1,ParaText2,ResultText1,ResultText2],Txt). | |
477 | get_transition_spec_txt(TS,'???') :- add_internal_error('Unknown transition spec:',TS). | |
478 | ||
479 | ||
480 | translate_unknown_operation(OldOpName,OldParas,[],OpTxt) :- !, | |
481 | translate_bindings(OldParas,OPS), | |
482 | append(OPS,[')'],OpParaAtoms), | |
483 | ajoin(['?',OldOpName,'('|OpParaAtoms],OpTxt). | |
484 | translate_unknown_operation(OldOpName,OldParas,OldResults,OpTxt) :- | |
485 | translate_bindings(OldResults,OPR), | |
486 | translate_unknown_operation(OldOpName,OldParas,[],Op1), | |
487 | ajoin([Op1,'-->'|OPR],OpTxt). | |
488 | ||
489 | % translate a list of json_bind terms into a list for use with ajoin for pretty-priting | |
490 | translate_bindings([],[]). | |
491 | translate_bindings([json_bind(ID,Val,_Type,_Pos)|TJ],[ID,'=',TVal|TT]) :- | |
492 | translate_bvalue_with_limit(Val,50,TVal), | |
493 | (TJ = [] -> TT=[] | |
494 | ; TT = [','|TT2], translate_bindings(TJ,TT2)). | |
495 | ||
496 | ||
497 | get_bind_txt(Bind,Txt) :- is_bind(Bind,Id,Val), | |
498 | simple_val(Val), !, % TODO: add parameter for short/long text | |
499 | translate_bvalue_with_limit(Val,100,V1), | |
500 | ajoin([Id,'=',V1],Txt). | |
501 | get_bind_txt(Bind,Id) :- is_bind(Bind,Id,_). | |
502 | ||
503 | simple_val(V) :- var(V),!,fail. | |
504 | simple_val(int(_)). | |
505 | simple_val(pred_false). | |
506 | simple_val(pred_true). | |
507 | simple_val(string(_)). | |
508 | simple_val(fd(_,_)). | |
509 | ||
510 | ||
511 | % perform some static checks on a transition spec: check if operations, parameters, variables exist | |
512 | check_and_adapt_trace_step(transition_spec(OpName, Meta, _, _, DestStore, UnchangedVars,Preds, Postconditions), Step, | |
513 | transition_spec(OpName, Meta, [], [], DestStore2,UnchangedVars2,Preds, Postconditions)) --> | |
514 | {\+ b_or_z_mode},!, | |
515 | exclude_and_collect_errors(unknown_variable_binding(Step,OpName),DestStore,DestStore2), | |
516 | exclude_and_collect_errors(unknown_variable(Step,OpName),UnchangedVars,UnchangedVars2). | |
517 | check_and_adapt_trace_step(transition_spec(OpName, Meta, ParaStore,ResultStore, DestStore, UnchangedVars,Preds, Postconditions), | |
518 | Step, | |
519 | transition_spec(OpName2,Meta2,ParaStore2,ResultStore2,DestStore2,UnchangedVars2,Preds2, Postconditions2)) --> | |
520 | {b_get_operation_typed_results(OpName,TOpResults)}, | |
521 | {b_get_operation_typed_paras(OpName,TOpParas)}, | |
522 | !, | |
523 | {OpName2=OpName}, | |
524 | {Meta2=Meta}, % TO DO: include excluded infos | |
525 | {Preds2=Preds}, %TO DO: check if all identifiers bound | |
526 | {Postconditions2=Postconditions}, % TODO Check operation names and predicate identifiers | |
527 | exclude_and_collect_errors(unknown_para_binding(OpName,TOpParas,'parameter',Step),ParaStore,ParaStore2), | |
528 | exclude_and_collect_errors(unknown_para_binding(OpName,TOpResults,'result variable',Step),ResultStore,ResultStore2), | |
529 | ({is_setup_constants_op(OpName)} | |
530 | -> exclude_and_collect_errors(unknown_constant_binding(Step),DestStore,DestStore2), | |
531 | {UnchangedVars2 = []}, | |
532 | ({UnchangedVars = []} -> [] | |
533 | ; {add_error(b_intelligent_trace_replay,'Illegal unchanged info for SETUP_CONSTANTS',UnchangedVars)} | |
534 | ) | |
535 | ; exclude_and_collect_errors(unknown_variable_binding(Step,OpName),DestStore,DestStore2), | |
536 | exclude_and_collect_errors(unknown_variable(Step,OpName),UnchangedVars,UnchangedVars2) | |
537 | ). | |
538 | check_and_adapt_trace_step(transition_spec(OpName, Meta, PS, RS, DestStore, UnchangedVars,Preds, Postconditions), Step, | |
539 | transition_spec(_, Meta2, [], [], DestStore2, UnchangedVars2,Preds, Postconditions)) --> | |
540 | add_replay_error('Unknown operation: ',OpName), % TODO treat $JUMP | |
541 | {Meta2 = [unknown_operation/op(OpName,PS,RS) | Meta], | |
542 | findall(KOpid,b_top_level_operation(KOpid),Ops), | |
543 | (get_possible_fuzzy_matches_and_completions_msg(OpName,Ops,FMsg) | |
544 | -> ajoin(['Unknown operation in replay step ',Step,' (did you mean the operation ',FMsg,' ?) : '], Msg) | |
545 | ; ajoin(['Unknown operation in replay step ',Step,': '], Msg)), | |
546 | (member(pos/Pos,Meta) -> true ; Pos=unknown), | |
547 | add_error(b_intelligent_trace_replay,Msg,OpName,Pos) | |
548 | }, | |
549 | % TODO: maybe do a fuzzy match and check if a new operation not used in the trace file exists | |
550 | exclude_and_collect_errors(unknown_variable_binding(Step,OpName),DestStore,DestStore2), | |
551 | exclude_and_collect_errors(unknown_variable(Step,OpName),UnchangedVars,UnchangedVars2). | |
552 | ||
553 | check_step_postconditions(transition_spec(_, _, _, _, _, _, _, Postconditions),StateID) --> | |
554 | check_postconditions(Postconditions,1,StateID). | |
555 | ||
556 | check_postconditions([],_,_) --> []. | |
557 | check_postconditions([Postcondition|Postconditions],Nr,StateID) --> | |
558 | check_postcondition(Postcondition,Nr,StateID), | |
559 | {Nr1 is Nr+1}, | |
560 | check_postconditions(Postconditions,Nr1,StateID). | |
561 | ||
562 | check_postcondition(state_predicate(Pred),Nr,StateID) --> | |
563 | {get_state_for_b_formula(StateID,Pred,State)}, | |
564 | ({b_test_boolean_expression_for_ground_state(Pred,[],State,'trace replay postconditions',Nr)} -> | |
565 | [] | |
566 | ; | |
567 | add_replay_error('Failed postcondition (predicate):',Nr) | |
568 | ). | |
569 | check_postcondition(operation_enabled(OpName,Pred,Enabled),Nr,StateID) --> | |
570 | { | |
571 | precise_match_spec(MatchSpec), | |
572 | TransitionSpec = transition_spec(OpName,[],[],[],[],[],[Pred],[]), | |
573 | (perform_single_replay_step(StateID,_,_,MatchSpec,TransitionSpec) -> Actual = enabled ; Actual = disabled) | |
574 | }, | |
575 | ({Enabled == Actual} -> | |
576 | [] | |
577 | ; | |
578 | {ajoin(['Failed postcondition (operation ',OpName,' should be ',Enabled,'):'],Msg)}, | |
579 | add_replay_error(Msg,Nr) | |
580 | ). | |
581 | ||
582 | % a version of exclude which also collects errors | |
583 | exclude_and_collect_errors(_Pred,[],[]) --> []. | |
584 | exclude_and_collect_errors(Pred,[H|T],Res) --> {call(Pred,H,Error)},!, | |
585 | [Error], | |
586 | exclude_and_collect_errors(Pred,T,Res). | |
587 | exclude_and_collect_errors(Pred,[H|T],[H|Res]) --> % include item | |
588 | exclude_and_collect_errors(Pred,T,Res). | |
589 | ||
590 | ||
591 | add_replay_error(Msg,Term) --> {gen_replay_error(Msg,Term,Err)}, [Err]. | |
592 | gen_replay_error(Msg,Term,rerror(FullMSg)) :- ajoin([Msg,Term],FullMSg). | |
593 | replay_error_occured(Errors) :- member(rerror(_),Errors). | |
594 | get_replay_error(rerror(Msg),Msg). | |
595 | ||
596 | :- use_module(probsrc(btypechecker), [unify_types_strict/2]). | |
597 | :- use_module(probsrc(kernel_objects), [infer_value_type/2]). | |
598 | :- use_module(probsrc(translate), [pretty_type/2]). | |
599 | ||
600 | unknown_para_binding(OpName,TParas,Kind,Step,json_bind(ID,Value,ValType,ValPos),ErrorTerm) :- | |
601 | ( get_texpr_id(TID,ID), | |
602 | member(TID,TParas), get_texpr_type(TID,Type) | |
603 | -> illegal_type(ID,Type,Value,ValType,ValPos,Kind,Step,ErrorTerm) | |
604 | ; ajoin(['Ignoring unknown ',Kind,' for operation ',OpName,' at step ', Step, ': '],Msg), | |
605 | gen_replay_error(Msg,ID,ErrorTerm) | |
606 | ). | |
607 | unknown_variable_binding(Step,_OpName,json_bind(Var,Value,ValType,ValPos),ErrorTerm) :- b_is_variable(Var,Type),!, | |
608 | illegal_type(Var,Type,Value,ValType,ValPos,'variable',Step,ErrorTerm). | |
609 | unknown_variable_binding(Step,OpName,json_bind(Var,_,_,_),ErrorTerm) :- unknown_variable(Step,OpName,Var,ErrorTerm). | |
610 | unknown_variable(Step,OpName,Var,ErrorTerm) :- \+ b_is_variable(Var), | |
611 | (b_is_constant(Var) | |
612 | -> ajoin(['Ignoring constant at step ', Step, ' for ', OpName, ' (a variable is expected here): '],Msg) | |
613 | ; b_get_machine_variables(TVars),get_texpr_ids(TVars,Vars), | |
614 | get_possible_fuzzy_matches_and_completions_msg(Var,Vars,FMsg) | |
615 | -> ajoin(['Ignoring unknown variable (did you mean ',FMsg,' ?) at step ', Step, ' for ', OpName, ': '],Msg) | |
616 | ; ajoin(['Ignoring unknown variable at step ', Step, ' for ', OpName, ': '],Msg) | |
617 | ), | |
618 | gen_replay_error(Msg,Var,ErrorTerm). | |
619 | unknown_constant_binding(Step,json_bind(Var,Value,ValType,ValPos),ErrorTerm) :- b_is_constant(Var,Type),!, | |
620 | illegal_type(Var,Type,Value,ValType,ValPos,'constant',Step,ErrorTerm). | |
621 | unknown_constant_binding(Step,json_bind(Var,_,_,_),ErrorTerm) :- | |
622 | (b_is_variable(Var) | |
623 | -> ajoin(['Ignoring variable at step ', Step, ' (a constant is expected here): '],Msg) | |
624 | ; b_get_machine_constants(TVars),get_texpr_ids(TVars,Vars), | |
625 | get_possible_fuzzy_matches_and_completions_msg(Var,Vars,FMsg) | |
626 | -> ajoin(['Ignoring unknown constant (did you mean ',FMsg,' ?) at step ', Step, ': '],Msg) | |
627 | ; ajoin(['Ignoring unknown constant at step ', Step, ': '],Msg) | |
628 | ), | |
629 | gen_replay_error(Msg,Var,ErrorTerm). | |
630 | ||
631 | ||
632 | illegal_type(Var,Type,_Value,ValType,_ValPos,Kind,Step,ErrorTerm) :- | |
633 | \+ unify_types_strict(Type,ValType), | |
634 | pretty_type(ValType,VTS), pretty_type(Type,TS), | |
635 | ajoin(['Ignoring ',Kind, ' at step ', Step, | |
636 | ' due to unexpected type of value (', VTS, ' instead of ',TS,') for: '],ErrMsg), | |
637 | gen_replay_error(ErrMsg,Var,ErrorTerm). | |
638 | /* | |
639 | | ?- perform_single_replay_step(X,TID,Dest,Match,transition_spec(Op,[],[],[],[active])). | |
640 | X = 3, | |
641 | TID = 86, | |
642 | Dest = 5, | |
643 | Match = match_spec(match,match,match,match,match), | |
644 | Op = new ? | |
645 | yes | |
646 | ||
647 | */ | |
648 | ||
649 | % ------------------ | |
650 | %precise_replay_trace(Trace,FromID,TransIds,DestID) :- | |
651 | % precise_match_spec(MatchSpec), % require precise replay | |
652 | % replay_trace(Trace,[MatchSpec],[],1,FromID,TransIds,DestID,[],_). % Todo : check errors | |
653 | ||
654 | :- use_module(tools_printing,[format_with_colour/4]). | |
655 | :- use_module(probsrc(debug),[formatsilent_with_colour/4]). | |
656 | % Note if we leave RestSpecs as a variable this will always do deterministic replay | |
657 | % to achieve backtracking RestSpecs must be set to [] | |
658 | replay_trace([],_MatchSpecs,_Opts,_,ID,[],ID,[],[]). | |
659 | replay_trace([TransSpec|T],MatchSpecs,Options,Step,FromID,TransIds,DestID,RestSpecs, | |
660 | [replay_step(MatchInfo,Errors)|OtherMatches]) :- | |
661 | get_transition_spec_txt(TransSpec,TTxt), | |
662 | formatsilent_with_colour(user_output,[blue],'==> Replay step ~w: from state ~w for ~w~n',[Step,FromID,TTxt]), | |
663 | statistics(walltime,[W1|_]), | |
664 | % first perform static check of step: | |
665 | phrase(check_and_adapt_trace_step(TransSpec,Step,CorrectedTransSpec),Errors,Errors1), | |
666 | if((TransIds=[TransID|TTrans], | |
667 | flexible_perform_single_replay_step(FromID,TransID,ID2,MatchSpecs,CorrectedTransSpec,MatchInfo)), | |
668 | (phrase(check_step_postconditions(CorrectedTransSpec,ID2),Errors1), | |
669 | statistics(walltime,[W2|_]), WTime is W2-W1, | |
670 | get_transition_name(FromID,TransID,OpName), % show operation name used as feedback in case errors occur | |
671 | (Errors == [] -> | |
672 | formatsilent_with_colour(user_output,[green],'==> Replay step ~w successful (~w, ~w ms) leading to state ~w~n',[Step,MatchInfo,WTime,ID2]) | |
673 | ; Errors = [rerror(OneErr)] -> | |
674 | formatsilent_with_colour(user_output,[red,bold],'==> Replay step ~w successful WITH ERROR (~w, ~w, ~w ms) leading to state ~w via ~w~n',[Step,MatchInfo,OneErr,WTime,ID2,OpName]) | |
675 | ; | |
676 | length(Errors,NrErrors), Errors = [rerror(OneErr)|_], | |
677 | formatsilent_with_colour(user_output,[red,bold],'==> Replay step ~w successful WITH ERRORS (~w, ~w errors [~w,...], ~w ms) leading to state ~w via ~w~n',[Step,MatchInfo,NrErrors,OneErr,WTime,ID2,OpName]) | |
678 | ; | |
679 | length(Errors,NrErrors), | |
680 | formatsilent_with_colour(user_output,[red,bold],'==> Replay step ~w successful WITH ERRORS (~w, ~w errors, ~w ms) leading to state ~w via ~w~n',[Step,MatchInfo,NrErrors,WTime,ID2,OpName]) | |
681 | ), | |
682 | (get_preference(deterministic_trace_replay,true) -> ! | |
683 | % TO DO: use info from MatchSpec? (e.g., det for perfect match) | |
684 | ; true | |
685 | ; formatsilent_with_colour(user_output,[orange],'==> Backtracking replay step ~w (~w) leading to state ~w~n',[Step,MatchInfo,ID2])), | |
686 | S1 is Step+1, | |
687 | replay_trace(T,MatchSpecs,Options,S1,ID2,TTrans,DestID,RestSpecs,OtherMatches) | |
688 | ), | |
689 | (format_with_colour(user_output,[red,bold],'==> Replay step ~w FAILED~n',[Step]), | |
690 | get_transition_spec_txt(TransSpec,Txt), formatsilent_with_colour(user_output,[red,bold],' ~w~n',[Txt]), | |
691 | Errors1=[], | |
692 | MatchInfo=failed, | |
693 | ||
694 | (T = [_|_], nonmember(stop_at_failure,Options) | |
695 | -> % try and skip this step and continue replay | |
696 | RestSpecs=[TransSpec|RT], | |
697 | TransIds=[skip|TTrans], % -1 signifies skipped transition | |
698 | S1 is Step+1, | |
699 | replay_trace(T,MatchSpecs,Options,S1,FromID,TTrans,DestID,RT,OtherMatches) | |
700 | ; RestSpecs=[TransSpec|T], % the steps that were not replayed | |
701 | TransIds=[], DestID=FromID, | |
702 | OtherMatches=[] | |
703 | ) | |
704 | ) | |
705 | ). | |
706 | ||
707 | % ------------------ | |
708 | ||
709 | ||
710 | tcltk_replay_json_trace_file(FileName,ReplayStatus,list([Header|Entries])) :- | |
711 | replay_json_trace_file_with_check(FileName,TransSpecs,ReplayStatus,TransIds,MatchInfoList), | |
712 | try_set_trace_by_transition_ids(TransIds), | |
713 | Header = list(['Step', 'TraceFile','Replayed', 'Match','Mismatches','Errors','State ID']), | |
714 | (tk_get_trace_info(TransSpecs,root,1,TransIds,MatchInfoList,Entries) | |
715 | -> true | |
716 | ; add_internal_error('Could not compute replay table:',TransSpecs), Entries=[]). | |
717 | ||
718 | ||
719 | :- use_module(probsrc(translate),[translate_event_with_limit/3]). | |
720 | ||
721 | tk_get_trace_info([],_,_,_,_,[]). | |
722 | tk_get_trace_info([TransSpec|TS2],CurID,Step,TransIds,MatchInfoList, | |
723 | [list([Step,Txt,OpTxt,MI,list(DeltaList),list(Errors),CurID])|RestInfo]) :- | |
724 | get_from_match_list(MatchInfoList,MI,Errors,MIL2), | |
725 | get_transition_spec_txt(TransSpec,Txt), | |
726 | (TransIds=[TID1|TI2], transition(CurID,OperationTerm,TID1,ToID) | |
727 | -> %get_operation_internal_name(OperationTerm,OpName) | |
728 | translate_event_with_limit(OperationTerm,30,OpTxt), | |
729 | analyse_step_match(TransSpec,CurID,TID1,DeltaList) | |
730 | ; TransIds=[TID1|TI2], | |
731 | (number(TID1) -> TID1<0 ; true) % not a valid transition number; -1 or skip | |
732 | -> ToID=CurID, OpTxt='skipped', DeltaList=[] | |
733 | ; TI2=[], ToID=CurID, OpTxt='-', DeltaList=[] | |
734 | ), | |
735 | Step1 is Step+1, | |
736 | tk_get_trace_info(TS2,ToID,Step1,TI2,MIL2,RestInfo). | |
737 | ||
738 | get_from_match_list([replay_step(MI,Errors)|T],MatchInfo,TkErrors,T) :- | |
739 | maplist(get_replay_error,Errors,TkErrors), | |
740 | (MI=precise,TkErrors=[_|_] -> MatchInfo=precise_with_errs ; MatchInfo=MI). | |
741 | get_from_match_list([],'-',['-'],[]). | |
742 | ||
743 | % analyse how good a step matches the transition spec | |
744 | % useful after replay to provide explanations to the user | |
745 | analyse_step_match(TransSpec,FromID,TransID,DeltaList) :- | |
746 | TransSpec = transition_spec(OpName, _, ParaStore, ResultStore, DestStore, _UnchangedVars, _Preds, _Postconditions), | |
747 | get_sorted_transition_details(FromID,TransID,DestID,TOpName,FullParaStore,FullResultStore), | |
748 | (TOpName=OpName -> DeltaList=DL1 ; DeltaList=['Name'|DL1]), | |
749 | delta_store_match(FullParaStore,paras,ParaStore,DL1,DL2), | |
750 | delta_store_match(FullResultStore,results,ResultStore,DL2,DL3), | |
751 | visited_expression(DestID,DestState), | |
752 | get_dest_store(DestState,FullDestStore), | |
753 | delta_store_match(FullDestStore,dest,DestStore,DL3,DL4), | |
754 | % TO DO: UnchangedVars and _Preds | |
755 | DL4=[],!. | |
756 | analyse_step_match(TransSpec,FromID,TransID,DeltaList) :- | |
757 | add_internal_error('Call failed:',analyse_step_match(TransSpec,FromID,TransID,DeltaList)), | |
758 | DeltaList=['??ERROR??']. | |
759 | ||
760 | delta_store_match(_,_,[]) --> !. | |
761 | delta_store_match([],_Key,Rest) | |
762 | --> add_rest(Rest). % these bindings were probably filtered out during replay and error messages were generated | |
763 | delta_store_match([Bind1|T],Key,[Bind2|T2]) --> | |
764 | {is_bind(Bind1,ID,Val), is_bind(Bind2,ID,Val2)}, | |
765 | !, | |
766 | ({check_value_equal(ID,Val,Val2)} | |
767 | -> [] | |
768 | ; %translate_bvalue_with_limit(Val,100,V1), translate_bvalue_with_limit(Val2,100,V2), | |
769 | [ID] % TO DO: provided detailed explanation using values | |
770 | ),delta_store_match(T,Key,T2). | |
771 | delta_store_match([_ID|T],Key,PartialStore) --> delta_store_match(T,Key,PartialStore). | |
772 | ||
773 | add_rest([]) --> []. | |
774 | add_rest([Bind|T]) --> {is_bind(Bind,ID,_)}, [ID], add_rest(T). | |
775 | ||
776 | % ----------------------- | |
777 | ||
778 | replay_json_trace_file(FileName,ReplayStatus) :- | |
779 | replay_json_trace_file_with_check(FileName,_,ReplayStatus,TransIds,_), | |
780 | try_set_trace_by_transition_ids(TransIds). | |
781 | ||
782 | ||
783 | % generate error/warning for imperfect or partial replay | |
784 | replay_json_trace_file_with_check(FileName,Trace,ReplayStatus,TransIds,MatchInfoList) :- | |
785 | replay_json_trace_file(FileName,Trace,ReplayStatus,TransIds,MatchInfoList), | |
786 | length(TransIds,Steps), | |
787 | length(Trace,AllSteps), | |
788 | check_replay_status(ReplayStatus,Steps,AllSteps). | |
789 | ||
790 | check_replay_status(imperfect,Steps,_) :- !, | |
791 | ajoin(['Imperfect replay, steps replayed: '],Msg), | |
792 | add_warning(replay_json_trace_file,Msg, Steps). | |
793 | check_replay_status(partial,Steps,AllSteps) :- !, | |
794 | ajoin(['Replay of all ',AllSteps,' steps not possible, steps replayed: '],Msg), | |
795 | add_error(replay_json_trace_file,Msg, Steps). | |
796 | check_replay_status(perfect,Steps,_) :- | |
797 | add_message(replay_json_trace_file,'Perfect replay possible, steps replayed: ', Steps). | |
798 | ||
799 | % ------------ | |
800 | ||
801 | :- use_module(tools,[start_ms_timer/1, stop_ms_timer_with_msg/2]). | |
802 | :- use_module(bmachine_construction,[dummy_machine_name/2]). | |
803 | ||
804 | replay_json_trace_file(FileName,Trace,ReplayStatus,TransIds,MatchInfoList) :- \+ bmachine_is_precompiled,!, | |
805 | add_error(replay_json_trace_file,'No specification loaded, cannot replay trace file:',FileName), | |
806 | Trace=[], TransIds=[], ReplayStatus=partial, MatchInfoList=[]. | |
807 | replay_json_trace_file(FileName,Trace,ReplayStatus,TransIds,MatchInfoList) :- | |
808 | start_ms_timer(T1), | |
809 | read_json_trace_file(FileName,ModelName,Trace), | |
810 | stop_ms_timer_with_msg(T1,'Loading JSON trace file'), | |
811 | precise_match_spec(MatchSpec), % require precise replay | |
812 | ignore_dest_match_spec(MS2), | |
813 | opname_optimize_match_spec(MS3), | |
814 | % was ignore_return_match_spec(MS3), % TODO: when no return and no non-det vars: do not try MS3 | |
815 | start_ms_timer(T2), | |
816 | temporary_set_preference(deterministic_trace_replay,true,CHNG), | |
817 | replay_trace(Trace,[MatchSpec,MS2,MS3],[],1,root,TransIds,_DestID,RestTrace,MatchInfoList), | |
818 | reset_temporary_preference(deterministic_trace_replay,CHNG), | |
819 | stop_ms_timer_with_msg(T2,'Replaying JSON trace file'), | |
820 | !, | |
821 | (RestTrace = [] | |
822 | -> ((match_spec_was_used(MS3) % Grade=3 | |
823 | ; match_spec_was_used(MS2) %Grade=2 | |
824 | ; member(replay_step(_,Errs),MatchInfoList), replay_error_occured(Errs) | |
825 | ) | |
826 | -> ReplayStatus=imperfect, | |
827 | check_model_name(ModelName) | |
828 | ; ReplayStatus=perfect | |
829 | ) | |
830 | ; ReplayStatus=partial, | |
831 | check_model_name(ModelName) | |
832 | ). | |
833 | ||
834 | :- use_module(specfile,[currently_opened_specification_name/1]). | |
835 | check_model_name(ModelName) :- | |
836 | currently_opened_specification_name(CurModelName),!, | |
837 | (CurModelName=ModelName -> true | |
838 | ; dummy_machine_name(ModelName,CurModelName) % CurModelName = MAIN_MACHINE_FOR_... | |
839 | -> true | |
840 | ; ModelName = 'dummy(uses)' | |
841 | -> true % if modelName is "null" this is the value used | |
842 | ; prob2_ui_suffix(ModelName,CurModelName) -> | |
843 | % happens when ProB2-UI saves trace files; sometimes it adds (2), ... suffix, see issue #243 | |
844 | add_message(replay_json_trace_file, 'JSON trace file model name has a ProB2-UI suffix: ', ModelName) | |
845 | ; ajoin(['JSON trace file model name ',ModelName,' does not match current model name: '],MMsg), | |
846 | add_warning(replay_json_trace_file, MMsg, CurModelName) | |
847 | ). | |
848 | check_model_name(ModelName) :- | |
849 | add_warning(replay_json_trace_file, 'Cannot determine current model name to check stored name:', ModelName). | |
850 | ||
851 | :- set_prolog_flag(double_quotes, codes). | |
852 | :- use_module(self_check). | |
853 | :- assert_must_succeed(b_intelligent_trace_replay:prob2_ui_suffix('b_mch', 'b')). | |
854 | :- assert_must_succeed(b_intelligent_trace_replay:prob2_ui_suffix('b_mch (2)', 'b')). | |
855 | :- assert_must_succeed(b_intelligent_trace_replay:prob2_ui_suffix('b (2)', 'b')). | |
856 | :- assert_must_fail(b_intelligent_trace_replay:prob2_ui_suffix('b', 'bc')). | |
857 | :- assert_must_fail(b_intelligent_trace_replay:prob2_ui_suffix('b (2)', 'bc')). | |
858 | % check if name matches current model name catering for ProB2-UI quirks | |
859 | prob2_ui_suffix(JSONModelName,CurModelName) :- | |
860 | atom_codes(JSONModelName,JCodes), | |
861 | atom_codes(CurModelName,TargetCodes), | |
862 | append(TargetCodes,After,JCodes), | |
863 | (append("_mch",After2,After) | |
864 | -> true % .eventb package file name | |
865 | ; append(".mch",After2,After) -> true | |
866 | ; After2=After), | |
867 | valid_prob2_ui_suffix(After2). | |
868 | ||
869 | valid_prob2_ui_suffix([]) :- !. | |
870 | valid_prob2_ui_suffix([32|_]). % we could check that we have (2), ... after; but is it necessary? | |
871 | ||
872 | ||
873 | error_occured_during_replay(MatchInfoList) :- | |
874 | member(replay_step(_,Errs),MatchInfoList), replay_error_occured(Errs),!. | |
875 | ||
876 | % --------------------------------- | |
877 | ||
878 | replay_prolog_trace_file(FileName) :- | |
879 | start_ms_timer(T1), | |
880 | read_prolog_trace_file(FileName,_ModelName,Trace), | |
881 | stop_ms_timer_with_msg(T1,'Loading Prolog trace file'), | |
882 | replay_prolog2(FileName,Trace). | |
883 | ||
884 | replay_prolog2(FileName,Trace) :- | |
885 | precise_match_spec(MatchSpec), | |
886 | start_ms_timer(T2), | |
887 | replay_trace(Trace,[MatchSpec],[stop_at_failure],1,root,TransIds,_DestID,RestTrace,MatchInfoList), | |
888 | stop_ms_timer_with_msg(T2,'Replaying JSON trace file'), | |
889 | RestTrace=[], % will initiate backtracking | |
890 | try_set_trace_by_transition_ids(TransIds), | |
891 | (error_occured_during_replay(MatchInfoList) | |
892 | -> add_error(replay_prolog_trace_file,'Errors occurred during replay of file:',FileName) | |
893 | ; true). | |
894 | replay_prolog2(FileName,_Trace) :- | |
895 | add_error(replay_prolog_trace_file,'Could not fully replay file:',FileName). | |
896 | ||
897 | read_prolog_trace_file(FileName,ModelName,Trace) :- | |
898 | open(FileName,read,Stream,[encoding(utf8)]), | |
899 | call_cleanup(parse_prolog_trace_file(FileName,Stream,ModelName,Trace), | |
900 | close(Stream)). | |
901 | ||
902 | parse_prolog_trace_file(File,Stream,ModelName,Trace) :- | |
903 | safe_read_stream(Stream,0,Term),!, | |
904 | (Term = end_of_file | |
905 | -> Trace = [], ModelName = 'dummy(uses)', | |
906 | add_warning(read_prolog_trace_file,'Empty trace file: ',File) | |
907 | ; (Term = machine(ModelName) | |
908 | -> Trace = T | |
909 | ; Trace = [Term|T], | |
910 | add_warning(read_prolog_trace_file,'File does not start with a machine/1 fact: ',Term) | |
911 | ), | |
912 | parse_prolog_trace_file_body(Stream,1,T) | |
913 | ). | |
914 | ||
915 | parse_prolog_trace_file_body(Stream,Step,Trace) :- | |
916 | safe_read_stream(Stream,Step,Term),!, | |
917 | (Term = end_of_file | |
918 | -> Trace = [] | |
919 | ; skip_prolog_term(Term) | |
920 | -> add_message(read_prolog_trace_file,'Skipping: ',Term), | |
921 | parse_prolog_trace_file_body(Stream,Step,Trace) | |
922 | ; Trace = [TransSpec|T], | |
923 | convert_prolog_trace_step(Term,TransSpec), | |
924 | S1 is Step + 1, | |
925 | parse_prolog_trace_file_body(Stream,S1,T) | |
926 | ). | |
927 | ||
928 | safe_read_stream(Stream,Step,T) :- | |
929 | catch(read(Stream,T), E, ( | |
930 | ajoin(['Exception while reading step ', Step, 'of trace file: '], Msg), | |
931 | add_error(read_prolog_trace_file,Msg,[E]), | |
932 | T=end_of_file | |
933 | )). | |
934 | ||
935 | skip_prolog_term('$check_value'(_ID,_Val)). | |
936 | convert_prolog_trace_step(Fact, | |
937 | transition_spec(OpName,Meta,ParaStore,ResultStore,DestStore,Unchanged,PredList,[])) :- | |
938 | PredList=[], Unchanged=[], Meta=[], DestStore = [], | |
939 | decompose_operation(Fact,OpName,ParaStore,ResultStore). | |
940 | % TODO: deal with '$check_value'(ID,Val) | |
941 | % decompose an operation term into name, parameter store and result store | |
942 | decompose_operation('-->'(OpTerm,Results),OpName,ParaStore,ResultStore) :- !, | |
943 | decompose_operation2(OpTerm,OpName,ParaStore), | |
944 | (b_get_machine_operation_result_names(OpName,ResultNames) | |
945 | -> create_sorted_store(ResultNames,Results,OpName,ResultStore) | |
946 | ; ResultStore = []). | |
947 | decompose_operation(OpTerm,OpName,ParaStore,[]) :- decompose_operation2(OpTerm,OpName,ParaStore). | |
948 | ||
949 | is_setup_or_init('$initialise_machine','$initialise_machine'). | |
950 | is_setup_or_init(initialise_machine,'$initialise_machine'). % old style | |
951 | is_setup_or_init(setup_constants,'$setup_constants'). % old style | |
952 | is_setup_or_init(Op,Op) :- is_setup_constants_op(Op). | |
953 | ||
954 | decompose_operation2(OpTerm,OpName,ParaStore) :- | |
955 | functor(OpTerm,Functor,Arity), | |
956 | is_setup_or_init(Functor,OpName), | |
957 | !, | |
958 | % the order of constants, variables etc has changed in ProB; | |
959 | % in general we cannot reconstruct the association of the arguments to variables or constants | |
960 | (Arity=0 -> true | |
961 | ; add_message(b_intelligent_trace_replay,'Ignoring parameters of:',OpName)), | |
962 | ParaStore=[]. | |
963 | decompose_operation2(OpTerm,OpName,ParaStore) :- | |
964 | OpTerm =.. [OpName|Paras], | |
965 | (b_get_machine_operation_parameter_names_for_animation(OpName,ParaNames) | |
966 | -> create_sorted_store(ParaNames,Paras,OpName,ParaStore) | |
967 | ; ParaStore = [], | |
968 | add_error(read_prolog_trace_file,'Unknown operation in trace file:',OpName) | |
969 | ). | |
970 | ||
971 | ||
972 | create_sorted_store([],Paras,OpName,SortedParaStore) :- Paras = [_|_], | |
973 | get_preference(show_eventb_any_arguments,false),!, | |
974 | add_message(b_intelligent_trace_replay,'Prolog trace file contains values for virtual parameters (set SHOW_EVENTB_ANY_VALUES to TRUE to better replay this trace file): ',OpName), | |
975 | SortedParaStore = []. | |
976 | create_sorted_store(ParaNames,[],_OpName,SortedParaStore) :- ParaNames = [_|_], | |
977 | get_preference(show_eventb_any_arguments,true),!, | |
978 | add_message(b_intelligent_trace_replay,'Prolog trace file contains no values for parameters (maybe SHOW_EVENTB_ANY_VALUES was FALSE when trace file was created): ',ParaNames), | |
979 | SortedParaStore = []. | |
980 | create_sorted_store(ParaNames,Paras,OpName,SortedParaStore) :- | |
981 | create_local_store_for_operation(ParaNames,Paras,OpName,ParaStore), | |
982 | sort(ParaStore,SortedParaStore). | |
983 | ||
984 | ||
985 | ||
986 | % ------------------------ | |
987 | ||
988 | :- use_module(extrasrc(json_parser),[json_parse_file/3]). | |
989 | ||
990 | % read a JSON ProB2-UI trace file and extract model name and transition_spec list | |
991 | read_json_trace_file(FileName,ModelName,Trace) :- | |
992 | json_parse_file(FileName,Term,[rest(_),position_infos(true),strings_as_atoms(false)]), | |
993 | %nl,print(Term),nl,nl, | |
994 | !, | |
995 | (extract_json_model_name(Term,M) -> ModelName=M ; ModelName = 'dummy(uses)'), | |
996 | (translate_json_trace_term(Term,FileName,Trace) -> true | |
997 | ; add_error(read_json_trace_file,'Could not translate JSON transitionList: ',Term), | |
998 | Trace = []). | |
999 | ||
1000 | % small JSON utilities; to do: merge with VisB utilities and factor out | |
1001 | get_json_attribute(Attr,ObjList,Value) :- member(Equality,ObjList), | |
1002 | is_json_equality_attr(Equality,Attr,Value). | |
1003 | get_json_attribute_with_pos(Attr,ObjList,File,Value,Pos) :- | |
1004 | member(Equality,ObjList), | |
1005 | is_json_equality_attr_with_pos(Equality,File,Attr,Value,Pos). | |
1006 | ||
1007 | is_json_equality_attr('='(Attr,Val),Attr,Val). | |
1008 | is_json_equality_attr('='(Attr,Val,_Pos),Attr,Val). % we have position infos | |
1009 | ||
1010 | is_json_equality_attr_with_pos('='(Attr,Val),_File,Attr,Val,unknown). | |
1011 | is_json_equality_attr_with_pos('='(Attr,Val,JPos),File,Attr,Val,ProBPos) :- create_position(JPos,File,ProBPos). | |
1012 | ||
1013 | create_position(From-To,File,ProBPos) :- | |
1014 | ProBPos=src_position_with_filename_and_ec(From,1,To,1,File). | |
1015 | % -------- | |
1016 | ||
1017 | :- use_module(probsrc(preferences), [reset_temporary_preference/2,temporary_set_preference/3, get_preference/2]). | |
1018 | translate_json_trace_term(json(ObjList),FileName,Trace) :- | |
1019 | get_json_key_list(transitionList,ObjList,List), | |
1020 | List \= [], % optimization, dont set preferences | |
1021 | !, | |
1022 | % TODO: why is this not a call_cleanup? | |
1023 | temporary_set_preference(repl_cache_parsing,true,CHNG), | |
1024 | eval_strings:turn_normalising_off, | |
1025 | maplist(translate_json_operation(FileName),List,Trace), | |
1026 | eval_strings:turn_normalising_on, | |
1027 | reset_temporary_preference(repl_cache_parsing,CHNG). | |
1028 | ||
1029 | % no transitionList => just use empty list | |
1030 | translate_json_trace_term(json(_),_,[]) :- !. | |
1031 | ||
1032 | % extract model name from metadata which looks like this | |
1033 | /* | |
1034 | "metadata": { | |
1035 | "fileType": "Trace", | |
1036 | "formatVersion": 1, | |
1037 | "savedAt": "2021-10-13T13:38:02Z", | |
1038 | "creator": "tcltk (leuschel)", | |
1039 | "proBCliVersion": "1.11.1-nightly", | |
1040 | "proBCliRevision": "3cb800bbadfeaf4f581327245507a55ae5a5e66d", | |
1041 | "modelName": "scheduler", | |
1042 | "modelFile": "/Users/bourkaki/B/Benchmarks/scheduler.mch" | |
1043 | } | |
1044 | */ | |
1045 | ||
1046 | extract_json_model_name(json(ObjList),MachineName) :- | |
1047 | get_json_key_object(metadata,ObjList,List), | |
1048 | get_json_attribute(modelName,List,string(MC)), | |
1049 | atom_codes(MachineName,MC). | |
1050 | ||
1051 | ||
1052 | % translate a single JSON transition entry into a transition_spec term for replay_trace | |
1053 | /* here is a typical entry for the scheduler model: | |
1054 | { | |
1055 | "name": "ready", | |
1056 | "params": { | |
1057 | "rr": "process3" | |
1058 | }, | |
1059 | "results": { | |
1060 | }, | |
1061 | "destState": { | |
1062 | "active": "{process3}", | |
1063 | "waiting": "{}" | |
1064 | }, | |
1065 | "destStateNotChanged": [ | |
1066 | "ready" | |
1067 | ], | |
1068 | "preds": null | |
1069 | }, | |
1070 | */ | |
1071 | translate_json_operation(FileName,json(Json), | |
1072 | transition_spec(OpName,Meta, | |
1073 | ParaStore,ResultStore,DestStore,Unchanged,PredList,Postconditions) ) :- | |
1074 | get_json_attribute_with_pos(name,Json,FileName,string(OpNameC),Position), % name is required! | |
1075 | atom_codes(OpName,OpNameC), | |
1076 | (debug_mode(off) -> true ; add_message(translate_json_operation,'Processing operation: ',OpName,Position)), | |
1077 | (get_json_key_object(params,Json,Paras) | |
1078 | -> translate_json_paras(Paras,params(OpName),FileName,OpName,Bindings), | |
1079 | sort(Bindings,ParaStore) % put the parameters into the standard Prolog order | |
1080 | ; ParaStore = [] | |
1081 | ), | |
1082 | (get_json_key_object(results,Json,ResParas) | |
1083 | -> translate_json_paras(ResParas,results(OpName),FileName,OpName,Bindings2), | |
1084 | sort(Bindings2,ResultStore) | |
1085 | ; ResultStore = [] | |
1086 | ), | |
1087 | (get_json_key_object(destState,Json,DestState) | |
1088 | -> translate_json_paras(DestState,destState,FileName,OpName,Bindings3), | |
1089 | sort(Bindings3,DestStore) | |
1090 | ; DestStore = [] | |
1091 | ), | |
1092 | (get_json_key_list(destStateNotChanged,Json,UnchList) | |
1093 | -> maplist(translate_json_string,UnchList,UnchAtoms), | |
1094 | sort(UnchAtoms,Unchanged) | |
1095 | ; Unchanged = [] | |
1096 | ), | |
1097 | (get_json_key_list(preds,Json,JPredList) | |
1098 | -> (maplist(translate_json_pred,JPredList,PredList) -> true | |
1099 | ; add_error(translate_json_operation,'Unable to parse predicates for operation:',OpName,Position), | |
1100 | PredList = [] | |
1101 | ) | |
1102 | ; PredList = [] | |
1103 | ), | |
1104 | (get_json_key_list(postconditions,Json,PostconditionList) | |
1105 | -> maplist(translate_postcondition,PostconditionList,Postconditions) | |
1106 | ; Postconditions = [] | |
1107 | ), | |
1108 | (get_json_attribute(description,Json,string(DescCodes)) | |
1109 | -> atom_codes(Desc,DescCodes), Meta = [description/Desc,pos/Position] | |
1110 | ; Meta = [pos/Position] | |
1111 | ). | |
1112 | ||
1113 | translate_postcondition(Json,Postcondition) :- | |
1114 | get_json_attribute(kind,Json,string(KindCodes)), | |
1115 | atom_codes(Kind,KindCodes), | |
1116 | (translate_postcondition_kind(Kind,Json,Postcondition) -> true ; add_error(translate_postcondition,'translate_postcondition_kind failed',Json), fail). | |
1117 | ||
1118 | translate_postcondition_kind('PREDICATE',Json,state_predicate(TPred)) :- | |
1119 | !, | |
1120 | get_json_attribute(predicate,Json,PredString), | |
1121 | translate_json_pred(PredString,TPred). | |
1122 | translate_postcondition_kind(Kind,Json,operation_enabled(OpName,TPred,Enabled)) :- | |
1123 | enabled_kind(Kind,Enabled), | |
1124 | !, | |
1125 | get_json_attribute(operation,Json,string(OpNameCodes)), | |
1126 | atom_codes(OpName,OpNameCodes), | |
1127 | get_json_attribute(predicate,Json,PredString), | |
1128 | (PredString = string([]) -> TPred = b(truth,pred,[]) | |
1129 | ; translate_json_pred(PredString,TPred) | |
1130 | ). | |
1131 | ||
1132 | enabled_kind('ENABLEDNESS',enabled). | |
1133 | enabled_kind('DISABLEDNESS',disabled). | |
1134 | ||
1135 | ||
1136 | get_json_key_object(Key,JSON,Object) :- | |
1137 | get_json_attribute(Key,JSON,JObject), | |
1138 | JObject \= @(null), | |
1139 | (JObject = json(Object) -> true | |
1140 | ; add_internal_error('Illegal JSON object for key:',Key:JObject), | |
1141 | fail | |
1142 | ). | |
1143 | get_json_key_list(Key,JSON,List) :- | |
1144 | get_json_attribute(Key,JSON,JList), | |
1145 | JList \= @(null), | |
1146 | (JList = array(List) -> true | |
1147 | ; add_internal_error('Illegal JSON list for key:',Key:JList), | |
1148 | fail | |
1149 | ). | |
1150 | ||
1151 | translate_json_string(string(AtomCodes),Atom) :- atom_codes(Atom,AtomCodes). | |
1152 | ||
1153 | translate_json_paras([],_,_,_,R) :- !, R=[]. | |
1154 | translate_json_paras([Eq|T],Kind,FileName,OpName,[Bind|BT]) :- | |
1155 | translate_json_para(Eq,Kind,FileName,OpName,Bind),!, | |
1156 | translate_json_paras(T,Kind,FileName,OpName,BT). | |
1157 | translate_json_paras([_|T],Kind,FileName,OpName,BT) :- translate_json_paras(T,Kind,FileName,OpName,BT). | |
1158 | ||
1159 | ||
1160 | :- use_module(probsrc(b_global_sets),[add_prob_deferred_set_elements_to_store/3]). | |
1161 | % TO DO: using eval_strings is very ugly, use a better API predicate | |
1162 | translate_json_para(Equality,Kind,FileName,OpName,json_bind(Name,Value,Type,Pos)) :- | |
1163 | is_json_equality_attr_with_pos(Equality,FileName,Name,string(ExpressionCodes),Pos), | |
1164 | !, | |
1165 | %format('Translating JSON Para ~w : ~s~n',[Name,C]), | |
1166 | (eval_strings:repl_parse_expression(ExpressionCodes,Typed,Type,Error) | |
1167 | -> (Error \= none -> add_parameter_error(Error,Name,ExpressionCodes,OpName,Pos) | |
1168 | ; Type = pred -> add_parameter_error('use of predicate instead of expression',Name,ExpressionCodes,OpName,Pos) | |
1169 | ; Type = subst -> add_parameter_error('unexpected substitution',Name,ExpressionCodes,OpName,Pos) | |
1170 | ; (add_prob_deferred_set_elements_to_store([],EState,visible), % value should not depend on any state | |
1171 | eval_strings:eval_expression_direct(Typed,EState,Value) | |
1172 | -> \+ illegal_json_binding_type(Kind,Name,Type,Pos) | |
1173 | ; add_parameter_error('evaluation error',Name,ExpressionCodes,OpName,Pos) | |
1174 | ) | |
1175 | ) | |
1176 | ; add_parameter_error('parsing failed error',Name,ExpressionCodes,OpName,Pos) | |
1177 | ). | |
1178 | translate_json_para(Para,_,_,_,_) :- | |
1179 | add_error(translate_json_para,'Unknown JSON para:',Para),fail. | |
1180 | ||
1181 | :- use_module(specfile,[translate_operation_name/2]). | |
1182 | add_parameter_error(Error,Name,ExpressionCodes,OpName,Pos) :- | |
1183 | translate_operation_name(OpName,TOp), | |
1184 | ajoin(['Ignoring JSON value for parameter ',Name,' of ',TOp,' due to ',Error,':'], Msg), | |
1185 | atom_codes(A,ExpressionCodes), | |
1186 | add_error(translate_json_para,Msg,A,Pos),fail. | |
1187 | ||
1188 | %evaluate_codes_value(ExpressionCodes,Type,Value) :- | |
1189 | % eval_strings:repl_parse_expression(ExpressionCodes,Typed,Type,Error), Error=none, | |
1190 | % eval_strings:eval_expression_direct(Typed,Value). | |
1191 | ||
1192 | illegal_json_binding_type(destState,ID,Type,Pos) :- get_expected_type(ID,Kind,ExpectedType),!, | |
1193 | \+ unify_types_strict(Type,ExpectedType), pretty_type(Type,TS), pretty_type(ExpectedType,ETS), | |
1194 | ajoin(['Ignoring JSON destState value for ',Kind,' ',ID,' due to illegal type ',TS, ', expected:'], Msg), | |
1195 | add_error(translate_json_para,Msg,ETS,Pos). | |
1196 | illegal_json_binding_type(destState,ID,_Type,Pos) :- | |
1197 | add_error(translate_json_para,'Ignoring JSON destState value for unknown identifier:',ID,Pos). | |
1198 | % unknown operations are now dealt with later in check_and_adapt_trace_step | |
1199 | %illegal_json_binding_type(params(Op),ID,_Type,Pos) :- | |
1200 | % b_or_z_mode, % otherwise no types available | |
1201 | % \+ b_top_level_operation(Op), !, | |
1202 | % findall(KOpid,b_top_level_operation(KOpid),Ops), | |
1203 | % (get_possible_fuzzy_matches_and_completions_msg(Op,Ops,FMsg) | |
1204 | % -> ajoin(['Ignoring JSON value for parameter ',ID,' of unknown operation (did you mean the operation ',FMsg,' ?) : '], Msg) | |
1205 | % ; ajoin(['Ignoring JSON value for parameter ',ID,' of unknown operation: '], Msg)), | |
1206 | % add_error(translate_json_para,Msg,Op,Pos). | |
1207 | illegal_json_binding_type(params(Op),ID,Type,Pos) :- | |
1208 | b_or_z_mode, % otherwise no types available | |
1209 | b_top_level_operation(Op), | |
1210 | b_get_machine_operation_typed_parameters_for_animation(Op,Params), | |
1211 | member(b(identifier(ID),ExpectedType,_),Params),!, | |
1212 | \+ unify_types_strict(Type,ExpectedType), pretty_type(Type,TS), pretty_type(ExpectedType,ETS), | |
1213 | ajoin(['Ignoring JSON value for parameter ',ID,' of operation ', Op, ' due to illegal type ',TS, ', expected:'], Msg), | |
1214 | add_error(translate_json_para,Msg,ETS,Pos). | |
1215 | illegal_json_binding_type(params(Op),ID,_Type,Pos) :- | |
1216 | b_or_z_mode, % otherwise show_eventb_any_arguments makes no sense | |
1217 | b_top_level_operation(Op), | |
1218 | (b_get_machine_operation_typed_parameters_for_animation(Op,[]), | |
1219 | get_preference(show_eventb_any_arguments,false) | |
1220 | % TODO: check if ID is a valid virtual parameter, or if trace file was generated with preference set to true | |
1221 | -> ajoin(['Ignoring JSON value for parameter ',ID,', operation has no parameters (setting SHOW_EVENTB_ANY_VALUES to TRUE may help):'],Msg) | |
1222 | ; ajoin(['Ignoring JSON value for unknown parameter ',ID,' for:'],Msg) | |
1223 | ), | |
1224 | add_error(translate_json_para,Msg,Op,Pos). | |
1225 | illegal_json_binding_type(results(Op),ID,Type,Pos) :- | |
1226 | b_or_z_mode, % otherwise there are no typred results available | |
1227 | b_get_machine_operation_typed_results(Op,Results), member(b(identifier(ID),ExpectedType,_),Results),!, | |
1228 | \+ unify_types_strict(Type,ExpectedType), pretty_type(Type,TS), pretty_type(ExpectedType,ETS), | |
1229 | ajoin(['Ignoring JSON value for result ',ID,' of operation ', Op, ' due to illegal type ',TS], Msg), | |
1230 | add_error(translate_json_para,Msg,ETS,Pos). | |
1231 | % unknown operations are now dealt with later in check_and_adapt_trace_step | |
1232 | %illegal_json_binding_type(results(_Op),ID,_Type,Pos) :- | |
1233 | % add_error(translate_json_para,'Ignoring JSON value for unknown operation result:',ID,Pos). | |
1234 | ||
1235 | get_expected_type(ID,variable,ExpectedType) :- bmachine_is_precompiled, b_is_variable(ID,ExpectedType). | |
1236 | get_expected_type(ID,constant,ExpectedType) :- bmachine_is_precompiled, b_is_constant(ID,ExpectedType). | |
1237 | ||
1238 | % TODO: already check results, ... | |
1239 | ||
1240 | translate_json_pred(string(PredCodes),TPred) :- | |
1241 | OuterQuantifier = no_quantifier, | |
1242 | % TO DO: parse in context of operation ! otherwise we get type error for pp=pp for example where pp is a parameter | |
1243 | eval_strings:repl_parse_predicate(PredCodes,OuterQuantifier,TPred,_TypeInfo). % , print(pred_ok(TPred)),nl. | |
1244 | ||
1245 | /* | |
1246 | after reading a JSON ProB2-UI file looks like this: | |
1247 | ||
1248 | json([description=string([70,105,108,101,32,99,114,101,97,116,101,100,32,98,121,32,80,114,111,66,32,84,99,108,47,84,107]),transitionList=array([json([name=string([36,105,110,105,116,105,97,108,105,115,101,95,109,97,99,104,105,110,101]),params=json([]),results=json([]),destState=json([active=string([123,125]),ready=string([123,125]),waiting=string([123,125])]),destStateNotChanged=array([]),preds=@(null)]),json([name=string([110,101,119]),params=json([pp=string([112,114,111,99,101,115,115,51])]),results=json([]),destState=json([waiting=string([123,112,114,111,99,101,115,115,51,125])]),destStateNotChanged=array([string([97,99,116,105,118,101]),string([114,101,97,100,121])]),preds=@(null)]),json([name=string([114,101,97,100,121]),params=json([rr=string([112,114,111,99,101,115,115,51])]),results=json([]),destState=json([active=string([123,112,114,111,99,101,115,115,51,125]),waiting=string([123,125])]),destStateNotChanged=array([string([114,101,97,100,121])]),preds=@(null)]),json([name=string([115,119,97,112]),params=json([]),results=json([]),destState=json([active=string([123,125]),waiting=string([123,112,114,111,99,101,115,115,51,125])]),destStateNotChanged=array([string([114,101,97,100,121])]),preds=@(null)]),json([name=string([110,101,119]),params=json([pp=string([112,114,111,99,101,115,115,50])]),results=json([]),destState=json([waiting=string([123,112,114,111,99,101,115,115,50,44,112,114,111,99,101,115,115,51,125])]),destStateNotChanged=array([string([97,99,116,105,118,101]),string([114,101,97,100,121])]),preds=@(null)])]),metadata=json([fileType=string([84,114,97,99,101]),formatVersion=number(1),savedAt=string([50,48,50,49,45,49,48,45,49,51,84,49,51,58,51,56,58,48,50,90]),creator=string([116,99,108,116,107,32,40,108,101,117,115,99,104,101,108,41]),proBCliVersion=string([49,46,49,49,46,49,45,110,105,103,104,116,108,121]),proBCliRevision=string([51,99,98,56,48,48,98,98,97,100,102,101,97,102,52,102,53,56,49,51,50,55,50,52,53,53,48,55,97,53,53,97,101,53,97,53,101,54,54,100]),modelName=string([115,99,104,101,100,117,108,101,114]),modelFile=string([47,85,115,101,114,115,47,108,101,117,115,99,104,101,108,47,103,105,116,95,114,111,111,116,47,112,114,111,98,95,101,120,97,109,112,108,101,115,47,112,117,98,108,105,99,95,101,120,97,109,112,108,101,115,47,66,47,66,101,110,99,104,109,97,114,107,115,47,115,99,104,101,100,117,108,101,114,46,109,99,104])])]) | |
1249 | ||
1250 | */ | |
1251 | ||
1252 | % ------------------------- | |
1253 | ||
1254 | % Interactive Trace Replay API | |
1255 | ||
1256 | % load a trace file and store it for interactive replay | |
1257 | load_json_trace_file_for_ireplay(FileName) :- | |
1258 | read_json_trace_file(FileName,ModelName,Trace), | |
1259 | reset_json_trace_replay, | |
1260 | store_json_trace(Trace,0,Len), | |
1261 | assert(current_replay_step(1)), | |
1262 | assert(loaded_json_trace_file(FileName,ModelName,Len)). | |
1263 | ||
1264 | :- dynamic loaded_json_trace_file/3, json_trace_replay_step/3. | |
1265 | :- dynamic current_replay_step/1, json_trace_replayed_info/2. | |
1266 | ||
1267 | ||
1268 | :- use_module(eventhandling,[register_event_listener/3]). | |
1269 | :- register_event_listener(clear_specification,reset_json_trace_replay, | |
1270 | 'Reset interactive trace replay.'). | |
1271 | ||
1272 | reset_json_trace_replay :- | |
1273 | retractall(current_replay_step(_)), | |
1274 | retractall(loaded_json_trace_file(_,_,_)), | |
1275 | retractall(json_trace_replay_step(_,_,_)), | |
1276 | retractall(json_trace_replayed_info(_,_)). | |
1277 | ||
1278 | store_json_trace([],L,L). | |
1279 | store_json_trace([TransSpec|T],StepNr,Len) :- | |
1280 | S1 is StepNr + 1, | |
1281 | get_transition_spec_txt(TransSpec,TTxt), | |
1282 | formatsilent_with_colour(user_output,[blue],'==> Trace step ~w: ~w~n',[S1,TTxt]), | |
1283 | phrase(check_and_adapt_trace_step(TransSpec,S1,CorrectedTransSpec),StaticErrors), | |
1284 | assert(json_trace_replay_step(S1,CorrectedTransSpec,StaticErrors)), | |
1285 | store_json_trace(T,S1,Len). | |
1286 | ||
1287 | get_trace_step_info(StepNr,StepDescr) :- | |
1288 | json_trace_replay_step(StepNr,TransSpec,StaticErrors), | |
1289 | get_transition_spec_txt(TransSpec,TTxt), | |
1290 | (StaticErrors = [] -> StaticTT=[] | |
1291 | ; length(StaticErrors,NrStaticErrors), | |
1292 | StaticTT = [' (static errors: ',NrStaticErrors,')'] | |
1293 | ), | |
1294 | (json_trace_replayed_info(StepNr,Info), | |
1295 | get_replay_info_text(Info,TextAtoms) | |
1296 | -> append(StaticTT,TextAtoms,TT), | |
1297 | ajoin([StepNr,': ',TTxt | TT],StepDescr) | |
1298 | ; ajoin([StepNr,': ',TTxt | StaticTT],StepDescr) | |
1299 | ). | |
1300 | ||
1301 | get_replay_info_text(replay(FromID,TransID,MatchInfo,Errors),TextAtoms) :- | |
1302 | transition(FromID,OperationTerm,TransID,_ToID), | |
1303 | translate_event_with_limit(OperationTerm,40,OpTxt), | |
1304 | !, | |
1305 | (Errors=[] -> TextAtoms = [' * REPLAYED (', MatchInfo,') : ',OpTxt] | |
1306 | ; length(Errors,NrErrors), | |
1307 | TextAtoms = [' * REPLAYED (', MatchInfo, ', errors: ',NrErrors,') : ', OpTxt] | |
1308 | ). | |
1309 | get_replay_info_text(skipped,[' SKIPPED']) :- !. | |
1310 | get_replay_info_text(X,[X]). | |
1311 | ||
1312 | tk_get_stored_json_trace_description(list(List)) :- | |
1313 | findall(StepDescr,get_trace_step_info(_,StepDescr),List). | |
1314 | ||
1315 | try_replay_next_step(MatchSpecs,CurStepNr,FromID,TransID,MatchInfo,TkTransInfos,TkErrors) :- | |
1316 | current_replay_step(CurStepNr), | |
1317 | current_state_id(FromID), | |
1318 | json_trace_replay_step(CurStepNr,TransSpec,StaticErrors), | |
1319 | flexible_perform_single_replay_step(FromID,TransID,DestId,MatchSpecs,TransSpec,MatchInfo), | |
1320 | phrase(check_step_postconditions(TransSpec,DestId),Errors,StaticErrors), | |
1321 | get_transition_name(FromID,TransID,OpName), | |
1322 | length(Errors,NrErrs), | |
1323 | formatsilent_with_colour(user_output,[green], '==> Replay step ~w (~w) ~w leading to state ~w~n',[CurStepNr,MatchInfo,OpName,DestId]), | |
1324 | (NrErrs>0 -> formatsilent_with_colour(user_output,[orange],' Errors (~w): ~w~n',[NrErrs,Errors]) ; true), | |
1325 | findall(TInfo,get_transition_info(TransSpec,TInfo),TkTransInfos), | |
1326 | maplist(get_rerror,Errors,TkErrors). % make errors atomic for Tk | |
1327 | ||
1328 | get_rerror(Err,Msg) :- get_replay_error(Err,Msg),!. | |
1329 | get_rerror(Err,Msg) :- atom(Err),!,Msg=Err. | |
1330 | get_rerror(Err,F) :- functor(Err,F). | |
1331 | ||
1332 | :- use_module(translate,[translate_bexpression_with_limit/3]). | |
1333 | % get text descriptions for a transition_spec, to be shown to user e.g. in Tk listbox: | |
1334 | get_transition_info(transition_spec(_Op, _Meta, Paras, Results, DestStore,_UnchangedVars,_Preds,_Post),InfoTxt) :- | |
1335 | (Kind=para, List=Paras | |
1336 | ; Kind=result, List=Results | |
1337 | ; Kind=dest, List=DestStore), | |
1338 | get_binding_txt(List,Txt), | |
1339 | ajoin([Kind,' : ',Txt],InfoTxt). | |
1340 | get_transition_info(transition_spec(_Op, _, _, _, _,UnchangedVars,_,_),InfoTxt) :- | |
1341 | member(ID,UnchangedVars), | |
1342 | ajoin(['unchanged : ',ID],InfoTxt). | |
1343 | get_transition_info(transition_spec(_Op, _, _, _, _,_,Preds,_),InfoTxt) :- | |
1344 | member(TP,Preds), | |
1345 | translate_bexpression_with_limit(TP,250,TPS), | |
1346 | ajoin(['pred : ',TPS],InfoTxt). | |
1347 | ||
1348 | get_binding_txt(List,BindingText) :- | |
1349 | member(json_bind(Var,Value,_ValType,_ValPos),List), | |
1350 | translate_bvalue_with_limit(Value,200,VS), | |
1351 | ajoin([Var,'=',VS],BindingText). | |
1352 | ||
1353 | default_match_specs([MatchSpec,MS2,MS3]) :- | |
1354 | precise_match_spec(MatchSpec), % require precise replay | |
1355 | ignore_dest_match_spec(MS2), | |
1356 | opname_optimize_match_spec(MS3). | |
1357 | ||
1358 | ireplay :- default_match_specs(MS), | |
1359 | ireplay(MS). | |
1360 | ||
1361 | :- use_module(probsrc(state_space), [extend_trace_by_transition_ids/1]). | |
1362 | ireplay(MatchSpecs) :- | |
1363 | replay_current_step(MatchSpecs,_),!, | |
1364 | ireplay(MatchSpecs). | |
1365 | ireplay(MatchSpecs) :- | |
1366 | skip_current_ireplay_step(_), | |
1367 | ireplay(MatchSpecs). | |
1368 | ||
1369 | % get information about how the status of replaying a loaded trace is: | |
1370 | get_ireplay_status(CurStepNr,Steps,Finished) :- | |
1371 | loaded_json_trace_file(_,_,Steps), | |
1372 | current_replay_step(CurStepNr), | |
1373 | (CurStepNr =< Steps | |
1374 | -> Finished=not_finished | |
1375 | ; Finished=finished). | |
1376 | ||
1377 | replay_of_current_step_is_possible(CurStepNr,OpName,MatchInfo,list(TransInfos),list(Errors)) :- | |
1378 | default_match_specs(MS), | |
1379 | try_replay_next_step(MS,CurStepNr,FromID,TransID,MatchInfo,TransInfos,Errors), | |
1380 | get_transition_name(FromID,TransID,OpName). | |
1381 | ||
1382 | % try and replay as much as possible | |
1383 | ireplay_fast_forward(NrReplayed) :- | |
1384 | default_match_specs(MS), ireplay_fast_forward(MS,0,NrReplayed). | |
1385 | ||
1386 | ireplay_fast_forward(MatchSpecs,Nr,NrReplayed) :- | |
1387 | replay_current_step(MatchSpecs,_),!, | |
1388 | N1 is Nr+1, | |
1389 | ireplay_fast_forward(MatchSpecs,N1,NrReplayed). | |
1390 | ireplay_fast_forward(_,NrReplayed,NrReplayed). | |
1391 | ||
1392 | % replay the currently selected step of the JSON trace, matching one step of the trace with one animation step | |
1393 | replay_current_step(CurStepNr) :- | |
1394 | default_match_specs(MS), replay_current_step(MS,CurStepNr). | |
1395 | ||
1396 | % try and replay current step according to match specifications | |
1397 | replay_current_step(MatchSpecs,CurStepNr) :- | |
1398 | try_replay_next_step(MatchSpecs,CurStepNr,FromID,TransID,MatchInfo,_,Errors),!, | |
1399 | extend_trace_by_transition_ids([TransID]), % update state space | |
1400 | assert(json_trace_replayed_info(CurStepNr,replay(FromID,TransID,MatchInfo,Errors))), | |
1401 | increase_step_nr. | |
1402 | ||
1403 | % skip the current replay step and go to the next one | |
1404 | skip_current_ireplay_step(CurStepNr) :- | |
1405 | current_replay_step(CurStepNr), | |
1406 | json_trace_replay_step(CurStepNr,TransSpec,_StaticErrors), | |
1407 | get_transition_spec_txt(TransSpec,TTxt), | |
1408 | formatsilent_with_colour(user_output,[orange],'Skipping replay step ~w : ~w~n',[CurStepNr,TTxt]), | |
1409 | assert(json_trace_replayed_info(CurStepNr,skipped)), | |
1410 | increase_step_nr. | |
1411 | ||
1412 | increase_step_nr :- | |
1413 | retract(current_replay_step(CurStepNr)), | |
1414 | S1 is CurStepNr+1, | |
1415 | assert(current_replay_step(S1)). | |
1416 | ||
1417 | % :- b_intelligent_trace_replay:load_json_trace_file_for_interactive_replay('/Users/leuschel/git_root/prob_examples/public_examples/B/CBC/ConstantsAndVars/MyAwesomeLift.prob2trace'), b_intelligent_trace_replay:ireplay. | |
1418 | ||
1419 | ||
1420 |