| 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(enabling_analysis,[reset_enabling_analysis/0, |
| 6 | | tcltk_cbc_cfg_analysis/1, tcltk_dot_cfg_analysis/1, |
| 7 | | tcltk_cbc_enabling_analysis/1, tcltk_cbc_enabling_analysis/2, |
| 8 | | tcltk_cbc_enabling_relations_for_operation/3, |
| 9 | | tcltk_cbc_enabling_relations_after_operation/3, compute_cbc_enable_rel/4, |
| 10 | | tcltk_cbc_simultaneous_enabling_analysis/1, |
| 11 | | tcltk_cbc_dependence_analysis/1, print_enable_table/1, |
| 12 | | eop_node_predicate/6,init_or_op/1, |
| 13 | | enabling_dot_graph_attrs/2, enabling_dot_node/4, enabling_dot_edge_attrs/4, |
| 14 | | feasible_operation/2, feasible_operation_with_timeout/3, |
| 15 | | check_if_feasible_operation/5, |
| 16 | | feasible_operation_cache/2,infeasible_operation/1, infeasible_operation_cache/1, |
| 17 | | cbc_enable_analysis/4, |
| 18 | | is_timeout_enabling_result/1, |
| 19 | | translate_enable_res/6, |
| 20 | | operation_sequence_possible/3, operation_can_be_enabled_by/4, |
| 21 | | partition_predicate/4, |
| 22 | | cbc_enable_analysis_cache/4 % for prologTasks |
| 23 | | ]). |
| 24 | | |
| 25 | | :- use_module(probsrc(module_information),[module_info/2]). |
| 26 | | :- module_info(group,cbc). |
| 27 | | :- module_info(description,'This module computes enabling relations for B operations.'). |
| 28 | | % -------------- |
| 29 | | |
| 30 | | :- use_module(probporsrc(static_analysis), [action_dependent_to_itself/4, get_conj_inv_predicate/3, is_timeout/1]). |
| 31 | | :- use_module(probsrc(bmachine), [b_top_level_operation/1]). |
| 32 | | :- use_module(probsrc(b_state_model_check),[get_negated_guard/3,get_negated_guard/4]). |
| 33 | | :- use_module(library(lists)). |
| 34 | | :- use_module(library(ordsets),[ord_intersect/2]). |
| 35 | | :- use_module(probsrc(error_manager)). |
| 36 | | :- use_module(probsrc(debug)). |
| 37 | | :- use_module(probsrc(bsyntaxtree), [conjunct_predicates/2]). |
| 38 | | :- use_module(probsrc(translate), [translate_bexpression/2, translate_bexpression_with_limit/3]). |
| 39 | | :- use_module(probsrc(b_read_write_info), |
| 40 | | [b_get_read_write/3,b_get_read_write_vars/5, b_get_operation_read_guard_vars/4]). |
| 41 | | :- use_module(probsrc(static_enabling_analysis),[static_cannot_enable/2, static_disabled_after/2]). |
| 42 | | :- use_module(cbcsrc(cbc_path_solver), [testcase_path_timeout_catch/9]). |
| 43 | | :- use_module(probsrc(tools),[ajoin/2,start_ms_timer/1,stop_ms_timer_with_debug_msg/2]). |
| 44 | | |
| 45 | | % -------------------- BINARY STATIC ANALYSIS ------------------------- |
| 46 | | |
| 47 | | % certain static analyses used by CBC Test case generation |
| 48 | | |
| 49 | | % return Res=impossible if from Invariant we can never execute OpName1 followed by OpName2 |
| 50 | | % is used by sap:check_operation_sequence_possible |
| 51 | | operation_sequence_possible(OpName1,OpName2,Res) :- |
| 52 | | static_disabled_after(OpName1,OpName2), % OpName1 always disables OpName2 |
| 53 | | !, |
| 54 | | Res = impossible. |
| 55 | | operation_sequence_possible(OpName1,OpName2,Res) :- |
| 56 | | static_cannot_enable(OpName1,OpName2), % OpName1 cannot enable OpName2 if it was disabled before |
| 57 | | !, |
| 58 | | Res = activation_independent. |
| 59 | | operation_sequence_possible(OpName1,OpName2,Res) :- |
| 60 | | % we assume OpName2 is feasible on its own |
| 61 | | get_negated_guard(OpName2,PosGuard2,_NegGuard2), |
| 62 | | Timeout1 = 200, |
| 63 | | % print('Guard: '),translate:print_bexpr(PosGuard2),nl, |
| 64 | | (testcase_path_timeout_catch(invariant,Timeout1,[OpName1],PosGuard2,_Csts,_Ops,_TestS,_TI,Res) |
| 65 | | -> true |
| 66 | | ; Res = impossible |
| 67 | | ). |
| 68 | | |
| 69 | | |
| 70 | | % return whether OpName1 can enable a previously disabled OpName2 |
| 71 | | operation_can_be_enabled_by(OpName1,OpName2,TIMEOUT,Res) :- |
| 72 | | b_top_level_operation(OpName1), |
| 73 | | b_get_read_write(OpName1,_Reads1,Writes1), |
| 74 | | b_top_level_operation(OpName2), |
| 75 | | formatsilent("~nCHECKING if ~w (~w) can enable ~w~n",[OpName1,Writes1,OpName2]), |
| 76 | | \+ static_cannot_enable(OpName1,OpName2), |
| 77 | | get_negated_guard(OpName2,PosGuard2,_NegGuard2,Precise), |
| 78 | | filter_predicate(PosGuard2,Writes1,FilteredPosGuard2), |
| 79 | | create_negation(FilteredPosGuard2,FilteredNegGuard2), |
| 80 | | % TIMEOUT used to be hardwired to 500 |
| 81 | | % print('Before: '), translate:print_bexpr(FilteredNegGuard2),nl,print('After: '), translate:print_bexpr(PosGuard2),nl, |
| 82 | | neg_guard_path(Precise,pred(FilteredNegGuard2),TIMEOUT,[OpName1],PosGuard2,Res), |
| 83 | | println_silent(can_be_enabled_result(Res)). |
| 84 | | |
| 85 | | % a search where the before or after predicate contains the negation of an operation's guard; |
| 86 | | % if the guard is imprecise (an over-approximation of the enabling condition) its negation is too strong |
| 87 | | % and a failing search proves nothing: we do not search and assume a path exists |
| 88 | | neg_guard_path(imprecise,_,_,_,_,Res) :- !, Res=imprecise_guard. |
| 89 | | neg_guard_path(_,Before,Timeout,Path,After,Res) :- |
| 90 | | testcase_path_timeout_catch(Before,Timeout,Path,After,_,_,_,_,Res). |
| 91 | | |
| 92 | | % true if the guard of an operation is precise, and thus also the variables read by its guard |
| 93 | | precise_guard_reads(OpName) :- b_get_operation_read_guard_vars(OpName,true,_,precise). |
| 94 | | |
| 95 | | |
| 96 | | |
| 97 | | % -------------------- CFG ANALYSIS ------------------------- |
| 98 | | :- use_module(dotsrc(dot_graph_generator), [gen_dot_graph/6, use_new_dot_attr_pred/7, dot_no_same_rank/1, dot_no_subgraph/3]). |
| 99 | | tcltk_dot_cfg_analysis(File) :- |
| 100 | | enabling_dot_graph_attrs('control flow graph',GraphAttrs), |
| 101 | | gen_dot_graph(File,GraphAttrs, |
| 102 | | use_new_dot_attr_pred(enabling_analysis:enabling_dot_node(with_init)), |
| 103 | | use_new_dot_attr_pred(enabling_analysis:cfg_dot_edge), |
| 104 | | dot_no_same_rank,dot_no_subgraph). |
| 105 | | |
| 106 | | :- public cfg_dot_edge/3. |
| 107 | | cfg_dot_edge(NodeID,SuccID,Attrs) :- |
| 108 | | cbc_quick_cfg_analysis(NodeID,SuccID,Res), |
| 109 | | cfg_res_kind(Res,NodeID,SuccID,Kind), |
| 110 | | enabling_dot_edge_kind_attrs(Kind,Attrs). |
| 111 | | |
| 112 | | cfg_res_kind(cannot_enable,_,_,_) :- !, fail. |
| 113 | | cfg_res_kind(syntactic_independent,X,Y,independent) :- !,X=Y. % only show if source & dest identical |
| 114 | | cfg_res_kind(race_dependent,X,Y,race_independent) :- !,X=Y. % only show if source & dest identical |
| 115 | | cfg_res_kind(possible,_,_,can_enable) :- !. |
| 116 | | cfg_res_kind(timeout_possible,_,_,timeout(can_enable)) :- !. |
| 117 | | cfg_res_kind(X,_,_,unknown(X)). |
| 118 | | |
| 119 | | :- use_module(probsrc(preferences),[get_preference/2]). |
| 120 | | op_node_predicate(NodeID,SubGraph,NodeDesc,Shape,Style,Color) :- |
| 121 | | get_preference(dot_enabling_show_readwrites,false), !, |
| 122 | | SubGraph=none, Shape=box, Style=none, |
| 123 | | b_top_level_operation(Op), NodeID = Op, NodeDesc=Op, Color=blue. |
| 124 | | op_node_predicate(NodeID,SubGraph,NodeDesc,Shape,Style,Color) :- SubGraph=none, |
| 125 | | Shape=record, Style=none, |
| 126 | | b_top_level_operation(Op), NodeID = Op, |
| 127 | | %NodeDesc = NodeID, |
| 128 | | b_get_read_write(Op,Reads2,Writes1), |
| 129 | | b_get_operation_read_guard_vars(Op,false,ReadsGrd,_Precise), |
| 130 | | insert_commas(ReadsGrd,0,R1), insert_commas(Reads2,0,R2), insert_commas(Writes1,0,W1), |
| 131 | | append(W1,['}|'],W2), |
| 132 | | append(R1,['\\n|reads: '|R2],Reads), |
| 133 | | append(Reads,['\\n|writes: '|W2],As), |
| 134 | | ajoin(['|{',Op,'\\n|reads (guard): '|As],NodeDesc), |
| 135 | | Color=blue. |
| 136 | | |
| 137 | | insert_commas([],_,[]). |
| 138 | | insert_commas([H],_,R) :- !, R=[H]. |
| 139 | | insert_commas([H|T],N,[H,Sep|IT]) :- |
| 140 | | (N>5 -> N1=0, Sep=',\\n' ; N1 is N+1, Sep=', '), |
| 141 | | insert_commas(T,N1,IT). |
| 142 | | |
| 143 | | % compute a Control Flow Graph very quickly |
| 144 | | % ideal when we have program counter style variables |
| 145 | | tcltk_cbc_cfg_analysis(list([list(['Origin'|SOps])|Result])) :- |
| 146 | | findall(Op, b_top_level_operation(Op), Ops), sort(Ops,SOps), |
| 147 | | findall(list([OpName1|EnableList]), |
| 148 | | (b_get_sorted_op(OpName1), |
| 149 | | findall(Possible,cbc_quick_cfg_analysis(OpName1,_OpName2,Possible),EnableList)), |
| 150 | | Result), |
| 151 | | (debug_mode(on) -> print_enable_table([list(['Origin'|SOps])|Result]) ; true). |
| 152 | | |
| 153 | | cbc_quick_cfg_analysis(OpName1,OpName2,Res) :- |
| 154 | | OpName1='INITIALISATION', |
| 155 | | b_top_level_operation(OpName2), |
| 156 | | b_get_read_write(OpName2,Reads2,Writes2), |
| 157 | | formatsilent(user_output,"COMPUTING CFG: INITIALISATION --> ~w r:~w / w:~w~n~n", |
| 158 | | [OpName2,Reads2,Writes2]), |
| 159 | | ( testcase_path_timeout_catch(init,250,[OpName2],b(truth,pred,[]),_Constants,_Ops,_TestS,_TI,R1) |
| 160 | | -> formatsilent(user_output," ~w can be enabled by ~w (~w)!~n",[OpName2,OpName1,R1]), |
| 161 | | (is_timeout(R1) -> Res=timeout_possible; Res = possible) |
| 162 | | ; formatsilent(user_output," ~w cannot be enabled by ~w!~n",[OpName2,OpName1]), |
| 163 | | Res = cannot_enable |
| 164 | | ). |
| 165 | | cbc_quick_cfg_analysis(OpName1,OpName2,Res) :- |
| 166 | | b_top_level_operation(OpName1), % top_level |
| 167 | | b_get_read_write(OpName1,Reads1,Writes1), |
| 168 | | %get_negated_guard(OpName1,PosGuard1,NegGuard1), |
| 169 | | b_get_sorted_op(OpName2), |
| 170 | | (b_get_read_write(OpName2,Reads2,Writes2), |
| 171 | | formatsilent(user_output,"COMPUTING CFG: ~w r:~w / w:~w --> ~w r:~w / w:~w~n~n", |
| 172 | | [OpName1,Reads1,Writes1,OpName2,Reads2,Writes2]), |
| 173 | | (\+ ord_intersect(Writes1,Reads2) |
| 174 | | -> (ord_intersect(Writes1,Writes2) |
| 175 | | -> Res = race_dependent, formatsilent(user_output," ~w cannot be enabled/disabled by ~w!~n",[OpName2,OpName1]) |
| 176 | | ; Res = syntactic_independent, formatsilent(user_output," ~w cannot be enabled/disabled/modified by ~w!~n",[OpName2,OpName1]) |
| 177 | | ) |
| 178 | | ; get_negated_guard(OpName2,PosGuard2,NegGuard2,Precise2), |
| 179 | | (debug_mode(on) -> print('Guard: '), translate:print_bexpr(PosGuard2),nl ; true), |
| 180 | | ((neg_guard_path(Precise2,typing(NegGuard2),60,[OpName1],PosGuard2,_R0), % quick check without invariants & properties |
| 181 | | % TO DO: project constants, variables onto the ones really needed |
| 182 | | neg_guard_path(Precise2,pred(NegGuard2),250,[OpName1],PosGuard2,R1)) |
| 183 | | -> formatsilent(user_output," ~w can be enabled by ~w (~w)!~n",[OpName2,OpName1,R1]), |
| 184 | | (is_timeout(R1) -> Res=timeout_possible; Res = possible) |
| 185 | | ; formatsilent(user_output," ~w cannot be enabled by ~w!~n",[OpName2,OpName1]), |
| 186 | | Res = cannot_enable) |
| 187 | | ) |
| 188 | | ). |
| 189 | | |
| 190 | | %use_module(enabling_analysis),enabling_analysis:cbc_quick_cfg_analysis('SEQUENCER2','SEQUENCER3',Res). |
| 191 | | %use_module(cbcsrc(sap)),use_module(enabling_analysis),enabling_analysis:cbc_quick_cfg_analysis('COMPUTE_SDDBs_points','prop_COMPUTE_0',Res). |
| 192 | | %use_module(cbcsrc(sap)),use_module(enabling_analysis),enabling_analysis:cbc_quick_cfg_analysis('COMPUTE_SDDBs_points','COMPUTE_SDDBs_length',Res). |
| 193 | | |
| 194 | | % -------------------- DEPENDENCE ANALYSIS ---------------------- |
| 195 | | /* |
| 196 | | |
| 197 | | Independency between two actions in a B/Event-B model can be expressed also by means of LTL-formulas: |
| 198 | | 1. Two actions a and b are independent if the following LTL-formula is satisfied by the model: |
| 199 | | "G ((e(b) & [a] => X e(b)) & (e(a) & [b] => X e(a)))" (ind) |
| 200 | | 2. If (ind) is violated by the model then a and b are dependent actions. |
| 201 | | |
| 202 | | Note: The race_dependent condition cannot be covered by the above LTL-formula. If two actions are race dependent, but never simultaniously enabled in a state |
| 203 | | from the state space, then (ind) will be satisfied. |
| 204 | | |
| 205 | | */ |
| 206 | | |
| 207 | | tcltk_cbc_dependence_analysis(list([list(['Origin'|SOps])|Result])) :- |
| 208 | | %findall(Op, b_get_machine_operation(Op,_,_,_), Ops), sort(Ops,SOps), |
| 209 | | findall(Op, b_top_level_operation(Op), Ops), sort(Ops,SOps), |
| 210 | | findall(list([OpName1|EnableList]), |
| 211 | | (b_get_sorted_op(OpName1), |
| 212 | | findall(Enable,cbc_dependence_analysis(OpName1,_OpName2,Enable),EnableList)), |
| 213 | | Result). |
| 214 | | %,print_enable_table([list(['Origin'|Ops])|Result]). |
| 215 | | |
| 216 | | b_get_sorted_op(Op) :- findall(Op, b_top_level_operation(Op), Ops), |
| 217 | | sort(Ops,SOps), member(Op,SOps). |
| 218 | | |
| 219 | | cbc_dependence_analysis(OpName1,OpName2,Res) :- |
| 220 | | b_top_level_operation(OpName1), |
| 221 | | b_get_read_write_vars(OpName1,GReads1,AReads1,Reads1,Writes1), |
| 222 | | b_get_sorted_op(OpName2), |
| 223 | | (OpName1=OpName2 -> action_dependent_to_itself(OpName1,GReads1,Writes1,Res) % TO DO: check if it is possible that for two different parameter values of the same event there is dependence |
| 224 | | ; OpName2 @< OpName1 -> Res = '-' % our checking is symmetric; only check one pair |
| 225 | | ; |
| 226 | | b_get_read_write_vars(OpName2,GReads2,AReads2,Reads2,Writes2), |
| 227 | | formatsilent("CHECKING DEPENDENCE: ~w gr:~w / ar:~w / w:~w <--> ~w gr:~w / ar:~w / w:~w~n",[OpName1,GReads1,AReads1,Writes1,OpName2,GReads2,AReads2,Writes2]), |
| 228 | | ( syntactical_independence(Reads1,Writes1,Reads2,Writes2) -> Res = syntactic_independent |
| 229 | | ; ord_intersect(Writes1,Writes2) -> Res = race_dependent |
| 230 | | ; (ord_intersect(AReads1,Writes2);ord_intersect(AReads2,Writes1)) -> Res = race_dependent |
| 231 | | % TO DO: in this case check if there is indeed a race dependence (e.g. in scheduler new and ready_active are actually independent !) |
| 232 | | % Set up [OpName1,OpName2], [OpName2,OpName1] and see if for ord_intersect(Writes1,Writes2) we can find different values |
| 233 | | ; |
| 234 | | % get constraints for G_{Op_1} \leadsto{Op_2} not(G_{Op_1}) |
| 235 | | get_negated_guard(OpName1,PosGuard1,NegGuard1,Precise1), |
| 236 | | get_conj_inv_predicate([NegGuard1],1,NegGuard1_Inv), |
| 237 | | % get constraints for G_{Op_2} \leadsto{Op_1} not(G_{Op_2}) |
| 238 | | get_negated_guard(OpName2,PosGuard2,NegGuard2,Precise2), |
| 239 | | get_conj_inv_predicate([NegGuard2],1,NegGuard2_Inv), |
| 240 | | conjunct_predicates([PosGuard1,PosGuard2],GuardsConj), |
| 241 | | (may_disable(Precise1,GReads1,Writes2,GuardsConj,OpName2,NegGuard1_Inv,R1) |
| 242 | | -> formatsilent(" ~w may disable ~w (~w)!~n",[OpName2,OpName1,R1]), |
| 243 | | (is_timeout(R1) -> Res=timeout_dependent; Res = dependent) |
| 244 | | ; may_disable(Precise2,GReads2,Writes1,GuardsConj,OpName1,NegGuard2_Inv,R2) |
| 245 | | -> formatsilent(" ~w may disable ~w (~w)!~n",[OpName1,OpName2,R2]), |
| 246 | | (is_timeout(R2) -> Res=timeout_dependent; Res = dependent) |
| 247 | | ; Res = independent))). |
| 248 | | |
| 249 | | % check if executing OpName2 (writing Writes2) can disable an operation with guard reads GReads1 and negated guard NegGuard1_Inv |
| 250 | | % for an imprecise guard GReads1 may be incomplete and NegGuard1_Inv too strong: we assume OpName2 may disable it |
| 251 | | may_disable(imprecise,_,Writes2,_,_,_,Res) :- !, Writes2 \= [], Res=imprecise_guard. |
| 252 | | may_disable(_,GReads1,Writes2,GuardsConj,OpName2,NegGuard1_Inv,Res) :- |
| 253 | | ord_intersect(GReads1,Writes2), |
| 254 | | testcase_path_timeout_catch(pred(GuardsConj),500,[OpName2],NegGuard1_Inv,_,_,_,_,Res). |
| 255 | | |
| 256 | | syntactical_independence(Reads1,Writes1,Reads2,Writes2) :- |
| 257 | | \+ ord_intersect(Writes1,Reads2), % op1 does not modify guard/effect of op2 |
| 258 | | \+ ord_intersect(Writes2,Reads1), % op2 does not modify guard/effect of op1 |
| 259 | | \+ ord_intersect(Writes1,Writes2). % no race condition |
| 260 | | |
| 261 | | % -------------- ENABLING ANALYSIS -------------- |
| 262 | | |
| 263 | | :- dynamic cbc_enable_analysis_cache/4. |
| 264 | | |
| 265 | | % check which operations can be enabled after executing another operation |
| 266 | | % to do: move maybe to another module; provide proper CSV export using Sebastian's modules |
| 267 | | cbc_enable_analysis(OpName1,OpName2,Enable,ExtraTimeout) :- |
| 268 | ? | init_or_op(OpName1), |
| 269 | | if(cbc_enable_analysis_cache(OpName1,OpName2,Result,ExtraTimeout), |
| 270 | | Result = Enable, % already computed |
| 271 | | (% now compute all enablings form OpName1 |
| 272 | ? | cbc_enable_analysis_calc(OpName1,Op2,R,ExtraTimeout), |
| 273 | | assertz(cbc_enable_analysis_cache(OpName1,Op2,R,ExtraTimeout)), |
| 274 | | fail |
| 275 | | ; |
| 276 | | % now look up: |
| 277 | ? | cbc_enable_analysis_cache(OpName1,OpName2,Enable,ExtraTimeout) |
| 278 | | )). |
| 279 | | |
| 280 | | |
| 281 | | cbc_enable_analysis_calc(OpName1,OpName2,Enable,ExtraTimeout) :- |
| 282 | | OpName1='INITIALISATION',printsilent('CHECKING ENABLING AFTER INITIALISATION'),nls, |
| 283 | ? | b_top_level_operation(OpName2), |
| 284 | | printsilent('INITIALISATION'), printsilent(' ---> '), printsilent(OpName2), printsilent(' :: '), |
| 285 | | add_to_time_out(ExtraTimeout,200,Timeout), |
| 286 | | ( testcase_path_timeout_catch(init,Timeout,[OpName2],b(truth,pred,[]),_Constants,_Ops,_TestS,_TI,R) |
| 287 | | -> printsilent(R), printsilent(' : '), |
| 288 | | get_negated_guard(OpName2,_,NegGuard,Precise), |
| 289 | | (neg_guard_path(Precise,init,Timeout,[],NegGuard,R2) |
| 290 | | -> printsilent(R2), printsilent(' : '),Enable=possible |
| 291 | | ; Enable=guaranteed) |
| 292 | | ; Enable=impossible), |
| 293 | | printsilent(Enable),nls. |
| 294 | | cbc_enable_analysis_calc(OpName1,OpName2,Enable,ExtraTimeout) :- |
| 295 | | b_top_level_operation(OpName1), |
| 296 | | b_get_read_write(OpName1,Reads1,Writes1), |
| 297 | | %b_get_operation_read_guard_vars(OpName1,true,Reads1,_Precise), |
| 298 | | %get_negated_guard(OpName1,PosGuard1,_NegGuard1), |
| 299 | | formatsilent("CHECKING ENABLING AFTER: ~w r:~w / w:~w~n",[OpName1,Reads1,Writes1]), |
| 300 | ? | b_top_level_operation(OpName2), |
| 301 | | formatsilent('~w ---> ~w :: ',[OpName1,OpName2]), |
| 302 | | cbc_enable_calc_aux(OpName1,Writes1,OpName2,Enable,ExtraTimeout), |
| 303 | | formatsilent('Enable=~w~n',[Enable]). |
| 304 | | |
| 305 | | :- use_module(probporsrc(static_analysis),[syntactic_independence/3]). |
| 306 | | cbc_enable_calc_aux(OpName1,_Writes1,_OpName2,Enable,ExtraTimeout) :- |
| 307 | | (ExtraTimeout>500 -> infeasible_operation(OpName1) |
| 308 | | ; infeasible_operation_cache(OpName1)), % only check cached version |
| 309 | | !, |
| 310 | | Enable=infeasible. |
| 311 | | cbc_enable_calc_aux(_OpName1,_Writes1,OpName2,Enable,ExtraTimeout) :- |
| 312 | | (ExtraTimeout>500 -> infeasible_operation(OpName2) |
| 313 | | ; infeasible_operation_cache(OpName2)), % only check cached version |
| 314 | | !, |
| 315 | | Enable=impossible_infeasible. |
| 316 | | cbc_enable_calc_aux(OpName1,Writes1,OpName2,Enable,_) :- |
| 317 | | % first check if we can syntactically determine independence; this relies on the guard reads of OpName2 |
| 318 | | % which are incomplete for an imprecise guard; then we can only use the full reads of OpName2 |
| 319 | | (precise_guard_reads(OpName2) -> true |
| 320 | | ; b_get_read_write(OpName2,Reads2,_), \+ ord_intersect(Writes1,Reads2)), |
| 321 | | syntactic_independence(OpName1,OpName2,Res), |
| 322 | | !, |
| 323 | | Enable=Res. |
| 324 | | cbc_enable_calc_aux(OpName1,Writes1,OpName2,Enable,ExtraTimeout) :- |
| 325 | | % now we do the semantic checks |
| 326 | | add_to_time_out(ExtraTimeout,200,Timeout1), |
| 327 | | add_to_time_out(ExtraTimeout,300,Timeout2), |
| 328 | | |
| 329 | | get_negated_guard(OpName2,PosGuard2,NegGuard2,Precise), |
| 330 | | filter_predicate(PosGuard2,Writes1,FilteredPosGuard2), |
| 331 | | create_negation(FilteredPosGuard2,FilteredNegGuard2), |
| 332 | | cbc_enable_calc_aux2(OpName1,OpName2,Enable,Timeout1,Timeout2,Precise,PosGuard2,NegGuard2,FilteredNegGuard2). |
| 333 | | % TO DO: return timeout results and compute timeout info here |
| 334 | | |
| 335 | | |
| 336 | | % Precise=imprecise: PosGuard2 is an over-approximation: a failing search with PosGuard2 is still conclusive, |
| 337 | | % but not a failing search with NegGuard2 or FilteredNegGuard2 (see neg_guard_path) |
| 338 | | cbc_enable_calc_aux2(OpName1,OpName2,Enable,Timeout1,Timeout2,Precise,PosGuard2,NegGuard2,FilteredNegGuard2) :- |
| 339 | | %format('Check if ~w can be enabled after ~w ~n',[OpName2,OpName1]), |
| 340 | | %print(' Pos Guard: '),translate:print_bexpr(PosGuard2),nl, |
| 341 | | %((OpName1=winc,OpName2=winc) -> trace ; true), |
| 342 | | testcase_path_timeout_catch(invariant,Timeout1,[OpName1],PosGuard2,_Csts,_Ops,_TestS,_TI,R), % advantage over version with [OpName1,OpName2] : one less state to setup and enumerate; but inner guards may not be checked |
| 343 | | !, |
| 344 | | printsilent(can_be_enabled_after(R)), printsilent(' : '), |
| 345 | | % print('Neg Guard: '),translate:print_bexpr(NegGuard2),nl, |
| 346 | | % TO DO: first check whether OpName2 can be disabled given Invariant ? |
| 347 | | (neg_guard_path(Precise,invariant,Timeout1,[OpName1],NegGuard2,R2) |
| 348 | | -> printsilent(can_be_disabled_after(R2)), printsilent(' : '), |
| 349 | | % TO DO: test if NegGuard2 holds initially it is possible to execute [OpName1,OpName2]; if not: OpName1 cannot enable OpName2, only keep it -> Enable=keep_possible |
| 350 | | % then we could check if OpName1 can disable OpName2: PosGuard2,[OpName1],NegGuard2 |
| 351 | | (OpName1\=OpName2, % otherwise OpName2 must be enabled if [OpName1] can be executed |
| 352 | | %testcase_path_timeout_catch(pred(NegGuard2),Timeout2,[OpName1,OpName2],b(truth,pred,[]),_,_,_,_,R3) |
| 353 | | neg_guard_path(Precise,pred(NegGuard2),Timeout2,[OpName1],PosGuard2,R3) |
| 354 | | -> printsilent(can_enable(R3)), printsilent(' : '), |
| 355 | | %nl, translate:print_bexpr(PosGuard2),nl,print(OpName1),nl,translate:print_bexpr(NegGuard2),nl, |
| 356 | | %print('FILTERED: '), translate:print_bexpr(FilteredNegGuard2),nl, |
| 357 | | (neg_guard_path(Precise,pred(PosGuard2),Timeout2,[OpName1],FilteredNegGuard2,R4) |
| 358 | | -> printsilent(can_disable(R4)), printsilent(' : '), |
| 359 | | (contains_timeout([R,R2,R3,R4]) -> Enable=timeout_possible ; Enable=possible) |
| 360 | | ; printsilent('cannot_disable : '), |
| 361 | | (contains_timeout([R,R2,R3]) -> Enable=timeout_possible_enable ; Enable=possible_enable) /* Opname cannot disable OpName2; only enable it */ |
| 362 | | ) |
| 363 | | ; /* OpName1 cannot enable OpName2; only preserve it */ |
| 364 | | printsilent('cannot_enable : '), |
| 365 | | (neg_guard_path(Precise,pred(PosGuard2),Timeout2,[OpName1],FilteredNegGuard2,R4) |
| 366 | | -> printsilent(can_disable(R4)), printsilent(' : '), |
| 367 | | (contains_timeout([R,R2,R4]) -> Enable=timeout_possible_disable; Enable=possible_disable) |
| 368 | | ; (contains_timeout([R,R2]) -> Enable=timeout_keep; Enable=keep) /* Opname cannot enable or disable */ |
| 369 | | ) |
| 370 | | ) |
| 371 | | ; (is_timeout(R) -> Enable=timeout_guaranteed; Enable=guaranteed) |
| 372 | | ). |
| 373 | | cbc_enable_calc_aux2(OpName1,_OpName2,Enable,_Timeout1,Timeout2,Precise,PosGuard2,_NegGuard2,FilteredNegGuard2) :- |
| 374 | | % OpName2 can never be enabled after OpName1; check whether it can be enabled before |
| 375 | | % Note: we could replace FilteredNegGuard2 by truth |
| 376 | | neg_guard_path(Precise,pred(PosGuard2),Timeout2,[OpName1],FilteredNegGuard2,R4), |
| 377 | | !, |
| 378 | | printsilent(can_disable(R4)), printsilent(' : '), |
| 379 | | (is_timeout(R4) -> Enable=timeout_impossible; Enable=impossible). |
| 380 | | cbc_enable_calc_aux2(_OpName1,_OpName2,Enable,_,_,_Precise,_PosGuard2,_NegGuard2,_FilteredNegGuard2) :- |
| 381 | | % OpName2 can never ben enabled after nor before |
| 382 | | Enable=impossible_keep. |
| 383 | | |
| 384 | | |
| 385 | | % check if contains timeout or similar result |
| 386 | ? | contains_timeout(List) :- member(R,List), is_timeout(R), !. |
| 387 | | |
| 388 | | :- use_module(probsrc(self_check)). |
| 389 | | :- assert_must_succeed(contains_timeout([ok,virtual_time_out])). |
| 390 | | :- assert_must_succeed(contains_timeout([time_out])). |
| 391 | | :- assert_must_succeed(contains_timeout([ok,timeout])). |
| 392 | | :- assert_must_fail(contains_timeout([ok,imprecise_guard])). |
| 393 | | |
| 394 | | |
| 395 | | :- use_module(probsrc(bsyntaxtree),[conjunction_to_list/2,some_id_occurs_in_expr/2,create_negation/2]). |
| 396 | | % remove all predicates which are not modified |
| 397 | | filter_predicate(Pred,ModifiedVars,FilteredPred) :- partition_predicate(Pred,ModifiedVars,FilteredPred,_). |
| 398 | | |
| 399 | | % partition all predicates into those which are not modified and those that can be |
| 400 | | partition_predicate(Pred,Var,FilteredPred,Rest) :- var(Var),!, |
| 401 | | add_internal_error('Missing write variables:',partition_predicate(Pred,Var,FilteredPred,Rest)), |
| 402 | | FilteredPred=Pred, Rest=b(truth,pred,[]). |
| 403 | | partition_predicate(Pred,ModifiedVars,FilteredPred,Rest) :- |
| 404 | | conjunction_to_list(Pred,List), |
| 405 | | partition(can_be_modified(ModifiedVars),List,FilteredList,[],UnFilteredList), |
| 406 | | conjunct_predicates(FilteredList,FilteredPred), |
| 407 | | conjunct_predicates(UnFilteredList,Rest). |
| 408 | | |
| 409 | | can_be_modified(ModifiedVars,Pred,Res) :- |
| 410 | | some_id_occurs_in_expr(ModifiedVars,Pred),!, Res='<'. |
| 411 | | can_be_modified(_,_,'>'). |
| 412 | | |
| 413 | | |
| 414 | | |
| 415 | | :- use_module(probsrc(preferences),[get_time_out_preference_with_factor/2, add_time_outs/3]). |
| 416 | | |
| 417 | | add_to_time_out(Extra,OriginalTimeout,New) :- |
| 418 | | (OriginalTimeout < 300 % default for timeout_cbc_analysis is 300 |
| 419 | | -> E is (Extra * OriginalTimeout) // 300 |
| 420 | | ; E is Extra |
| 421 | | ), |
| 422 | | add_time_outs(OriginalTimeout,E,New). |
| 423 | | |
| 424 | | tcltk_cbc_enabling_analysis(L) :- |
| 425 | | get_time_out_preference_with_factor(0.1,TOF), |
| 426 | | (TOF > 250 -> ExtraTO is TOF-250 ; ExtraTO = 0), |
| 427 | | tcltk_cbc_enabling_analysis(ExtraTO,L). |
| 428 | | tcltk_cbc_enabling_analysis(ExtraTimeout,list([list(['Origin'|Ops])|Result])) :- |
| 429 | | findall(Op, b_top_level_operation(Op), Ops), |
| 430 | | %statistics(walltime,[WT1,_]), |
| 431 | | findall(list([OpName1|EnableList]), |
| 432 | | (init_or_op(OpName1), |
| 433 | | findall(Enable,cbc_enable_analysis(OpName1,_OpName2,Enable,ExtraTimeout),EnableList)), |
| 434 | | Result). |
| 435 | | %statistics(walltime,[WT2,_]), Time is WT2-WT1, |
| 436 | | %format('Runtime for enabling analysis: ~w ms~n',[Time]) |
| 437 | | %,print_enable_table([list(['Origin'|Ops])|Result]). |
| 438 | | |
| 439 | | % ----------------- |
| 440 | | % compute the four edges of the enable relation for a particular operation |
| 441 | | tcltk_cbc_enabling_relations_for_operation(OpName2,ExtraTimeout, |
| 442 | | list([list(['Event','Enable','KeepEnabled','Disable','KeepDisabled'])|Results])) :- |
| 443 | | findall(list([OpName1|ResOp2]), |
| 444 | | compute_cbc_enable_rel(OpName1,OpName2,ExtraTimeout,ResOp2), Results). |
| 445 | | tcltk_cbc_enabling_relations_after_operation(OpName1,ExtraTimeout, |
| 446 | | list([list(['Event','Enable','KeepEnabled','Disable','KeepDisabled'])|Results])) :- |
| 447 | | findall(list([OpName2|ResOp2]), |
| 448 | | compute_cbc_enable_rel(OpName1,OpName2,ExtraTimeout,ResOp2), Results). |
| 449 | | |
| 450 | | % compute the four edges of the enable relation for a pair of events: |
| 451 | | compute_cbc_enable_rel(OpName1,OpName2,ExtraTimeout,[Enable,KeepEnabled,Disable,KeepDisabled]) :- |
| 452 | | b_top_level_operation(OpName1), |
| 453 | | b_get_read_write(OpName1,_,Writes1), |
| 454 | | b_get_operation_read_guard_vars(OpName1,true,Reads1,_Precise), |
| 455 | | formatsilent("CHECKING ENABLING RELATION AFTER: ~w r:~w / w:~w~n",[OpName1,Reads1,Writes1]), |
| 456 | | b_top_level_operation(OpName2), |
| 457 | | get_negated_guard(OpName2,PosGuard2,_NegGuard2,Precise), |
| 458 | | partition_predicate(PosGuard2,Writes1,FilteredPosGuard2,StaticPosGuard2), |
| 459 | | create_negation(FilteredPosGuard2,FilteredNegGuard2), |
| 460 | | Timeout1 is 300+ExtraTimeout, |
| 461 | | formatsilent('~w ---> ~w :: ',[OpName1,OpName2]), |
| 462 | | %print(' FPOS: '),translate:print_bexpr(FilteredPosGuard2),nl, |
| 463 | | %print('Neg Guard: '),translate:print_bexpr(NegGuard2),nl, |
| 464 | | %print(' Filtered: '),translate:print_bexpr(FilteredNegGuard2),nl, |
| 465 | | % TO DO: use syntactic conditions |
| 466 | | my_testcase_path_timeout(pred(PosGuard2),Timeout1,[OpName1],FilteredPosGuard2,KeepEnabled), |
| 467 | | my_neg_testcase_path_timeout(Precise,pred(PosGuard2),Timeout1,[OpName1],FilteredNegGuard2,Disable), |
| 468 | | (OpName1==OpName2 -> KeepDisabled=false, Enable=false, |
| 469 | | formatsilent(' KeepEnabled=~w, Disable=~w~n',[KeepEnabled,Disable]) |
| 470 | | ; conjunct_predicates([StaticPosGuard2,FilteredNegGuard2],NegEnableGuard2), % as StaticPosGuard2 must be same before after: it must be true before to be true after |
| 471 | | my_neg_testcase_path_timeout(Precise,pred(NegEnableGuard2),Timeout1,[OpName1],FilteredPosGuard2,Enable), |
| 472 | | % this is the unoptimised call: |
| 473 | | %my_testcase_path_timeout(pred(NegGuard2),Timeout1,[OpName1],NegGuard2,KeepDisabled), |
| 474 | | % we do two calls for KeepDisabled: pred(NegStaticGuard2) true after, or StaticGuard2 & NegFiltered -> NegFiltered |
| 475 | | create_negation(StaticPosGuard2,StaticNegGuard2), |
| 476 | | my_neg_testcase_path_timeout(Precise,pred(StaticNegGuard2),Timeout1,[OpName1],b(truth,pred,[]),KeepDisabled1), |
| 477 | | (KeepDisabled1 = ok -> KeepDisabled=ok |
| 478 | | ; my_neg_testcase_path_timeout(Precise,pred(NegEnableGuard2),Timeout1,[OpName1],FilteredNegGuard2,KeepDisabled) |
| 479 | | ), |
| 480 | | formatsilent(' KeepEnabled=~w, Disable=~w, Enable=~w, KeepDisabled=~w~n', |
| 481 | | [KeepEnabled,Disable,Enable,KeepDisabled]) |
| 482 | | ). |
| 483 | | |
| 484 | | % a search involving the negated guard: inconclusive for imprecise guards (see neg_guard_path) |
| 485 | | my_neg_testcase_path_timeout(imprecise,_,_,_,_,Result) :- !, Result=imprecise_guard. |
| 486 | | my_neg_testcase_path_timeout(_,Before,Timeout,Path,After,Result) :- |
| 487 | | my_testcase_path_timeout(Before,Timeout,Path,After,Result). |
| 488 | | |
| 489 | | my_testcase_path_timeout(Before,Timeout,Path,After,Result) :- |
| 490 | | (testcase_path_timeout_catch(Before,Timeout,Path,After,_,_,_,_,Result) -> true |
| 491 | | ; Result = false). |
| 492 | | |
| 493 | | % ---------------------------------------------- |
| 494 | | |
| 495 | | |
| 496 | | init_or_op('INITIALISATION'). |
| 497 | ? | init_or_op(OpName1) :- b_top_level_operation(OpName1). %b_get_machine_operation(OpName1,_,_,_). |
| 498 | | |
| 499 | | % print table in CSV format |
| 500 | | print_enable_table(R) :- print_enable_table_list(R). |
| 501 | | print_enable_table_list([]) :- nl,nl. |
| 502 | | print_enable_table_list([list(L)|T]) :- |
| 503 | | print_csv_list(L), print_enable_table_list(T). |
| 504 | | print_csv_list([]) :- !,nl. |
| 505 | | print_csv_list([H]) :- !,print(H),nl. |
| 506 | | print_csv_list([H|T]) :- !,print(H), print(','), print_csv_list2(T). |
| 507 | | print_csv_list(X) :- print(illegal_list(X)),nl, add_internal_error('Illegal list: ', print_csv_list(X)). |
| 508 | | |
| 509 | | print_csv_list2([H]) :- !,print_enable_info(H),nl. |
| 510 | | print_csv_list2([H|T]) :- !,print_enable_info(H), print(','), print_csv_list2(T). |
| 511 | | print_csv_list2(X) :- print(illegal_list(X)),nl, add_internal_error('Illegal list: ', print_csv_list(X)). |
| 512 | | |
| 513 | | print_enable_info(I) :- (translate_enable_info(I,TI) -> print(TI) ; print_for_csv(I)). |
| 514 | | % simplify result further for output to CSV; reason: also testing (e.g., test 1360 which sometimes causes overflow on Mac, and time_out on Linux) |
| 515 | | translate_enable_info(timeout,unknown). |
| 516 | | translate_enable_info(time_out,unknown). |
| 517 | | translate_enable_info(overflow,unknown). |
| 518 | | translate_enable_info(virtual_time_out,unknown). |
| 519 | | |
| 520 | | print_for_csv([]) :- !, print('{}'). |
| 521 | | print_for_csv([H|T]) :- !, % print list without commas , |
| 522 | | format('{~w',[H]), maplist(print_csv_el,T), print('}'). |
| 523 | | print_for_csv(Term) :- print(Term). |
| 524 | | print_csv_el(H) :- format(' ~w',[H]). |
| 525 | | |
| 526 | | |
| 527 | | % ---------- create enable graph ------------------ |
| 528 | | |
| 529 | | eop_node_predicate(NodeID,SubGraph,NodeDesc,Shape,Style,black) :- |
| 530 | | op_node_predicate(NodeID,SubGraph,NodeDesc,Shape,Style,_Color). |
| 531 | | eop_node_predicate('INITIALISATION',SubGraph,'INITIALISATION',Shape,Style,Color) :- SubGraph=none, |
| 532 | | Shape=hexagon, Style=none, Color=olivedrab2. |
| 533 | | |
| 534 | | translate_enable_res(Rel,_,_,'impossible',red,_) :- impossible(Rel),!, fail. |
| 535 | | translate_enable_res(guaranteed,_,_,'guaranteed',olivedrab2,solid) :- !. |
| 536 | | translate_enable_res(Rel,X,Y,TransRel,TCol,bold) :- independent(Rel,Col), |
| 537 | | !,X=Y, % only show if source & dest identical |
| 538 | | TransRel=Rel,TCol=Col. |
| 539 | | translate_enable_res(possible,_,_,'possible',black,solid) :- !. |
| 540 | | translate_enable_res(keep,_,_,'keep',lightgray,solid) :- !. |
| 541 | | translate_enable_res(possible_enable,_,_,'(enable)',black,solid) :- !. |
| 542 | | translate_enable_res(possible_disable,_,_,'(disable)',lightblue,solid) :- !. |
| 543 | | translate_enable_res(timeout_keep,_,_,'keep?',tomato,dashed) :- !. |
| 544 | | translate_enable_res(timeout_possible,_,_,'possible?',tomato,dashed) :- !. |
| 545 | | translate_enable_res(timeout_possible_disable,_,_,'(disable?)',tomato,dashed) :- !. |
| 546 | | translate_enable_res(timeout_possible_enable,_,_,'(enable?)',tomato,dashed) :- !. |
| 547 | | translate_enable_res(pred(Expr),_,_,String,black,dashed) :- !, |
| 548 | | translate_bexpression(Expr,String). |
| 549 | | translate_enable_res(predicate(Expr),_,_,String,black,dashed) :- !, |
| 550 | | translate_bexpression(Expr,String). |
| 551 | | translate_enable_res(dependent,_,_,dependent,green,solid) :- !. |
| 552 | | translate_enable_res(race_dependent,_,_,race_dependent,green,solid) :- !. |
| 553 | | translate_enable_res(guard_dependent,_,_,guard_dependent,olive,dashed) :- !. |
| 554 | | translate_enable_res(action_dependent,_,_,action_dependent,green,dashed) :- !. |
| 555 | | translate_enable_res(X,_,_,'independent',lightgray,solid) :- |
| 556 | | memberchk(X,[independent,syntactic_independent,syntactic_fully_independent,syntactic_unchanged]),!. |
| 557 | | translate_enable_res(X,_,_,X,red,solid). |
| 558 | | |
| 559 | | |
| 560 | | impossible(impossible). |
| 561 | | impossible(impossible_keep). |
| 562 | | impossible(impossible_disable). |
| 563 | | impossible(impossible_infeasible). |
| 564 | | impossible(infeasible). |
| 565 | | |
| 566 | | independent(independent,yellow). |
| 567 | | independent(syntactic_keep,lightgray). |
| 568 | | independent(syntactic_independent,lightgray). |
| 569 | | independent(syntactic_fully_independent,lightgray). |
| 570 | | independent(syntactic_unchanged,lightgray). |
| 571 | | independent(activation_independent,lightgray). |
| 572 | | |
| 573 | | % ---------- new style dot graph support for enabling/dependence/CFG graphs ------------------ |
| 574 | | % shared by dot_graphs_static_analysis, enable_graph and the CFG analysis above |
| 575 | | |
| 576 | | :- use_module(probsrc(tools),[html_escape/2]). |
| 577 | | :- use_module(probsrc(tools_strings),[ajoin_with_sep/3]). |
| 578 | | |
| 579 | | % graph attributes for gen_dot_graph/6; Kind is used in the title |
| 580 | | enabling_dot_graph_attrs(Kind,[graph_name/enabling_graph, no_page_size, fontname/'Helvetica', |
| 581 | | nodesep/0.5, ranksep/0.6, |
| 582 | | node_defaults/[fontname/'Helvetica', shape/box, style/'rounded,filled', margin/'0.1,0.05'], |
| 583 | | edge_defaults/[fontname/'Helvetica', fontsize/9, arrowsize/0.8] |
| 584 | | | TitleAttrs]) :- |
| 585 | | (get_preference(dot_print_graph_title,true), bmachine:b_machine_name(Main) |
| 586 | | -> ajoin([Main,' - ',Kind],Title), |
| 587 | | get_preference(dot_title_font_size,TFS), TitleAttrs = [label/Title, labelloc/t, fontsize/TFS] |
| 588 | | ; TitleAttrs = []). |
| 589 | | |
| 590 | | % enabling_dot_node(InitOpt,NodeID,SubGraph,Attrs): one node per operation (+ INITIALISATION if InitOpt=with_init) |
| 591 | | :- public enabling_dot_node/4. |
| 592 | | enabling_dot_node(_,Op,none,[label/htmllabel(Label), tooltip/Tooltip, fillcolor/'#E3EDF7', color/'#1F5F99']) :- |
| 593 | | b_top_level_operation(Op), |
| 594 | | (get_preference(dot_enabling_show_readwrites,false) |
| 595 | | -> html_escape(Op,EOp), ajoin(['<<b>',EOp,'</b>>'],Label), Tooltip=Op |
| 596 | | ; b_get_read_write(Op,Reads,Writes), |
| 597 | | b_get_operation_read_guard_vars(Op,false,ReadsGrd,_Precise), |
| 598 | | html_escape(Op,EOp), |
| 599 | | maplist(rw_row,['guard reads','reads','writes'],[ReadsGrd,Reads,Writes],Rows), |
| 600 | | ajoin(['<<table border="0" cellborder="0" cellspacing="0" cellpadding="1">', |
| 601 | | '<tr><td colspan="2"><b>',EOp,'</b></td></tr><hr/>'|Rows],L0), |
| 602 | | ajoin([L0,'</table>>'],Label), |
| 603 | | maplist(rw_text,[ReadsGrd,Reads,Writes],[TG,TR,TW]), |
| 604 | | ajoin([Op,'\nguard reads: ',TG,'\nreads: ',TR,'\nwrites: ',TW],Tooltip) |
| 605 | | ). |
| 606 | | enabling_dot_node(with_init,'INITIALISATION',none, |
| 607 | | [label/htmllabel('<<b>INITIALISATION</b>>'), tooltip/'INITIALISATION', |
| 608 | | fillcolor/'#E4F2E5', color/'#2E7D32', shape/box, style/'filled,bold']). |
| 609 | | |
| 610 | | rw_text([],'-') :- !. |
| 611 | | rw_text(L,T) :- ajoin_with_sep(L,', ',T). |
| 612 | | |
| 613 | | rw_row(Title,Ids,Row) :- |
| 614 | | rw_html(Ids,Html), |
| 615 | | ajoin(['<tr><td align="right" valign="top"><font point-size="9" color="#707070">',Title,' ', |
| 616 | | '</font></td><td align="left" balign="left"><font point-size="10">',Html,'</font></td></tr>'],Row). |
| 617 | | |
| 618 | | rw_html([],'<font color="#A0A0A0">–</font>') :- !. |
| 619 | | rw_html(Ids,Html) :- maplist(html_escape,Ids,EIds), wrap_ids(EIds,0,Parts), ajoin(Parts,Html). |
| 620 | | |
| 621 | | % insert line breaks after about 30 characters |
| 622 | | wrap_ids([],_,[]). |
| 623 | | wrap_ids([H],_,[H]) :- !. |
| 624 | | wrap_ids([H|T],Len,[H,Sep|R]) :- |
| 625 | | atom_length(H,HL), Len1 is Len+HL+2, |
| 626 | | (Len1 > 30 -> Sep=',<br/>', Len2=0 ; Sep=', ', Len2=Len1), |
| 627 | | wrap_ids(T,Len2,R). |
| 628 | | |
| 629 | | % translate an enabling result (as computed by cbc_enable_analysis, enable_analysis, ...) into dot edge attributes |
| 630 | | % fails if no edge should be drawn |
| 631 | | enabling_dot_edge_attrs(Res,X,Y,Attrs) :- |
| 632 | | enable_res_kind(Res,X,Y,Kind), |
| 633 | | enabling_dot_edge_kind_attrs(Kind,Attrs). |
| 634 | | |
| 635 | | enable_res_kind(Rel,_,_,_) :- impossible(Rel),!, fail. |
| 636 | | enable_res_kind(guaranteed,_,_,guaranteed) :- !. |
| 637 | | enable_res_kind(Rel,X,Y,independent) :- independent(Rel,_), !, X=Y. % only show if source & dest identical |
| 638 | | enable_res_kind(possible,_,_,possible) :- !. |
| 639 | | enable_res_kind(keep,_,_,keep) :- !. |
| 640 | | enable_res_kind(possible_enable,_,_,possible_enable) :- !. |
| 641 | | enable_res_kind(possible_disable,_,_,possible_disable) :- !. |
| 642 | | enable_res_kind(timeout_keep,_,_,timeout(keep)) :- !. |
| 643 | | enable_res_kind(timeout_possible,_,_,timeout(possible)) :- !. |
| 644 | | enable_res_kind(timeout_possible_disable,_,_,timeout(possible_disable)) :- !. |
| 645 | | enable_res_kind(timeout_possible_enable,_,_,timeout(possible_enable)) :- !. |
| 646 | | enable_res_kind(timeout,_,_,timeout(possible)) :- !. |
| 647 | | enable_res_kind(pred(Expr),_,_,predicate(Expr)) :- !. |
| 648 | | enable_res_kind(predicate(Expr),_,_,predicate(Expr)) :- !. |
| 649 | | enable_res_kind(Dep,_,_,Dep) :- memberchk(Dep,[dependent,race_dependent,guard_dependent,action_dependent]), !. |
| 650 | | enable_res_kind(X,_,_,unknown(X)). |
| 651 | | |
| 652 | | % enabling_dot_edge_kind_attrs(Kind,Attrs) |
| 653 | | enabling_dot_edge_kind_attrs(Kind,Attrs) :- |
| 654 | | edge_kind(Kind,Label,Col,Style,PW,LText,Order), !, |
| 655 | | (get_preference(dot_print_legend,true), LText \= '' |
| 656 | | -> Attrs0 = [legend/LText, legend_order/Order], ShowLabel=false |
| 657 | | ; Attrs0 = [], ShowLabel=true), |
| 658 | | edge_label_attrs(Kind,Label,ShowLabel,Col,LAttrs), |
| 659 | | (PW=1 -> PAttrs=[] ; PAttrs=[penwidth/PW]), |
| 660 | | append([[color/Col, style/Style],PAttrs,LAttrs,Attrs0],Attrs). |
| 661 | | |
| 662 | | % labels of edges with a condition or a timeout are always shown; the others only if there is no legend |
| 663 | | edge_label_attrs(predicate(Expr),_,_,Col,[label/htmllabel(HL), fontcolor/Col, tooltip/Tooltip]) :- !, |
| 664 | | translate_bexpression_with_limit(Expr,250,S), |
| 665 | | html_escape(S,ES), |
| 666 | | atom_codes(ES,Codes), split_conj_codes(Codes,Parts), |
| 667 | | ajoin_with_sep(Parts,' &<br/>',Body), |
| 668 | | ajoin(['<',Body,'>'],HL), |
| 669 | | ajoin(['enabled if: ',S],Tooltip). |
| 670 | | edge_label_attrs(timeout(_),Label,_,Col,[label/Label, fontcolor/Col, tooltip/Label]) :- !. |
| 671 | | edge_label_attrs(unknown(_),Label,_,Col,[label/Label, fontcolor/Col, tooltip/Label]) :- !. |
| 672 | | edge_label_attrs(_,Label,true,Col,[label/Label, fontcolor/Col, tooltip/Label]) :- !. |
| 673 | | edge_label_attrs(_,Label,_,_,[tooltip/Label]). |
| 674 | | |
| 675 | | split_conj_codes(Codes,[P|Rest]) :- % split at conjunctions (also nested ones; good enough for display) |
| 676 | | append(Prefix,Tail,Codes), append(PC," & ",Prefix), !, |
| 677 | | atom_codes(P,PC), split_conj_codes(Tail,Rest). |
| 678 | | split_conj_codes(Codes,[P]) :- atom_codes(P,Codes). |
| 679 | | |
| 680 | | % edge_kind(Kind,Label,Colour,Style,PenWidth,LegendText,LegendOrder) |
| 681 | | edge_kind(guaranteed,guaranteed,'#2E7D32',solid,2,'guaranteed to enable',10). |
| 682 | | edge_kind(possible,possible,'#1F5F99',solid,1,'can enable',20). |
| 683 | | edge_kind(possible_enable,enable,'#1F5F99',solid,1,'can enable',20). |
| 684 | | edge_kind(can_enable,'','#1F5F99',solid,1,'can enable (control flow)',20). |
| 685 | | edge_kind(predicate(_),condition,'#6A1B9A',dashed,1,'enables under condition',30). |
| 686 | | edge_kind(keep,keep,gray60,solid,1,'keeps enabled',40). |
| 687 | | edge_kind(possible_disable,disable,'#C62828',solid,1,'can disable',50). |
| 688 | | edge_kind(dependent,dependent,'#C06040',solid,1,'dependent',60). |
| 689 | | edge_kind(race_dependent,race,'#C62828',solid,1,'race dependent',61). |
| 690 | | edge_kind(action_dependent,action,'#1F5F99',dashed,1,'action dependent',62). |
| 691 | | edge_kind(guard_dependent,guard,'#8A6D1E',dashed,1,'guard dependent',63). |
| 692 | | edge_kind(independent,independent,gray70,dotted,1,'independent of itself',70). |
| 693 | | edge_kind(race_independent,'race independent',gray70,dotted,1,'race independent of itself',71). |
| 694 | | edge_kind(timeout(K),Label,'#E65100',dashed,1,'unknown (timeout)',80) :- |
| 695 | | (edge_kind(K,KL,_,_,_,_,_), KL \= '' -> true ; KL = enable), |
| 696 | | ajoin([KL,'?'],Label). |
| 697 | | edge_kind(unknown(X),X,red,solid,1,'',90). |
| 698 | | |
| 699 | | is_timeout_enabling_result(timeout_keep). |
| 700 | | is_timeout_enabling_result(timeout_possible). |
| 701 | | is_timeout_enabling_result(timeout_possible_disable). |
| 702 | | is_timeout_enabling_result(timeout_possible_enable). |
| 703 | | is_timeout_enabling_result(timeout). |
| 704 | | is_timeout_enabling_result(timeout_dependent). |
| 705 | | is_timeout_enabling_result(timeout_guaranteed). |
| 706 | | is_timeout_enabling_result(timeout_impossible). |
| 707 | | |
| 708 | | % Simultaneous Enabling Analysis |
| 709 | | % Find events which cannot be enabled simultaneously |
| 710 | | % Usage: |
| 711 | | % a) if one event is known to be enabled we do not need to check the impossible ones |
| 712 | | % b) if one event is known to be disabled we do not need check events which guarantee it |
| 713 | | |
| 714 | | % use_module(enabling_analysis), tcltk_cbc_simultaneous_enabling_analysis(R). |
| 715 | | |
| 716 | | tcltk_cbc_simultaneous_enabling_analysis(list([list(['Origin'|SOps])|Result])) :- |
| 717 | | findall(Op, b_top_level_operation(Op), Ops), |
| 718 | | sort(Ops,SOps), % sort operations to ensure nice triangular display |
| 719 | | statistics(walltime,[WT1,_]), |
| 720 | | findall(list([OpName1|EnableList]), |
| 721 | | (member(OpName1,SOps), |
| 722 | | findall(SimulRes,tcltk_simult(OpName1,SOps,SimulRes),EnableList)), |
| 723 | | Result), |
| 724 | | statistics(walltime,[WT2,_]), Time is WT2-WT1, |
| 725 | | formatsilent('Runtime for simultaneous enabling analysis: ~w ms~n',[Time]) |
| 726 | | . %,print_enable_table([list(['Origin'|Ops])|Result]). |
| 727 | | |
| 728 | | tcltk_simult(Op1,SOps,Res) :- member(Op2,SOps),tcltk_simult2(Op1,Op2,Res). |
| 729 | | tcltk_simult2(Op1,Op2,Res) :- |
| 730 | | Op1 = Op2, % Op2 @=< Op1, % the simultaneous impossible analysis is symmetric: TO DO: do not compute/display info twice |
| 731 | | !, |
| 732 | | Res = '-'. |
| 733 | | tcltk_simult2(Op1,Op2,TRes) :- |
| 734 | | simult_enable_analysis(Op1,Op2,1,fixed_time_out(150),TRes). |
| 735 | | |
| 736 | | simult_enable_analysis(OpName1,OpName2,UseInvariant,TimeoutFactor,Res) :- |
| 737 | | b_top_level_operation(OpName1), |
| 738 | | b_top_level_operation(OpName2), |
| 739 | | %OpName2 @>OpName1, |
| 740 | | get_negated_guard(OpName1,PosGuard1,_NegGuard1), |
| 741 | | get_negated_guard(OpName2,PosGuard2,NegGuard2,Precise2), |
| 742 | | get_conj_inv_predicate([PosGuard1,PosGuard2],UseInvariant,Pred), |
| 743 | | %print(checking_simultaneous(OpName1,OpName2)),nl, |
| 744 | | (solve_predicate_with_chr(Pred,_State,TimeoutFactor,[],RR) |
| 745 | | -> %print(state(_State)),nl, print(result(RR)),nl, |
| 746 | | ((RR = contradiction_found ; RR = no_solution_found(unfixed_deferred_sets)) |
| 747 | | -> Res = impossible % we do not compute PosGuard1,NegGuard2 in this case: otherwise this would mean that Operation 1 can never fire ! |
| 748 | | ; Precise2 = imprecise -> % NegGuard2 too strong: we cannot establish that OpName1 guarantees OpName2 |
| 749 | | combine_simult_result(RR,imprecise_guard,Res) |
| 750 | | ; get_conj_inv_predicate([PosGuard1,NegGuard2],UseInvariant,Pred2), |
| 751 | | solve_predicate_with_chr(Pred2,_State2,TimeoutFactor,[],RR2), |
| 752 | | combine_simult_result(RR,RR2,Res)) |
| 753 | | ; add_internal_error('Solver failed for simultaneous enabling analysis: ',OpName1/OpName2), |
| 754 | | Res = internal_error). |
| 755 | | |
| 756 | | :- assert_must_succeed(combine_simult_result(solution(x),error,error)). |
| 757 | | :- assert_must_succeed(combine_simult_result(error,contradiction_found,error)). |
| 758 | | :- assert_must_succeed(combine_simult_result(solution(x),contradiction_found,guaranteed)). |
| 759 | | combine_simult_result(A,B,R) :- (A==error ; B==error), !, R=error. |
| 760 | | combine_simult_result(solution(_),solution(_),R) :- !,R=possible. |
| 761 | | combine_simult_result(solution(_),imprecise_guard,R) :- !,R=possible. |
| 762 | | combine_simult_result(solution(_),B,R) :- |
| 763 | | (B = contradiction_found ; B = no_solution_found(unfixed_deferred_sets)), !, |
| 764 | | R=guaranteed. |
| 765 | | combine_simult_result(A,B,Res) :- functor(A,FA,_), functor(B,FB,_), |
| 766 | | ajoin([FA,'_',FB],Res). |
| 767 | | |
| 768 | | % --------------------------------------------------- |
| 769 | | |
| 770 | | :- use_module(probsrc(eventhandling),[register_event_listener/3]). |
| 771 | | :- register_event_listener(specification_initialised,reset_enabling_analysis, |
| 772 | | 'Initialise module enabling analysis.'). |
| 773 | | |
| 774 | | :- dynamic feasible_operation_cache/2. |
| 775 | | reset_enabling_analysis :- |
| 776 | | %retractall(disable_graph(_)), |
| 777 | | retractall(feasible_operation_cache(_,_)), |
| 778 | | retractall(cbc_enable_analysis_cache(_,_,_,_)). |
| 779 | | |
| 780 | | % in principle you should only call this for Op with operation_name_not_yet_covered(Op) |
| 781 | | feasible_operation(Op,Res) :- var(Op), !, b_top_level_operation(Op), feasible_operation(Op,Res). |
| 782 | | feasible_operation(Op,Res) :- feasible_operation_cache(Op,CR),!,Res=CR. |
| 783 | | feasible_operation(Op,Res) :- UseInvariant=1, |
| 784 | | check_if_feasible_operation(Op,UseInvariant,fixed_time_out(500),CR,_), |
| 785 | | assertz(feasible_operation_cache(Op,CR)), |
| 786 | | Res=CR. |
| 787 | | |
| 788 | | feasible_operation_with_timeout(Op,TimeOut,Res) :- var(Op), !, |
| 789 | ? | b_top_level_operation(Op), feasible_operation_with_timeout(Op,TimeOut,Res). |
| 790 | | feasible_operation_with_timeout(Op,TimeOut,Res) :- feasible_operation_cache(Op,CR), |
| 791 | | (\+ is_timeout(CR) ; TimeOut =< 500), % feasible_operation/2 uses 500; recompute timeouts with a larger timeout |
| 792 | | !, Res=CR. |
| 793 | | feasible_operation_with_timeout(Op,TimeOut,Res) :- check_if_feasible_operation(Op,1,fixed_time_out(TimeOut),CR,_), |
| 794 | | retractall(feasible_operation_cache(Op,_)), % must be a timeout result if it was asserted before |
| 795 | | assertz(feasible_operation_cache(Op,CR)), |
| 796 | | Res=CR. |
| 797 | | |
| 798 | | |
| 799 | | :- use_module(probsrc(state_space),[operation_not_yet_covered/1]). |
| 800 | | infeasible_operation(Op) :- var(Op), !, b_top_level_operation(Op), infeasible_operation(Op). |
| 801 | | infeasible_operation(Op) :- feasible_operation_cache(Op,CR),!, CR=impossible. |
| 802 | | infeasible_operation(Op) :- operation_not_yet_covered(Op), |
| 803 | | feasible_operation(Op,impossible). % will also assert feasible_operation_cache |
| 804 | | |
| 805 | | infeasible_operation_cache(Op) :- feasible_operation_cache(Op,impossible). |
| 806 | | |
| 807 | | :- use_module(probsrc(solver_interface), [solve_predicate/5]). |
| 808 | | solve_predicate_with_chr(Pred,State,TimeoutFactor,Options,RR) :- |
| 809 | | solve_predicate(Pred,State,TimeoutFactor,['CHR','CLPFD','SMT'|Options],RR). |
| 810 | | |
| 811 | | :- use_module(probsrc(debug), [debug_mode/1]). |
| 812 | | :- use_module(probsrc(store),[normalise_store/2]). |
| 813 | | % check if an operation is feasible |
| 814 | | % UseInvariant = 1 : assume invariant |
| 815 | | check_if_feasible_operation(OpName1,UseInvariant,TimeoutFactor,Res,NormalisedResultState) :- |
| 816 | | b_top_level_operation(OpName1), |
| 817 | | (debug_mode(on) |
| 818 | | -> print(checking_feasibility(OpName1)),nl,tools:start_ms_timer(TIMER) ; true), |
| 819 | | if(check_if_feasible_operation2(OpName1,UseInvariant,TimeoutFactor,Res,State),true, |
| 820 | | (add_internal_error('Failed:',check_if_feasible_operation2(OpName1,UseInvariant,TimeoutFactor,Res,State)),fail)), |
| 821 | | % TO DO: add missing variables and put into order |
| 822 | | normalise_enabling_store(State,NormalisedResultState), |
| 823 | | %b_interpreter:sort_variable_binding(NormalisedResultState,SortedStore), |
| 824 | | stop_ms_timer_with_debug_msg(TIMER,feasible(OpName1)). |
| 825 | | |
| 826 | | normalise_enabling_store('$UNKNOWN_STATE',R) :- !, R='$UNKNOWN_STATE'. |
| 827 | | normalise_enabling_store(CState,State) :- normalise_store(CState,State). |
| 828 | | |
| 829 | | :- use_module(probsrc(tools_timeout),[get_time_out_with_factor/2]). |
| 830 | | check_if_feasible_operation2(OpName1,UseInvariant,TimeoutFactor,Res,ResultState) :- |
| 831 | | get_negated_guard(OpName1,PosGuard1,_NegGuard1,Precise), |
| 832 | | % format('Guard is ~w for ~w~n',[Precise,OpName1]), |
| 833 | | Precise = precise, |
| 834 | | !, |
| 835 | | % Warning: guard is under approximation for sequential composition, while loops, ... |
| 836 | | get_conj_inv_predicate([PosGuard1],UseInvariant,Pred), |
| 837 | | (debug_mode(on) -> translate:nested_print_bexpr(Pred),nl ; true), |
| 838 | | % visualize_graph:print_predicate_dependency_as_graph_for_dot(Pred,'~/Desktop/out.dot'), |
| 839 | | (solve_predicate_with_chr(Pred,State,TimeoutFactor,[full_machine_state],RR) |
| 840 | | -> %print(state(_State)),nl, print(result(RR)),nl, |
| 841 | | ((RR=no_solution_found(unfixed_deferred_sets) ; RR = contradiction_found) |
| 842 | | -> Res = impossible, ResultState = '$UNKNOWN_STATE' |
| 843 | | %,format('Computing unsat core for infeasible ~w~n',[OpName1]),unsat_cores:unsat_core(Pred,no_solution_found,Core),print('CORE: '),nl,translate:nested_print_bexpr_as_classicalb(Core),nl |
| 844 | | ; RR=solution(_) -> Res = possible, |
| 845 | | (debug_mode(on) -> print('SOLUTION: '),translate:print_bstate(State),nl ; true), |
| 846 | | ResultState = State |
| 847 | | ; simplify_for_output(RR,Res), ResultState = '$UNKNOWN_STATE' ) |
| 848 | | ; add_internal_error('Failed to compute feasibility for: ',OpName1), |
| 849 | | translate:print_bexpr(Pred),nl, |
| 850 | | Res = internal_error, ResultState = '$UNKNOWN_STATE'). |
| 851 | | check_if_feasible_operation2(OpName1,UseInvariant,TimeoutFactor,Res,ResultState) :- |
| 852 | | formatsilent('Guard computation is imprecise for ~w. Not using predicate solver (but B interpreter).~n',[OpName1]), |
| 853 | | % this version uses sap:testcase_path: this works for things like sequential composition and WHILE loops better |
| 854 | | get_time_out_with_factor(TimeoutFactor,TO), |
| 855 | | (UseInvariant=1 -> INV=invariant ; INV=typing), |
| 856 | | (testcase_path_timeout_catch(INV,TO,[OpName1],b(truth,pred,[]),_Constants,Ops,StateSequence,_TI,R1) |
| 857 | | -> (is_timeout(R1) -> Res=timeout ; Res = possible), |
| 858 | | (nonvar(StateSequence),StateSequence=[ResultState|_] -> true ; ResultState = '$UNKNOWN_STATE'), |
| 859 | | (debug_level_inactive_for(19) -> true |
| 860 | | ; print(feasibility_result(OpName1,R1,Ops)),nl, |
| 861 | | (ResultState = '$UNKNOWN_STATE' -> true |
| 862 | | ; print('SOLUTION: '),translate:print_bstate(ResultState),nl) |
| 863 | | ) |
| 864 | | ; Res=impossible,ResultState = '$UNKNOWN_STATE'). |
| 865 | | |
| 866 | | |
| 867 | | % translate a solve_predicate result (other than solution(_), contradiction_found) for output |
| 868 | | simplify_for_output(no_solution_found(enumeration_warning(_,_,_,_,_)),R) :- !, R=virtual_time_out. |
| 869 | | simplify_for_output(no_solution_found(clpfd_overflow),R) :- !, R=overflow. |
| 870 | | simplify_for_output(no_solution_found(_),R) :- !, R=virtual_time_out. |
| 871 | | simplify_for_output(time_out,R) :- !, R=time_out. |
| 872 | | simplify_for_output(overflow,R) :- !, R=overflow. |
| 873 | | simplify_for_output(error,R) :- !, R=error. % errors occurred during solving, e.g., well-definedness errors |
| 874 | | simplify_for_output(Other,R) :- add_internal_error('Unknown solver result:',Other), R=unknown. |
| 875 | | |
| 876 | | :- assert_must_succeed(simplify_for_output(error,error)). |
| 877 | | :- assert_must_succeed(simplify_for_output(overflow,overflow)). |
| 878 | | :- assert_must_succeed(simplify_for_output(no_solution_found(clpfd_overflow),overflow)). |
| 879 | | :- assert_must_succeed(simplify_for_output(no_solution_found(enumeration_warning(a,b,c,d,critical)),virtual_time_out)). |
| 880 | | |