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