1 % (c) 2014-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 % A model checker for safety properties.
6
7 % At this point the property intended to be checked has been recognized as a safety property.
8 % As a consequence the negation of the property is represented by a finite automaton which
9 % accepts all bad prefixes violating the safety property. The algorithm implemented in this module
10 % traverses on-the-fly the state space and simulates at the same time movement through the automaton
11 % in regard to the evaluation of the atomic propositions. The algorithm halts when either a final
12 % state of the automaton is visited (bad prefix for the property has been found and will be reported as a counter-example)
13 % or when all nodes of the state space has been visited.
14
15 % TODO: we currently ignore ltllimit in the safety model checker
16 % TODO/Warning: the atom/3 terms contain a list of formulas; this seems relevant only for ProB for Rodin
17 % and do_ltl_modelcheck in eclipse_interface; we set the list to [1] on the assumption there is only one safety
18 % formula; but this is probably wrong for more complex safety properties and will break Rodin LTL visualization
19
20 :- module(safety_mc,[start_mc_safety_property/4]).
21
22 :- use_module(probsrc(error_manager), [add_error_fail/3, add_error/3, add_warning/2, add_error_and_fail/2, add_error/2, add_warning/3]).
23 %:- use_module(probsrc(preferences),[set_preference/2, get_preference/2]).
24 :- use_module(probsrc(specfile),[animation_mode/1]).
25 :- use_module(probsrc(debug),[debug_println/2, formatsilent/2, debug_format/3]).
26 :- use_module(probsrc(tools),[cputime/1]).
27 :- use_module(probsrc(bsyntaxtree),[is_truth/1]).
28
29 %%% state space exploration modules
30 :- use_module(probsrc(state_space),[visited_expression_id/1,
31 transition/4, visited_expression/2,
32 not_all_transitions_added/1, retract_open_node/1,
33 set_context_state/1, clear_context_state/0]).
34 %% :- use_module(probsrc(state_space_open_nodes), [retract_open_node_direct/1]).
35
36 %%% ltl modules
37 %:- use_module(probsrc(state_space),[find_initialised_states/1]).
38 :- use_module(probltlsrc(ltl_safety),[aut_transition/3, aut_final_state/1, aut_initial_state/1]).
39 :- use_module(probltlsrc(state_space_explorer),[compute_transitions_opt/3,get_b_optimisation_options/2]).
40
41 %%% library modules
42 :- use_module(library(file_systems)).
43 :- use_module(library(ordsets)).
44 :- use_module(library(random)).
45 :- use_module(library(lists)).
46
47 :- use_module(probsrc(module_information),[module_info/2]).
48 :- module_info(group,ltl).
49 :- module_info(description,'This module provides predicates for automata-based model checking of safety properties.').
50
51 :- dynamic visited_pair/3, visited_pair_origin/6, visited_pair_df/2, accepting_state/3.
52
53 %% profiler modules
54 %% :- use_module('../../extensions/profiler/profiler.pl').
55 %% :- use_module('../../extensions/profiler/profiler_te.pl').
56
57 %% :- enable_profiling(add_state_at_end/3).
58 %% :- enable_profiling(pop_state_from_end/1).
59 %% :- enable_profiling(check_for_aut_transition/4).
60 %% :- enable_profiling(compute_all_product_transitions/4).
61
62 % the automata-based safety model checker
63 % Ltl is the formula whose models are counterexamples (the negated property), as for the C checker
64 :- dynamic safety_ltl_formula/1.
65 start_mc_safety_property(SearchMode,Ltl,StartNodes,Result) :-
66 prepare_mc_safety_property,
67 retractall(safety_ltl_formula(_)), assertz(safety_ltl_formula(Ltl)),
68 animation_mode(MODE),
69 get_b_optimisation_options(MODE,Optimizations),
70 %% perform_static_analyses(MODE,Optimizations),
71 initial_states_mc_safety_property(StartNodes,Optimizations,ProductInitStates),
72 (ProductInitStates == [], non_empty_automaton
73 -> %add_warning(run_mc_safety_property,'No initial states of the product transition system have been generated.'),
74 Result = none,
75 formatsilent('All relevant states visited (no initial LTL states feasible)!~n Product states analysed: 0~n',[])
76 ;
77 (\+ aut_final_state_reached(_)
78 -> add_warning(run_mc_safety_property,'No accept_all state has been found. The property is possibly not a safety property.')
79 ; true),
80 (select((State,AutState,AId),ProductInitStates,Rest), aut_final_state_reached(AutState)
81 -> Initial = [(State,AutState,AId) | Rest]
82 ; Initial = ProductInitStates
83 ),
84 %format('Running LTL Safety MC in mode ~w : ~w~n',[SearchMode,Initial]),
85 run_mc_safety_property(SearchMode,Initial,Optimizations,Result)
86 ).
87
88 run_mc_safety_property(breadth_first,ProductInitStates,Optimizations,Result) :- !,
89 cputime(T1),
90 run_mc_safety_property_bf(ProductInitStates,Optimizations,IntermediateResult),
91 cputime(T2), D is T2-T1,
92 formatsilent('% Overall safety model checking time: ~w ms~n',[D]),
93 cputime(T1_CE),
94 get_bf_search_result(IntermediateResult,Result,Length),
95 cputime(T2_CE),
96 D_CE is T2_CE-T1_CE,
97 debug_println(19,result(Result)),
98 formatsilent('% Time to get result (~w) of length ~w from breadth-first safety model checking: ~w ms~n',[IntermediateResult,Length,D_CE]).
99 run_mc_safety_property(random,ProductInitStates,Optimizations,Result) :- !,
100 cputime(T1),
101 (run_mc_safety_property_df(ProductInitStates,Optimizations,Result) ->
102 cputime(T2),
103 formatsilent('Counter-example has been found~n',[]),
104 debug_format(19,'Counter-example: ~w~n',[Result])
105 ;
106 cputime(T2),
107 get_state_space_statistics(TS,Product),
108 formatsilent('All nodes visited. No counter-example has been found.~nNumber of states checked: ~w, where number of states of the transition system is: ~w~n',[Product,TS]),
109 Result = none
110 ),
111 D is T2-T1,
112 formatsilent('% Overall safety model checking time: ~w ms~n',[D]).
113 run_mc_safety_property(Otherwise,_ProductInitStates,_,_Result) :-
114 add_error_fail(run_mc_safety_property,'Unknown search mode: ', Otherwise).
115
116 /* The algorithm for performing breadth-first search LTL safety model checking */
117 run_mc_safety_property_bf(ProductInitStates,Optimizations,Result) :-
118 maplist(create_and_add_init_state,ProductInitStates),
119 Nr = 0,
120 statistics(walltime,[W1,_]),
121 open_search(Nr,W1,Optimizations,Result).
122
123 :- use_module(probsrc(model_checker),[get_model_check_stats/6]).
124 open_search(Nr,RefWallTime,Optimizations,Result) :-
125 get_next_node(CurId,AutState,ActionId),!,
126 debug_format(5,'~nProcessing B state ~w, BA state:~w, next action:~w~n',[CurId,AutState,ActionId]),
127 (aut_final_state_reached(AutState) -> % bad prefix has been found
128 Result = counter_example_found,
129 formatsilent('Found counter example (bad prefix) leading to state [~w: BA ~w]~n',[CurId,AutState])
130 ; % continue the search
131 Nr1 is Nr +1,
132 (print_progress(Nr1,RefWallTime,NewRefTime)
133 -> DeltaW is (NewRefTime-RefWallTime)/1000,
134 get_model_check_stats(SS,TT,_,Perc,_,_),
135 formatsilent('Safety model checker progress: ~w product states processed (delta = ~1f sec)~nState space statistics: ~w states checked (~1f%), ~w transitions~n',[Nr1,DeltaW,SS,Perc,TT])
136 ; NewRefTime=RefWallTime),
137 compute_state_space_transitions_if_necessary(CurId,Optimizations,ActionsAndIDs),
138 % format(' Transitions for ~w: ~w~n',[CurId,ActionsAndIDs]),
139 (ActionsAndIDs = [], deadlock_path_is_counter_example(CurId,AutState,ActionId)
140 -> Result = counter_example_found,
141 formatsilent('Found counter example ending in deadlock state ~w [BA ~w]~n',[CurId,AutState])
142 ; compute_all_product_transitions(CurId,AutState,ActionId,ActionsAndIDs,Optimizations),
143 open_search(Nr1,NewRefTime,Optimizations,Result)
144 )
145 ).
146
147 open_search(Nr,_,_Optimizations,Res) :-
148 Res = none,
149 formatsilent('All relevant states visited!~nProduct states analysed: ~w~n',[Nr]). % these are product states
150
151
152 % A path ending in a deadlock is a maximal path, on which X(...) and [Op] are false at the end
153 % (as in the C LTL checker and ltl_verification). The Buchi automaton cannot express this
154 % (e.g., it waits for the next state for G({P} => X {Q})), so we evaluate the formula on the path.
155 :- use_module(probltlsrc(ltl_verification),[evaluate_ltl_formula/6]).
156 deadlock_path_is_counter_example(CurId,AutState,ActionId) :-
157 var(ActionId), % no transition required by the automaton (otherwise CurId is no deadlock)
158 safety_ltl_formula(Ltl),
159 get_counter_example_list(CurId,AutState,ActionId,[atom(CurId,[1],none)],Atoms),
160 maplist(atom_to_strans,Atoms,StateTransitions),
161 evaluate_ltl_formula(Ltl,StateTransitions,no_loop,ltl:check_ap,ltl:callback_tp,true),
162 asserta(accepting_state(CurId,AutState,ActionId)). % used to build the counter example
163 atom_to_strans(atom(StateId,_,TransId),strans(StateId,TransId)).
164 print_progress(Nr1,_,NewRefTime) :- Nr1 mod 10000 =:= 0, statistics(walltime,[NewRefTime,_]).
165 print_progress(Nr1,RefWallTime,NewRefTime) :- Nr1 mod 100 =:= 0, statistics(walltime,[NewRefTime,_]),
166 NewRefTime-RefWallTime > 10000. % more than 10 secs passed
167
168 compute_state_space_transitions_if_necessary(CurId,Optimizations,ActionsAndIDs) :-
169 compute_state_space_transitions_if_necessary2(CurId,Optimizations),
170 findall((ActId,ActionAsTerm,DestId),
171 transition(CurId,ActionAsTerm,ActId,DestId),
172 ActionsAndIDs).
173
174 compute_state_space_transitions_if_necessary2(CurId,Optimizations) :-
175 (not_all_transitions_added(CurId),
176 %format('Computing transitions for ~w (~w)~n',[CurId,Optimizations]),
177 (retract_open_node(CurId) -> true ; add_warning(safety_mc,'Not open node: ',CurId))
178 -> % if node is still not retracted from queue
179 % TODO: keep track of ltllimit maximum number of new nodes to explore
180 set_context_state(CurId),
181 visited_expression(CurId,CurState),
182 compute_transitions_opt(Optimizations,CurId,CurState),
183 clear_context_state
184 ; true % nothing to do
185 ).
186
187 % compute new AutomataState
188 % The formal model reaches a new state (DestId), we execute a Büchi Automaton transition
189 % based on the atomic properties of DstId
190 % ActionID is either a variable, or a transition number ensuring that AutState is mapped to CurID (for [X] properties)
191 compute_all_product_transitions(CurId,AutState,ActionId,ActionsAndIDs,Optimizations) :-
192 copy_term(ActionId,OrigActionId),
193 member((ActionId,_Term,DestId),ActionsAndIDs),
194 % format(' Processing state transition: (B:~w,BuechiAut:~w) --~w--> B:~w~n',[CurId,AutState,ActionId,DestId]),
195 compute_state_space_transitions_if_necessary2(DestId,Optimizations),
196 get_aut_successor(AutState,NextAutState),
197 % backtrack over candidate successor automata states first to perform visited_pair check,
198 % before finding all matching B transitions (NextActionId, if any)
199
200 % format(' Attempting to find compatible transitions to BA state ~w~n',[NextAutState]),
201 % if NextActionId is a variable, then any transition is ok
202 % now add all next actions to the queue which lead to NextAutState
203 check_for_aut_transition(AutState,DestId,NextAutState,NextActionId), % NextActionId is variable if no constraint on it
204 % format(' reaching: ~w * ~w (automata state via forced next action ~w)~n',[DestId,NextAutState,NextActionId]),
205
206 \+ already_visited(DestId,NextAutState,NextActionId), % pair not yet processed;
207 % format(' Asserting ~w~n',[visited_pair(DestId,NextAutState,NextActionId)]),
208 assertz(visited_pair(DestId,NextAutState,NextActionId)), % mark as processed / to-be-processed in queue
209 (aut_final_state_reached(NextAutState) ->
210 add_state_to_queue(DestId,NextAutState,NextActionId,coming_from(CurId,AutState,OrigActionId))
211 ;
212 add_state_at_queue_end(DestId,NextAutState,NextActionId,coming_from(CurId,AutState,OrigActionId)),
213 fail
214 ).
215 compute_all_product_transitions(_CurId,_AutState,_ActionId,_ActionsAndIDs,_).
216
217 already_visited(DestId,NextAutState,NextActionId) :-
218 visited_pair(DestId,NextAutState,OtherActionId),
219 (var(OtherActionId) -> true % we have already processed this product state without a constraint on the next action
220 ; var(NextActionId) -> false % we now process the product state with unrestricted next action id: could be more behaviour
221 ; NextActionId = OtherActionId).
222
223 %%% getting the initial states with respect to the the safety property
224 initial_states_mc_safety_property(StartNodes,Optimizations,ProductInitStatesSet) :-
225 findall(Init,aut_initial_state(Init),InitStates),
226 get_product_init_states(StartNodes,InitStates,Optimizations,ProductInitStates),
227 list_to_ord_set(ProductInitStates,ProductInitStatesSet),
228 debug_println(9,init_states(ProductInitStatesSet)).
229
230 get_product_init_states([],_InitStates,_Optimizations,[]).
231 get_product_init_states([StartNode|Nodes],InitStates,Optimizations,Res) :-
232 (ltl_safety:tps_occurring % for transition properties we need to have outgoing transitions
233 -> compute_state_space_transitions_if_necessary2(StartNode,Optimizations)
234 ; true),
235 get_init_product(StartNode,InitStates,IStates),
236 append(IStates,Res1,Res),
237 get_product_init_states(Nodes,InitStates,Optimizations,Res1).
238
239 get_init_product(_StartNode,[],[]).
240 get_init_product(StartNode,[Init|Inits],Res) :-
241 findall(Succ,get_ba_successor_state(StartNode,Init,Succ),L),
242 append(L,Res1,Res),
243 get_init_product(StartNode,Inits,Res1).
244
245 % after reaching an initial state we execute one transition in the Büchi Automata
246 get_ba_successor_state(StartNode,Init,(StartNode,Next,ActionId)) :-
247 aut_transition(Init,Label,Next), % Büchi Automata transition via Label
248 check_transition(Label,StartNode,ActionId). % ActionId: id required to satisfy Label (if it has a transition property)
249
250 %---------------------------------------------------------------------
251
252 %%% utility predicates
253 prepare_mc_safety_property :-
254 retractall(product(_,_,_)),
255 retractall(visited_pair(_,_,_)),
256 retractall(visited_pair_origin(_,_,_, _,_,_)),
257 retractall(visited_pair_df(_,_)),
258 retractall(accepting_state(_,_,_)).
259
260 % automata relevant functions
261 aut_final_state_reached(AutState) :- aut_final_state(AutState),
262 (AutState = 'accept_all' -> true ; aut_transition(AutState,true,AutState)).
263
264 % Checking whether the automaton expresses the trivial property 'true'.
265 % If so, a counter-example [inital_state] is returned
266 trivial_automaton :-
267 aut_transition(P,L,Q),
268 (P=='accept_init',(L = ap(bpred(Pred))->is_truth(Pred);fail),Q =='accept_init').
269
270 non_empty_automaton :-
271 aut_transition(_P,_L,_Q).
272
273 % state space statistics
274 get_state_space_statistics(TS,Product) :-
275 findall((X,Y),visited_pair_df(X,Y),L),
276 debug_println(9,product_states(L)),
277 length(L,Product),
278 findall(ID,visited_expression_id(ID),IDL),
279 length(IDL,TS).
280 %-------------------------------------------
281
282 %%% Queue operations
283 :- dynamic product/3.
284
285 create_and_add_init_state((CurId,AutState,ActionId)) :-
286 debug_format(5,'Creating initial state [~w:BA ~w] -- ~w -->~n',[CurId,AutState,ActionId]),
287 (already_visited(CurId,AutState,ActionId)
288 -> true % e.g., (S,Q,_) and (S,Q,ActId) for a label with or: the first one covers the second
289 ; assertz(visited_pair(CurId,AutState,ActionId)),
290 add_state_to_queue(CurId,AutState,ActionId,coming_from(root,root,none))
291 ).
292
293 get_next_node(CurId,AutState,ActionId) :-
294 pop_state_from_end(product(CurId,AutState,ActionId)).
295
296 add_state_to_queue(State,AutState,ActionId,From) :-
297 (aut_final_state_reached(AutState) ->
298 add_state_at_queue_front(State,AutState,ActionId,From),
299 debug_format(19,'** Found Accepting state ~w~n',[(State,AutState,ActionId)]),
300 assertz(accepting_state(State,AutState,ActionId)) % save accepting state (for building later the counter-example)
301 ; add_state_at_queue_end(State,AutState,ActionId,From)
302 ).
303 add_state_at_queue_end(State,AutState,ActionId,coming_from(PrevState,PrevAutState,PrevActionId)) :-
304 % format('QUEUING: (~w,~w) --~w--> ~n',[State,AutState,ActionId]),
305 assertz(product(State,AutState,ActionId)),
306 assertz(visited_pair_origin(State,AutState,ActionId,PrevState,PrevAutState,PrevActionId)).
307 add_state_at_queue_front(State,AutState,ActionId,coming_from(PrevState,PrevAutState,PrevActionId)) :-
308 % format('QUEUING at FRONT: (~w,~w) --~w--> ~n',[State,AutState,ActionId]),
309 asserta(product(State,AutState,ActionId)),
310 assertz(visited_pair_origin(State,AutState,ActionId,PrevState,PrevAutState,PrevActionId)).
311
312 %add_state_at_front(State,AutState,ActionId) :-
313 % asserta(product(State,AutState,ActionId)).
314 pop_state_from_end(Node) :-
315 retract(product(State,AutState,ActionId)),!,
316 Node = product(State,AutState,ActionId).
317 %-------------------
318
319 /* An algorithm for checking safety LTL properties in depth-first manner.
320 First tryouts showed that the depth-first search is much more effective than the breadth-first mode. */
321
322 run_mc_safety_property_df(InitStates,Optimizations,Result) :-
323 member((CurId,AutState,ActionId),InitStates),
324 (trivial_automaton -> % the automaton is trivial , i.e. we have checked the formula 'false' and need to return one of the initial states as a counter-example
325 %% CEPath = [atom(CurId,CurId,none)],
326 Result=model([atom(CurId,[1],none)],no_loop)
327 ;
328 mc_safety_property_init((CurId,AutState,ActionId),Optimizations,CEPath,CEPath,Result)
329 ).
330
331 % at the init state we already checked for next automaton transitions
332 mc_safety_property_init((CurId,AutState,ActionId),Optimizations,History,HTail,Result) :-
333 (transition(CurId,_Op,ActionId,CurId) -> true ; assertz(visited_pair_df(CurId,AutState))),
334 mc_safety_property_next_state((CurId,AutState,ActionId),Optimizations,History,HTail,Result).
335
336 % model_checker: get_open_node_to_check(DFMODE,NodeID)
337 mc_safety_property((CurId,AutState,ActionId),Optimizations,History,HTail,Result) :-
338 assertz(visited_pair_df(CurId,AutState)),
339 debug_println(19,current_history(CurId,History)),
340 check_for_aut_transition(AutState,CurId,NextAutState,ActionId),
341 mc_safety_property_next_state((CurId,NextAutState,ActionId),Optimizations,History,HTail,Result).
342
343 mc_safety_property_next_state((CurId,AutState,ActionId),_Optimizations,History,[atom(CurId,[1],ActionId1)],Result) :-
344 aut_final_state_reached(AutState),!,
345 ( nonvar(ActionId) -> ActionId1=ActionId; ActionId1 = none),
346 debug_format(19,'Counter example found in state ~w: ~w~n',[CurId,History]),
347 Result = model(History,no_loop).
348 mc_safety_property_next_state((CurId,AutState,ActionId),Optimizations,History,HTail,Result) :-
349 compute_state_space_transitions_if_necessary(CurId,Optimizations,ActionsAndIDs),
350 random_permutation(ActionsAndIDs,ActionsAndIDs1),
351 HTail = [atom(CurId,[1],ActionId)|HTail2],
352 get_next_state(ActionsAndIDs1,ActionId,DestId),
353 \+ visited_pair_df(DestId,AutState),
354 (ltl_safety:tps_occurring % if there is a transition property, we need outgoing transitions
355 -> compute_state_space_transitions_if_necessary2(DestId,Optimizations)
356 ; true),
357 mc_safety_property((DestId,AutState,_NewActionId),Optimizations,History,HTail2,Result).
358
359 % try and get accepting state first
360 get_aut_successor(AutState,accept_all) :- (aut_transition(AutState,_,accept_all) -> true).
361 get_aut_successor(AutState,NextAutState) :- % TODO: randomise and succeed only once if multiple transitions lead
362 aut_transition(AutState,true,NextAutState),
363 %(the idea is to execute as soon as possible transitions that do not have the label 'true')
364 NextAutState \= accept_all.
365 get_aut_successor(AutState,NextAutState) :- % TODO: randomise and succeed only once if multiple transitions lead
366 aut_transition(AutState,Label,NextAutState), Label \= true, NextAutState \= accept_all.
367
368 check_for_aut_transition(AutState,CurId,NextAutState,ActionId) :-
369 aut_transition(AutState,Label,NextAutState),
370 check_transition(Label,CurId,ActionId). % check if label matches transition ActionId from B State CurId
371
372 /*
373 check_for_aut_transition_random(AutState,CurId,NextAutState,ActionId) :-
374 get_randomised_next_aut_states(AutState,Trans),
375 member((Label,NextAutState),Trans),
376 %format('Checking transition ~w from state ~w with label ~w (~w->~w)~n',[ActionId,CurId,Label,AutState,NextAutState]),
377 check_transition(Label,CurId,ActionId).
378
379 get_randomised_next_aut_states(AutState,Res) :-
380 % here we want to test first the non-truth automata transitions in order to terminate earlier in case of a counter-example
381 % this is a type of selective randomising (the idea is to execute as soon as possible transitions that do not have the label 'true')
382 findall((Labl,Next),(aut_transition(AutState,Labl,Next),\+is_truth_label(Labl)),L),
383 random_permutation(L,L1),
384 findall((Labl,Next),(aut_transition(AutState,Labl,Next),is_truth_label(Labl)),LTruth),
385 append(L1,LTruth,Res).
386 % TODO: optimize this; not very efficient
387
388 is_truth_label(true).
389 */
390
391 get_next_state(ActionsAndIDs,ActionId,DstId) :-
392 member((ActionId,_Term,DstId),ActionsAndIDs).
393
394 %%% for getting the result from LTL Safety Model Check
395 get_bf_search_result(none,Result,Len) :- !, Result = none, Len=0.
396 get_bf_search_result(counter_example_found,Result,Length) :- !,
397 get_counter_example_from_product(Result,Length),
398 debug_println(9,counter_example(Length,Result)).
399 get_bf_search_result(Arg,_Result,_Len) :-
400 add_error_fail(run_mc_safety_property_bf, 'Unknow result: ', Arg).
401
402 get_counter_example_from_product(Result,Length) :-
403 accepting_state(State,AutState,ActionId),!,
404 (nonvar(ActionId) -> Trans = ActionId; Trans = none),
405 debug_format(19,'Start constructing counter-example backwards from ~w:~w~n',[State,AutState]),
406 %listing(accepting_state/3),nl, listing(visited_pair/3),nl, listing(visited_pair_origin/6),nl,
407 get_counter_example_list(State,AutState,ActionId,[atom(State,[1],Trans)],ResList),
408 Result = model(ResList,no_loop),
409 length(ResList,Length).
410 get_counter_example_from_product(Result,0) :-
411 add_error(safety_mc,'No accepting state was found (this is an internal error in the model checker).'),
412 Result=[].
413
414 get_counter_example_list(State,AutState,ActionId,CurRes,Result) :-
415 visited_pair_origin(State,AutState,StoredActionId,root,root,none),
416 (ActionId==StoredActionId -> true ; var(StoredActionId), var(ActionId)),
417 !, % we found our way back to an initial state
418 Result = CurRes.
419 get_counter_example_list(State,AutState,ActionId,CurRes,Result) :-
420 (%visited_pair(State,AutState,PrevState,PrevAutState,StoredActionId), % Note there could be several entries
421 visited_pair_origin(State,AutState,StoredActionId,PrevState,PrevAutState,PrevActionId),
422 (ActionId==StoredActionId -> true ; var(StoredActionId), var(ActionId)),
423 debug_format(5,' [~w:BA ~w] ---counter_ex_trans:~w---> previous:[~w:BA ~w ~w]~n',[State,AutState,ActionId,PrevState,PrevAutState,PrevActionId]),
424 copy_term(PrevActionId,GroundPrevActionId),
425 transition(PrevState,_ActionAsTerm,GroundPrevActionId,State), % has only one solution if ActionId ground
426 aut_transition(PrevAutState,_,AutState)
427 -> get_counter_example_list(PrevState,PrevAutState,PrevActionId,
428 [atom(PrevState,[1],GroundPrevActionId)|CurRes], Result)
429 ; add_error(get_counter_example_list,'Could not find visited predecessor for: ',(State:AutState)),
430 Result=CurRes
431 ).
432 %get_counter_example_list(State,AutState,CurRes,Result) :-
433 % add_error(get_counter_example_list,'No visited_pair found for: ',(State:AutState)),
434 % listing(safety_mc:visited_pair/4),
435 % Result=CurRes.
436
437
438 %------------------------------------------------------------------------------------------------------------------
439
440
441 %:- use_module(ltl_propositions,[check_enabled/2]).
442 :- use_module(probltlsrc(ltl_propositions), [check_transition_pred/5, check_ap/2]).
443 %%% synchronising the automaton transitions with the successor states of the model
444 %%% for transition predicates (like [Event]) the ActionId will be instantiated, and the BA property relies on the Action to be taken next
445 %%% if the ActionId remains a variable, then the property only depends on the state (like, {Pred} )
446 check_transition(true,_StateId,_ActionId) :- !.
447 check_transition(false,_StateId,_ActionId) :- !, fail.
448 check_transition(ap(AP),StateId,_ActionId) :- !,
449 check_ap_at_state(AP,StateId).
450 check_transition(tp(TP),StateId,ActionId) :- !,
451 %check_enabled(TP,StateId), % not sure why this is required: it checks there is one solution; the code below will do that as well
452 transition(StateId,Op,ActionId,DestId),
453 check_transition_pred(TP,Op,StateId,ActionId,DestId). % ltl_propositions
454 check_transition(not(AF),StateId,ActionId) :- !,
455 check_transition_fail(AF,StateId,ActionId).
456 check_transition(and(AF,AG),StateId,ActionId) :- !,
457 check_transition(AF,StateId,ActionId),
458 check_transition(AG,StateId,ActionId).
459 check_transition(or(AF,AG),StateId,ActionId) :- !,
460 (check_transition(AF,StateId,ActionId) ;
461 check_transition(AG,StateId,ActionId)).
462 check_transition(Label,_StateId,_ActionId) :-
463 add_error_fail(check_transition,'Unknown Buchi automaton transition label: ', Label).
464
465 check_transition_fail(true,_StateId,_ActionId) :- !, fail.
466 check_transition_fail(false,_StateId,_ActionId) :- !.
467 check_transition_fail(ap(AP),StateId,_ActionId) :- !,
468 (check_ap_at_state(AP,StateId)-> fail; true).
469 check_transition_fail(tp(TP),StateId,ActionId) :- !,
470 (transition(StateId,_,_,_)
471 -> transition(StateId,Op,ActionId,DestId), % we require DestId to be known
472 \+ ltl_propositions:check_transition_pred(TP,Op,StateId,ActionId,DestId)
473 ; true). % in a deadlock state, [TP] is false (and ActionId stays unconstrained)
474 check_transition_fail(not(AF),StateId,ActionId) :- !,
475 check_transition(AF,StateId,ActionId).
476 check_transition_fail(and(AF,AG),StateId,ActionId) :- !,
477 (check_transition_fail(AF,StateId,ActionId) ;
478 check_transition_fail(AG,StateId,ActionId) ).
479 check_transition_fail(or(AF,AG),StateId,ActionId) :- !,
480 check_transition_fail(AF,StateId,ActionId),
481 check_transition_fail(AG,StateId,ActionId).
482 check_transition_fail(Label,_StateId,_ActionId) :-
483 add_error_fail(check_transition,'Unknown Buchi automaton transition label: ', Label).
484
485 check_ap_at_state(deadlock,StateId) :- !,
486 animation_mode(MODE),
487 get_b_optimisation_options(MODE,Optimizations),
488 compute_state_space_transitions_if_necessary2(StateId,Optimizations),
489 \+ transition(StateId,_ActionId,_ActionAsTerm,_DestId).
490 check_ap_at_state(AP,StateId) :-
491 check_ap(AP,StateId).
492