1 % (c) 2012-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(ample_sets, [
6 compute_ample_set2/3,
7 clear_dynamic_predicates_for_POR/0,
8 reset_runtime_dynamic_predicates_POR/0, % now done via event-handling
9 set_por_visible_properties/3, % called by the model checker
10 stutter_action/1, visible_action/1,check_cycle_proviso/0 % exported for ltl.pl for asserting and retracting stutter/visible actions
11 ]).
12
13 /* SICSTUS Libraries */
14 :- use_module(library(ordsets)).
15 :- use_module(library(random)).
16 :- use_module(library(lists)).
17 :- use_module(library(ugraphs)).
18 %:- use_module(library(timeout)).
19
20 /* B module sources */
21 :- use_module(probsrc(bmachine),[b_get_invariant_from_machine/1,b_operation_preserves_full_invariant/1]).
22 :- use_module(probsrc(bsyntaxtree),[conjunction_to_list/2,occurs_in_expr/2]).
23 :- use_module(probsrc(specfile),[prepare_state_for_specfile_trans/3,
24 specfile_possible_trans_name_for_successors/2,specfile_trans/6]).
25 :- use_module(probsrc(state_space),[visited_expression/2,use_no_timeout/1]).
26
27 /* POR modules */
28 :- use_module(probporsrc(enable_graph)).
29 % --> /* The Static Analysis' module */
30 :- use_module(probporsrc(static_analysis),[enable_graph/1,dependent_act/4]).
31
32 /* Importing predicates for handling the states of a respective B/Event-B machine */
33 :- use_module(probsrc(specfile),[state_corresponds_to_initialised_b_machine/2]).
34 :- use_module(probsrc(store), [empty_state/1]).
35
36 /* Other modules from ProB */
37 :- use_module(probsrc(debug),[debug_println/2]).
38 :- use_module(probsrc(preferences), [get_preference/2]).
39 :- use_module(probsrc(error_manager), [time_out_with_enum_warning_for_findall/3, add_warning/3]).
40
41 % Cosider to remove this import
42 :- use_module(probsrc(b_read_write_info),[b_get_read_write/3]).
43
44 /* Modules and Infos for the code coverage analysis */
45 :- use_module(probsrc(module_information)).
46 :- module_info(group,model_checker).
47 :- module_info(description,'This module contains predictes for partial order reduction using the ample sets\' theory. It is only applicable in B mode (not CSP||B).').
48
49 % Dynamic predicates for keeping track of visible and stutter actions,
50 % as well as of all dependent reations between the actions of the B model
51 :- dynamic stutter_action/1,visible_action/1,enables/2,explored_state/1,check_cycle_proviso/0.
52 :- dynamic por_invariant_not_visible/0, por_visible_predicate/1.
53
54
55 :- public reset_static_dynamic_predicates_POR/0.
56 clear_dynamic_predicates_for_POR :-
57 reset_static_dynamic_predicates_POR,
58 reset_runtime_dynamic_predicates_POR.
59
60 :- use_module(probsrc(eventhandling),[register_event_listener/3]).
61 :- register_event_listener(clear_specification,clear_dynamic_predicates_for_POR,
62 'Reset Ample Sets.').
63 :- register_event_listener(reset_specification,clear_dynamic_predicates_for_POR,
64 'Reset Ample Sets.'). % the state space is reset: explored_state/1 etc. refer to old state ids
65
66 % predicates that will be asserted once on every loading of the machine
67 reset_static_dynamic_predicates_POR :-
68 retractall(dependent_act(_,_,_,_)),
69 retractall(enable_graph(_)).
70
71 % predicates that will be asserted at runtime
72 reset_runtime_dynamic_predicates_POR :-
73 retractall(por_invariant_not_visible),
74 retractall(por_visible_predicate(_)),
75 retractall(stutter_action(_)),
76 retractall(visible_action(_)),
77 retractall(explored_state(_)),
78 retractall(enables(_,_)),
79 retractall(check_cycle_proviso). % used to not track in the enable graph
80
81 /* This implementation of the ample sets approach sastisfies all four conditions of the ample sets theory (not proved yet).
82 The conditions are taken from the book "Principles of Model Checking" written by C.Baier and J.P.Katoen.
83
84 For every state s in the state space, where Act(s) stands for all enabled actions in state s, the following conditions
85 must be satisfied:
86
87 1. Nonemptiness condition (A1) and terminal states
88 (A 1.1) ample(s) <: Act(s) (set equality is possible)
89 (A 1.2) ample(s) = {} <=> Act(s) = {}
90
91 2. Dependency condition
92 (A 2) Let s -a_{1}> s_{1} -a_{2}> ... -a_{n}> s_{n} -a> s' be a finite execution
93 fragment in the original state space. If a depends on ample(s), then a_{i}:ample(s) for some 0<i<=n.
94
95 This condition can be satisfied by the following conditions (Local Criteria):
96 (A 2.1) Any action b: Act(s)\ample(s) is independent of any a:ample(s) (Here we use the dependency graph generated from the Enabling Analysis)
97 (A 2.2) Any disabled action b:Act\en(s) which is dependent on ample(s) cannot become enabled by a possible sequence of actions starting with an action
98 from en(s)\ample(s). (We check this condition by means of using the enable graph generated from the Enabling Analysis)
99
100 Note that determining the local criteria's conditions above is dependent on the formalism on which we are applying the partial order reduction method.
101 The conditions (A 2.1) and (A 2.2) are adjusted to the semantics of the B method/Event-B. For other formalisms such as CSP we will need to define
102 the conditions differently. On the one hand because we cannot use the Constraint Based Checker of ProB (it is not implemented for CSP yet), on the other hand
103 because CSP differs semantically from B/Event-B (the process structure). An idea to apply Partial Order Reduction on CSP is to make the reduction
104 on the CSP operator-level ('|||', [|X|] and so on). For more detailed description of how to apply POR on CSP read the following papers:
105 - H. Wehrheim. Partial order reductions for failure refinement.
106 - J. Sung, Y. Liu and J.S. Dong. Model Checking CSP Revisited: Introducing a Process Analysis Toolkit.
107
108 3. Stutter condition
109 (A 3) If ample(s) /= Act(s) then any action t:ample(s) is a stutter action.
110
111 4. Cycle condition
112 (A 4) For any cycle s_{0}s_{1}...s_{n} in the reduced state space and actions t:Act(s_{i}), for some
113 0 < i <= n, there exists j:{1..n} such that t:ample(s_{j}). (s_{0},...,s_{n} are states)
114
115 */
116
117 % compute_ample_set2(+CurID,+CurState,+POROptions)
118 %
119 % The main predicate for computing ample sets by respecting the four ample set conditions specified above.
120 % A subset of all possible enabled outgoing transitions in CurState will be computed and added to the state space. The predicate is similar
121 % to the compute_all_transitions/2 predicate implemented in tcltk_interface.pl.
122
123 :- use_module(probsrc(succeed_max),[reset_max_reached/0, max_reached/0]).
124 :- use_module(probsrc(state_space),[assert_max_reached_for_node/1]).
125 :- use_module(probsrc(value_persistance),[save_constants/1, add_new_transitions_to_cache/1]).
126
127 % Arguments:
128 % CurID - the current ID of the state.
129 % CurState - the current evaluation of all global variables (and constants) in the machine.
130 % FindInvViolations (obsolete) - possible values 1 (option setted) and 0 (option not setted). Conditions (A 3) and (A 4) checked if check_cycle_proviso/0 is set.
131 % POROptions - the options being set for exploring the state space by means of partial order reduction
132 %
133 compute_ample_set2(CurID,CurState,por(TYPE,UseEnableGraph,Depth,PGE)) :-
134 reset_max_reached,
135 prepare_state_for_specfile_trans(CurState,CurID,PreparedCurState),
136 get_enabled_actions_at_state(CurID,PreparedCurState,TopLevelActions,Transitions,_DisabledTopLevelActions),
137 % we want only unique action names for analyzing and computing of ample sets
138 list_to_ord_set(TopLevelActions,TopLevelActionsSet),
139 % compute a sound ample set from all actions enabled in state CurID
140 ? compute_ample_actions(CurID,TopLevelActionsSet,por(TYPE,UseEnableGraph,Depth,PGE),AmpleSet),
141 debug_println(20,sets(CurID,allActions(TopLevelActionsSet),ampleSet(AmpleSet))),
142 % mark state as visited (Cycle Condition (A 4))
143 assertz(explored_state(CurID)),
144 ( AmpleSet \= [] ->
145 % compute just the actions from the ample set (case of state space reduction)
146 % AmpleSet is a non-empty subset of enabled(CurID), AmpleSet can be equal to AllEnabledActions
147 ? add_transitions(AmpleSet,CurID,Transitions,_ActNameSuccessors)
148 ;
149 true /* DEADLOCK */
150 ),
151 (max_reached ->
152 assert_max_reached_for_node(CurID),
153 MaxReached=true
154 ; MaxReached=false
155 ),
156 (CurID==root ->
157 save_constants(MaxReached)
158 ; add_new_transitions_to_cache(CurID)
159 ),!. % once we have computed the ample set in CurState we don't want to backtrack
160
161 % computing a sound ample set of actions.
162 compute_ample_actions(CurID,AllEnabledActions,POROptions,AmpleSet) :-
163 % get_ample_actions/5 call only reasonable to be executed when more than one actions are enabled in CurID
164 (more_than_one_event(AllEnabledActions) ->
165 ? get_ample_actions(CurID,AllEnabledActions,POROptions,AmpleSet)
166 ;
167 AmpleSet = AllEnabledActions
168 ).
169
170 more_than_one_event(SetOfActions) :-
171 length(SetOfActions,L),L>1.
172
173 /************************** ADD TRANSITIONS (BEGIN) ******************************/
174
175 % +ActNameSuccessors: a list of tuples (Act,SuccIds), where 'SuccIds' is the list of all state ids that are successor states of Act
176 add_transitions(AmpleSet,CurID,Transitions,ActNameSuccessors) :-
177 ? add_transitions(no,AmpleSet,CurID,Transitions,[],ActNameSuccessors).
178
179 % first argument tests if the state has been fully explored while adding the
180 % ample set transitions to the state space ('yes' means state has been fully explored, so we can stop,
181 % 'no' means that the rest of the ample set transitions should be added to the state space until
182 % no more ample actions in the list or until the state is fully explored)
183 add_transitions(yes,_AmpleSet,_CurID,_Transitions,ActNameSuccessors,ActNameSuccessors).
184 add_transitions(_FullyExplored,[],_CurID,_Transitions,ActNameSuccessors,ActNameSuccessors).
185 add_transitions(no,[ActionName|ActNames],CurID,Transitions,ActNameSuccessorsGen,ActNameSuccessors) :-
186 add_action_name_transitions(Transitions,CurID,ActionName,[],RemTransitions,FullyExplored,ActNameSuccessorsGen,ActNameSuccessorsGen1),
187 ? add_transitions(FullyExplored,ActNames,CurID,RemTransitions,ActNameSuccessorsGen1,ActNameSuccessors).
188
189 add_action_name_transitions([],_CurID,_ActionName,RemTransitions,RemTransitions,no,ActSuccsGen,ActSuccsGen).
190 add_action_name_transitions([(ActionName1,Transitions)|R],CurID,ActionName,IgnoredActions_so_far,
191 RemTransitions,FullyExplored,ActSuccsGen,ActSuccs) :-
192 (ActionName == ActionName1 ->
193 % in this case we do not need to go through the rest of the transitions list
194 add_action_transitions(ActionName,Transitions,CurID,NewIds),
195 ActSuccsGen1 = [(ActionName,NewIds)|ActSuccsGen],
196 append(IgnoredActions_so_far,R,AllRemainedActions),
197 ? (cycle_condition_check_positive_l(NewIds) ->
198 debug_println(9,'*********** POSSIBLE CYCLE DETECTED *************'),
199 debug_println(9,computing_all_other_enabled_actions(back_transition(ActionName),AllRemainedActions)),
200 add_all_other_enabled_transitions(AllRemainedActions,CurID,ActSuccsGen1,ActSuccs),
201 RemTransitions=[],
202 FullyExplored = yes
203 ; FullyExplored = no, RemTransitions = AllRemainedActions, ActSuccs=ActSuccsGen1)
204 ;
205 % keeping track of all ignored actions so far, we don't want to go through the same order of actions every time we
206 % want to add a transition from AmpleSet to the state space
207 ord_add_element(IgnoredActions_so_far, (ActionName1,Transitions), IgnoredActions_so_far1),
208 add_action_name_transitions(R,CurID,ActionName,IgnoredActions_so_far1,RemTransitions,FullyExplored,ActSuccsGen,ActSuccs)
209 ).
210
211 % cycle condition A4 check
212 cycle_condition_check_positive_l(NewIds) :-
213 check_cycle_proviso,!,
214 ? member(NewId,NewIds),
215 nonvar(NewId),
216 explored_state(NewId).
217
218 add_all_other_enabled_transitions([],_CurID,ActSuccsGen,ActSuccsGen).
219 add_all_other_enabled_transitions([(ActionName,Transitions)|R],CurID,ActSuccsGen,ActSuccs) :-
220 add_action_transitions(ActionName,Transitions,CurID,NewIds),
221 ActSuccsGen1 = [(ActionName,NewIds)|ActSuccsGen],
222 add_all_other_enabled_transitions(R,CurID,ActSuccsGen1,ActSuccs).
223
224 add_action_transitions(_ActionName,Transitions,CurID,NewIds) :-
225 findall(NewId,
226 (member((Act,NewExpression,TransInfo,Residue),Transitions),
227 tcltk_interface: add_trans_id_with_infos(CurID,[],Act,NewExpression,Residue,NewId,TransInfo,_)),NewIds).
228
229
230 /************************** ADD TRANSITIONS (END) ******************************/
231
232 /* Auxiliary predicates for getting action names enabled at the current state */
233
234 % getting action names from the action tuples
235 %get_action_names_only(Actions,ActionNames) :-
236 % maplist(get_action_name,Actions,ActionNames).
237 %get_action_name((ActionName,_Representations),ActionName).
238
239 get_enabled_actions_at_state(CurID,CurState,EnabledTopLevelActions,Transitions,DisabledTopLevelActions) :-
240 get_specfile_possible_trans_names(CurID,CurState,TopLevelActions,DisabledOperations),
241 get_enabled_actions_at_state_aux(TopLevelActions,CurID,CurState,EnabledTopLevelActions,Transitions,DisabledOperationsNew),
242 list_to_ord_set(DisabledOperationsNew,DisabledOperationsNewSet),
243 ord_union(DisabledOperations,DisabledOperationsNewSet,DisabledTopLevelActions).
244
245 get_specfile_possible_trans_names(_CurID,CurState,TopLevelActions,DisabledOperations) :-
246 get_specfile_possible_trans_names(CurState,TopLevelActions),
247 DisabledOperations = [].% in case PGE is not on we don't have any information about the disabledness of actions
248
249
250 get_specfile_possible_trans_names(CurState,TopLevelActions) :-
251 findall(ActionName,specfile_possible_trans_name_for_successors(CurState,ActionName),TopLevelActions).
252
253 get_enabled_actions_at_state_aux(TopLevelActions,CurID,CurState,EnabledTopLevelActions,Transitions,DisabledTopLevelActionsNew) :-
254 get_enabled_actions_at_state_aux2(TopLevelActions,CurID,CurState,[],EnabledTopLevelActions,[],Transitions,[],DisabledTopLevelActionsNew).
255
256 get_enabled_actions_at_state_aux2([],_CurID,_CurState,EnabledTopLevelActions,EnabledTopLevelActions,Transitions,Transitions,DisabledNew,DisabledNew).
257 get_enabled_actions_at_state_aux2([TopLevelAction|R],CurID,CurState,EnabledTopLevelActionsGen,EnabledTopLevelActions,TransitionsGen,Transitions,DisabledNewGen,DisabledNew) :-
258 (get_top_level_action_transitions(CurID,CurState,TopLevelAction,ActionTransitionsTuple) ->
259 EnabledTopLevelActionsGen1 = [TopLevelAction|EnabledTopLevelActionsGen],
260 TransitionsGen1 = [ActionTransitionsTuple|TransitionsGen],
261 DisabledNewGen1 = DisabledNewGen
262 ;
263 EnabledTopLevelActionsGen1 = EnabledTopLevelActionsGen,
264 TransitionsGen1 = TransitionsGen,
265 DisabledNewGen1 = [TopLevelAction|DisabledNewGen]
266 ),
267 get_enabled_actions_at_state_aux2(R,CurID,CurState,EnabledTopLevelActionsGen1,EnabledTopLevelActions,TransitionsGen1,Transitions,DisabledNewGen1,DisabledNew).
268
269 get_top_level_action_transitions(CurID,CurState,TopLevelAction,ActionTransitionsTuple) :-
270 findall((Act,NewExpression,TransInf,Residue),
271 specfile_trans_timeout(CurID,CurState,TopLevelAction,Act,NewExpression,TransInf,Residue),
272 Transitions),
273 % in case TopLevelAction is not enabled we do not save the transitions in the list
274 Transitions\=[],
275 ActionTransitionsTuple = (TopLevelAction,Transitions).
276
277 specfile_trans_timeout(CurID,CurState,ActionName,Act,NewExpression,TransInf,Residue) :-
278 (use_no_timeout(CurID) ->
279 specfile_trans(CurState,ActionName,Act,NewExpression,TransInf,Residue)
280 ; preferences:preference(time_out,CurTO),
281 ? time_out_with_enum_warning_for_findall(specfile_trans(CurState,ActionName,Act,NewExpression,TransInf,Residue),CurTO,TimeOutRes),
282 check_timeout_result(CurID,ActionName,CurTO,TimeOutRes),
283 Residue==[]
284 ).
285
286 :- use_module(probsrc(error_manager),[is_time_out_result/1]).
287 :- use_module(probsrc(state_space),[ assert_time_out_for_node/3]).
288 :- use_module(probsrc(tools), [print_message/1]).
289 check_timeout_result(CurID,ActionName,CurTO,TimeOutRes) :-
290 (is_time_out_result(TimeOutRes)
291 ->
292 ( TimeOutRes = virtual_time_out(_) ->
293 true
294 ; print_message('TIME OUT: '), print_message(CurID), print_message(CurTO),
295 print_message(ActionName)
296 ),
297 (var(ActionName)
298 -> assert_time_out_for_node(CurID,'unspecified_operation',TimeOutRes)
299 ; assert_time_out_for_node(CurID,ActionName,TimeOutRes))
300 ; true
301 ).
302
303 /* Auxiliary predicates for getting action names enabled at the curent state */
304 %---------------------------------------------------------------------------------------------------------------------------------
305 /************************** DETERMINING POSSIBLE AMPLE SETS (BEGIN) ******************************/
306
307 /************************** PREDICATES COMPUTING AMPLE SETS WITH RESPECT TO CONDITIONS (A 2) and (A 3) *************************/
308
309 /*
310
311 implementation of the ample-sets approach satisfying the dependency condition (A 2)
312 by using enabling and dependence analysis (see module enabling analysis)
313
314 */
315
316 get_ample_actions(CurID,AllEnabledActions,POROptions,AmpleSet) :-
317 get_preference(por_heur,Pref),
318 ? get_ample_actions_pref(Pref,CurID,AllEnabledActions,POROptions,AmpleSet).
319
320 get_ample_actions_pref(minimal,CurID,AllEnabledActions,POROptions,AmpleSet) :- !,
321 get_possible_ample_actions_minimal(AllEnabledActions,CurID,AllEnabledActions,POROptions,AmpleSet).
322 get_ample_actions_pref(Pref,CurID,AllEnabledActions,POROptions,AmpleSet) :-
323 ? get_possible_ample_actions(Pref,AllEnabledActions,CurID,AllEnabledActions,POROptions,AmpleSet).
324
325 get_possible_ample_actions(_Pref,[],_CurID,AllEnabledActions,_POROptions,AllEnabledActions).
326 get_possible_ample_actions(Pref,Actions,CurID,AllEnabledActions,POROptions,AmpleSet) :-
327 % choosing of action for computing a possible ample set for CurID should be random
328 select_enabled_action_pref(Pref,Actions,Act,RemainedActions),
329 (get_valid_ample_set_act(CurID,POROptions,Act,AllEnabledActions,AmpleSet) -> % get ample set satisfying (A 2) and (A 3) i.r.t. Act
330 true
331 ; % search for another valid ample set
332 ? get_possible_ample_actions(Pref,RemainedActions,CurID,AllEnabledActions,POROptions,AmpleSet)
333 ).
334
335 get_valid_ample_set_act(CurID,POROptions,Act,AllEnabledActions,AmpleSet) :-
336 POROptions = por(ample_sets2,_,_,_),!,
337 %% trying to find an ample set by the closure method
338 get_possible_ample_set_closure(CurID,Act,AllEnabledActions,AmpleSet).
339 get_valid_ample_set_act(CurID,POROptions,Act,AllEnabledActions,AmpleSet) :-
340 get_valid_ample_set_act_normal(CurID,POROptions,Act,AllEnabledActions,AmpleSet).
341
342 get_valid_ample_set_act_normal(CurID,POROptions,Act,AllEnabledActions,AmpleSet) :-
343 % finding a set (DepActs) satisfying (A 2.1) // there is only one possible set, do not recall here
344 ? get_dependent_actions(CurID,POROptions,Act,AllEnabledActions,DepActs,IndActs),!,
345 ( (is_enabling_dependency_condition_satisfied(IndActs,DepActs,AllEnabledActions,CurID,POROptions), % check whether DepActs satisfies (A 2.2)
346 check_stutter_condition(DepActs,AllEnabledActions)) % checking (A 3)
347 -> AmpleSet = DepActs
348 ; fail). % should fail in order to try to get another valid ample set
349
350 select_enabled_action_pref(random,Actions,Act,RemainedActions):- !,
351 random_select(Act,Actions,RemainedActions).
352 select_enabled_action_pref(first,[Act|RemainedActions],Act,RemainedActions):- !.
353 select_enabled_action_pref(Pref,Actions,Act,RemainedActions) :-
354 add_warning(ample_sets,'Unknown preference for choosing an action from the set of enabled actions: ', Pref),
355 random_select(Act,Actions,RemainedActions). % choose the default option for choosing an action
356
357 check_stutter_condition(AmpleSet,AllEnabledActions) :-
358 \+ ord_seteq(AmpleSet,AllEnabledActions),!,
359 check_cycle_proviso(AmpleSet).
360
361 % this actually checks condition A3 not the cycle condition A4:
362 check_cycle_proviso(AmpleSet) :-
363 % in case that we don't check for invariant violations, we don't have to prove the stuttering condition for AmpleSet
364 (check_cycle_proviso % true when we search for invariant violations
365 -> stutter_actions(AmpleSet)
366 ; true).
367
368 get_possible_ample_actions_minimal(AllEnabledActions,CurID,AllEnabledActions,POROptions,AmpleSet) :-
369 maplist(get_valid_ample_set_act_nonfail(CurID,AllEnabledActions,POROptions),AllEnabledActions,AmpleSets),
370 get_minimal_set(AmpleSets,AmpleSet),
371 debug_println(9,minimal_ample_set(AmpleSet,AmpleSets)).
372
373 get_minimal_set([AmpleSet|Rest],MinAmpleSet) :-
374 length(AmpleSet,Length),
375 get_minimal_set_length(Length,AmpleSet,Rest,MinAmpleSet).
376
377 get_minimal_set_length(1,AmpleSet,_Rest,AmpleSet).
378 get_minimal_set_length(_Length,AmpleSet,[],AmpleSet).
379 get_minimal_set_length(Length,AmpleSet,[AmpleSet2|Rest],MinAmpleSet) :-
380 length(AmpleSet2,Length2),
381 (Length2<Length ->
382 get_minimal_set_length(Length2,AmpleSet2,Rest,MinAmpleSet)
383 ;
384 get_minimal_set_length(Length,AmpleSet,Rest,MinAmpleSet)).
385
386 get_valid_ample_set_act_nonfail(CurID,AllEnabledActions,POROptions,Act,AmpleSet) :-
387 (get_valid_ample_set_act(CurID,POROptions,Act,AllEnabledActions,AmpleSet)
388 -> true
389 ; AmpleSet = AllEnabledActions).
390
391 %%%%%% the closure method for computing ample sets %%%%%%
392 get_possible_ample_set_closure(CurID,Act,AllEnabledActions,AmpleSet) :-
393 get_valid_ample_set_act_closure(CurID,Act,AllEnabledActions,AmpleSet),!,
394 check_stutter_condition(AmpleSet,AllEnabledActions). % checking (A 3)
395
396 get_valid_ample_set_act_closure(CurID,Act,AllEnabledActions,AmpleSet) :-
397 %(length(AllEnabledActions,3) -> trace;true),
398 list_to_ord_set([Act],WorkSet),
399 list_to_ord_set([],ClosureSet),
400 compute_closure_set(WorkSet,CurID,ClosureSet,AllEnabledActions,Result),
401 ord_intersection(Result,AllEnabledActions,AmpleSet).
402
403 compute_closure_set([],_CurID,ClosureSet,_AllEnabledActions,ClosureSet).
404 compute_closure_set([Act|Acts],CurID,ClosureSet,AllEnabledActions,Result) :-
405 ord_add_element(ClosureSet,Act,ClosureSet1),
406 get_new_work_set(Act,Acts,CurID,AllEnabledActions,ClosureSet1,NewWorkSet),
407 compute_closure_set(NewWorkSet,CurID,ClosureSet1,AllEnabledActions,Result).
408
409 get_new_work_set(Act,WorkSet,CurID,AllEnabledActions,ClosureSet1,NewWorkSet) :-
410 ord_member(Act,AllEnabledActions),!,
411 get_dependency_set_closure(Act,WorkSet,CurID,ClosureSet1,NewWorkSet).
412 get_new_work_set(Act,WorkSet,CurID,AllEnabledActions,ClosureSet1,NewWorkSet) :-
413 get_enabling_set_for_act(Act,WorkSet,CurID,AllEnabledActions,ClosureSet1,NewWorkSet).
414
415 get_dependency_set_closure(Act,WorkSet,CurID,ClosureSet1,NewWorkSet) :-
416 findall(Act1,(is_dependent_to_and_coenabled(Act,Act1,CurID),ord_nonmember(Act1,ClosureSet1)),NewActions),
417 list_to_ord_set(NewActions,NewActionsSet),
418 ord_union(WorkSet,NewActionsSet,NewWorkSet).
419
420 get_enabling_set_for_act(Act,WorkSet,CurID,AllEnabledActions,ClosureSet1,NewWorkSet) :-
421 % Act is disabled at current state
422 findall(Act1,find_enabling_action(Act,CurID,AllEnabledActions,ClosureSet1,Act1),NewActions),
423 list_to_ord_set(NewActions,NewActionsSet),
424 ord_union(WorkSet,NewActionsSet,NewWorkSet).
425
426 find_enabling_action(Act,CurID,AllEnabledActions,ClosureSet,Act1) :-
427 ? member(Act1,AllEnabledActions),
428 ord_nonmember(Act1,ClosureSet),
429 can_enable_certain_action(Act1,Act,CurID).
430
431 can_enable_certain_action(Act,ActionToBeEnabled,CurID) :-
432 ( enables(Act,ActionToBeEnabled) ->
433 debug_println(9,enables(Act,ActionToBeEnabled))
434 ;
435 enable_graph(EnableGraph0),
436 enable_graph_without_predicates(EnableGraph0,EnableGraph), % min_path is for ugraphs
437 min_path(Act,ActionToBeEnabled,EnableGraph,P,L),
438 debug_print_path(CurID,P,L),
439 assertz(enables(Act,ActionToBeEnabled))).
440
441 % get_dependent_actions(+CurID,+POROptions,+Act,+AllEnabledActions,+EnGraph,-DepActs)
442 % CurID - the ID of the current state (used here only for debugging purposes)
443 % POROptions - partial order reduction options (e.g. UseEnableGraph, Depth)
444 % Act - the action with which the current dependency class will be computed
445 % AllEnabledActions - the set of all in CurID enabled actions and it is an ordinary set
446 % DepActs - the computed candidate for ample set
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 % EnableGraph - the enable graph for all possible enable actions' relations in the current machine. The enable graph will
449 % be computed only single time (before starting with the verification of the state space).
450
451 % the enable graph has the format V-[N-Pred] when the enable_graph preference is true (see static_analysis.pl)
452 enable_graph_without_predicates([],[]).
453 enable_graph_without_predicates([V-Ns|T],[V-UNs|UT]) :-
454 maplist(neighbour_without_predicate,Ns,Ns1), sort(Ns1,UNs),
455 enable_graph_without_predicates(T,UT).
456 neighbour_without_predicate(N-_Pred,R) :- !, R=N.
457 neighbour_without_predicate(N,N).
458
459 get_dependent_actions(CurID,_POROptions,Act,AllEnabledActions,DepActs,IndActions) :-
460 % finding all dependent actions to Act (A 2.1)
461 ? get_dependency_set(Act,AllEnabledActions,CurID,DepActs),
462 ord_subtract(AllEnabledActions,DepActs,IndActions).
463
464 is_enabling_dependency_condition_satisfied([],_DepActs,_AllEnabledActions,_CurID,_POROptions) :- !,fail.
465 is_enabling_dependency_condition_satisfied(IndActs,DepActs,AllEnabledActions,CurID,POROptions) :-
466 enable_graph(EnableGraph),
467 vertices(EnableGraph,Vertices), % getting all possible actions that can be enabled
468 % DisabledActions = Vertices\AllEnabledActions
469 ord_subtract(Vertices,AllEnabledActions,DisabledActions),
470 % searching for an independent action to Act that could enable a currently disabled action that is dependent to Act (A 2.2)
471 ? \+may_enable_dep_action_list(IndActs,DepActs,DisabledActions,EnableGraph,CurID,POROptions).
472
473 may_enable_dep_action_list(IndActs,DepActs,DisabledActions,EnableGraph,CurID,POROptions) :-
474 ? member(Act2,IndActs),
475 may_enable_dep_action(Act2,DepActs,DisabledActions,EnableGraph,CurID,POROptions).
476
477
478 get_dependency_set(Act,AllEnabledActions,CurID,DepActions_so_far) :-
479 vertices_edges_to_ugraph([],[],G),
480 get_dependency_tuples_converted_to_edges(Act,AllEnabledActions,RestEnabledActions,CurID,Edges),
481 add_edges(G,Edges,G1),
482 ? get_dependency_set1(Act,G1,[],RestEnabledActions,CurID,DepActions_so_far).
483
484 get_dependency_tuples_converted_to_edges(Act,AllEnabledActions,RestEnabledActions,CurID,Res) :-
485 findall(Act1,(member(Act1,AllEnabledActions), is_dependent_to(Act,Act1,CurID)),DepActs),
486 findall([Act-Act1,Act1-Act],member(Act1,DepActs),Edges),
487 ord_subtract(AllEnabledActions,DepActs,RestEnabledActions),
488 append(Edges,Res).
489
490 get_dependency_set1(Act,G,G,_Actions,_CurID,Res) :-
491 reachable(Act,G,Res).
492 get_dependency_set1(Act,G,_GPrime,Actions,CurID,Res) :-
493 GPrime1 = G,
494 vertices(G,Vertices),
495 findall([V-Act1,Act1-V],(member(Act1,Actions),member(V,Vertices),is_dependent_to(V,Act1,CurID)),NewEdges),
496 append(NewEdges,NewEdges1),
497 add_edges(G,NewEdges1,G1),vertices(G1,Vertices1),
498 ord_subtract(Actions,Vertices1,Actions1),
499 ? get_dependency_set1(Act,G1,GPrime1,Actions1,CurID,Res).
500
501 is_dependent_to(Act,Act1,CurID) :-
502 (dependent_act(Act,Act1,Status,_Coenabled) ->
503 (Status=predicate(Pred) -> check_enable_dependency(Act,Act1,Pred,CurID); true)
504 ;
505 fail
506 ).
507
508 is_dependent_to_and_coenabled(Act,Act1,CurID) :-
509 ? dependent_act(Act,Act1,Status,true),
510 (Status=predicate(Pred) -> check_enable_dependency(Act,Act1,Pred,CurID); true).
511
512 check_enable_dependency(Act,Act1,Pred,CurID) :-
513 debug_println(9,checking_guard_dependency(Act,Act1,Pred)),
514 visited_expression(CurID,RawState),
515 state_corresponds_to_initialised_b_machine(RawState,State),
516 empty_state(LocalState),
517 % if the enable predicate Pred evaluates to false we know that (Act,Act1) is a dependent pair
518 \+ b_interpreter:b_test_boolean_expression_cs(Pred,LocalState,State,'enable dependency',Act:Act1).
519
520 /* searching for enabling path beginning from an action which is not from AmpleSet and
521 which enables action dependent on AmpleSet */
522 may_enable_dep_action(Act,AmpleSet,DisabledActions,EnableGraph,CurID,por(_TYPE,UseEnableGraph,Depth,_PGE)) :-
523 ( UseEnableGraph = true ->
524 ? check_enabling_path(CurID,Act,DisabledActions,AmpleSet,EnableGraph,Depth)
525 ;
526 ? member(DisAct,DisabledActions),
527 % we check here only currently disabled actions dependent on AmpleSet that can be enabled later
528 ? depends_on_ample_set_and_coenabled(DisAct,AmpleSet,_DepAct),
529 % is there a path that can enable Act1, which is dependent on Act
530 enables_action(CurID,Act,DisAct,AmpleSet,EnableGraph)
531 ),
532 !.
533
534 enables_action(CurID,Act,DisAct,AmpleSet,EnableGraph) :-
535 ( enables(Act,DisAct) ->
536 % should improve performance a little bit (not applicable when using enable graphs)
537 debug_println(9,enables(Act,DisAct))
538 ; check_if_enabled(CurID,Act,DisAct,AmpleSet,EnableGraph) ->
539 true
540 ;
541 fail
542 ).
543
544 check_if_enabled(CurID,Act,DisAct,AmpleSet,EnableGraph) :-
545 min_path(Act,DisAct,EnableGraph,P,L),
546 sets_disjoint(AmpleSet,P),
547 debug_print_path(CurID,P,L),
548 assertz(enables(Act,DisAct)).
549
550 % predicates used for the enable graph feature
551 check_enabling_path(CurID,Act,DisabledActions,AmpleSet,EnableGraph,Depth) :-
552 findall(DepDisAct,(member(DepDisAct,DisabledActions),depends_on_ample_set_and_coenabled(DepDisAct,AmpleSet,_Act)),DependentDisabledActions),
553 ( DependentDisabledActions = [] ->
554 fail % in this case we know that no action dependent on AmpleSet can be enabled after executing Act
555 ;
556 ? enables_pred_action1(Act,DependentDisabledActions,AmpleSet,CurID,EnableGraph,Depth)).
557
558 enables_pred_action1(Act,DepDisActs,AmpleSet,CurID,EnableGraph,Depth) :-
559 % visited_expression/2 and state_corresponds_to_initialised_b_machine/2 should be called once for each state,
560 % avoid multiple calls per state
561 (Depth==0 ->
562 empty_state(ST)
563 ;
564 visited_expression(CurID,RawState),
565 state_corresponds_to_initialised_b_machine(RawState,ST)
566 ),
567 %Pred =.. [evaluate_enabling_predicate_for_por, ST, AmpleSet],
568 ? enable_graph: path_set(EnableGraph,Act,DepDisActs,enable_graph,
569 evaluate_enabling_predicate_for_por(ST,AmpleSet),Depth,Path,Length),
570 debug_print_path(CurID,Path,Length).
571
572 % guaranteeing that the dependent action to AmpleSet has been enabled from an action off AmpleSet
573 sets_disjoint(AmpleSet,Path) :-
574 list_to_ord_set(Path,OrdSet),
575 \+ord_intersection(AmpleSet,OrdSet).
576
577 depends_on_ample_set_and_coenabled(Act,AmpleSet,Act1) :-
578 ? member(Act1,AmpleSet),
579 dependent_act(Act,Act1,_Status,Coenabled),
580 Coenabled==true.
581
582 debug_print_path(CurID,Path,Length) :-
583 debug_println(20,'***** Enable Path *****'),
584 debug_println(20,path(CurID,Path,Length)),
585 debug_println(20,'***********************').
586
587 /************************** DETERMINING POSSIBLE AMPLE SETS (END) ******************************/
588
589 /************************** CHECKING IF ALL ACTIONS FROM AMPLE SET ARE STUTTER ACTIONS (BEGIN) WITH REGARD TO THE INVARIANT ******************************/
590 /************************** PREDICATES WHICH SATISFY CONDITION (A3) ******************************/
591
592 stutter_actions(Actions) :-
593 maplist(is_stutter_action,Actions).
594
595 % visible actions are non stutter actions
596 is_stutter_action(Act) :-
597 ( stutter_action(Act) ->
598 debug_println(19,stutter_action(Act))
599 ; visible_action(Act) ->
600 debug_println(19,visible_action(Act)),
601 fail
602 ;
603 check_if_action_is_stutter_wrt_inv(Act)
604 ).
605
606 % A stutter action
607 % * either cannot change the current invariant value or
608 % * if it violates the invariant then no independent action can undo the change
609 % That's why we must check if the particular action preserves
610 % the full invariant in order to determine if the action is
611 % stutter under the following LTL formula:
612 % G Phi, where Phi is the invariant.
613 % The same applies to the GOAL and dynamic ASSERTIONS when they are checked (G not(Goal), G Assertion),
614 % see set_por_visible_properties/3.
615 check_if_action_is_stutter_wrt_inv(Act) :- %print(check_stutter(Act)),nl,
616 ? (action_is_stutter_wrt_invariant(Act),
617 action_is_stutter_wrt_visible_predicates(Act)
618 -> assertz(stutter_action(Act))
619 ; assertz(visible_action(Act)),fail
620 ).
621
622 action_is_stutter_wrt_invariant(_) :- por_invariant_not_visible, !.
623 action_is_stutter_wrt_invariant(Act) :-
624 b_operation_preserves_full_invariant(Act),!,
625 debug_println(19,stutter_according_to_proof_info(Act)).
626 action_is_stutter_wrt_invariant(Act) :-
627 b_specialized_invariant_for_op(Act,Invariant), %translate:print_bexpr(Invariant),nl,
628 conjunction_to_list(Invariant,Conjunctions), % getting all conjunctions from the invariant
629 b_get_read_write(Act,_Reads,Writes), % print(writes(Act,Writes)),nl,
630 ? action_does_not_modify_inv_or_inv_cannot_be_repaired(Writes,Conjunctions),
631 debug_println(19,stutter_or_not_repairable(Act)).
632
633 action_is_stutter_wrt_visible_predicates(Act) :-
634 findall(P,por_visible_predicate(P),Preds),
635 (Preds = [] -> true
636 ; b_get_read_write(Act,_Reads,Writes),
637 action_does_not_modify_inv_or_inv_cannot_be_repaired(Writes,Preds)).
638
639 % called by the model checker before POR model checking: the stutter/visible status of actions depends on it
640 :- use_module(probsrc(bmachine),[b_get_machine_goal/1, b_get_dynamic_assertions_from_machine/1]).
641 set_por_visible_properties(FindInvViolations,FindGoal,FindAssViolations) :-
642 retractall(stutter_action(_)), retractall(visible_action(_)), % e.g., computed for an LTL formula
643 retractall(por_invariant_not_visible), retractall(por_visible_predicate(_)),
644 retractall(check_cycle_proviso),
645 (FindInvViolations == 1 -> true ; assertz(por_invariant_not_visible)),
646 (FindGoal == 1, b_get_machine_goal(Goal) -> assertz(por_visible_predicate(Goal)) ; true),
647 (FindAssViolations == 1, b_get_dynamic_assertions_from_machine(Ass)
648 -> maplist(assert_por_visible_predicate,Ass) ; true),
649 ((FindInvViolations == 1 ; FindGoal == 1 ; FindAssViolations == 1)
650 -> assertz(check_cycle_proviso) % the stutter condition (A3) and cycle proviso (A4) are needed
651 ; true).
652 assert_por_visible_predicate(P) :- assertz(por_visible_predicate(P)).
653
654 :- use_module(probsrc(bsyntaxtree),[find_identifier_uses/3]).
655 :- use_module(probsrc(bmachine),[b_specialized_invariant_for_op/2]).
656 % either an action does not affect an invariant or it writes *all* variables of an invariant
657 % TODO: a third option would be if no other independent event modifies the variable of this invariant
658 % one could even look whether any (enabled) event in the ample set modifies the given conjunct
659 % For example if we modify x and invariant is x<y, but if no other enabled event modifies x,y we are fine
660 action_does_not_modify_inv_or_inv_cannot_be_repaired(_Vars,[]).
661 action_does_not_modify_inv_or_inv_cannot_be_repaired(Vars,[Conj|Conjs]) :-
662 find_identifier_uses(Conj,[],UsedIds),
663 ord_intersection(UsedIds,Vars,Common),
664 (Common=[]
665 -> true % invariant not affected by action
666 ; ord_seteq(Common,UsedIds) % we write all variables of an invariant; no independent action can change/repair its status
667 ),
668 ? action_does_not_modify_inv_or_inv_cannot_be_repaired(Vars,Conjs).
669
670
671
672 /************************** CHECKING IF ALL ACTIONS FROM AMPLE SET ARE STUTTER ACTIONS (END) ******************************/