1 % (c) 2009-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(state_space,
6 [get_state_space_stats/3, get_state_space_stats/4,
7 gen_new_state_id/1,
8 history/1, forward_history/1,
9 get_action_trace/1, % old trace/1
10 get_action_trace_with_limit/2,
11 get_action_term_trace/1,
12 op_trace_ids/1, add_to_op_trace_ids/1, remove_from_op_trace_ids/1, reset_op_trace_ids/0,
13 get_state_id_trace/1,
14 current_state_id/1, current_expression/2,
15 set_current_state_id/1,
16 current_options/1, set_current_options/1,
17 get_current_predecessor_state_id/1,
18
19 add_id_at_front/1, add_id_at_end/1,
20 add_id_random/1, /* from state_space_open_nodes_c */
21 add_id_with_weight/2, add_id_to_process/3,
22 pop_id_from_front/1, pop_id_from_end/1, pop_id_oldest/1,
23 retract_open_node/1, open_ids_empty/0,
24 top_front_id/1, top_front_weight/1,
25
26 visited_expression/3, visited_expression/2, visited_expression_id/1,
27 find_hashed_packed_visited_expression/3,
28 retract_visited_expression/2,
29 not_all_transitions_added/1,
30 not_invariant_checked/1, set_invariant_checked/1,
31 invariant_not_yet_checked/1, invariant_still_to_be_checked/1,
32 not_interesting/1, % nodes ignored because they do not satisfy a user-provided scope predicate
33 mark_as_not_interesting/1,
34 max_reached_for_node/1,
35 max_reached_or_timeout_for_node/1,
36 use_no_timeout/1, time_out_for_node/1, time_out_for_node/3,
37 hash_to_id/2,id_to_marker/2, hash_to_nauty_id/2,
38 register_back_edge/2, try_compute_depth_of_state_id/2,
39 invariant_violated/1, time_out_for_invariant/1, time_out_for_assertions/1,
40 set_invariant_violated/1,
41
42 state_error_exists/0, state_error/3, store_state_error/3,
43 set_context_state/1, set_context_state/2,
44 update_context_state/1, clear_context_state/0, get_current_context_state/1,
45 store_error_for_context_state/2,
46 copy_current_errors_to_state/2,
47 store_abort_error_for_context_state_if_possible/4,
48 abort_error_exists_in_context_state/1,
49
50 transition/3,transition/4, any_transition/3,
51 store_transition/4,
52 deadlocked_state/1, % no outgoing edge
53 is_initial_state_id/1, is_concrete_constants_state_id/1,
54 multiple_concrete_constants_exist/0,
55 get_constants_state_for_id/2, get_constants_state_for_id/3,
56 get_constants_state_id_for_id/2,
57 try_get_unique_constants_state/1,
58 get_constants_id_for_state_id/2,
59 get_variables_state_for_id/2,
60 out_degree/2,
61 operation_not_yet_covered/1, operation_name_not_yet_covered/1,
62 get_operation_name_coverage_infos/4,
63 mark_operation_as_covered/1,
64 initialise_operation_not_yet_covered/0,
65
66 transition_info/2, store_transition_infos/2,
67 keep_transition_info/1, check_relevant_transition_info_stored/2,
68 compute_transitions_if_necessary/1,
69
70 state_space_initialise/0, state_space_initialise_with_stats/0,
71 state_space_reset/0,
72 state_space_add/2, state_space_packed_add/2,
73 delete_node/1,
74
75 current_state_corresponds_to_initialised_b_machine/0,
76 current_state_corresponds_to_fully_setup_b_machine/0,
77 current_state_corresponds_to_setup_constants_b_machine/0,
78 visited_state_corresponds_to_initialised_b_machine/1,
79 visited_state_corresponds_to_setup_constants_b_machine/1,
80
81 specialized_inv/2, %reuse_operation/4,
82 assert_max_reached_for_node/1, assert_time_out_for_node/3,
83 assert_time_out_for_invariant/1, assert_time_out_for_assertions/1,
84
85 set_max_nr_of_new_impl_trans_nodes/1,
86 get_max_nr_of_new_impl_trans_nodes/1,
87 impl_trans_term/3, impl_trans_term/4,
88 impl_trans_term_all/2,
89 impl_trans_id/4, impl_trans_not_complete/1,
90 compute_transitions_if_necessary_saved/1,
91 max_nr_of_new_nodes_limit_not_reached/0,
92
93 find_trace_to_initial_state/2, find_initialised_states/1,
94
95 tcltk_save_state_space/1, tcltk_load_state/1,
96 compute_full_state_space_hash/1,
97
98 execute_id_trace_from_current/1, execute_id_trace_from_current/3,
99 set_trace_by_transition_ids/1, try_set_trace_by_transition_ids/1,
100 extend_trace_by_transition_ids/1,
101 extract_term_trace_from_transition_ids/2,
102
103 add_counterexample_node/1, add_counterexample_op/1,
104 reset_counterexample/0, set_counterexample_by_transition_ids/1,
105 counterexample_node/1, counterexample_op/1 % specific predicates to register counter examples
106 ]).
107
108 :- use_module(library(lists)).
109
110 :- use_module(self_check).
111 :- use_module(error_manager).
112 :- use_module(gensym).
113 :- use_module(preferences).
114 :- use_module(tools).
115 :- use_module(debug,[debug_println/1, debug_println/2]).
116 %:- use_module(state_space_exploration_modes,[compute_hash/3]).
117
118 :- use_module(extension('counter/counter'),
119 [counter_init/0, new_counter/1, get_counter/2, inc_counter/1, inc_counter/2, inc_counter_by/2, reset_counter/1, set_counter/2]).
120
121 :- use_module(module_information).
122 :- module_info(group,state_space).
123 :- module_info(description,'This module keeps track of the visited states by the animator/model checker.').
124
125 % ----------------------------------
126
127 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
128 :- if((environ(prob_myheap,false) ; \+ predicate_property(load_foreign_resource(_), _))).
129 :- use_module(state_space_open_nodes). %% comment in to use only Prolog datastructures
130 :- else.
131 :- use_module(state_space_open_nodes_c). %% comment in to use C++ multimap queue; can make use of HEURISTIC_FUNCTION
132 :- endif.
133
134 % ----------------------------------
135
136 get_state_space_stats(NrNodes,NrTransitions,ProcessedNodes) :-
137 get_counter(states,N), NrNodes is N+1,
138 get_counter(transitions,NrTransitions),
139 get_counter(processed_nodes,ProcessedNodes).
140
141 get_state_space_stats(NrNodes,NrTransitions,ProcessedNodes,IgnoredNodes) :-
142 get_state_space_stats(NrNodes,NrTransitions,ProcessedNodes),
143 get_counter(not_interesting_nodes,IgnoredNodes).
144
145 gen_new_state_id(Nr) :-
146 inc_counter(states,N1), Nr is N1-1. % only one C call
147 %get_counter(states,Nr), inc_counter(states).
148 reset_state_counter :- reset_counter(states).
149 reset_state_counter(Nr) :- set_counter(states,Nr).
150
151 get_state_id_trace(StateIds) :-
152 history(History),
153 current_state_id(CurID),
154 reverse([CurID|History],StateIds).
155
156 get_current_predecessor_state_id(PriorID) :-
157 history([PriorID|_]).
158
159 :- dynamic history/1.
160 history([]).
161
162
163 :- dynamic forward_history/1.
164
165 :- dynamic current_state_id/1.
166 current_state_id(root).
167 /* INITIAL STATE, third arg: clp(fd) constraints as generated
168 by fd_copy_term */
169
170 current_expression(ID,State) :- current_state_id(ID),
171 visited_expression(ID,State).
172 current_packed_expression(ID,State) :- current_state_id(ID),
173 packed_visited_expression(ID,State).
174
175 :- use_module(specfile,[state_corresponds_to_initialised_b_machine/1,
176 state_corresponds_to_fully_setup_b_machine/1,
177 state_corresponds_to_set_up_constants/1]).
178 current_state_corresponds_to_initialised_b_machine :-
179 current_packed_expression(_,PS), unpack_state_top_level(PS,TLState),
180 state_corresponds_to_initialised_b_machine(TLState).
181 visited_state_corresponds_to_initialised_b_machine(ID) :-
182 packed_visited_expression(ID,PS), unpack_state_top_level(PS,TLState),
183 state_corresponds_to_initialised_b_machine(TLState).
184
185 current_state_corresponds_to_fully_setup_b_machine :-
186 current_packed_expression(_,PS), unpack_state_top_level(PS,TLState),
187 state_corresponds_to_fully_setup_b_machine(TLState).
188
189 current_state_corresponds_to_setup_constants_b_machine :-
190 current_packed_expression(_,PS), unpack_state_top_level(PS,TLState),
191 state_corresponds_to_set_up_constants(TLState).
192
193 visited_state_corresponds_to_setup_constants_b_machine(ID) :-
194 packed_visited_expression(ID,PS), unpack_state_top_level(PS,TLState),
195 state_corresponds_to_set_up_constants(TLState).
196
197 :- dynamic current_options/1.
198 current_options([]).
199
200 set_current_options(Options) :-
201 retractall( current_options(_) ),
202 assertz( current_options(Options) ).
203
204 :- dynamic packed_visited_expression/2.
205 %packed_visited_expression(v_0,true).
206
207 :- use_module(state_packing).
208
209 retract_visited_expression(ID,State) :- retract(packed_visited_expression(ID,PState)),
210 unpack_state(PState,State).
211
212 retractall_visited_expression(ID) :- retractall(packed_visited_expression(ID,_)).
213
214 state_space_packed_add(Id,PackedTerm) :- assertz(packed_visited_expression(Id,PackedTerm)).
215
216 state_space_add(Id,Term) :-
217 (pack_state(Term,PackedTerm) -> assertz(packed_visited_expression(Id,PackedTerm))
218 ; add_internal_error('State packing failed: ',pack_state(Term,_)),
219 assertz(packed_visited_expression(Id,Term))).
220
221 % deprecated:
222 visited_expression(ID,State,true) :- visited_expression(ID,State).
223 % not call(CurBody); for the moment we always have true as last argument
224
225 ?visited_expression(A,B) :- packed_visited_expression(A,PB),
226 (unpack_state(PB,R) -> B=R ; add_internal_error('Unpacking state failed: ',unpack_state(PB,R)),R=A).
227
228 %visited_expression_id(A) :- packed_visited_expression(A,_). % avoid unpacking state
229 % even better: not to look up fact at all to avoid constructing state term, this is done below:
230 :- use_module(library(between),[between/3]).
231 visited_expression_id(ID) :- number(ID),!, ID>=0, get_counter(states,N), ID<N.
232 visited_expression_id(ID) :- ID==root,!.
233 visited_expression_id(root).
234 ?visited_expression_id(Nr) :- Nr \== root, get_counter(states,N), N1 is N-1, between(0,N1,Nr).
235
236 % given a hash and a packed state: find ID (fail if does not exist)
237 find_hashed_packed_visited_expression(Hash,PackedState,ID) :-
238 hash_to_id(Hash,ID),
239 (packed_visited_expression(ID,PackedState)
240 -> true /* warning: may instantiate State if not ground */
241 ; functor(PackedState,concrete_constants,1),
242 preference(maxNrOfInitialisations,Max), Max =< 4
243 -> debug_println(19,hash_collision_for_constants(Hash,ID)),fail
244 % this is not too bad if maxNrOfInitialisations small;
245 % can happen due to depth limit in term_depth_hash in compute_hash
246 ; write(hash_collision(Hash,ID)),nl,fail
247 ).
248
249
250 :- dynamic not_invariant_checked/1.
251 set_invariant_checked(ID) :- %print(inv_checked(ID)),nl,
252 retract(not_invariant_checked(ID)).
253
254 invariant_not_yet_checked(ID) :-
255 not_all_transitions_added(ID) ; /* assumption: if not all transitions added then we haven't checked invariant yet */
256 not_invariant_checked(ID) ;
257 not_interesting(ID). % assumption: if a node is marked as not interesting it will not be examined
258
259 % difference with invariant_not_yet_checked: not interesting nodes not reported and cannot be backtracked
260 invariant_still_to_be_checked(ID) :-
261 (not_all_transitions_added(ID) -> true ; not_invariant_checked(ID) -> true).
262
263 :- dynamic not_interesting/1.
264 %not_interesting(v_0).
265
266 :- dynamic max_reached_for_node/1.
267 /* true if not all outgoing transistions were computed due to the limit
268 on the number of operations/initialisations computed */
269 :- dynamic time_out_for_node/3, use_no_timeout/1, time_out_for_invariant/1, time_out_for_assertions/1.
270
271 time_out_for_node(ID) :- (var(ID) -> visited_expression_id(ID) ; true),
272 (time_out_for_node(ID,_,_) -> true ; fail).
273
274 :- dynamic transition/4.
275 %transition(v_0,a,1,v_1).
276
277 store_transition(Org,Action,Dest,Id) :-
278 %get_counter(transitions,Id), inc_counter(transitions),
279 inc_counter(transitions,Id1), Id is Id1-1, % only one C call
280 assertz(transition(Org,Action,Id,Dest)).
281
282 ?deadlocked_state(Origin) :- \+ any_transition(Origin,_,_).
283
284 is_concrete_constants_state_id(ID) :-
285 ? transition(root,_,ID),
286 packed_visited_expression(ID,concrete_constants(_)).
287
288 % check if we have multiple constant setups
289 multiple_concrete_constants_exist :-
290 ? is_concrete_constants_state_id(ID),
291 ? is_concrete_constants_state_id(ID2), ID2 \= ID,!.
292
293
294 is_initial_state_id(InitialStateID) :-
295 transition(root,_,State),
296 packed_visited_expression(State,P),
297 (P = concrete_constants(_) % also covers '$partial_setup_constants'
298 -> state_space:transition(State,_,InitialStateID)
299 ; InitialStateID=State).
300
301 % get the constants for a state (if there are constants), avoid unpacking variables
302 get_constants_state_for_id(ID,CS) :- get_constants_state_for_id(ID,[],CS).
303 get_constants_state_for_id(ID,Opts,ConstantsState) :-
304 packed_visited_expression(ID,PState),
305 unpack_state_top_level(PState,TLState),
306 (TLState = const_and_vars(CID,_) -> get_constants_state_for_id(CID,[],ConstantsState)
307 ; unpack_state(PState,UPState),
308 (UPState = concrete_constants(CS) -> ConstantsState=CS
309 ; UPState = [_|_], member(allow_variable_list,Opts),
310 ConstantsState=UPState,
311 % could be that we have a state where constants and variables are mixed in a single list
312 % e.g., ProB2's FindStateCommand does currently not create new intermediate constant states
313 add_message(get_constants_state_for_id,'State is a list of bindings and has no reference to a constant valuation: ',ID)
314 )
315 ).
316
317 % get the state id for associated constants state for a state (if there are constants)
318 get_constants_state_id_for_id(ID,CstID) :-
319 packed_visited_expression(ID,PState),
320 unpack_state_top_level(PState,TLState),
321 get_constants_id_for_id_aux(TLState,ID,CstID).
322 get_constants_id_for_id_aux(concrete_constants(_),ID,ID).
323 get_constants_id_for_id_aux(const_and_vars(ID,_),_,ID).
324
325 get_variables_state_for_id(ID,VarState) :-
326 visited_expression(ID,State),
327 get_vars_aux(State,VarState).
328 %get_vars_aux(concrete_constants(ConstantsState),[]).
329 get_vars_aux(const_and_vars(_,Vars),State) :- !, State=Vars.
330 get_vars_aux([],[]).
331 get_vars_aux([H|T],[H|T]).
332
333 % check if there is a unique constants state:
334 try_get_unique_constants_state(ConstantsState) :-
335 transition(root,_,_TransID,DestID),
336 \+ (transition(root,_,_,DestID2), DestID2 \= DestID), % no other transition exists
337 \+ max_reached_or_timeout_for_node(root),
338 DestID \= root, % DestID=root should never happen
339 get_constants_state_for_id(DestID,ConstantsState).
340
341 % returns id of constants state for a state (if it exists)
342 get_constants_id_for_state_id(ID,ConstID) :-
343 packed_visited_expression(ID,'$cst_vars'(ConstID,_)).
344
345 ?any_transition(Origin,TransID,Destination) :- transition(Origin,_,TransID,Destination).
346
347 ?transition(Origin,Action,Destination) :- transition(Origin,Action,_TransID,Destination).
348
349 :- dynamic transition_info/2.
350 store_transition_infos([],_TransId).
351 store_transition_infos([Info|Irest],TransId) :-
352 store_transition_info(Info,TransId),
353 store_transition_infos(Irest,TransId).
354 store_transition_info(Info,TransId) :- %print(info(Info,TransId)),nl,
355 (keep_transition_info(Info)
356 -> assertz(transition_info(TransId,Info))
357 ; true).
358
359 % Do not store path info by default
360 keep_transition_info(path(_)) :- !,
361 preference(eventtrace,true). % STORE_DETAILED_TRANSITION_INFOS
362 keep_transition_info(eventtrace(_)) :- !,preference(eventtrace,true).
363 keep_transition_info(event(_)) :- !,
364 preference(store_event_transinfo,true). % store info about parameters and abstract Rodin events
365 keep_transition_info(_). % store everything else
366
367 % check if all relevant transition infos have been stored for a transition
368 check_relevant_transition_info_stored([],_).
369 check_relevant_transition_info_stored([Info|Irest],TransId) :-
370 (keep_transition_info(Info) -> transition_info(TransId,Info) ; true),
371 check_relevant_transition_info_stored(Irest,TransId).
372
373
374 reset_transition_store :-
375 retractall(transition(_,_,_,_)),
376 retractall(transition_info(_,_)),
377 reset_counter(transitions),
378 reset_counterexample.
379
380 /*
381 Version with packing of transitions:
382 store_transition(Org,Action,Dest,Id) :-
383 retract(transition_counter(Id)),
384 NewId is Id+1,
385 assertz(transition_counter(NewId)),
386 Action =.. [ActionName|Parameters],
387 pack_values(Parameters,PackedParameters),
388 assertz(packed_transition(Org,ActionName,PackedParameters,Id,Dest)).
389
390 transition(Origin,Action,TransID,Destination) :- nonvar(Action),!,
391 Action =.. [ActionName|Parameters],
392 packed_transition(Origin,ActionName,PackedParameters,TransID,Destination),
393 unpack_values(PackedParameters,Parameters).
394 transition(Origin,Action,TransID,Destination) :-
395 packed_transition(Origin,ActionName,PackedParameters,TransID,Destination),
396 unpack_values(PackedParameters,Parameters),
397 Action =.. [ActionName|Parameters].
398 any_transition(Origin,TransID,Destination) :- packed_transition(Origin,_,_,TransID,Destination).
399 */
400
401 % compute out-degree of a node
402 out_degree(ID,OutDegree) :- findall(0, transition(ID,_,_,_), L), length(L,OutDegree).
403
404 operation_name_not_yet_covered(OpName) :- operation_not_yet_covered(OpName).
405
406
407 get_operation_name_coverage_infos(PossibleNr,FeasibleNr,UncovNr,UncoveredList) :-
408 findall(ON, specfile:get_possible_event(ON), Possible), length(Possible,PossibleNr),
409 findall(OF, specfile:get_feasible_event(OF), Feasible), length(Feasible,FeasibleNr),
410 findall(OpName, state_space: operation_name_not_yet_covered(OpName), UncoveredList),
411 length(UncoveredList,UncovNr).
412
413
414 :- dynamic operation_not_yet_covered/1.
415 %operation_not_yet_covered(b).
416
417 :- use_module(probsrc(debug),[formatsilent/2]).
418 mark_operation_as_covered(OpName) :-
419 (operation_not_yet_covered(OpName), % small improvement in B/PerformanceTests/ModelChecking/EnumSetLookups.mch
420 retract(operation_not_yet_covered(OpName))
421 -> (preferences:get_preference(provide_trace_information,true)
422 -> formatsilent('Covered ~w~n',[OpName])
423 ; true),
424 ? (operation_not_yet_covered(_) -> true ; formatsilent('~nALL OPERATIONS COVERED~n',[]))
425 ; true
426 ).
427
428 :- use_module(bmachine,[b_top_level_operation/1]).
429 :- use_module(probcspsrc(haskell_csp),[channel/2]).
430 initialise_operation_not_yet_covered :- retractall(operation_not_yet_covered(_)),
431 b_or_z_mode,
432 ? b_top_level_operation(Name),
433 % b_get_machine_operation(Name,_,Par,_), length(Par,Arity), functor(Op,Name,Arity),
434 % Note: no '-->' added
435 assertz(operation_not_yet_covered(Name)),
436 fail.
437 /* Missing: treat operations with return values */
438 initialise_operation_not_yet_covered :- csp_mode, \+ csp_with_bz_mode,
439 channel(Name,_),
440 assertz(operation_not_yet_covered(Name)),
441 fail.
442 initialise_operation_not_yet_covered.
443
444 state_error_exists :- state_error(_,_,_),!.
445 :- dynamic state_error/3.
446
447 %state_error([],invariant_violated).
448
449 reset_next_state_error_id_counter :- reset_counter(next_state_error_id).
450 :- use_module(tools_printing, [print_error/1, format_error_with_nl/2]).
451 :- use_module(error_manager,[print_error_span/1]).
452 store_state_error(State,Error,Id) :- state_error(State,Id,Error),!. % do not store identical error twice
453 store_state_error(State,Error,Id) :-
454 %retract( next_state_error_id(Id) ),
455 inc_counter(next_state_error_id,Id),
456 % tools_printing:print_term_summary(Error),nl, tools_printing:nested_print_term(Error),nl,
457 assertz( state_error(State,Id,Error) ).
458 store_error_for_context_state(Error,Id) :-
459 ( context_state(State,Errs) ->
460 (Errs<25
461 -> store_state_error(State,Error,Id), E1 is Errs+1,
462 %assertz(context_state(State,E1))
463 set_context_number_of_errors(E1)
464 ; store_state_error(State,max_state_errors_reached(25),Id)
465 )
466 ;
467 add_internal_error('No known context when calling store_error_for_context_state: ',store_error_for_context_state(Error,Id)),
468 fail).
469
470 % check if we already have a certain type of error in the current context state
471 abort_error_exists_in_context_state(ErrType) :- get_current_context_state(State),
472 Error = abort_error(ErrType,_Msg,_Term,_Pos),
473 state_error(State,_Id,Error).
474
475 % copy current errors from error_manager to state errors
476 copy_current_errors_to_state(StateID,Context) :-
477 % error_manager:logged_error(Source,ErrMsg,_Context,Span), % will not retract
478 error_manager:get_error_with_span(Source,ErrMsg,Span), % will retract
479 store_state_error(StateID,abort_error(Source,ErrMsg,'',span_context(Span,Context)),SID),
480 % TO DO: use other error class
481 debug_println(stored(Source,SID,_Context,ErrMsg)),
482 fail.
483 copy_current_errors_to_state(_,_).
484
485 store_abort_error_for_context_state_if_possible(ErrType,Msg,Term,Span) :-
486 %print(store(Msg,Term,Span)),nl,
487 ( get_current_context_state(State) ->
488 error_manager:get_error_context(Context),
489 (abort_error_for_same_location_exists(State,Id1,ErrType,Msg,Span),
490 abort_error_for_same_location_exists(State,Id2,ErrType,Msg,Span),
491 Id2>Id1
492 -> /* two errors of same type, for same state and same source location exists */
493 /* TO DO: maybe merge state errors */
494 simplify_span(Span,Span1),
495 compress_span(Span1,Span2),
496 store_state_error(State,abort_error(ErrType,'Further identical errors occurred (not stored !)',Term,span_context(Span2,Context)),_)
497 ;
498 format_error_with_nl('! An error occurred in state ~w: ~w !',[State,ErrType]),
499 % usual errors: precondition_error, while_invariant_violation, while_variant_error,
500 % assert_error, well_definedness_error, card_overflow_error
501 print_error(Msg),
502 print_error_term(Term,Span),
503 % print_error(context_state_id(State)), % printed by print_error_context
504 print_error_context,
505 (debug_mode(on),visited_expression(State,S) -> translate:translate_bstate(S,O),print_error(O) ; true),
506 compress_span(Span,Span2),
507 print_error_span(Span2),
508 store_state_error(State,abort_error(ErrType,Msg,Term,span_context(Span2,Context)),_)
509 ),
510 (add_new_event_in_error_scope(abort_error(ErrType)) -> true ; true), % should we use well_definedness_error?
511 assert_real_error_occurred(abort_error) % Note that in this case the error manager list of errors maybe empty even though real_error_occured is true. (see ProB2 kernel test de.prob.cli.integration.rules.RulesMachineTest > testReuseStateSpace)
512 ; % no current context_state exists:
513 compress_span(Span,Span2),
514 add_error(ErrType,Msg,Term,Span2)
515 ).
516
517
518 :- use_module(bsyntaxtree, [find_identifier_uses/3]).
519 compress_span(span_context(Span,C),span_context(CS,C)) :- !,
520 compress_span(Span,CS).
521 compress_span(pos_context(Span1,C,Span2),pos_context(CS1,C,CS2)) :- !,
522 compress_span(Span1,CS1),
523 compress_span(Span2,CS2).
524 compress_span(span_predicate(Pred,LS,S),Res) :- find_identifier_uses(Pred,[],Ids),
525 sort(Ids,SIds),
526 filter_state(LS,SIds,FLS),
527 filter_state(S,SIds,FS), % TODO: do we need to store the global state? we can reconstruct it ?
528 % format('Compressed span_predicate (~w)~n',[Ids]),
529 !,
530 Res = span_predicate(Pred,FLS,FS).
531 compress_span(S,S).
532 % avoid storing large useless values
533 % TO DO: probably we should stop storing spans when a certain threshold of number of errors is reached
534
535
536 :- use_module(library(ordsets),[ord_member/2]).
537 filter_state([],_,[]).
538 filter_state([bind(ID,L)|T],Vars,Res) :-
539 (ord_member(ID,Vars) -> Res = [bind(ID,L)|RT] ; Res=RT),
540 filter_state(T,Vars,RT).
541
542
543 :- use_module(translate, [translate_error_term/3]).
544 print_error_term(T,S) :- (var(T);var(S)),!,
545 print_error('### VARIABLE error term or span:'), print_error(print_error_term(T,S)).
546 print_error_term(Term,Span) :- translate_error_term(Term,Span,S),
547 (S='' -> true ; print_error(S)).
548
549 abort_error_for_same_location_exists(State,Id,ErrType,Msg,Span) :-
550 state_error(State,Id,abort_error(ErrType,Msg,_Term2,span_context(Span2,_Ctxt2))),
551 same_span_location(Span2,Span).
552 % should be moved to error_manager ?
553 same_span_location(span_context(Span1,C),span_context(Span2,C)) :- !, same_span_location(Span1,Span2).
554 same_span_location(pos_context(Span1,C,_),pos_context(Span2,C,_)) :- !,
555 same_span_location(Span1,Span2). % should we check second span?
556 same_span_location(span_predicate(Pred1,_,_),span_predicate(Pred2,_,_)) :- !,Pred1=Pred2.
557 same_span_location(X,X).
558
559 :- dynamic saved_nested_context_state/2.
560 save_nested_context_state(_S) :-
561 bb_get(state_space_context_state,ID),
562 bb_get(state_space_context_errors,Errs),!,
563 %print(saving_context_state(_S,ID,Errs)),nl,
564 asserta(saved_nested_context_state(ID,Errs)).
565 save_nested_context_state(_).
566
567 % actually pops context state
568 clear_context_state :-
569 retract(saved_nested_context_state(ID,Errs)),!, %print(restoring_nested(ID,Errs)),nl,
570 bb_put(state_space_context_state,ID),
571 bb_put(state_space_context_errors,Errs).
572 clear_context_state :-
573 (bb_delete(state_space_context_state,_) -> true ; true).
574 %(retract(context_state(_,_)) -> true ; true). % retractall seems to be slowing down with use
575
576 % Note: Each Prolog module maintains its own blackboard for bb_get/bb_put
577 context_state(ID,Errs) :-
578 bb_get(state_space_context_state,ID), bb_get(state_space_context_errors,Errs).
579
580 % sets a new context state; pushing the previous one if necessary
581 set_context_state(State) :- %print(set_id(State)),nl,
582 save_nested_context_state(State),
583 bb_put(state_space_context_state,State),
584 bb_put(state_space_context_errors,0).
585
586 set_context_state(State,_Context) :- % Context can be used for debugging later
587 set_context_state(State).
588
589 % update current context state, without storing nested states
590 update_context_state(State) :-
591 bb_put(state_space_context_state,State),
592 bb_put(state_space_context_errors,0).
593
594 get_current_context_state(ID) :- bb_get(state_space_context_state,ID).
595 %get_current_context_state(ID) :- context_state(ID,_).
596
597 set_context_number_of_errors(Errs) :- bb_put(state_space_context_errors,Errs).
598
599 retractall_invariant_violated(State) :-
600 retractall(state_error(State,_,invariant_violated)).
601 invariant_violated(State) :-
602 state_error(State,_,invariant_violated).
603 set_invariant_violated(State) :-
604 ( invariant_violated(State) -> true
605 ; time_out_for_invariant(ID) -> print('Timeout for node: '), print(ID),nl,
606 print('Not setting invariant violation status'),nl
607 ; store_state_error(State,invariant_violated,_)
608 ).
609
610 %:- set_invariant_violated([]). % why is this ??
611
612
613 :- dynamic hash_to_id/2.
614 :- dynamic id_to_marker/2.
615 :- dynamic id_back_edge/3. % optional facts to keep track where a state was constructed from
616
617 :- dynamic hash_to_nauty_id/2. % used in nauty mode to map nauty id's to hash values
618
619 :- dynamic specialized_inv/2. /* stores whether for a node a specialized invariant
620 version could be computed */
621
622 % :- dynamic reuse_operation/4. /* when for a state and given operation name we can reuse the operation computed for another state */
623 % used to be used for OPERATION_REUSE TRUE
624
625 :- use_module(hashing).
626 state_space_startup :- % call once at startup to ensure all counters exist
627 counter_init,
628 new_counter(states), new_counter(processed_nodes), new_counter(transitions),
629 new_counter(next_state_error_id),
630 new_counter(not_interesting_nodes),
631 reset_open_ids. % also calls myheap init
632 state_space_initialise :- counter_init, reset_gennum, reset_gensym,
633 new_counter(states), new_counter(processed_nodes), new_counter(transitions),
634 new_counter(next_state_error_id), new_counter(not_interesting_nodes),
635 reset_state_counter, reset_processed_nodes_counter, reset_next_state_error_id_counter,
636 retractall_visited_expression(_),
637 reset_open_ids,
638 reset_stored_values, % state_packing
639 retractall(not_invariant_checked(_)),
640 reset_not_interesting,
641 retractall(max_reached_for_node(_)),
642 retractall(time_out_for_node(_,_,_)),
643 retractall(time_out_for_invariant(_)),
644 retractall(time_out_for_assertions(_)),
645 retractall(use_no_timeout(_)),
646 retractall(state_error(_,_,_)),
647 clear_context_state,
648 reset_transition_store,
649 retractall(operation_not_yet_covered(_)),
650 retractall(hash_to_id(_,_)),
651 retractall(id_back_edge(_,_,_)),
652 retractall(hash_to_nauty_id(_,_)),
653 retractall(id_to_marker(_,_)),
654 retractall(specialized_inv(_,_)),
655 %retractall(reuse_operation(_,_,_,_)),
656 state_space_add(root,root),
657 add_id_at_front(root),
658 my_term_hash(root,RootHash),
659 assertz(hash_to_id(RootHash,root)),
660 %assertz(not_invariant_checked(root)),
661 state_space_reset.
662
663 :- use_module(eventhandling,[register_event_listener/3]).
664 :- register_event_listener(startup_prob,state_space_startup,
665 'Initialise Statespace Counters.').
666 :- register_event_listener(reset_specification,state_space_initialise,
667 'Reset Statespace.').
668 :- register_event_listener(change_of_animation_mode,state_space_initialise,
669 'Reset Statespace.').
670 :- register_event_listener(specification_initialised,initialise_operation_not_yet_covered,
671 'Init coverage info.').
672 :- register_event_listener(reset_prob,state_space_initialise,
673 'Reset Statespace.').
674
675 /* A version of reset which checks how much memory is used by each fact */
676 /* state_space:init_with_stats */
677 state_space_initialise_with_stats :-
678 reset_gennum, reset_gensym, reset_state_counter, reset_processed_nodes_counter,
679 reset_next_state_error_id_counter,
680 retract_open_ids_with_statistics,
681 retract_with_statistics(state_space,[packed_visited_expression(_,_),
682 not_invariant_checked(_),
683 not_interesting(_),
684 max_reached_for_node(_),
685 time_out_for_node(_,_,_),
686 time_out_for_invariant(_),
687 time_out_for_assertions(_),
688 use_no_timeout(_),
689 state_error(_,_,_),
690 transition(_,_,_,_),
691 transition_info(_,_),
692 operation_not_yet_covered(_),
693 hash_to_id(_,_),
694 hash_to_nauty_id(_,_),
695 id_to_marker(_,_),
696 specialized_inv(_,_),
697 %reuse_operation(_,_,_,_),
698 history(_), forward_history(_), op_trace_ids(_)]),
699 reset_not_interesting,
700 retract_stored_values_with_statistics,
701 clear_context_state,
702 reset_transition_store,
703 state_space_add(root,root),
704 add_id_at_front(root),
705 %assertz(not_invariant_checked(root)),
706 state_space_reset,
707 initialise_operation_not_yet_covered.
708
709
710
711 :- dynamic op_trace_ids/1.
712 reset_trace :- retractall(op_trace_ids(_)), assertz(op_trace_ids([])).
713 get_action_trace(T) :- trace(T).
714 get_action_term_trace(PT) :- get_action_trace_with_limit(0,T), project_on_action_term(T,PT).
715 trace(Trace) :- get_action_trace_with_limit(500,Trace).
716 get_action_trace_with_limit(Limit,Trace) :-
717 op_trace_ids(IDT), reverse(IDT,RIDT),
718 extract_trace_from_transition_ids(RIDT,root,Limit,[],Trace).
719
720 reset_op_trace_ids :- retractall(op_trace_ids(_)), assertz(op_trace_ids([])).
721 add_to_op_trace_ids(OpID) :- retract(op_trace_ids(OpIDS)), assertz(op_trace_ids([OpID|OpIDS])).
722 remove_from_op_trace_ids(OpID) :- retract(op_trace_ids(OpIDS)),
723 (OpIDS = [R|Rest]
724 -> assertz(op_trace_ids(Rest)), OpID = R
725 ; assertz(op_trace_ids(OpIDS)), fail).
726
727 % translate a list of transition ids (from root) into a list of operation terms
728 extract_term_trace_from_transition_ids(TransIDListFromRoot,Trace) :-
729 extract_trace_from_transition_ids(TransIDListFromRoot,root,0,[],ActionTrace),
730 reverse_and_project_on_action_term(ActionTrace,[],Trace).
731
732 reverse_and_project_on_action_term([],A,A).
733 reverse_and_project_on_action_term([action(_,Term)|T],Acc,Res) :- !,
734 reverse_and_project_on_action_term(T,[Term|Acc],Res).
735 reverse_and_project_on_action_term([H|T],Acc,Res) :-
736 add_error(reverse_and_project_on_action_term,'Illegal action: ',H),
737 reverse_and_project_on_action_term(T,[H|Acc],Res).
738
739 project_on_action_term([],[]).
740 project_on_action_term([action(_,Term)|T],Res) :- !, Res=[Term|TR],
741 project_on_action_term(T,TR).
742 project_on_action_term([H|T],Res) :-
743 add_error(project_on_action_term,'Illegal action: ',H),
744 project_on_action_term(T,Res).
745
746 extract_trace_from_transition_ids([],_CurrentState,_,Trace,Trace).
747 extract_trace_from_transition_ids([TransId|Rest],CurrentState,Limit,AccTrace,Trace) :-
748 compute_op_string(TransId,CurrentState,Limit,OpTerm,OpString,DestState),!,
749 extract_trace_from_transition_ids(Rest,DestState,Limit,
750 [action(OpString,OpTerm)|AccTrace],Trace).
751 extract_trace_from_transition_ids([TransId|_],CurrentState,_,_,_Trace) :-
752 add_error(state_space,'Could not execute transition id: ', TransId:from(CurrentState)),fail.
753
754 :- use_module(translate,[translate_event_with_src_and_target_id/5]).
755 compute_op_string(jump(TO),_CurID,_,Term,String,DestID) :- !, Term=jump,String=jump,DestID=TO.
756 compute_op_string(TransId,CurID,Limit,Term,String,DestID) :- transition(CurID,Term,TransId,DestID),
757 translate_event_with_src_and_target_id(Term,CurID,DestID,Limit,String).
758
759 % reset history and forward history, but not state-space itself
760 state_space_reset :-
761 reset_trace,
762 retractall(history(_)),
763 retractall(forward_history(_)),
764 retractall(current_state_id(_)),
765 retractall(current_options(_)),
766 assertz(history([])),
767 assertz(current_state_id(root)).
768
769 reset_not_interesting :- retractall(not_interesting(_)), reset_counter(not_interesting_nodes).
770
771 mark_as_not_interesting(ID) :- assertz(not_interesting(ID)), inc_counter(not_interesting_nodes).
772
773 set_current_state_id(ID) :- (retract(current_state_id(_)) -> true ; true),
774 assertz(current_state_id(ID)).
775
776 state_space_clean_all :-
777 retractall(state_space_version_in_file(_)),
778 retractall_visited_expression(_),
779 reset_open_ids,
780 retractall(not_invariant_checked(_)),
781 reset_not_interesting,
782 retractall(max_reached_for_node(_)),
783 retractall(time_out_for_node(_,_,_)),
784 retractall(time_out_for_invariant(_)),
785 retractall(time_out_for_assertions(_)),
786 retractall(use_no_timeout(_)),
787 retractall(state_error(_,_,_)),
788 clear_context_state,
789 reset_transition_store,
790 retractall(operation_not_yet_covered(_)),
791 retractall(hash_to_id(_,_)),
792 retractall(id_back_edge(_,_,_)),
793 retractall(hash_to_nauty_id(_,_)),
794 retractall(id_to_marker(_,_)),
795 retractall(specialized_inv(_,_)),
796 %retractall(reuse_operation(_,_,_,_)),
797 retractall(history(_)),
798 retractall(forward_history(_)),
799 retractall(op_trace_ids(_)),
800 retractall(current_state_id(_)),
801 retractall(current_options(_)).
802
803 % this is only used from within the Tcl/Tk animator at the moment:
804 delete_node(ID) :- print(deleting(ID)),nl,
805 retractall_visited_expression(ID),
806 retractall_invariant_violated(ID),
807 retract_open_node_and_update_processed_nodes(ID),
808 retractall(not_invariant_checked(ID)),
809 (retract(not_interesting(ID)) -> inc_counter_by(not_interesting_nodes,-1) ; true),
810 retractall(max_reached_for_node(ID)),
811 retractall(time_out_for_node(ID,_,_)),
812 retractall(time_out_for_invariant(ID)),
813 retractall(time_out_for_assertions(ID)),
814 retractall(use_no_timeout(ID)),
815 retractall(state_error(ID,_,_)),
816 retractall(transition(ID,_,_,_)),
817 % to do: check if operation_not_yet_covered(_) changes
818 retract_hash(ID),
819 retractall(id_to_marker(ID,_)).
820
821 retract_hash(ID) :- retract(hash_to_id(Hash,ID)), retractall(hash_to_nauty_id(_TermHash,Hash)),fail.
822 retract_hash(_).
823
824 % assert max_operations reachoed
825 assert_max_reached_for_node(Id) :- %print_message(max_reached_for_node(Id)),
826 (max_reached_for_node(Id) -> true ; assertz(max_reached_for_node(Id))).
827
828 :- use_module(probsrc(debug),[debug_mode/1]).
829 assert_time_out_for_node(Id,OpName,TypeOfTimeOut) :-
830 (debug_mode(off),functor(TypeOfTimeOut,virtual_time_out,_) -> true % can easily happen when parameters are unbounded
831 ; print_message(time_out_for_node(Id,OpName,TypeOfTimeOut))),
832 (time_out_for_node(Id,OpName,_) -> true ; assertz(time_out_for_node(Id,OpName,TypeOfTimeOut))).
833 assert_time_out_for_invariant(Id) :- print_message(time_out_for_invariant(Id)),
834 (time_out_for_invariant(Id) -> true ; assertz(time_out_for_invariant(Id))).
835 assert_time_out_for_assertions(Id) :- print_message(time_out_for_assertions(Id)),
836 (time_out_for_assertions(Id) -> true ; assertz(time_out_for_assertions(Id))).
837
838 max_reached_or_timeout_for_node(Id) :-
839 (max_reached_for_node(Id) ; time_out_for_node(Id,_,_)).
840 /* ---------------------- */
841 /* state space saving */
842 /* ---------------------- */
843
844 :- dynamic state_space_version_in_file/1. %
845 state_space_version(1).
846
847 check_state_space_version :- state_space_version(V),
848 (state_space_version_in_file(F) -> true ; F=0),
849 (V>F -> add_message(state_space,'Warning: saved state_space may be incompatible with current version: ',F:V) ; true).
850
851 % save all infos of state space (transitions, evaluated invariants, ...)
852 tcltk_save_state_space(File) :-
853 print('% saving full state space to: '), print(File),nl,
854 open(File,write,Stream,[encoding(utf8)]),
855 print_state_space(Stream),
856 close(Stream),
857 print_message(done).
858
859
860 :- use_module(tools_printing, [print_dynamic_fact/2,print_dynamic_pred/4]).
861 print_state_space(Stream) :-
862 state_space_version(V),
863 print_dynamic_fact(Stream,state_space_version_in_file(V)),
864 % TO DO: maybe also save some important preferences, and warn user and/or propose to adapt preferences ?
865 print_dynamic_pred(Stream,state_space,history,1),
866 print_dynamic_pred(Stream,state_space,forward_history,1),
867 print_dynamic_pred(Stream,state_space,op_trace_ids,1),
868 print_dynamic_pred(Stream,state_space,current_state_id,1),
869 print_dynamic_pred(Stream,state_space,current_options,1),
870 print_dynamic_pred(Stream,state_space,packed_visited_expression,2),
871 print_dynamic_pred(Stream,state_space,not_invariant_checked,1),
872 print_dynamic_pred(Stream,state_space,not_interesting,1),
873 print_dynamic_pred(Stream,state_space,max_reached_for_node,1),
874 print_dynamic_pred(Stream,state_space,time_out_for_node,3),
875 print_dynamic_pred(Stream,state_space,use_no_timeout,1),
876 print_dynamic_pred(Stream,state_space,transition,4),
877 print_dynamic_pred(Stream,state_space,transition_info,2),
878 print_dynamic_pred(Stream,state_space,operation_not_yet_covered,1),
879 print_dynamic_pred(Stream,state_space,state_error,3),
880 print_state_space_open_nodes(Stream),
881 print_stored_values(Stream),
882 get_counter(states,X),
883 write_term(Stream,saved_gennum_count(X),[quoted(true)]),write(Stream,'.'),nl(Stream).
884
885 saved_gennum_count(99999).
886
887 /* ---------------------- */
888 /* state space loading */
889 /* ---------------------- */
890
891 tcltk_load_state(File) :- state_space_clean_all,
892 print('Loading: '), print(File),nl,
893 user_consult_without_redefine_warning(File), % this will read in bind_skeleton/2, ..., next_value_id/1
894 check_state_space_version,
895 print('Generating open node info'),nl,
896 transfer_open_node_info,
897 print('Transfer state packing info'),nl,
898 transfer_state_packing_info,
899 print('Recomputing hash index'),nl,
900 recompute_all_hash,
901 (saved_gennum_count(X) -> reset_state_counter(X) ; true),
902 reset_processed_nodes_counter, % TO DO: restore or save it
903 reset_next_state_error_id_counter, % DITTO
904 print('Done'),nl,!.
905 tcltk_load_state(File) :-
906 add_error(tcltk_load_state,'Could not load state from file: ',File),
907 state_space_initialise.
908
909 :- dynamic not_all_z_saved/1, not_all_transitions_added_saved/1.
910 :- dynamic bind_skeleton/2, stored_value/2, stored_value_hash_to_id/2, next_value_id/1.
911
912 % transfer facts read into state_space into other modules:
913 transfer_open_node_info :- retract(not_all_z_saved(X)), %print(not_all_z(X)),nl,
914 assert_not_all_z(X),fail.
915 transfer_open_node_info :- retract(not_all_transitions_added_saved(X)),
916 assert_not_all_transitions_added(X),fail.
917 transfer_open_node_info.
918 % now for transferring to state_packing module info generated by print_stored_values
919 transfer_state_packing_info :- retract(bind_skeleton(X,Y)), %print(skel(X)),nl,
920 assertz(state_packing:bind_skeleton(X,Y)),fail.
921 transfer_state_packing_info :- retract(stored_value(X,Y)),
922 assertz(state_packing:stored_value(X,Y)),fail.
923 transfer_state_packing_info :- retract(stored_value_hash_to_id(X,Y)),
924 assertz(state_packing:stored_value_hash_to_id(X,Y)),fail.
925 transfer_state_packing_info :- retract(next_value_id(X)),
926 state_packing:set_next_value_id(X),fail.
927 transfer_state_packing_info.
928
929 recompute_all_hash :-
930 retractall(hash_to_id(_,_)),retractall(id_to_marker(_,_)),
931 retractall(hash_to_nauty_id(_,_)),
932 visited_expression(ID,StateTemplate),
933 state_space_exploration_modes:compute_hash(StateTemplate,Hash,Marker),
934 assertz(hash_to_id(Hash,ID)),
935 assertz(id_to_marker(ID,Marker)),
936 fail.
937 recompute_all_hash.
938
939 :- use_module(hashing,[my_term_hash/2]).
940 % generates a hash for the entire state space not depending on the order in which states where added
941 compute_full_state_space_hash(Hash) :-
942 %listing(hash_to_id/2), listing(packed_visited_expression/2),
943 findall(Hash,hash_to_id(Hash,_),ListOfHashCodes),
944 sort(ListOfHashCodes,SortedList),
945 my_term_hash(SortedList,Hash).
946 % TO DO: also provide transition hashes
947
948 :- use_module(tools_meta,[safe_on_exception/3]).
949 user_consult_without_redefine_warning(File) :-
950 get_set_optional_prolog_flag(redefine_warnings, Old, off),
951 get_set_optional_prolog_flag(single_var_warnings, Old2, off),
952 (safe_on_exception(Exc,
953 %consult(File), %
954 load_files([File], [load_type(source),compilation_mode(consult),encoding(utf8)]),
955 (nl,print('Exception occurred:'),print(Exc),nl,fail))
956 -> OK=true ; OK=false),
957 get_set_optional_prolog_flag(redefine_warnings, _, Old),
958 get_set_optional_prolog_flag(single_var_warnings, _, Old2),
959 OK=true.
960
961 % execute a transition id list from current ID and update animator history,...
962 execute_id_trace_from_current(OperationIDList) :-
963 current_state_id(CurID),
964 get_state_list_from_opids(OperationIDList,CurID,StateIDList,NewCurID),
965 execute_id_trace_from_current(NewCurID,OperationIDList,StateIDList).
966
967 get_state_list_from_opids([],CurID,[],CurID).
968 get_state_list_from_opids([TransId|TO],CurID,[NextCurID|TS],FinalCurID) :-
969 transition(CurID,_,TransId,NextCurID),
970 get_state_list_from_opids(TO,NextCurID,TS,FinalCurID).
971
972
973 % execute_id_trace_from_current(NewCurID,OperationIDList,StateIDList)
974 execute_id_trace_from_current(ID,OpIDL,StateIDList) :-
975 current_state_id(CurID),
976 reverse([CurID|StateIDList],Rev),
977 Rev = [Dest|TRev],
978 (Dest==ID -> true ; print(not_eq(Dest,ID)),nl),
979 retract(history(H)),
980 update_forward_history(OpIDL), % check if OpIDL conforms to forward history and keep it
981 append(TRev,H,NewH),
982 assertz(history(NewH)),
983 retract(op_trace_ids(OldTrace)),
984 reverse(OpIDL,NewTrace),
985 append(NewTrace,OldTrace,Trace),
986 assertz(op_trace_ids(Trace)),
987 retractall(current_state_id(_)),
988 assertz(current_state_id(ID)).
989 %execute_trace_to_node(OpL,StateIDList). /* <----- BOTTLENECK FOR LONG SEQUENCES */
990 %generate_trace([],Acc,Acc).
991 %generate_trace([OpTerm|T],Acc,Res) :-
992 % translate:translate_event(OpTerm,OpString),
993 % generate_trace(T,[action(OpString,OpTerm)|Acc],Res).
994
995 update_forward_history(TransitionIds) :-
996 (retract(forward_history(Forward)),
997 prune_forward_history(TransitionIds,Forward,NewForward)
998 -> assert(forward_history(NewForward))
999 ; true).
1000 % try and prune forward history if it matches operation trace
1001 prune_forward_history([],ForwardHistory,ForwardHistory).
1002 prune_forward_history([TransID|T],[forward(_,TransID)|TF],Res) :-
1003 prune_forward_history(T,TF,Res).
1004
1005 try_set_trace_by_transition_ids(TransIds) :-
1006 (set_trace_by_transition_ids(TransIds) -> true
1007 ; add_internal_error('Call failed:',set_trace_by_transition_ids(TransIds))).
1008
1009 set_trace_by_transition_ids(TransitionIds) :-
1010 extract_history_from_transition_ids(TransitionIds,root,[],[],Last,History,OpTrace),
1011 %visited_expression(Last,LastState,LastCond),
1012 retractall(history(_)),
1013 retractall(forward_history(_)), % TODO: we could try and recover a new forward history if transition ids match current history^forward history
1014 update_forward_history(TransitionIds),
1015 retractall(current_state_id(_)),
1016 retractall(op_trace_ids(_)),
1017 assertz(history(History)),
1018 assertz(op_trace_ids(OpTrace)),
1019 assertz(current_state_id(Last)).
1020
1021
1022 extract_history_from_transition_ids([],CurrentState,History,Trace,CurrentState,History,Trace).
1023 extract_history_from_transition_ids([TransId|Rest],CurrentState,AccHist,AccTrace,Last,History,Trace) :-
1024 transition(CurrentState,_,TransId,DestState),!,
1025 extract_history_from_transition_ids(Rest,DestState,[CurrentState|AccHist],
1026 [TransId|AccTrace],Last,History,Trace).
1027 extract_history_from_transition_ids([skip|Rest],CurrentState,AccHist,AccTrace,Last,History,Trace) :- !,
1028 extract_history_from_transition_ids(Rest,CurrentState,AccHist,AccTrace,Last,History,Trace).
1029 extract_history_from_transition_ids([TransId|_],CurrentState,_,_,_,_,_Trace) :-
1030 add_error(state_space,'Could not execute transition id: ', TransId:from(CurrentState)),fail.
1031
1032 % extend trace from current state
1033 extend_trace_by_transition_ids(TransitionIds) :-
1034 current_state_id(CurID),
1035 history(OldH), op_trace_ids(OldOT),
1036 extract_history_from_transition_ids(TransitionIds,CurID,OldH,OldOT,Last,History,OpTrace),
1037 retractall(history(_)), retractall(forward_history(_)),
1038 retractall(current_state_id(_)),
1039 retractall(op_trace_ids(_)),
1040 assertz(history(History)),
1041 assertz(op_trace_ids(OpTrace)),
1042 assertz(current_state_id(Last)).
1043
1044 /* --------------------------------- */
1045 :- dynamic max_nr_of_new_nodes/1.
1046
1047 % negative number or non-number signifies no limit
1048 set_max_nr_of_new_impl_trans_nodes(MaxNrOfNewNodes) :-
1049 retractall(max_nr_of_new_nodes(_)),
1050 (number(MaxNrOfNewNodes), MaxNrOfNewNodes>=0
1051 -> assertz(max_nr_of_new_nodes(MaxNrOfNewNodes))
1052 ; true). % no need to store limit; we will explore as much as needed
1053
1054 get_max_nr_of_new_impl_trans_nodes(MaxNrOfNewNodes) :-
1055 (max_nr_of_new_nodes(Max) -> MaxNrOfNewNodes=Max; MaxNrOfNewNodes = 0).
1056
1057 % used e.g., in refinement or ltl checker
1058 impl_trans_term(From,ActionAsTerm,TransID,To) :-
1059 compute_transitions_if_necessary_saved(From),
1060 transition(From,ActionAsTerm,TransID,To).
1061 impl_trans_term(From,ActionAsTerm,To) :-
1062 compute_transitions_if_necessary_saved(From),
1063 transition(From,ActionAsTerm,_TID,To).
1064
1065 % a variation also giving the transition id:
1066 impl_trans_id(From,ActionAsTerm,TransitionID,To) :-
1067 compute_transitions_if_necessary_saved(From),
1068 transition(From,ActionAsTerm,TransitionID,To).
1069
1070 impl_trans_term_all(From,Ops) :-
1071 compute_transitions_if_necessary_saved(From),
1072 findall(op(Id,ActionAsTerm,To),
1073 transition(From,ActionAsTerm,Id,To),
1074 Ops).
1075
1076 % true if e.g., a time-out occurred during computation of all transitions
1077 impl_trans_not_complete(From) :- max_reached_or_timeout_for_node(From).
1078
1079 compute_transitions_if_necessary_saved(From) :-
1080 catch(
1081 compute_transitions_if_necessary(From),
1082 error(forced_interrupt_error('User has interrupted the current execution'),_),
1083 user_interrupts:process_interrupted_error_message).
1084
1085 :- use_module(tcltk_interface,[compute_all_transitions_if_necessary/2]).
1086 compute_transitions_if_necessary(From) :-
1087 not_all_transitions_added(From),!,
1088 decrease_max_nr_of_new_nodes(From),
1089 compute_all_transitions_if_necessary(From,false).
1090 compute_transitions_if_necessary(_From).
1091
1092 decrease_max_nr_of_new_nodes(ID) :-
1093 retract(max_nr_of_new_nodes(Max)),!,
1094 ( Max>0 ->
1095 NewMax is Max-1,
1096 assertz(max_nr_of_new_nodes(NewMax))
1097 ; Max=0 -> NM is -1,
1098 assertz(max_nr_of_new_nodes(NM)),
1099 add_warning(state_space,'Maximum number of new nodes reached for CTL/LTL/refinement check, node id = ',ID),
1100 fail
1101 ; % negative number: re-assert and fail
1102 assertz(max_nr_of_new_nodes(Max)),
1103 fail).
1104 decrease_max_nr_of_new_nodes(_). % no limit stored; just proceed
1105
1106 % will be called from TCL/TK side
1107 max_nr_of_new_nodes_limit_not_reached :-
1108 max_nr_of_new_nodes(N),N>0.
1109
1110 :- use_module(specfile,[b_or_z_mode/0, csp_mode/0, csp_with_bz_mode/0]).
1111 retract_open_node(NodeID) :- retract_open_node_and_update_processed_nodes(NodeID),
1112 (b_or_z_mode -> assertz(not_invariant_checked(NodeID)) ; true).
1113
1114 reset_processed_nodes_counter :- reset_counter(processed_nodes).
1115 %reset_processed_nodes_counter(Nr) :- set_counter(processed_nodes,Nr).
1116
1117 retract_open_node_and_update_processed_nodes(NodeID) :-
1118 retract_open_node_direct(NodeID),
1119 inc_processed.
1120
1121 inc_processed :-
1122 inc_counter(processed_nodes).
1123
1124 pop_id_from_front(ID) :- pop_id_from_front_direct(ID), inc_processed.
1125 pop_id_from_end(ID) :- pop_id_from_end_direct(ID), inc_processed.
1126 ?pop_id_oldest(ID) :- pop_id_oldest_direct(ID), inc_processed.
1127
1128
1129
1130 /* --------------------------------- */
1131
1132 % find initialised states; very similar to is_initial_state_id/1
1133 % but is used by ltl/ctl/sap
1134 % TO DO: merge these two variations of the same concept
1135
1136 :- use_module(specfile,[animation_mode/1]).
1137
1138 find_initialised_states(Init) :-
1139 animation_mode(Mode),
1140 ( init_states_mode_cst_init(Mode) ->
1141 findall(I,find_init1(root,I,_),Init)
1142 ; init_states_mode_one_step(Mode) ->
1143 next_states_from_root(Init)
1144 ;
1145 fail).
1146
1147 % find trace to some initialised state
1148 find_trace_to_initial_state(Target,Trace) :- animation_mode(Mode),
1149 find_aux(Mode,Target,Trace).
1150 find_aux(Mode,Target,[root,Target]) :-
1151 init_states_mode_one_step(Mode).
1152 find_aux(Mode,Target,[root|Trace]) :-
1153 init_states_mode_cst_init(Mode),
1154 find_init1(root,Target,Trace).
1155
1156
1157 init_states_mode_cst_init(b).
1158 init_states_mode_cst_init(z).
1159 init_states_mode_cst_init(csp_and_b).
1160
1161 init_states_mode_one_step(csp).
1162 init_states_mode_one_step(cspm).
1163 init_states_mode_one_step(xtl).
1164 %init_states_mode_one_step(promela).
1165
1166 next_states_from_root(States) :-
1167 impl_trans_term_all(root,Ops),
1168 findall(S, member(op(_Id,_,S),Ops), States).
1169
1170 find_init1(Start,Init,Trace) :- Start==Init,!,Trace=[]. % usually called with Start=Init=root
1171 find_init1(Start,Init,[State|Rest]) :-
1172 impl_trans_term(Start,O,State),
1173 find_init2(O,State,Init,Rest).
1174 find_init2(O,Init,Init,[]) :-
1175 has_functor_and_maybe_tau(O,'$initialise_machine').
1176 find_init2(O,State,Init,Path) :-
1177 has_functor_and_maybe_tau(O,'$setup_constants'),
1178 find_init1(State,Init,Path).
1179 find_init2(start_cspm_MAIN,State,Init,Path) :-
1180 find_init1(State,Init,Path).
1181 find_init2(start_cspm(_Proc),State,Init,Path) :-
1182 find_init1(State,Init,Path).
1183
1184 % has_functor_and_maybe_tau(Term,Functor)
1185 % checks if Term has the form "Functor(...)" or "tau(Functor(...))"
1186 % this is used for CSP||B specification where the initialisation is wrapped with
1187 % in a tau operator
1188 has_functor_and_maybe_tau(tau(Term),Functor) :-
1189 has_functor_and_maybe_tau(Term,Functor),!.
1190 has_functor_and_maybe_tau(Term,Functor) :-
1191 functor(Term,Functor,_).
1192
1193 % compute how far the state is from the root node using back_edge markers (if available)
1194 try_compute_depth_of_state_id(root,R) :- !, R=0.
1195 try_compute_depth_of_state_id(Node,Depth) :- id_back_edge(Node,Depth,_Back).
1196
1197 % optional registering of back_edges: to quickly find trace from root and to computed depth/diameter
1198 register_back_edge(ID,FromID) :-
1199 try_compute_depth_of_state_id(FromID,D),!,
1200 D1 is D+1,
1201 assertz(id_back_edge(ID,D1,FromID)).
1202 register_back_edge(ID,FromID) :- write(cannot_store_back_edge(ID,FromID)),nl.
1203
1204 /* --------------------------------- */
1205
1206 %
1207 % Code to compute equivalence classes
1208 % using the standard DFA minimization algorithm
1209
1210 :- dynamic equivalent/2.
1211 % state_space:compute_equivalence_classes
1212 :- public compute_equivalence_classes/0.
1213
1214 compute_equivalence_classes :- init_equi,
1215 split_equivalence_classes,nl,
1216 print_equi.
1217
1218 print_equi :- state_space:equivalent(A,B), visited_expression(A,State),
1219 visited_expression(B,StateB),
1220 nl,
1221 print(A), print(' : '), print(State),nl,
1222 print(B), print(' : '), print(StateB),nl,fail.
1223 print_equi.
1224
1225 init_equi :- retractall(equivalent(_,_)),
1226 packed_visited_expression(ID,_State),
1227 \+ not_all_transitions_added(ID),
1228 findall(Action,transition(ID,Action,_,_),List),
1229 packed_visited_expression(ID2,_S2), ID2 @> ID,
1230 \+ not_all_transitions_added(ID2),
1231 findall(Action,transition(ID2,Action,_,_),List),
1232 assertz(equivalent(ID,ID2)), % they have the same signature
1233 %print(equivalent(ID,ID2)),nl,
1234 fail.
1235 init_equi :- print(finished_initialising),nl.
1236
1237 split_equivalence_classes :- retractall(echange),
1238 equivalent(ID1,ID2),
1239 transition(ID1,A,_,Dest1),
1240 transition(ID2,A,_,Dest2),
1241 \+ check_equi(Dest1,Dest2),
1242 retract(equivalent(ID1,ID2)), % splitting class
1243 % print(diff(ID1,ID2, A, Dest1, Dest2)),nl,
1244 assert_echange,
1245 fail.
1246 split_equivalence_classes :- echange -> split_equivalence_classes ; true.
1247
1248 :- dynamic echange/0.
1249 assert_echange :- echange -> true ; assertz(echange),print('.'),flush_output.
1250
1251 check_equi(A,B) :- A=B -> true ; A @<B -> equivalent(A,B) ; equivalent(B,A).
1252
1253 /*
1254 % benchmark how much time it takes to copy the state space state_space:bench_state_space.
1255 bench_state_space :-
1256 statistics(walltime,_),
1257 (state_space:packed_visited_expression(ID,S), assertz(pve(ID,S)),fail ; true),
1258 statistics(walltime,[_,Delta]), format('Time to copy packed_visited_expression: ~w ms~n',[Delta]),
1259 (state_space:transition(A,B,C,D), assertz(tr(A,B,C,D)),fail ; true),
1260 statistics(walltime,[_,Delta2]), format('Time to copy transition: ~w ms~n',[Delta2]),
1261 (state_packing:stored_value(A,B), assertz(sv(A,B)),fail ; true),
1262 (state_packing:stored_value_hash_to_id(A,B), assertz(svhi(A,B)),fail ; true),
1263 statistics(walltime,[_,Delta3]), format('Time to copy stored_value: ~w ms~n',[Delta3]).
1264 */
1265
1266 :- public portray_state_space/0.
1267 portray_state_space :- packed_visited_expression(ID,S), functor(S,F,N),
1268 format('State ~w : ~w/~w~n',[ID,F,N]), fail.
1269 portray_state_space :- transition(ID,Action,TransID,DestID),
1270 format(' ~w: ~w -- ~w --> ~w~n',[TransID,ID,Action,DestID]),fail.
1271 portray_state_space.
1272
1273 bench_state_space :- statistics(walltime,[W1,_]),
1274 (packed_visited_expression(_,_), fail ; true),
1275 statistics(walltime,[W2,_]), T1 is W2-W1,
1276 format('Time to inspect all states: ~w ms walltime~n',[T1]),
1277 (visited_expression(_,_), fail ; true),
1278 statistics(walltime,[W3,_]), T2 is W3-W2,
1279 format('Time to inspect and unpack all states: ~w ms walltime~n',[T2]),
1280 (transition(_,_,_,_), fail ; true),
1281 statistics(walltime,[W4,_]), T3 is W4-W3,
1282 format('Time to inspect all transitions: ~w ms walltime~n',[T3]),
1283 (visited_expression(_,E), my_term_hash(E,_), fail ; true),
1284 statistics(walltime,[W5,_]), T4 is W5-W4,
1285 format('Time to inspect, unpack and hash all states: ~w ms walltime~n',[T4]).
1286
1287 % ----------------------------
1288 % COUNTER EXAMPLE MANAGEMENT
1289
1290 % store counter example nodes and transition ids; used by LTL model checking for example
1291
1292
1293 :- dynamic counterexample_node/1.
1294 :- dynamic counterexample_op/1.
1295
1296 add_counterexample_node(NodeID) :- assertz(counterexample_node(NodeID)).
1297 add_counterexample_op(TransID) :-
1298 (counterexample_op(TransID) -> true ; assertz(counterexample_op(TransID))).
1299
1300
1301 reset_counterexample :-
1302 retractall(counterexample_node(_)),
1303 retractall(counterexample_op(_)).
1304
1305 :- register_event_listener(play_counterexample,reset_counterexample,
1306 'Reset marked nodes from previous counterexamples.').
1307
1308 set_counterexample_by_transition_ids(TransIds) :-
1309 set_trace_by_transition_ids(TransIds),
1310 maplist(add_counterexample_op,TransIds),
1311 extract_history_from_transition_ids(TransIds,root,[],[],_Last,History,_OpTrace),
1312 maplist(add_counterexample_node,History).