| 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(ctl,[ctl_model_check/4, ctl_model_check_with_ce/6, ctl_model_check_with_ast/6, tcltk_play_ctl_counterexample/2]). |
| 6 | | |
| 7 | | /* SICSTUS Libraries */ |
| 8 | | :- use_module(library(avl)). |
| 9 | | |
| 10 | | /* ProB Libraries */ |
| 11 | | :- use_module(probsrc(error_manager)). |
| 12 | | :- use_module(probsrc(state_space), [set_max_nr_of_new_impl_trans_nodes/1, |
| 13 | | current_state_id/1, not_all_transitions_added/1, |
| 14 | | transition/4]). |
| 15 | | :- use_module(probsrc(tools),[print_size_of_table/1]). |
| 16 | | :- use_module(probsrc(tools_printing), [print_error/1]). |
| 17 | | :- use_module(probsrc(debug)). |
| 18 | | |
| 19 | | :- use_module(probltlsrc(ltl)). |
| 20 | | :- use_module(probltlsrc(ltl_propositions), [trans/4, check_transition_pred/5, check_ap/2]). |
| 21 | | |
| 22 | | :- use_module(probsrc(module_information),[module_info/2]). |
| 23 | | :- module_info(group,ltl). |
| 24 | | :- module_info(description,'This module provides CTL model checking on models.'). |
| 25 | | |
| 26 | | :- meta_predicate printtime(0). |
| 27 | | |
| 28 | | /* CTL Model Checker */ |
| 29 | | |
| 30 | | /* sample formula: ef(e(b(stop_Moat/0))) */ |
| 31 | | |
| 32 | | sat(true,_E,T,R) :- !, T=[], R=true. |
| 33 | | sat(false,_E,T,R) :- !, T=[], R=false. |
| 34 | | sat(not(F),E,T,Res) :- !, sat(F,E,T,Res1), negate_result(Res1,Res). |
| 35 | ? | sat(ap(P),E,[],Res) :- !, (check_ap(P,E) -> Res=true ; Res=false). /* elementary atomic proposition */ |
| 36 | | sat(action(OpSpec),E,T,Res) :- !, /* transition proposition */ |
| 37 | | (trans(E,E2,Act,TransId), |
| 38 | ? | check_transition_pred(OpSpec,Act,E,TransId,E2) |
| 39 | | -> Res=true, T=[E2] |
| 40 | | ; T=[], (successors_incomplete(E) -> Res=incomplete ; Res=false)). |
| 41 | | % three-valued: false/true dominate, otherwise incomplete if one argument is incomplete |
| 42 | | sat(and(F,G),E,T,Res) :- !, |
| 43 | | sat(F,E,T1,Res1), |
| 44 | | (Res1=false -> Res=false,T=T1 |
| 45 | | ; sat(G,E,T2,Res2), |
| 46 | | (Res2=false -> Res=false, T=T2 |
| 47 | | ; Res1=incomplete -> Res=incomplete, T=[] |
| 48 | | ; Res=Res2, T=T2) |
| 49 | | ). |
| 50 | | sat(or(F,G),E,T,Res) :- !, |
| 51 | | sat(F,E,T1,Res1), |
| 52 | | (Res1=true -> Res=true,T=T1 |
| 53 | | ; sat(G,E,T2,Res2), |
| 54 | | (Res2=true -> Res=true, T=T2 |
| 55 | | ; Res1=incomplete -> Res=incomplete, T=[] |
| 56 | | ; Res=Res2, T=T2) |
| 57 | | ). |
| 58 | | sat(implies(A,B),E,T,Res) :- !, sat( or(not(A),B),E,T,Res). |
| 59 | | sat(equivalent(A,B),E,T,Res) :- !, sat( and(implies(A,B),implies(B,A)),E,T,Res). |
| 60 | | sat(an(F),E,T,Res) :- !, sat(not(en(not(F))),E,T,Res). |
| 61 | | sat(en(F),E,T,Res) :- !, /* exists next */ |
| 62 | ? | (trans(E,E2,_,_), |
| 63 | | sat(F,E2,T2,true) |
| 64 | | -> Res=true, T=[E2|T2] |
| 65 | | ; T=[], |
| 66 | | (successors_incomplete(E) -> Res=incomplete |
| 67 | ? | ; trans(E,E2,_,_), sat(F,E2,_,incomplete) -> Res=incomplete |
| 68 | | ; Res=false)). |
| 69 | | sat(action(OpSpec,F),E,T,Res) :- !, |
| 70 | | /* exists next with predicate on transition; ena renamed to action by preprocess_formula2 */ |
| 71 | ? | (trans(E,E2,Act,TransId), |
| 72 | | check_transition_pred(OpSpec,Act,E,TransId,E2),sat(F,E2,T2,true) |
| 73 | | -> Res=true, T=[E2|T2] |
| 74 | | ; T=[], |
| 75 | | (successors_incomplete(E) -> Res=incomplete |
| 76 | ? | ; trans(E,E2,Act,TransId), check_transition_pred(OpSpec,Act,E,TransId,E2), |
| 77 | | sat(F,E2,_,incomplete) -> Res=incomplete |
| 78 | | ; Res=false)). |
| 79 | | sat(eu(F,G),E,T,Res) :- !, /* exists until */ |
| 80 | | sat_eu(E,F,G,T,Res). |
| 81 | | sat(ef(F),E,T,Res) :- !, /* exists future */ |
| 82 | | sat(eu(true,F),E,T,Res). |
| 83 | | sat(ag(F),E,T,Res) :- !, sat(not(ef(not(F))),E,T,Res). |
| 84 | | sat(af(F),E,T,Res) :- !, sat(not(eg(not(F))),E,T,Res). |
| 85 | | sat(eg(F),E,T,Res) :- !, sat_eg(E,F,T,Res). |
| 86 | | sat(X,State,T,R) :- add_internal_error('Uncovered CTL Formula: ',sat(X,State,T,R)),fail. |
| 87 | | |
| 88 | | negate_result(true,false). |
| 89 | | negate_result(false,true). |
| 90 | | negate_result(incomplete,incomplete). |
| 91 | | |
| 92 | | :- dynamic sat_eg_table/3. |
| 93 | | % TO DO: also use Ids generated by formula_to_id to store in sat_eg_table |
| 94 | | % TO DO: try and use mutarry or mutdict from SICStus 4.7 |
| 95 | | sat_eg(E,F,[],R) :- |
| 96 | | sat_eg_table(E,F,Res),!, |
| 97 | | R=Res. % loop found or already computed |
| 98 | | % While E is on the current DFS path its entry holds the value of F in E (true or incomplete): |
| 99 | | % reaching E again closes a cycle on which F holds (or may hold). |
| 100 | | sat_eg(E,F,T,R) :- |
| 101 | | sat(F,E,_,FRes), |
| 102 | | (FRes = false |
| 103 | | -> assertz(sat_eg_table(E,F,false)), T=[], R=false |
| 104 | | ; assertz(sat_eg_table(E,F,FRes)), |
| 105 | ? | (trans(E,E2,_Act,_), sat_eg(E2,F,T2,R2), R2==true |
| 106 | | -> T1=[E2|T2], R1=FRes |
| 107 | | ; T1=[], |
| 108 | | (successors_incomplete(E) -> R1=incomplete |
| 109 | ? | ; trans(E,E2,_,_), sat_eg(E2,F,_,R3), R3==incomplete -> R1=incomplete % only table lookups now |
| 110 | | ; R1=false) % no successor starts an infinite F-path |
| 111 | | ), |
| 112 | | retract(sat_eg_table(E,F,FRes)), assertz(sat_eg_table(E,F,R1)), |
| 113 | | T=T1, R=R1 % R may be bound by the caller |
| 114 | | ). |
| 115 | | |
| 116 | | % the transitions of E have not been computed (e.g., because of -ltllimit) |
| 117 | ? | successors_incomplete(E) :- \+ trans(E,_,_,_), not_all_transitions_added(E). |
| 118 | | |
| 119 | | reset_ctl :- |
| 120 | | retractall(sat_eg_table(_,_,_)), |
| 121 | | retractall(sat_eu_table(_,_,_,_,_)), |
| 122 | | retractall(formula_registry(_,_)), |
| 123 | | retractall(next_id(_)), |
| 124 | | assertz(next_id(0)). |
| 125 | | |
| 126 | | % to minimize storage used for sat_eu_table and speedup lookup: we store formulas/subformulas with ids |
| 127 | | % TO DO: sat_eu_table : store formula ids using term_hash |
| 128 | | :- dynamic formula_registry/2, next_id/1. |
| 129 | | next_id(0). |
| 130 | | |
| 131 | | formula_to_id(Formula,Id) :- formula_registry(Formula,R),!,R=Id. |
| 132 | | formula_to_id(Formula,Id) :- retract(next_id(R)), |
| 133 | | R1 is R+1, assertz(next_id(R1)), |
| 134 | | assertz(formula_registry(Formula,R)), |
| 135 | | Id = R. |
| 136 | | |
| 137 | | |
| 138 | | :- dynamic sat_eu_table/5. |
| 139 | | sat_eu(E,F,G,T,R) :- formula_to_id(F,Fid), formula_to_id(G,Gid), |
| 140 | | sat_eu1(E,F,Fid,G,Gid,T,R). |
| 141 | | sat_eu1(E,F,Fid,G,Gid,T,R) :- |
| 142 | | empty_avl(Computing), %print(start_eu(E,F,G,T,R)),nl, |
| 143 | | sat_eu2(E,F,Fid,G,Gid,Computing,OutComputing,T,Res), % print(res(Res)),nl, |
| 144 | | ( Res = computing -> |
| 145 | | % we found only false and computing (i.e. cycles) nodes, |
| 146 | | % there is no true node to encounter |
| 147 | | assertz(sat_eu_table(E,Fid,Gid,[],false)), |
| 148 | | % we know that all visited nodes must be false, too |
| 149 | | mark_all_computing_nodes_as_false(Fid,Gid,OutComputing), |
| 150 | | R=false |
| 151 | | ; |
| 152 | | R = Res). |
| 153 | | % sat_eu2/9 is used recursively to find a reachable node where G is true |
| 154 | | % A set of elements that have already been checked is passed (InComputing and |
| 155 | | % OutComputing) |
| 156 | | sat_eu2(E,_F,_,_G,_,InComputing,OutComputing,T,R) :- |
| 157 | | % the node has already been checked, Result is "computing" |
| 158 | | avl_fetch(E,InComputing),!,R=computing,T=[],OutComputing = InComputing. |
| 159 | | sat_eu2(E,_F,Fid,_G,Gid,InComputing,OutComputing,T,R) :- |
| 160 | | % the value for this node has already been computed |
| 161 | ? | sat_eu_table(E,Fid,Gid,TT,Res),!, %print(sat_eu_table(E,F,G,TT,Res)),nl, |
| 162 | | R=Res,T=TT,OutComputing=InComputing. |
| 163 | | sat_eu2(E,_F,Fid,G,Gid,InComputing,OutComputing,T,Res) :- % print(sat_eu_eval(E,F,G)),nl, |
| 164 | | % the goal is true in this node |
| 165 | | Res=true, |
| 166 | | /* exists until */ |
| 167 | | sat(G,E,T,true),!, |
| 168 | | % print(set_eu_g_true(G,T)),nl, |
| 169 | | assertz(sat_eu_table(E,Fid,Gid,T,true)), |
| 170 | | %% assertz(sat_eu_table(E,Fid,Gid,[],true)), |
| 171 | | InComputing = OutComputing. |
| 172 | | sat_eu2(E,F,Fid,G,Gid,InComputing,OutComputing,T,Res) :- /* exists until */ |
| 173 | | % the goal is not true |
| 174 | | % sat(not(G),E,_), /* added to avoid extra answers */ |
| 175 | | sat(F,E,T1,Res1), |
| 176 | | % print(sat_eu_loop(E,F,T1,Res1)),nl, |
| 177 | | ( |
| 178 | | Res1=true -> |
| 179 | | avl_store(E,InComputing,true,Computing), |
| 180 | | sat_eu_step(E,F,Fid,G,Gid,Computing,OutComputing,T2,Res2), |
| 181 | | ( Res2 = true -> |
| 182 | | % we found a node with the goal, mark this as true |
| 183 | | assertz(sat_eu_table(E,Fid,Gid,T2,true)), %% < ------------- T2 |
| 184 | | Res=true, T=T2 |
| 185 | | ; Res2 = computing -> |
| 186 | | Res=computing, T=[] |
| 187 | | ; |
| 188 | | % we found only false or incomplete nodes, mark this as false / incomplete |
| 189 | | assertz(sat_eu_table(E,Fid,Gid,[],Res2)), |
| 190 | | Res=Res2, T=[] |
| 191 | | ) |
| 192 | | ; /* Res1 = false or incomplete */ |
| 193 | | % if F is false the result is false anyhow - we do not have to look into the future |
| 194 | | assertz(sat_eu_table(E,Fid,Gid,[],Res1)), |
| 195 | | Res=Res1, T=T1, OutComputing=InComputing |
| 196 | | ). |
| 197 | | sat_eu_step(E,F,Fid,G,Gid,InComputing,OutComputing,T,R) :- |
| 198 | | findall(N,trans(E,N,_,_),Nexts), % Nexts can be [] when max_nr_of_new_nodes reached; TO DO: catch error occuring in decrease_max_nr_of_new_nodes and set Result to incomplete |
| 199 | | (Nexts = [], not_all_transitions_added(E) -> InResult=incomplete ; InResult = false), |
| 200 | | sat_eu_step2(Nexts,F,Fid,G,Gid,InComputing,OutComputing,T,InResult,R). |
| 201 | | sat_eu_step2([],_F,_,_G,_,InComputing,OutComputing,T,In,Out) :- !,In=Out,T=[],InComputing=OutComputing. |
| 202 | | sat_eu_step2([Next|Nrest],F,Fid,G,Gid,InComputing,OutComputing,T,In,R) :- |
| 203 | | sat_eu2(Next,F,Fid,G,Gid,InComputing,Computing,_T2,Res), |
| 204 | | ( Res=true -> R=true, T=eu(Fid,Gid,Next), % --> we now store link to next node and not full trace: T=[Next|T2], |
| 205 | | OutComputing=Computing |
| 206 | | ; |
| 207 | | combine_result(In,Res,New), |
| 208 | | sat_eu_step2(Nrest,F,Fid,G,Gid,Computing,OutComputing,T,New,R)). |
| 209 | | combine_result(false,false,R) :- !,R=false. |
| 210 | | combine_result(incomplete,_,R) :- !,R=incomplete. |
| 211 | | combine_result(_,incomplete,R) :- !,R=incomplete. |
| 212 | | combine_result(_,_,computing). |
| 213 | | |
| 214 | | expand_trace([],R) :- !, R=[]. |
| 215 | | expand_trace([H|T],[H|ET]) :- !, expand_trace(T,ET). |
| 216 | | expand_trace(eu(Fid,Gid,Next),[Next|ET]) :- !, |
| 217 | | sat_eu_table(Next,Fid,Gid,T,_), |
| 218 | | expand_trace(T,ET). |
| 219 | | expand_trace(T,R) :- add_internal_error('Unknown trace: ',expand_trace(T,R)), T=R. |
| 220 | | |
| 221 | | mark_all_computing_nodes_as_false(Fid,Gid,Computing) :- |
| 222 | ? | avl_member(E,Computing), |
| 223 | | assertz(sat_eu_table(E,Fid,Gid,[],false)), |
| 224 | | fail. |
| 225 | | mark_all_computing_nodes_as_false(_F,_G,_Computing). |
| 226 | | |
| 227 | | |
| 228 | | % check CTL formula for all initial States (or for a single given start state) |
| 229 | | % (Note: a model/transition system satisfies a CTL formula iff the formula holds in all initial states.) |
| 230 | | ctl_sat_for_all(Formula,States,Trace,Res) :- |
| 231 | | length(States,Len), |
| 232 | | (Len>1 -> formatsilent('Checking CTL formula for ~w initial states~n',[Len]) ; true), |
| 233 | | % one can also use Mode=specific_node(NodeID), but then Len=1 |
| 234 | | ctl_sat_for_all2(States,Formula,[],unknown,Trace1,Res1), |
| 235 | | (Res1 = unknown -> Res=true, Trace=[] ; Res=Res1, Trace=Trace1). |
| 236 | | ctl_sat_for_all2([],_Formula,Trace,Res,Trace,Res). |
| 237 | | ctl_sat_for_all2([State|Srest],Formula,PrevTrace,PrevRes,Trace,Res) :- |
| 238 | | sat(Formula,State,Trace1,Res1), |
| 239 | | debug_format(19,'CTL result for state ~w: ~w~n',[State,Res1]), |
| 240 | | NewTrace = [State|Trace1], |
| 241 | | ( Res1=false -> |
| 242 | | % Counter-example found, use it and stop |
| 243 | | Res=false, Trace=NewTrace |
| 244 | | ; Res1=incomplete -> |
| 245 | | Res=incomplete, Trace=NewTrace |
| 246 | | ; PrevRes=unknown -> |
| 247 | | % First witness found, store and continue |
| 248 | | ctl_sat_for_all2(Srest,Formula,NewTrace,true,Trace,Res) |
| 249 | | ; |
| 250 | | % Another witness found, ignore it and use the old |
| 251 | | ctl_sat_for_all2(Srest,Formula,PrevTrace,true,Trace,Res)). |
| 252 | | |
| 253 | | |
| 254 | | :- use_module(probsrc(tools),[retract_with_statistics/2]). |
| 255 | | ctl_model_check2(_Formula,_Mode,[],Res) :- !, |
| 256 | | Res = nostart. % no initial state found, as for LTL |
| 257 | | ctl_model_check2(Formula,Mode,InitStates,Res) :- |
| 258 | | reset_ctl, |
| 259 | | debug_println(4,ctl_preprocessing), |
| 260 | | ltl: preprocess_formula(Formula,PFormula), |
| 261 | | debug_println(5,starting_ctl_model_check(PFormula,Mode,InitStates)), |
| 262 | | ( printtime(ctl_sat_for_all(PFormula,InitStates,Trace,Res)), |
| 263 | | debug_format(9,'CTL internal trace: ~w~n',[Trace]), |
| 264 | | Trace = [Initstate|_] -> |
| 265 | | ( Mode=init -> ltl: set_trace_to_init(Initstate) % we need to go to the initialised state |
| 266 | | % before the counterexample itself is shown |
| 267 | | ; Mode = specific_node(NodeID) -> ltl: set_trace_to_init(NodeID) |
| 268 | | ; true), |
| 269 | | expand_trace(Trace,ETrace), %debug_println(4,expanded(ETrace)), |
| 270 | | print_trace(Res,ETrace), |
| 271 | | ltl: executetrace(ETrace), |
| 272 | | (debug_mode(off) |
| 273 | | -> reset_ctl |
| 274 | | ; reset_ctl_with_stats |
| 275 | | ) |
| 276 | | %,(formula_registry(F,Id),format('Formula ~w = ~w~n',[Id,F]),fail ; true) |
| 277 | | %, nl, findall(T,sat_eu_table(_,_,_,T,_),Ts), lists:max_member(Mx,Ts), print(max_trace_length(Mx)),nl |
| 278 | | %, (sat_eu_table(E,Fid,Gid,T2,R), print(sat_eu_table(E,Fid,Gid,T2,R)),nl,fail ; true) |
| 279 | | ; |
| 280 | | add_internal_error('CTL model check failed: ',PFormula), |
| 281 | | print_size_of_table(ctl: sat_eu_table/5),fail |
| 282 | | ). |
| 283 | | |
| 284 | | reset_ctl_with_stats :- |
| 285 | | print_size_of_table(ctl: sat_eg_table/3), |
| 286 | | print_size_of_table(ctl: sat_eu_table/5), |
| 287 | | print_size_of_table(ctl: formula_registry/2), |
| 288 | | retract_with_statistics(ctl,[sat_eg_table(_,_,_),sat_eu_table(_,_,_,_,_)]). |
| 289 | | |
| 290 | | print_trace(_,_) :- silent_mode(on),!. |
| 291 | | print_trace(Res,Trace) :- |
| 292 | | (length(Trace,Len) -> true ; Len=0), |
| 293 | | ( Res = true -> format('CTL Witness of length ~w found~n',[Len]) |
| 294 | | ; Res = false -> format('CTL counterexample of length ~w found~n',[Len]) |
| 295 | | ; format('Could not decide CTL property, checking result: ~w~n',[Res]) |
| 296 | | ), |
| 297 | | (debug_mode(on) -> format('Trace: ~w~n',[Trace]) ; true). |
| 298 | | |
| 299 | | :- use_module(ltl_tools,[temporal_parser/3]). |
| 300 | | |
| 301 | | ctl_model_check(Formula,MaxNodes,Mode,Res) :- |
| 302 | | (temporal_parser(Formula,ctl,Ctl) -> true ; add_error(ctl,'CTL Parser failed: ',Formula),fail), |
| 303 | | set_max_nr_of_new_impl_trans_nodes(MaxNodes), |
| 304 | | select_ctl_mode(Mode,Ctl,Startnodes,Ctl2), |
| 305 | | ctl_model_check2(Ctl2,Mode,Startnodes,Res). |
| 306 | | |
| 307 | | :- use_module(probsrc(state_space),[find_initialised_states/1]). |
| 308 | | |
| 309 | | select_ctl_mode(starthere,Ctl,[Startnode],Ctl) :- |
| 310 | | !, current_state_id(Startnode), |
| 311 | | debug_format(19,'Checking CTL formula from current state: ~w~n',[Startnode]). |
| 312 | | select_ctl_mode(init,Ctl,Startnodes,Ctl) :- |
| 313 | | !, find_initialised_states(Startnodes), |
| 314 | | debug_format(19,'Checking CTL formula from all initial states: ~w~n',[Startnodes]). |
| 315 | | select_ctl_mode(specific_node(Startnode),Ctl,[Startnode],Ctl) :- |
| 316 | | !, |
| 317 | | debug_format(19,'Checking CTL formula from specific state: ~w~n',[Startnode]). |
| 318 | | select_ctl_mode(Mode,Ctl,[],Ctl) :- |
| 319 | | add_error(ltl,'Internal error: Unexpected mode', Mode), |
| 320 | | fail. |
| 321 | | |
| 322 | | /* --------------------------- */ |
| 323 | | |
| 324 | | :- use_module(probsrc(debug),[formatsilent/2]). |
| 325 | | printtime(C) :- |
| 326 | | statistics(runtime,[Start|_]), statistics(walltime,[StartW|_]), |
| 327 | | (call(C) -> Res=ok ; Res=failure), |
| 328 | | statistics(runtime,[End|_]), statistics(walltime,[EndW|_]), |
| 329 | | T is End - Start, TW is EndW - StartW, |
| 330 | | formatsilent('CTL check took ~3d seconds walltime (~3d seconds runtime) ~n', [TW,T]), |
| 331 | | Res=ok. |
| 332 | | |
| 333 | | :- public parse_and_pp_ctl_formula/2. |
| 334 | | parse_and_pp_ctl_formula(FormulaAsAtom,Text) :- |
| 335 | | temporal_parser(FormulaAsAtom,ctl,Ctl), |
| 336 | | ( ltl: pp_ltl_formula(Ctl,Text) |
| 337 | | -> true |
| 338 | | ; add_error_fail(ctl, 'Failed to pretty print CTL formula: ', Ctl) |
| 339 | | ). |
| 340 | | |
| 341 | | %%%%%%%%%%%%%%%%%%%% Used for the CSP-M Assertions Viewer %%%%%%%%%%%%%%%%%%%%%% |
| 342 | | :- dynamic ctl_counter_examples_cache/4. |
| 343 | | |
| 344 | | :- use_module(probsrc(user_interrupts),[catch_interrupt_assertion_call/1]). |
| 345 | | :- use_module(extension('user_signal/user_signal'), [user_interruptable_call_det/2]). |
| 346 | | interruptable_ctl_model_check_with_ce2(Proc,Formula,Mode,InitStates,Res,CE) :- |
| 347 | | user_interruptable_call_det(catch_interrupt_assertion_call(ctl: ctl_model_check_with_ce2(Proc,Formula,Mode,InitStates,Res,CE)),InterruptResult), |
| 348 | | (InterruptResult=interrupted -> |
| 349 | | Res=interrupted |
| 350 | | ; real_error_occurred -> |
| 351 | | print_error('% *** Errors occurred while CTL model checking ! ***'),nl,nl,fail |
| 352 | | ; formatsilent('CTL assertion check completed.~n',[]) |
| 353 | | ). |
| 354 | | |
| 355 | | :- public ctl_model_check_with_ce2/6. |
| 356 | | ctl_model_check_with_ce2(Proc,Formula,Mode,InitStates,Res,CE) :- |
| 357 | | reset_ctl, |
| 358 | | ltl: preprocess_formula(Formula,PFormula), |
| 359 | | ( printtime(ctl_sat_for_all(PFormula,InitStates,Trace,Res)) -> |
| 360 | | ( Res = true -> CE=none, printsilent('Witness found: '),nls |
| 361 | | ; Res = false -> formatsilent('CTL counterexample found~n',[]), convert_ctl_trace(Proc,PFormula,Mode,false,Trace,CE) |
| 362 | | ; CE=unexpected_result, formatsilent('Could not decide CTL property: ~w~n',[Res])), |
| 363 | | debug_format(19,'CTL trace: ~w~n',[Trace]), |
| 364 | | (silent_mode(on) -> reset_ctl |
| 365 | | ; reset_ctl_with_stats |
| 366 | | ) |
| 367 | | ; |
| 368 | | add_internal_error('CTL model check failed: ',PFormula), |
| 369 | | print_size_of_table(ctl: sat_eu_table/5),fail |
| 370 | | ),nl. |
| 371 | | |
| 372 | | %%%%%%%%%%%%%%%%%%%% Used by ProB2 Interface %%%%%%%%%%%%%%%%%%%%%% |
| 373 | | interruptable_ctl_model_check_with_ce3(Proc,Formula,Mode,InitStates,Res,CE) :- |
| 374 | | user_interruptable_call_det(catch_interrupt_assertion_call(ctl: ctl_model_check_with_ce3(Proc,Formula,Mode,InitStates,Res,CE)),InterruptResult), |
| 375 | | (InterruptResult=interrupted -> |
| 376 | | Res=interrupted |
| 377 | | ; real_error_occurred -> |
| 378 | | print_error('% *** Errors occurred while CTL model checking ! ***'),nl,nl,fail |
| 379 | | ; formatsilent('CTL assertion check completed.~n',[]) |
| 380 | | ). |
| 381 | | |
| 382 | | :- public ctl_model_check_with_ce3/6. |
| 383 | | ctl_model_check_with_ce3(Proc,Formula,Mode,InitStates,Res,CE) :- |
| 384 | | reset_ctl, |
| 385 | | ltl: preprocess_formula(Formula,PFormula), |
| 386 | | ( printtime(ctl_sat_for_all(PFormula,InitStates,Trace,Res)) -> |
| 387 | | ( Res = true -> CE=none, printsilent('Witness found: '),nls |
| 388 | | ; Res = false -> formatsilent('CTL counterexample found (~w)~n',[Mode]), |
| 389 | | convert_ctl_trace(Proc,PFormula,Mode,true,Trace,CE) |
| 390 | | ; CE=unexpected_result, formatsilent('Could not decide CTL property: ~w~n',[Res])), |
| 391 | | debug_format(19,'CTL trace: ~w~n',[Trace]), |
| 392 | | %expand_trace(Trace,ETrace), print(expanded(ETrace)),nl, |
| 393 | | (silent_mode(on) -> reset_ctl |
| 394 | | ; reset_ctl_with_stats |
| 395 | | ) |
| 396 | | ; |
| 397 | | add_internal_error('CTL model check failed: ',PFormula), |
| 398 | | print_size_of_table(ctl: sat_eu_table/5),fail |
| 399 | | ),nl. |
| 400 | | |
| 401 | | |
| 402 | | |
| 403 | | :- use_module(probsrc(eventhandling),[register_event_listener/3]). |
| 404 | | :- register_event_listener(reset_specification,reset_ctl_ce_cache, |
| 405 | | 'Reset CTL Counterexamples Cache.'). |
| 406 | | :- register_event_listener(reset_prob,reset_ctl_ce_cache, |
| 407 | | 'Reset CTL Counterexamples Cache.'). |
| 408 | | |
| 409 | | reset_ctl_ce_cache :- |
| 410 | | retractall(ctl_counter_examples_cache(_,_,_,_)). |
| 411 | | |
| 412 | | % TODO: for returning action traces of CTL counter-examples more |
| 413 | | % information is needed for determinig the entry loop |
| 414 | | convert_ctl_trace(Proc,Ctl,Mode,UseTransitionIDs,Trace,CE) :- |
| 415 | | expand_trace(Trace,Result), |
| 416 | | %% convert_ctl_trace1(ETrace,Result), |
| 417 | | % keep the latest counterexample: an earlier one may stem from another start state or state space |
| 418 | | retractall(ctl_counter_examples_cache(Proc,Ctl,Mode,_)), assertz(ctl_counter_examples_cache(Proc,Ctl,Mode,Result)), |
| 419 | | (UseTransitionIDs = true -> get_event_trace_transitions(Mode,Result,EventTrace) |
| 420 | | ; get_event_trace(Result,EventTrace)), |
| 421 | | CE=ce(EventTrace). |
| 422 | | |
| 423 | | get_event_trace(Trace,ETrace) :- |
| 424 | | get_event_trace1(Trace,Res), |
| 425 | | tools: ajoin_with_sep(Res,' -> ',ETrace). |
| 426 | | |
| 427 | | :- use_module(probsrc(translate),[translate_event/2]). |
| 428 | | get_event_trace1([],['']). |
| 429 | | get_event_trace1([_NodeID],['']). |
| 430 | | get_event_trace1([Src,Dest|Rest], [Event|Res]) :- |
| 431 | | transition(Src,Op,_OpId,Dest), |
| 432 | | translate_event(Op,Event), |
| 433 | | get_event_trace1([Dest|Rest],Res). |
| 434 | | |
| 435 | | :- use_module(probsrc(specfile),[get_operation_name/2]). |
| 436 | | % for ProB2-UI |
| 437 | | % TODO: the counter example is just a list of state ids, not the transition ids |
| 438 | | % for some counter examples it could be useful to actually enforce the right transitions to be used EX with action,... |
| 439 | | get_event_trace_transitions(init,[StartNode|T],CE) :- |
| 440 | | (transition(root,_,_,StartNode) -> |
| 441 | | get_event_trace_transitions2([root,StartNode|T],CE) |
| 442 | | ; transition(root,_,_,N1), transition(N1,_,_,StartNode) -> % setup constants first |
| 443 | | get_event_trace_transitions2([root,N1,StartNode|T],CE) |
| 444 | | ; |
| 445 | | get_event_trace_transitions2([root,StartNode|T],CE)). |
| 446 | | % for specific_node(Start) and starthere the trace already starts with the start node |
| 447 | | get_event_trace_transitions(specific_node(_Start),Trace,CE) :- % do we need a trace from root to Start ? |
| 448 | | get_event_trace_transitions2(Trace,CE). |
| 449 | | get_event_trace_transitions(starthere,Trace,CE) :- |
| 450 | | get_event_trace_transitions2(Trace,CE). |
| 451 | | |
| 452 | | % unit tests on a small hand-made state space ctls0 -a-> ctls1 -b-> ctls2 |
| 453 | | :- use_module(probsrc(self_check)). |
| 454 | | with_ctl_test_transitions(Goal) :- |
| 455 | | call_cleanup((assertz(state_space:transition(ctls0,a,ctlt1,ctls1)), |
| 456 | | assertz(state_space:transition(ctls1,b,ctlt2,ctls2)), |
| 457 | | call(Goal)), |
| 458 | | (retract(state_space:transition(ctls0,a,ctlt1,ctls1)), |
| 459 | | retract(state_space:transition(ctls1,b,ctlt2,ctls2)))). |
| 460 | | :- assert_must_succeed(with_ctl_test_transitions((get_event_trace_transitions(specific_node(ctls0),[ctls0,ctls1,ctls2],CE), |
| 461 | | CE == [transition(ctlt1,a,ctls0,ctls1),transition(ctlt2,b,ctls1,ctls2)]))). |
| 462 | | :- assert_must_succeed(with_ctl_test_transitions((get_event_trace_transitions(starthere,[ctls1,ctls2],CE), |
| 463 | | CE == [transition(ctlt2,b,ctls1,ctls2)]))). |
| 464 | | get_event_trace_transitions2([], Res) :- !, Res=[]. |
| 465 | | get_event_trace_transitions2([_NodeID],Res) :- !, Res=[]. |
| 466 | | get_event_trace_transitions2([Src,Dest|Rest], [transition(OpId,OpName,Src,Dest)|Res]) :- |
| 467 | | transition(Src,Op,OpId,Dest),!, |
| 468 | | get_operation_name(Op,OpName), |
| 469 | | get_event_trace_transitions2([Dest|Rest],Res). |
| 470 | | get_event_trace_transitions2([Src,Dest|_],[]) :- |
| 471 | | add_error(ctl_model_check,'Cannot replay counter example, no transition for:',(Src,Dest)). |
| 472 | | |
| 473 | | |
| 474 | | tcltk_play_ctl_counterexample(Proc,Ctl) :- |
| 475 | | ctl_counter_examples_cache(Proc,Ctl,Mode,ETrace), |
| 476 | | ETrace = [Initstate|_], |
| 477 | | ( Mode=init -> ltl: set_trace_to_init(Initstate) |
| 478 | | ; Mode = specific_node(NodeID) -> ltl: set_trace_to_init(NodeID) |
| 479 | | ; true), |
| 480 | | ltl: executetrace(ETrace). |
| 481 | | tcltk_play_ctl_counterexample(Proc,Ctl) :- |
| 482 | | add_error_fail(ctl_counter_example, 'Internal error: No counterexample has been asserted for the following configuration: ',(Proc,Ctl)). |
| 483 | | |
| 484 | | ctl_model_check_with_ce(Proc,Formula,MaxNodes,Mode,Res,CE) :- |
| 485 | | (temporal_parser(Formula,ctl,Ctl) -> true ; add_error_fail(ctl,'CTL Parser failed: ',Formula)), |
| 486 | | set_max_nr_of_new_impl_trans_nodes(MaxNodes), |
| 487 | | select_ctl_mode(Mode,Ctl,Startnodes,Ctl2), |
| 488 | | interruptable_ctl_model_check_with_ce2(Proc,Ctl2,Mode,Startnodes,Res,CE). |
| 489 | | |
| 490 | | |
| 491 | | %%%%%%%%%%%%%%%%%%%% Used by ProB2 Interface %%%%%%%%%%%%%%%%%%%%%% |
| 492 | | ctl_model_check_with_ast(Proc,Ctl,MaxNodes,Mode,Res,CE) :- |
| 493 | | set_max_nr_of_new_impl_trans_nodes(MaxNodes), |
| 494 | | select_ctl_mode(Mode,Ctl,Startnodes,Ctl2), |
| 495 | | interruptable_ctl_model_check_with_ce3(Proc,Ctl2,Mode,Startnodes,Res,CE). |