1 % (c) 2009-2024 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(xtl_interface, [open_xtl_file/1,
6 xtl_transition/3,
7 xtl_property/2, xtl_invariant_violated/1,
8 xtl_animation_function_result/2, xtl_animation_image/2,
9 xtl_heuristic_function_active/0,
10 xtl_heuristic_function_result/2,
11 xtl_animation_image_click_transition/6,
12 xtl_animation_image_right_click_transition/3,
13 xtl_get_definition_string/2,
14 xtl_game_info/3,
15
16 csp_initialisation_for_b/1,
17 csp_transition_for_b/5,
18 generate_b_operationargs_from_csp/2,
19
20 %open_promela_file/1, promela_transition/3, promela_property/2,
21 %open_smv_file/1, smv_transition/3, smv_property/2, % SMV mode broken
22
23 open_cspm_file/1, last_opened_cspm_file/1,
24 cspm_transition/3,
25 cspm_property/2,
26 set_cspm_main_process/1,
27 reset_xtl_interface/0]).
28
29
30 :- use_module(module_information).
31 :- module_info(group,animator).
32 :- module_info(description,'Provides an interface to the non-B animators depending on animation-mode.').
33
34 /* Typically the XTL specifications reside in a .P file with the following predicates
35 start/1 -> defining the initial states
36 trans/3 -> defining the transitions between states transition(Action, StateBefore, StateAfter)
37 prop/2 -> defining properties of states
38
39 For CSP specifications the interpreter is integrated into ProB
40
41 */
42
43 /* --------------- XTL ----------------- */
44 :- volatile prop/2, trans/3, start/1, animation_function_result/2, animation_image/2.
45 :- volatile animation_image_click_transition/6, animation_image_right_click_transition/3.
46 :- volatile heuristic_function_active/0, heuristic_function_result/2.
47 :- volatile prob_pragma_string/2, prob_game_info/3.
48 :- dynamic prop/2.
49 :- dynamic trans/3.
50 :- dynamic start/1.
51 :- dynamic animation_function_result/2.
52 :- dynamic animation_image/2.
53 :- dynamic animation_image_click_transition/6, animation_image_right_click_transition/3.
54 :- dynamic heuristic_function_active/0.
55 :- dynamic heuristic_function_result/2.
56 :- dynamic prob_pragma_string/2, prob_game_info/3.
57
58 % the following imports are required so that XTL .P files can make use of these functions:
59 :- use_module(library(lists)).
60 :- use_module(library(between)).
61 :- use_module(library(ordsets)).
62 :- use_module(library(samsort)).
63 :- use_module(library(random)).
64 :- use_module(library(avl)).
65 :- use_module(library(heaps)).
66 :- use_module(tools_portability, [exists_source/1]).
67 :- if(exists_source(library(logarr))).
68 :- use_module(library(logarr)). % not yet available in SWI Prolog
69 :- endif.
70
71 % ProB utilities (which can also be used by XTL code)
72 :- use_module(error_manager).
73 :- use_module(preferences,[get_preference/2]).
74 :- use_module(debug).
75 :- use_module(tools).
76
77 :- if(\+ current_prolog_flag(dialect, sicstus)).
78 abolish_all([]).
79 abolish_all([Pred|Preds]) :-
80 abolish(Pred),
81 abolish_all(Preds).
82 :- else.
83 abolish_all(Preds) :-
84 abolish(Preds, [force(true),tree(true)]).
85 :- endif.
86
87 open_xtl_file(File) :-
88 abolish_all([prop/2, trans/3, start/1]),
89 abolish_all([animation_image/2,animation_function_result/2,
90 animation_image_click_transition/6,animation_image_right_click_transition/3,
91 heuristic_function_active/0,
92 prob_pragma_string/2,
93 prob_game_info/3]),
94 assertz((heuristic_function_active :- fail)),
95 assertz((animation_image(_,_) :- fail)),
96 assertz((animation_function_result(_,_) :- fail)),
97 assertz((animation_image_click_transition(_,_,_,_,_,_) :- fail)),
98 assertz((animation_image_right_click_transition(_,_,_) :- fail)),
99 assertz((prob_pragma_string(_,_) :- fail)),
100 assertz((prob_game_info(_,_,_) :- fail)),
101 debug_println(9,tcltk_open_xtl_file(File)),
102 consult_without_redefine_warning(File),
103 debug_println(9,new_xtl_file(File)).
104
105
106 ?xtl_transition(root,start_xtl_system,NewState) :- start(NewState).
107 ?xtl_transition(State,Operation,NewState) :- State \= root, trans(Operation,State,NewState).
108
109 ?xtl_property(State,Property) :- State \= root, prop(State,Property).
110
111 % special Property is unsafe; see is_xtl_error_state in model_checker.pl
112 % Note for XTL we do not use not_invariant_checked/1 facts
113 ?xtl_invariant_violated(State) :- xtl_property(State,unsafe).
114
115 xtl_animation_function_result(State,AnimationMatrix) :- State \= root,
116 animation_function_result(State,AnimationMatrix).
117
118 xtl_animation_image(Nr,PathToGif) :-
119 %on_exception(error(existence_error(_,_),_),
120 animation_image(Nr,PathToGif).
121
122 % return a transition template to execute for simple clicks (From=To) or drags
123 % OperationTemplate can either be the template of an operation to match or a list of such templates
124 % (the operations will then be executed in order)
125 xtl_animation_image_click_transition(FromX,FromY,ToX,ToY,OperationTemplate,Image) :-
126 animation_image_click_transition(FromX,FromY,ToX,ToY,OperationTemplate,Image).
127
128 xtl_animation_image_right_click_transition(X,Y,OperationTemplate) :-
129 animation_image_right_click_transition(X,Y,OperationTemplate).
130
131 xtl_heuristic_function_active :-
132 heuristic_function_active.
133 xtl_heuristic_function_result(State,int(IntegerVal)) :- State \= root,
134 heuristic_function_result(State,Res),
135 (Res=int(R) -> IntegerVal=R
136 ; number(Res) -> IntegerVal=Res
137 ; add_error(xtl_heuristic_function_result,'heuristic_function_result must be integer: ',Res),fail
138 ).
139
140 xtl_game_info(Key,State,Value) :- prob_game_info(Key,State,Value).
141 %xtl_game_over(State) :- prob_game_info('GAME_OVER',State,true).
142 %xtl_game_value(State,Value) :- prob_game_info('GAME_VALUE',State,Value).
143 %xtl_game_player(State,Player) :- prob_game_info('GAME_PLAYER',State,Player).
144
145
146 % way to mimic DEFINITION Strings in XTL mode, such as ASSERT_LTL
147 xtl_get_definition_string(Def_Name,DefString) :-
148 ? prob_pragma_string(N,S),
149 get_atom_string(N,Def_Name),
150 get_atom_string(S,DefString).
151
152 :- use_module(tools,[safe_atom_codes/2]).
153 get_atom_string(Atom,Res) :- atom(Atom),!,Res=Atom.
154 get_atom_string([H|T],Res) :- safe_atom_codes(Atom,[H|T]), !, Res=Atom. % transform "abc" into 'abc'
155 get_atom_string(R,R).
156
157 consult_without_redefine_warning(File) :-
158 get_set_optional_prolog_flag(redefine_warnings, Old, off),
159 get_set_optional_prolog_flag(single_var_warnings, Old2, off),
160 (catch(my_compile(File),
161 error(existence_error(_,_),_),
162 add_error_fail(xtl,'XTL File does not exist:',File))
163 -> OK=true ; OK=false),
164 get_set_optional_prolog_flag(redefine_warnings, _, Old),
165 get_set_optional_prolog_flag(single_var_warnings, _, Old2),
166 OK=true.
167
168 my_compile(F) :- %get_preference(user_is_an_expert_with_accessto_source_distribution,true),
169 !, % it seems it is ok to call compile also in probcli binary; it may do consult though
170 compile(F).
171 my_compile(F) :- consult(F).
172
173
174 /* --------------- Promela ----------------- */
175
176 %:- use_module('promela/h_int').
177
178 /* --------------- SMV ----------------- */
179
180 % :- use_module('smv/smv_trans').
181
182
183 /* --------------- CSP-M ----------------- */
184
185 :- use_module(probcspsrc(haskell_csp),[parse_and_load_cspm_file/1,
186 cspm_trans_enum/3,
187 animatable_process/1, animatable_process_without_arguments/1,
188 get_symbol_span/2,force_evaluate_argument/2,normalise_cspm_state/2]).
189 :- use_module(probcspsrc(haskell_csp_analyzer),[cspPrintCompiled/2]).
190 :- use_module(probsrc(translate),[translate_cspm_state/2]).
191
192 :- dynamic last_opened_cspm_file/1. % useful for csp_and_b mode
193
194 open_cspm_file(File) :-
195 retractall(last_opened_cspm_file(_)),
196 debug_println(15,open_cspm_file(File)), flush_output(user_output),
197 parse_and_load_cspm_file(File),
198 assertz(last_opened_cspm_file(File)).
199
200 :- dynamic cspm_main_process/1.
201 cspm_main_process('MAIN').
202 set_cspm_main_process(M) :-
203 retractall(cspm_main_process(_)),
204 assertz(cspm_main_process(M)).
205
206 reset_xtl_interface :- retractall(last_opened_cspm_file(_)),
207 reset_cspm_main_process.
208 reset_cspm_main_process :- set_cspm_main_process('MAIN').
209
210 :- use_module(eventhandling,[register_event_listener/3]).
211 :- register_event_listener(clear_specification,reset_xtl_interface,
212 'Reset XTL Interface.').
213
214 cspm_transition(root,start_cspm_MAIN,NormalisedNewState) :-
215 cspm_main_process(MAIN),
216 ? animatable_process_without_arguments(MAIN),
217 get_start_expr(MAIN,NewState),
218 ? normalise_cspm_state(NewState,NormalisedNewState).
219 cspm_transition(root,start_cspm(X),NormalisedNewState) :- cspm_main_process(MAIN),
220 (get_preference(cspm_animate_all_processes_without_arguments,true)
221 ? ; \+ animatable_process_without_arguments(MAIN)),
222 ? animatable_process_without_arguments(X),
223 X\=MAIN,
224 get_start_expr(X,NewState),
225 ? normalise_cspm_state(NewState,NormalisedNewState).
226 cspm_transition(root,start_cspm(X),NormalisedNewState) :- cspm_main_process(MAIN),
227 get_preference(cspm_animate_all_processes,true),
228 animatable_process(X),
229 X\=MAIN,
230 get_start_expr(X,NewState),
231 normalise_cspm_state(NewState,NormalisedNewState).
232 cspm_transition(root,io([V1],print,no_loc_info_available),root) :-
233 ? cspPrintCompiled(Expr,CompiledExpr), debug_println(9,cspPrintCompiled(Expr,CompiledExpr)),
234 nl, translate:print_csp_value(Expr),
235 print(' == '), nl, print(' '),
236 ? force_evaluate_argument(CompiledExpr,V1),
237 translate:print_csp_value(V1),nl.
238 cspm_transition(root,no_process_to_animate,root) :-
239 ( get_preference(cspm_animate_all_processes,true) ->
240 \+ animatable_process(_)
241 ? ; \+ animatable_process_without_arguments(_)).
242 cspm_transition(State,Action,NormalisedNewState) :- State \= root,
243 %print(comp),nl,
244 ? cspm_trans_enum(State,Action,NewState),
245 ? normalise_cspm_state(NewState,NormalisedNewState).
246 %(ActionS = io(V,Ch,_Span) -> Action = io(V,Ch) ; Action=ActionS).
247 %print(new(NewState)),nl. /* TO DO: Normalise */
248
249 cspm_property(State,Property) :-
250 translate_cspm_state(State,Property).
251
252 /* --------------- CSP ----------------- */
253
254
255 get_start_expr(Proc,val_of(Proc,Span)) :- get_symbol_span(Proc,Span).
256
257
258
259 csp_initialisation_for_b(NewState) :- cspm_main_process(MAIN),
260 ? (animatable_process_without_arguments(MAIN) -> get_start_expr(MAIN,NewState);
261 (animatable_process_without_arguments(X)
262 -> add_error(csp_transition_for_b,'No MAIN process in the CSP file! I am animating:',X),
263 NewState = val_of(X)
264 ; add_error(csp_transition_for_b,'No animatable process in the CSP file!'), NewState = stop)
265 ).
266
267 csp_transition_for_b(State,Ch,Args,Action,NewState) :- State \= root,
268 % print(cspm_trans_enum(State,Action,NewState)),nl,
269 ? cspm_trans_enum(State,Action,NewState), %% TO DO: delay enumeration until B operation has been setup ?
270 % print(cspm_trans_enum(Action,NewState)),nl,
271 decompose_event(Action,Ch,Args).
272 % print(b(Ch,BArgs)),nl.
273
274
275 /* needed: an any operation: map any operation<------------- */
276
277 decompose_event(io(V,Ch,_Src),Ch,V).
278 decompose_event(tau(S),tau(S),[]).
279 %% decompose_event(i(S),i(S),[]). %% deprecated
280 decompose_event(tick(S),tick(S),[]).
281
282 generate_b_operationargs_from_csp(V,BArgs) :- l_copy_args_to_b(V,BArgs).
283
284
285 l_copy_args_to_b(tail_in(X),[Y]) :- translate_and_normalise_arg_to_b(X,Y).
286 l_copy_args_to_b([],[]).
287 l_copy_args_to_b([HCSP|T],[HB|TB]) :-
288 copy_args_to_b(HCSP,HB),
289 l_copy_args_to_b(T,TB).
290
291 copy_args_to_b(dot(X),Y) :- !,translate_and_normalise_arg_to_b(X,Y). /* is this still required with the new eval ?? */
292 copy_args_to_b(in(X),Y) :- !,translate_and_normalise_arg_to_b(X,Y).
293 copy_args_to_b(out(X),Y) :- !,translate_and_normalise_arg_to_b(X,Y).
294 copy_args_to_b(X,Y) :- translate_and_normalise_arg_to_b(X,Y).
295
296 :- use_module(store,[normalise_value_for_var/4]).
297
298 translate_and_normalise_arg_to_b(CSP,BN) :- translate_arg_to_b(CSP,B), normalise_value_for_var(csp,true,B,BN).
299
300 :- use_module(tools,[print_message/1, convert_list_into_pairs/2]).
301 :- use_module(custom_explicit_sets,[construct_avl_from_lists/2]).
302
303 %translate_arg_to_b(X,Y) :- print(translate_arg_to_b(X,Y)),nl,fail.
304 translate_arg_to_b(X,X) :- var(X),!.
305 translate_arg_to_b(X,int(X)) :- number(X),!,print_message(converted_int(X)).
306 translate_arg_to_b(fd(N,S),fd(N,S)) :- !. /* copy B SET element across */
307 translate_arg_to_b(string(S),string(S)) :- !. /* copy B STRING element across */
308 translate_arg_to_b(int(N),int(N)) :- !.
309 translate_arg_to_b(true,pred_true /* bool_true */) :- !.
310 translate_arg_to_b(false,pred_false /* bool_false */) :- !.
311 translate_arg_to_b(global_set(N),global_set(N)) :- !.
312 translate_arg_to_b(freetype(N),freetype(N)) :- !.
313 translate_arg_to_b(avl_set(N),avl_set(N)) :- !.
314 translate_arg_to_b(closure(A,B,C),closure(A,B,C)) :- !.
315 translate_arg_to_b(closure(A,B,C,E),closure(A,B,C,E)) :- !.
316 translate_arg_to_b(setValue(S),R) :- !, translate_arg_to_b(S,R1),
317 construct_avl_from_lists(R1,R).
318 %sort(R1,R). % IS SORTING NECESSARY?; we could translate to AVL
319 translate_arg_to_b(list(L),R) :- !, translate_list_to_b(L,1,R1),
320 custom_explicit_sets:construct_avl_from_lists(R1,R).
321 translate_arg_to_b([],[]) :- !.
322 translate_arg_to_b([H|T],[TH|TT]) :- !,translate_arg_to_b(H,TH), translate_arg_to_b(T,TT).
323 translate_arg_to_b((H,T),(TH,TT)) :- !,translate_arg_to_b(H,TH), translate_arg_to_b(T,TT).
324 translate_arg_to_b(na_tuple(L),Res) :- !,l_translate_arg_to_b(L,TL),
325 convert_list_into_pairs(TL,Res).
326 translate_arg_to_b(Constant,BRep) :- translate_b_constant(Constant,BRep),!. /* clause necessary?? */
327 translate_arg_to_b(term(Constant),BRep) :- translate_b_constant(Constant,BRep),!.
328 % TO DO: treat floats/reals
329 translate_arg_to_b(term(N),term(N)) :- !.
330 translate_arg_to_b(DeferredSetEl,FD) :-
331 is_deferred_set_element_name(DeferredSetEl,FD),!.
332 translate_arg_to_b(X,string(X)) :- atomic(X),!. % if the identfier X is not known: translate it to a string
333 % TO DO: some static checking: if no operation has a STRING parameter type, then we can skip this clause and generate an error message straightaway
334 translate_arg_to_b(X,term(X)) :- add_error(translate_arg_to_b,'Unknown CSP datatype, cannot convert to B:',X).
335 /* extend for other types */
336
337 translate_list_to_b([],_,[]).
338 translate_list_to_b([H|T],Nr,[(int(Nr),TH)|TT]) :- translate_arg_to_b(H,TH),
339 N1 is Nr+1, translate_list_to_b(T,N1,TT).
340
341 l_translate_arg_to_b([],[]).
342 l_translate_arg_to_b([H|T],[TH|TT]) :- translate_arg_to_b(H,TH),
343 l_translate_arg_to_b(T,TT).
344
345 :- use_module(tools,[safe_atom_codes/2]).
346 :- use_module(self_check).
347 :- assert_must_succeed( (xtl_interface:is_deferred_set_element_name('Code1',R),R=fd(1,'Code')) ).
348 :- assert_must_fail( xtl_interface:is_deferred_set_element_name('CodeXX',_R) ).
349 is_deferred_set_element_name(DeferredSetEl,fd(Nr,Set)) :- atomic(DeferredSetEl),
350 b_global_sets:b_global_deferred_set(Set), atom_codes(Set,SetCodes),
351 append(SetCodes,NrCodes,DC),
352 safe_atom_codes(DeferredSetEl,DC),
353 catch(number_codes(Nr,NrCodes),_,fail).
354
355 :- use_module(b_global_sets,[b_global_set/1, all_elements_of_type/2]).
356
357 translate_b_constant(GS,BRep) :- nonvar(GS),b_global_set(GS),all_elements_of_type(GS,BRep),!.
358 translate_b_constant(Constant,BRep) :- nonvar(Constant),b_global_sets:lookup_global_constant(Constant,BRep),!.