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