| 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 | :- module(static_analysis, [dependent_actions/5, enable_analysis/6, disable_analysis/5, | |
| 6 | action_dependent_to_itself/4, tcltk_enabling_analysis/2, | |
| 7 | compute_dependendency_relation_of_all_events_in_the_model/3, | |
| 8 | enable_graph/1,dependent_act/4, | |
| 9 | syntactic_independence/3, | |
| 10 | get_conj_inv_predicate/3, % predicate used in the enabling analysis module | |
| 11 | catch_and_ignore_well_definedness_error/2, | |
| 12 | test_path_non_failing/6, | |
| 13 | test_path/6, | |
| 14 | is_timeout/1 | |
| 15 | ]). | |
| 16 | ||
| 17 | :- meta_predicate catch_enumeration_warning(0,0). | |
| 18 | :- meta_predicate catch_and_ignore_well_definedness_error(0,*). | |
| 19 | :- meta_predicate catch_and_ignore_well_definedness_error(*,0,*). | |
| 20 | ||
| 21 | ||
| 22 | /* Modules and Infos for the code coverage analysis */ | |
| 23 | :- use_module(probsrc(module_information)). | |
| 24 | :- module_info(group,model_checker). | |
| 25 | :- module_info(description,'This module provides predicates for static analysis of Event-B and B models'). | |
| 26 | ||
| 27 | % Standard SICSTUS prolog libraries | |
| 28 | %% :- use_module(library(lists)). | |
| 29 | :- use_module(library(ordsets)). | |
| 30 | :- use_module(library(ugraphs)). | |
| 31 | ||
| 32 | % Classical B prolog modules | |
| 33 | :- use_module(probsrc(bmachine),[b_top_level_operation/1, b_get_invariant_from_machine/1, b_get_properties_from_machine/1]). | |
| 34 | :- use_module(probsrc(bsyntaxtree), [conjunct_predicates/2]). | |
| 35 | :- use_module(probsrc(b_state_model_check), [get_negated_guard/3,get_guard/2]). | |
| 36 | :- use_module(probsrc(bmachine),[b_get_machine_operation_for_animation/4]). % TODO: predicate used for determining whether an event is self-dependent to itself (consider whether to remove) | |
| 37 | ||
| 38 | % Event-B prolog modules | |
| 39 | ||
| 40 | % POR modules | |
| 41 | :- use_module(probporsrc(enabling_predicates),[compute_enabling_predicate/5]). | |
| 42 | ||
| 43 | % Importing predicates for using CBC | |
| 44 | :- use_module(cbcsrc(cbc_path_solver), [testcase_path_timeout/9]). | |
| 45 | :- use_module(probsrc(b_read_write_info), [b_get_read_write/3, b_get_read_write_vars/5, b_get_operation_read_guard_vars/4]). | |
| 46 | ||
| 47 | % Other modules (used mostly for debugging) | |
| 48 | :- use_module(probsrc(preferences),[get_preference/2]). | |
| 49 | :- use_module(probsrc(error_manager),[real_error_occurred/0, | |
| 50 | call_in_fresh_error_scope_for_one_solution/1, | |
| 51 | add_internal_error/2, add_error_fail/3, | |
| 52 | wd_error_occured/0, clear_wd_errors/0, | |
| 53 | critical_enumeration_warning_occured_in_error_scope/0, | |
| 54 | clear_enumeration_warnings/0]). | |
| 55 | :- use_module(probsrc(debug),[debug_println/2, debug_mode/1, debug_format/3, formatsilent/2]). | |
| 56 | :- use_module(probsrc(tools),[cputime/1]). | |
| 57 | ||
| 58 | % Importing unit tests predicates | |
| 59 | %% :- use_module(probsrc(self_check)). | |
| 60 | ||
| 61 | is_timeout(timeout). | |
| 62 | is_timeout(time_out). | |
| 63 | is_timeout(clpfd_overflow). | |
| 64 | is_timeout(overflow). | |
| 65 | is_timeout(virtual_time_out). | |
| 66 | is_timeout(interrupt). % user interrupt (CTRL-C) during a cbc_path_solver search: result is inconclusive | |
| 67 | ||
| 68 | % --------------------------- Static analysis for determining the dependece/independence of events (Begin) -----------------------------% | |
| 69 | ||
| 70 | % check syntactically influence of executing OpName1 on OpName2's enabling and effect | |
| 71 | syntactic_independence(OpName1,OpName2,Res) :- | |
| 72 | b_top_level_operation(OpName1), | |
| 73 | b_top_level_operation(OpName2), | |
| 74 | b_get_read_write_vars(OpName1,GReads1,AReads1,Reads1,Writes1), | |
| 75 | b_get_read_write_vars(OpName2,GReads2,AReads2,Reads2,Writes2), | |
| 76 | \+ ord_intersect(Writes1,GReads2),% otherwise Op1 can influence guard of Op2 | |
| 77 | (ord_intersect(Writes1,AReads2) | |
| 78 | -> % guard not modified, but effect of OpName2 could change | |
| 79 | Res = syntactic_keep | |
| 80 | ; (\+ ord_intersect(Writes1,Writes2), % no race | |
| 81 | \+ ord_intersect(GReads1,Writes2), % no enabling/disabling of OpName1 by OpName2 | |
| 82 | \+ ord_intersect(AReads1,Writes2)) % no change of effect | |
| 83 | -> (\+ ord_intersect(Reads1,Reads2) | |
| 84 | -> Res = syntactic_fully_independent | |
| 85 | ; Res = syntactic_independent | |
| 86 | ) | |
| 87 | ; Res = syntactic_unchanged % guard is kept and operation effect of OpName2 unchanged; but there could be race conditions on writes or effects of OpName2 on OpName1 | |
| 88 | ). | |
| 89 | ||
| 90 | dependent_actions1(OpName1,OpName2,FindInvViolations,Timeout,Res) :- | |
| 91 | b_top_level_operation(OpName1), | |
| 92 | b_top_level_operation(OpName2), | |
| 93 | b_get_read_write_vars(OpName1,GReads1,AReads1,Reads1,Writes1), | |
| 94 | ( OpName1=OpName2 -> action_dependent_to_itself(OpName1,GReads1,Writes1,Res) % TODO: Do we really need this kind of analysis??? | |
| 95 | ; OpName2 @< OpName1 -> Res = '-' % our checking is symmetric; check only one pair | |
| 96 | ; | |
| 97 | b_get_read_write_vars(OpName2,GReads2,AReads2,Reads2,Writes2), | |
| 98 | ( ord_intersect(Writes1,Writes2) -> Res = race_dependent | |
| 99 | ; (\+ ord_intersect(Writes1,Reads2),\+ ord_intersect(Writes2,Reads1)) -> Res = syntactic_independent | |
| 100 | ; | |
| 101 | (get_preference(use_cbc_analysis,true) -> % sometimes we don't want to use the CBC | |
| 102 | (\+ord_intersect(AReads1,Writes2), | |
| 103 | \+ord_intersect(AReads2,Writes1) -> | |
| 104 | (get_preference(dependency_enable_predicates,true) -> | |
| 105 | get_dependency_enabling_predicate(OpName1,GReads1,Writes1,OpName2,GReads2,Writes2,FindInvViolations,Timeout,Res) | |
| 106 | ; | |
| 107 | test_enabledness_condition(OpName1,GReads1,Writes1,OpName2,GReads2,Writes2,FindInvViolations,Timeout,Res) | |
| 108 | ) | |
| 109 | ; Res = action_dependent | |
| 110 | ) | |
| 111 | ; Res = dependent % use_cbc_analysis = false | |
| 112 | ) | |
| 113 | ) | |
| 114 | ). | |
| 115 | ||
| 116 | % Testing whether it is possible that the one of the events can disable the other one | |
| 117 | test_enabledness_condition(OpName1,GReads1,Writes1,OpName2,GReads2,Writes2,FindInvViolations,Timeout,Res) :- | |
| 118 | get_negated_guard(OpName1,PosGuard1,NegGuard1), | |
| 119 | get_negated_guard(OpName2,PosGuard2,NegGuard2), | |
| 120 | %% get_conj_inv_predicate([PosGuard1,PosGuard2],FindInvViolations,GuardsConj), | |
| 121 | conjunct_predicates([PosGuard1,PosGuard2],GuardsConj), % both events are enabled | |
| 122 | ( ord_intersect(GReads1,Writes2), | |
| 123 | debug_format(19,'Testing if ~w can disable ~w (check inv. = ~w)~n',[OpName2,OpName1,FindInvViolations]), | |
| 124 | test_path(GuardsConj,[OpName2],NegGuard1,FindInvViolations,Timeout,_R1) | |
| 125 | -> Res = guard_dependent | |
| 126 | ; ord_intersect(Writes1,GReads2), | |
| 127 | debug_format(19,'Testing if ~w can disable ~w (check inv. = ~w)~n',[OpName1,OpName2,FindInvViolations]), | |
| 128 | test_path(GuardsConj,[OpName1],NegGuard2,FindInvViolations,Timeout,_R2) | |
| 129 | -> Res = guard_dependent | |
| 130 | ; Res = independent | |
| 131 | ). | |
| 132 | ||
| 133 | get_dependency_enabling_predicate(OpName1,GReads1,Writes1,OpName2,GReads2,Writes2,FindInvViolations,Timeout,Res) :- | |
| 134 | get_negated_guard(OpName1,PosGuard1,NegGuard1), | |
| 135 | get_negated_guard(OpName2,PosGuard2,NegGuard2), | |
| 136 | conjunct_predicates([PosGuard1,PosGuard2],GuardsConj), | |
| 137 | ( (ord_intersect(GReads1,Writes2),test_path(GuardsConj,[OpName2],NegGuard1,FindInvViolations,Timeout,_R1)) -> | |
| 138 | % get enabling predicate after executing OpName2 | |
| 139 | compute_enabling_predicate(OpName2,true,PosGuard1,FindInvViolations,predicate(Enable1)), | |
| 140 | ( (ord_intersect(Writes1,GReads2),test_path(GuardsConj,[OpName1],NegGuard2,FindInvViolations,Timeout,_R2)) -> | |
| 141 | % get enabling predicate after executing OpName1 | |
| 142 | compute_enabling_predicate(OpName1,true,PosGuard2,FindInvViolations,predicate(Enable2)), | |
| 143 | conjunct_predicates([Enable1,Enable2],EnablingPredicate), | |
| 144 | Res = guard_dependent(EnablingPredicate) | |
| 145 | ; | |
| 146 | Res = guard_dependent(Enable1) | |
| 147 | ) | |
| 148 | ; (ord_intersect(Writes1,GReads2),test_path(GuardsConj,[OpName1],NegGuard2,FindInvViolations,Timeout,_R2)) -> | |
| 149 | compute_enabling_predicate(OpName1,true,PosGuard2,FindInvViolations,predicate(Enable2)), | |
| 150 | Res = guard_dependent(Enable2) | |
| 151 | ; | |
| 152 | Res = independent | |
| 153 | ), | |
| 154 | (Res = guard_dependent(Pred), debug_mode(on) -> | |
| 155 | print('####### Enabling Dependency Predicate for '), print(OpName1), print('<->'), print(OpName2), print(' #######'),nl, | |
| 156 | print('Predicate: '),translate:print_bexpr(Pred),nl, | |
| 157 | print('####################################'),nl | |
| 158 | ; true). | |
| 159 | ||
| 160 | action_dependent_to_itself(OpName,Reads,Writes,Res) :- | |
| 161 | b_get_machine_operation_for_animation(OpName,_Res,Params,_), | |
| 162 | length(Params,Len), | |
| 163 | (Len>0,\+ord_intersect(Reads,Writes) -> | |
| 164 | Res = self_independent | |
| 165 | ; Len = 0 -> | |
| 166 | Res = '=' | |
| 167 | ; | |
| 168 | Res = self_dependent | |
| 169 | ). | |
| 170 | ||
| 171 | % --------------------------- Static analysis for determining the dependece/independence of events (End) -----------------------------% | |
| 172 | ||
| 173 | ||
| 174 | % --------------------------- Enabling anaysis used for the computation of ample sets (Begin) -----------------------------% | |
| 175 | ||
| 176 | enable_analysis(OpName1,OpName2,FindInvViolations,UseEnableGraph,Timeout,Enable) :- | |
| 177 | b_top_level_operation(OpName1), | |
| 178 | b_top_level_operation(OpName2), | |
| 179 | (OpName1 == OpName2 -> | |
| 180 | Enable=impossible % on top level an operation cannot enable itself | |
| 181 | ; | |
| 182 | b_get_read_write(OpName1,_Reads1,Writes1), | |
| 183 | b_get_operation_read_guard_vars(OpName2,true,GReads2,_Precise), | |
| 184 | get_negated_guard(OpName2,PosGuard2,NegGuard2), | |
| 185 | %enabling_analysis: filter_predicate(PosGuard2,Writes1,FilteredPosGuard2), | |
| 186 | (get_preference(use_cbc_analysis,false) -> | |
| 187 | (ord_intersect(Writes1,GReads2) -> | |
| 188 | get_enabling_result(UseEnableGraph,OpName1,PosGuard2,FindInvViolations,_CBCResult,Enable) | |
| 189 | ; | |
| 190 | Enable=possible_keep | |
| 191 | ) | |
| 192 | ; | |
| 193 | get_guard(OpName1,Guard1), | |
| 194 | conjunct_predicates([Guard1,NegGuard2],StartPred), | |
| 195 | ( (ord_intersect(Writes1,GReads2),test_path(StartPred,[OpName1],PosGuard2,FindInvViolations,Timeout,R)) -> | |
| 196 | get_enabling_result(UseEnableGraph,OpName1,PosGuard2,FindInvViolations,R,Enable) | |
| 197 | ; | |
| 198 | Enable=possible_keep_or_disable | |
| 199 | ) | |
| 200 | ) | |
| 201 | ). | |
| 202 | ||
| 203 | get_enabling_result(UseEnableGraph,OpName1,PosGuard2,FindInvViolations,CBCResult,Enable) :- | |
| 204 | (UseEnableGraph=true -> %(OpName1 = close_door, OpName2 = push_call_button -> trace;true), | |
| 205 | % check further if Enable is inconsistent, if so then we do not need to add an edge to the enable graph | |
| 206 | ? | (compute_enabling_predicate(OpName1,true,PosGuard2,FindInvViolations,Enable) -> |
| 207 | true | |
| 208 | ; | |
| 209 | add_error_fail(enable_analysis,'Error occurred while computing enabling predicate for ', OpName1/PosGuard2) | |
| 210 | ) | |
| 211 | ; | |
| 212 | (is_timeout(CBCResult)-> Enable=CBCResult; Enable=possible_enable) | |
| 213 | ). | |
| 214 | ||
| 215 | catch_enumeration_warning(Call,Handler) :- | |
| 216 | % throw/1 predicate raises instantiation_error | |
| 217 | catch(Call, enumeration_warning(enumerating(_),_Type,_,_,critical), call(Handler)). | |
| 218 | ||
| 219 | catch_and_ignore_well_definedness_error(Call,Result) :- | |
| 220 | catch_and_ignore_well_definedness_error('',Call,Result). | |
| 221 | :- use_module(probsrc(debug), [silent_mode/1, set_silent_mode/1]). | |
| 222 | catch_and_ignore_well_definedness_error(Ctxt,Call,Result) :- | |
| 223 | silent_mode(CurMode), set_silent_mode(on), | |
| 224 | call_cleanup(catch_enumeration_warning(Call, true), | |
| 225 | set_silent_mode(CurMode)), | |
| 226 | (real_error_occurred -> Result=error ; Result=success), | |
| 227 | cleanup_enum_and_wd(Ctxt,Result). % only cleanup when we succeed; TODO: do we need to cover the failure of Call ? | |
| 228 | ||
| 229 | cleanup_enum_and_wd(Ctxt,Result) :- | |
| 230 | ? | (critical_enumeration_warning_occured_in_error_scope -> |
| 231 | formatsilent('Enumeration warning occurred and will be ignored; result=~w. ~w~n',[Result,Ctxt]), | |
| 232 | clear_enumeration_warnings | |
| 233 | ; true), | |
| 234 | (wd_error_occured -> | |
| 235 | formatsilent('WD errors occurred and will be discarded; result=~w. ~w~n',[Result,Ctxt]), | |
| 236 | clear_wd_errors | |
| 237 | ; true). | |
| 238 | ||
| 239 | disable_analysis(OpName1,OpName2,FindInvViolations,Timeout,Result) :- | |
| 240 | b_top_level_operation(OpName1), | |
| 241 | b_top_level_operation(OpName2), | |
| 242 | b_get_read_write(OpName1,_Reads1,Writes1), | |
| 243 | b_get_operation_read_guard_vars(OpName2,true,GReads2,_Precise), | |
| 244 | get_negated_guard(OpName2,PosGuard2,NegGuard2), | |
| 245 | (get_preference(use_cbc_analysis,false) -> | |
| 246 | (ord_intersect(Writes1,GReads2) -> Result=possible_disable ; Result=possible_keep) | |
| 247 | ; | |
| 248 | get_guard(OpName1,PosGuard1), | |
| 249 | conjunct_predicates([PosGuard1,PosGuard2],StartPred), | |
| 250 | ( ord_intersect(Writes1,GReads2), | |
| 251 | debug_format(19,'~nTesting if ~w (writing ~w) can disable ~w (reading ~w) with inv. = ~w~n',[OpName1,Writes1,OpName2,GReads2,FindInvViolations]), | |
| 252 | %print('Start: '),translate:print_bexpr(StartPred),nl, print('Target: '),translate:print_bexpr(NegGuard2),nl, | |
| 253 | test_path(StartPred,[OpName1],NegGuard2,FindInvViolations,Timeout,CBCResult) | |
| 254 | -> | |
| 255 | debug_format(19,'CBCResult=~w~n',[CBCResult]), | |
| 256 | (is_timeout(CBCResult) -> Result=CBCResult | |
| 257 | ; Result=possible_disable) | |
| 258 | ; | |
| 259 | Result=cannot_disable | |
| 260 | ) | |
| 261 | ). | |
| 262 | ||
| 263 | % test_path(+Start,+Path,+Goal,+WithInv,+Timeout,-R) | |
| 264 | % Runs the CBC to check whether there is a possible execution of sequence of events Path in the loaded model | |
| 265 | % starting at a state s satisfying Start and reaching a state s' satisfying Goal whitin Timeout milliseconds | |
| 266 | % + Start: B predicate | |
| 267 | % + Path: a list of events/operations | |
| 268 | % + Goal: B predicate | |
| 269 | % + Timeout: specifying a timeout (in milliseconds) | |
| 270 | % - R: result (result could be either 'ok', 'timeout', 'interrupt', or 'unknown') | |
| 271 | %%% pred(P) - adds P to invariant | |
| 272 | %%% typing(P) - just use typing from invariant and add P | |
| 273 | ||
| 274 | test_path(Start,Path,Goal,WithInv,Timeout,Res) :- | |
| 275 | (Start=init -> | |
| 276 | Start1 = init | |
| 277 | ; | |
| 278 | (WithInv=1 -> | |
| 279 | Start1 = pred(Start), | |
| 280 | get_conj_inv_predicate([Goal],WithInv,Goal_Inv) | |
| 281 | ; Start1=typing(Start), | |
| 282 | Goal_Inv = Goal) | |
| 283 | ), | |
| 284 | catch_and_ignore_well_definedness_error(path(Path),testcase_path_timeout(Start1,Timeout,Path,Goal_Inv,_,_,_,_,R),ER), | |
| 285 | check_result(R,ER,Res). | |
| 286 | ||
| 287 | test_path_non_failing(Start,Path,Goal,WithInv,Timeout,R) :- | |
| 288 | (test_path(Start,Path,Goal,WithInv,Timeout,R) -> true; R = false). | |
| 289 | ||
| 290 | check_result(R,ER,Res) :- | |
| 291 | (nonvar(R) -> Res=R | |
| 292 | ; ER = success -> Res = 'ok' | |
| 293 | ; Res = 'unknown'). | |
| 294 | ||
| 295 | % conjoin a predicate with invariant and properties if specified | |
| 296 | get_conj_inv_predicate(Preds,FindInvViolations,StartPred) :- | |
| 297 | (FindInvViolations==1 -> | |
| 298 | b_get_invariant_from_machine(Inv), | |
| 299 | b_get_properties_from_machine(Prop), | |
| 300 | conjunct_predicates([Prop,Inv|Preds],StartPred) % TO DO: project away | |
| 301 | ; | |
| 302 | (FindInvViolations==0 -> true ; add_internal_error('Illegal flag: ',get_conj_inv_predicate(Preds,FindInvViolations,StartPred))), | |
| 303 | conjunct_predicates(Preds,StartPred) | |
| 304 | ). | |
| 305 | ||
| 306 | %reads_writes_intersection(Writes, Reads, ComputedResult, PositiveResult, Res) :- | |
| 307 | % (ord_intersect(Writes,Reads) -> Res=PositiveResult ; Res=ComputedResult). | |
| 308 | ||
| 309 | tcltk_enabling_analysis(list([list(['Origin'|Ops])|Result]),POR) :- | |
| 310 | get_preference(timeout_cbc_analysis,Timeout), | |
| 311 | %% compute_dependendency_relation_of_all_events_in_the_model(1,por(ample_sets,0,0,0),EnableGraph), | |
| 312 | %% suitable_for_por(EnableGraph,POR), | |
| 313 | POR = option_disabled, | |
| 314 | findall(Op, b_top_level_operation(Op), Ops), | |
| 315 | findall(list([OpName1|EnableList]), | |
| 316 | (b_top_level_operation(OpName1), | |
| 317 | findall(Enable,enable_analysis(OpName1,_OpName2,1,1,Timeout,Enable),EnableList)), | |
| 318 | Result) | |
| 319 | . %,print_enable_table([list(['Origin'|Ops])|Result]). | |
| 320 | ||
| 321 | %% suitable_for_por(EnGraph,Result) :- | |
| 322 | %% (not_all_reachable(EnGraph) -> | |
| 323 | %% Result=true | |
| 324 | %% ; Result=false | |
| 325 | %% ). | |
| 326 | ||
| 327 | %% not_all_reachable(EnGraph) :- | |
| 328 | %% ample_sets: vertices(EnGraph,Vertices), | |
| 329 | %% member(V,Vertices), | |
| 330 | %% \+ (ample_sets: reachable(V,EnGraph,Reachable),Reachable=Vertices). | |
| 331 | % --------------------------- Used for the ample sets (End) -----------------------------% | |
| 332 | ||
| 333 | ||
| 334 | ||
| 335 | ||
| 336 | /************************** DEPENDENCY RELATION (BEGIN) ******************************/ | |
| 337 | ||
| 338 | /* predicate which determines the dependency relations between all actions in the model, | |
| 339 | it should be called only once (prior to the model checking) */ | |
| 340 | ||
| 341 | :- dynamic enable_graph/1, dependent_act/4, enable_graph_options/1. | |
| 342 | ||
| 343 | :- use_module(probsrc(runtime_profiler),[start_profile/2, stop_profile/4]). | |
| 344 | ||
| 345 | compute_dependendency_relation_of_all_events_in_the_model(FindInvViolations,por(_TYPE,UseEnableGraph,Depth,_PGE),EnableGraph) :- | |
| 346 | Options = options(FindInvViolations,UseEnableGraph,Depth), | |
| 347 | % the graph format (ugraph or egraph with predicates) and the dependencies depend on these options | |
| 348 | (enable_graph_options(Options), enable_graph(EnableGraph) -> | |
| 349 | % In case the dependency relations and the enable graph are already computed. | |
| 350 | % Case possible if the user stopped the model checking for a while (and meanwhile didn't reloaded the model) | |
| 351 | % and in case that he wants to continue the verification of the model the enable graph don't have | |
| 352 | % to be computed again. | |
| 353 | true | |
| 354 | ; | |
| 355 | start_profile(partial_order_reduction,Timer), | |
| 356 | findall(Action, b_top_level_operation(Action),Actions), | |
| 357 | sort(Actions,AllActions), | |
| 358 | retractall(enable_graph(_)), retractall(enable_graph_options(_)), | |
| 359 | retractall(dependent_act(_,_,_,_)), % computed for other options | |
| 360 | debug_println(19,'********** DETERMINE ACTION DEPENDENCIES AND ENABLE GRAPH *************'), | |
| 361 | get_preference(timeout_cbc_analysis,Timeout), | |
| 362 | cputime(T1), | |
| 363 | ? | call_in_fresh_error_scope_for_one_solution( |
| 364 | determine_dependency_enabling_relations(AllActions,Timeout,FindInvViolations,UseEnableGraph,Depth,EnableGraph)), | |
| 365 | cputime(T2), | |
| 366 | debug_println(19,'********** FINISHED ANALYSIS *************'), | |
| 367 | D is T2-T1, formatsilent('Dependency Analysis Time: ~w ms~n',[D]), | |
| 368 | assertz(enable_graph(EnableGraph)), assertz(enable_graph_options(Options)), | |
| 369 | stop_profile(partial_order_reduction,compute_enable_graph,unknown,Timer) | |
| 370 | ), | |
| 371 | debug_println(9,enable_graph(EnableGraph)). | |
| 372 | ||
| 373 | determine_dependency_enabling_relations(AllActions,Timeout,FindInvViolations,UseEnableGraph,Depth,EnableGraph) :- | |
| 374 | determine_dependency_enabling_relations(AllActions,AllActions,Timeout,FindInvViolations,UseEnableGraph,Depth,Edges), | |
| 375 | (UseEnableGraph = true -> | |
| 376 | ? | enable_graph: vertices_edges_predicates_to_egraph(AllActions,Edges,EnableGraph) |
| 377 | ; % to be removed later (here we just want to compare the graph implementation's performances of enable_graph and ugraph) | |
| 378 | vertices_edges_to_ugraph(AllActions,Edges,EnableGraph) | |
| 379 | ). | |
| 380 | ||
| 381 | ||
| 382 | %determine_dependency_enabling_relations(AllActions,Timeout,FindInvViolations,Depth,EnableGraph) :- | |
| 383 | % determine_dependency_enabling_relations(AllActions,AllActions,Timeout,FindInvViolations,Depth,Edges), | |
| 384 | % enable_graph: vertices_edges_predicates_to_egraph(AllActions,Edges,EnableGraph). | |
| 385 | ||
| 386 | determine_dependency_enabling_relations([],_AllActions,_Timeout,_FindInvViolations,_UseEnableGraph,_Depth,[]). | |
| 387 | determine_dependency_enabling_relations([Act|Actions],AllActions,Timeout,FindInvViolations,UseEnableGraph,Depth,Edges) :- | |
| 388 | determine_dependency_enabling_relations1(AllActions,Act,Timeout,FindInvViolations,UseEnableGraph,Depth,Edges1), | |
| 389 | append(Edges1,Rest,Edges), | |
| 390 | determine_dependency_enabling_relations(Actions,AllActions,Timeout,FindInvViolations,UseEnableGraph,Depth,Rest). | |
| 391 | ||
| 392 | determine_dependency_enabling_relations1([],_Act,_Timeout,_FindInvViolations,_UseEnableGraph,_Depth,[]). | |
| 393 | determine_dependency_enabling_relations1([Act2|Acts],Act1,Timeout,FindInvViolations,UseEnableGraph,Depth,Result) :- | |
| 394 | ( (Act1==Act2,\+dependent_act(Act1,Act1,_Status,_)) -> | |
| 395 | assertz(dependent_act(Act1,Act1,self,true)), | |
| 396 | debug_println(9,dependent_act(Act1,Act1,self,true)) | |
| 397 | ; dependent_actions_symm(Act1,Act2,FindInvViolations,Timeout,Status,Coenabled) -> | |
| 398 | assertz(dependent_act(Act1,Act2,Status,Coenabled)), % the dependency relation is symmetric | |
| 399 | assertz(dependent_act(Act2,Act1,Status,Coenabled)), | |
| 400 | debug_println(9,dependent_act(Act1,Act2,Status,Coenabled)) | |
| 401 | ; | |
| 402 | true % no new dependencies have been discovered | |
| 403 | ), | |
| 404 | (may_enable(Act1,Act2,FindInvViolations,Timeout,UseEnableGraph,Depth,Edge) -> | |
| 405 | Result = [Edge|Res1] | |
| 406 | ; | |
| 407 | Result = Res1 | |
| 408 | ), | |
| 409 | %compute_if_coenabled(Act1,Act2,FindInvViolations,Timeout), | |
| 410 | determine_dependency_enabling_relations1(Acts,Act1,Timeout,FindInvViolations,UseEnableGraph,Depth,Res1). | |
| 411 | ||
| 412 | may_enable(Act1,Act2,FindInvViolations,Timeout,UseEnableGraph,_Depth,Edge) :- | |
| 413 | enable_analysis(Act1,Act2,FindInvViolations,UseEnableGraph,Timeout,Enable), | |
| 414 | (memberchk(Enable,[guaranteed,possible,possible_enable]) -> | |
| 415 | (UseEnableGraph = true -> | |
| 416 | Edge = Act1-b(truth,pred,[])-Act2 | |
| 417 | ; | |
| 418 | Edge = Act1-Act2 | |
| 419 | ) | |
| 420 | ; Enable = predicate(Expr) -> Edge = Act1-Expr-Act2 | |
| 421 | ; is_timeout(Enable) -> Edge = Act1-Act2 % assume it is possible to enable Act2 | |
| 422 | ; fail | |
| 423 | ). | |
| 424 | ||
| 425 | /* | |
| 426 | dependent_actions/2 determine statically or dynamically if two actions are dependent | |
| 427 | two events/actions are syntactically dependent in the following three cases: | |
| 428 | 1. If both events modify at least one common variable. | |
| 429 | 2. Act1 modifies a variable in the guard of Act2. (Act1 can enable or disable Act2) | |
| 430 | 3. Act2 modifies a variable in the guard of Act1. (Act2 can enable or disable Act1) | |
| 431 | */ | |
| 432 | ||
| 433 | %%% Act1 and Act2 are enabled in current state State, | |
| 434 | %%% we don't have to check if both actions are enabled in | |
| 435 | %%% some state | |
| 436 | dependent_actions_symm(Act1,Act2,_FindInvViolations,_Timeout,Status,Coenabled) :- | |
| 437 | dependent_act(Act1,Act2,Status,Coenabled),!,fail. % dependent relation is already computed | |
| 438 | dependent_actions_symm(Act1,Act2,FindInvViolations,Timeout,Status,Coenabled) :- | |
| 439 | dependent_actions_coenabled(Act1,Act2,FindInvViolations,Timeout,Status,Coenabled). | |
| 440 | ||
| 441 | dependent_actions_coenabled(Act1,Act2,FindInvViolations,Timeout,Status,Coenabled) :- | |
| 442 | dependent_actions1(Act1,Act2,FindInvViolations,Timeout,Res), | |
| 443 | ( Res == '-' -> dependent_actions_coenabled(Act2,Act1,FindInvViolations,Timeout,Status,Coenabled) % Res = '-' : not checked due to symmetry | |
| 444 | ; Res == syntactic_independent -> fail | |
| 445 | ; Res == independent -> fail | |
| 446 | ; Res == self_independent -> fail | |
| 447 | ; | |
| 448 | get_dependency_status(Res,Status), | |
| 449 | check_if_coenabled_nonfailing(Act1,Act2,FindInvViolations,Timeout,Coenabled) | |
| 450 | ). | |
| 451 | ||
| 452 | dependent_actions(Act1,Act2,FindInvViolations,Timeout,Status) :- | |
| 453 | dependent_actions1(Act1,Act2,FindInvViolations,Timeout,Res), | |
| 454 | ( Res == '-' -> dependent_actions(Act2,Act1,FindInvViolations,Timeout,Status) % Res = '-' : not checked due to symmetry | |
| 455 | ; Res == syntactic_independent -> fail | |
| 456 | ; Res == independent -> fail | |
| 457 | ; Res == self_independent -> fail | |
| 458 | ; get_dependency_status(Res,Status)). | |
| 459 | ||
| 460 | ||
| 461 | get_dependency_status(race_dependent,Status) :- !,Status=race. | |
| 462 | get_dependency_status(action_dependent,Status) :- !,Status=action. | |
| 463 | get_dependency_status(guard_dependent,Status) :- !,Status=guard. | |
| 464 | get_dependency_status(guard_dependent(Pred),Status) :- !,Status=predicate(Pred). | |
| 465 | get_dependency_status(dependent,Status) :- !,Status=general. | |
| 466 | get_dependency_status(self_dependent,Status) :- !,Status=general. % not sure this is correct | |
| 467 | get_dependency_status('=',Status) :- !, Status=general. | |
| 468 | get_dependency_status(DStatus,_) :- add_error_fail(get_dependency_status, 'Unknown dependency status: ', DStatus). | |
| 469 | ||
| 470 | % currently unused | |
| 471 | % computes which events are co-enabled | |
| 472 | % co-enabled relations is reflexive and symmetric | |
| 473 | %:- dynamic coenabled/2. | |
| 474 | %compute_if_coenabled(OpName1,OpName2,FindInvViolations,Timeout) :- | |
| 475 | % (OpName1 == OpName2 -> | |
| 476 | % assertz(coenabled(OpName1,OpName1)) | |
| 477 | % ;OpName2 @< OpName1 -> | |
| 478 | % true % due symmetry we skip the computation of coenabled events in that case | |
| 479 | % ;check_if_coenabled(OpName1,OpName2,FindInvViolations,Timeout) -> | |
| 480 | % assertz(coenabled(OpName1,OpName2)), | |
| 481 | % assertz(coenabled(OpName2,OpName1)) | |
| 482 | % ; % both events cannot be co-enabled | |
| 483 | % true). | |
| 484 | ||
| 485 | check_if_coenabled_nonfailing(OpName1,OpName2,FindInvViolations,Timeout,Result) :- | |
| 486 | ? | (check_if_coenabled(OpName1,OpName2,FindInvViolations,Timeout) -> |
| 487 | Result = true | |
| 488 | ; Result = false). | |
| 489 | ||
| 490 | :- use_module(cbcsrc(cbc_path_solver), [testcase_predicate_timeout/3]). | |
| 491 | check_if_coenabled(OpName1,OpName2,FindInvViolations,Timeout) :- | |
| 492 | get_guard(OpName1,PosGuard1), | |
| 493 | get_guard(OpName2,PosGuard2), | |
| 494 | conjunct_predicates([PosGuard1,PosGuard2],GuardsConj), % both events are enabled | |
| 495 | (FindInvViolations=1 -> Pred = pred(GuardsConj) ; Pred=typing(GuardsConj)), | |
| 496 | ? | testcase_predicate_timeout(Pred,Timeout,_R). |
| 497 | ||
| 498 | /************************** DEPENDENCY RELATION (END) ******************************/ | |
| 499 | ||
| 500 | ||
| 501 | /* | |
| 502 | ||
| 503 | The meaning of the following comments here is to give an idea how to validate the results from the Enabling analysis. | |
| 504 | We use here the Linear Time Logic (proposed by Amir Pnueli) in order to give LTL-formulas to each possible table entry result that can prove that | |
| 505 | the relation results between the operations of machine M produced by the Enabling analysis are correct. For instance, | |
| 506 | if the Enabling analysis says that it is impossible operation B to be preceded by operation A in the state space of M (i.e. the result in | |
| 507 | cell (A,B) of the table is 'impossible') then this property can be expressed by the LTL-formula "G ([a] => not X e(b))" and checked by | |
| 508 | the LTL model checker of ProB. | |
| 509 | ||
| 510 | * impossible: | |
| 511 | The impossibility an action 'a' to enable action 'b' can be expressed by means of LTL-formulas as follows: | |
| 512 | "G ([a] => not X e(b))" (dis) | |
| 513 | ||
| 514 | If a disables b then (dis) should be satisfied by the model. The result of the enabling analysis | |
| 515 | returns 'impossible' in cell (a,b) of the Enabling analysis table. | |
| 516 | ||
| 517 | * guaranteed: | |
| 518 | Action 'a' guaranteed enables action 'b' if there is an execution in the state space where after 'a' follows 'b' and | |
| 519 | 'a' and 'b' are never enabled simultaneously in the same state: | |
| 520 | "G ( ([a] => X e(b)) & (not (e(a) & e(b))) )" (en) | |
| 521 | ||
| 522 | * keep: | |
| 523 | Action 'a' keeps action 'b' enabled: | |
| 524 | "(G ((e(b) & [a]) => X e(b))) or (G ((not e(b) & [a]) => X not e(b)))" (keep) | |
| 525 | */ |