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