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