1 % (c) 2009-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(xtl_interface, [open_xtl_file/1,
6 xtl_transition/3, xtl_transition/4,
7 xtl_transition_with_symbolic/3, xtl_transition_with_symbolic/4,
8 xtl_symbolic_transition_potentially_enabled/2, xtl_symbolic_transition_potentially_enabled/3,
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 xtl_search_scope/1, xtl_set_search_scope/1,
22 xtl_main_file_name/1,
23
24 csp_initialisation_for_b/1,
25 csp_transition_for_b/5,
26 generate_b_operationargs_from_csp/2,
27
28 %open_promela_file/1, promela_transition/3, promela_property/2,
29 %open_smv_file/1, smv_transition/3, smv_property/2, % SMV mode broken
30
31 open_cspm_file/1, last_opened_cspm_file/1,
32 cspm_transition/3,
33 cspm_property/2,
34 set_cspm_main_process/1,
35
36 get_disprover_po/6,
37 reset_xtl_interface/0]).
38
39
40 :- use_module(module_information).
41 :- use_module(self_check).
42 :- module_info(group,animator).
43 :- module_info(description,'Provides an interface to the non-B animators depending on animation-mode.').
44
45 /* Typically the XTL specifications reside in a .P file with the following predicates
46 start/1 -> defining the initial states
47 trans/3 -> defining the transitions between states transition(Action, StateBefore, StateAfter)
48 prop/2 -> defining properties of states
49
50 start/2, trans/4: same as start/1 and trans/3, but last argument allows to provide a list of
51 additional transition infos (stored in state_space), e.g. [description('Desc')]
52
53 For CSP specifications the interpreter is integrated into ProB
54
55 */
56
57 /* --------------- XTL ----------------- */
58 :- volatile prop/2, trans/3, trans/4, trans_prop/2, start/1, start/2, symb_trans/3, symb_trans/4, symb_trans_enabled/2.
59 :- volatile nr_state_properties/1, animation_function_result/2, animation_image/2.
60 :- volatile animation_image_click_transition/6, animation_image_right_click_transition/3.
61 :- volatile animation_image_right_click_transition/4.
62 :- volatile heuristic_function_active/0, heuristic_function_result/2.
63 :- volatile prob_pragma_string/2, prob_game_info/3.
64 :- volatile xtl_search_scope/1, xtl_main_file_name/1.
65 :- volatile disprover_po/6.
66 :- dynamic prop/2.
67 :- dynamic trans/3, trans/4.
68 :- dynamic trans_prop/2.
69 :- dynamic symb_trans/3, symb_trans/4, symb_trans_enabled/2.
70 :- dynamic start/1, start/2.
71 :- dynamic nr_state_properties/1.
72 :- dynamic animation_function_result/2.
73 :- dynamic animation_image/2.
74 :- dynamic animation_image_click_transition/6, animation_image_right_click_transition/3.
75 :- dynamic animation_image_right_click_transition/4.
76 :- dynamic heuristic_function_active/0.
77 :- dynamic heuristic_function_result/2.
78 :- dynamic prob_pragma_string/2, prob_game_info/3.
79 :- dynamic xtl_search_scope/1, xtl_main_file_name/1.
80 :- dynamic disprover_po/6.
81
82
83 % the following imports are required so that XTL .P files can make use of these functions:
84 :- use_module(library(lists)).
85 :- use_module(library(between)).
86 :- use_module(library(ordsets)).
87 :- use_module(library(samsort)).
88 :- use_module(library(random)).
89 :- use_module(library(avl)).
90 :- use_module(library(heaps)).
91 :- use_module(tools_portability, [exists_source/1]).
92 :- if(exists_source(library(logarr))).
93 :- use_module(library(logarr)). % not yet available in SWI Prolog
94 :- endif.
95
96 % ProB utilities (which can also be used by XTL code)
97 :- use_module(error_manager).
98 :- use_module(preferences,[get_preference/2]).
99 :- use_module(debug).
100 :- use_module(tools).
101
102 :- if(\+ current_prolog_flag(dialect, sicstus)).
103 abolish_all([]).
104 abolish_all([Pred|Preds]) :-
105 abolish(Pred),
106 abolish_all(Preds).
107 :- else.
108 abolish_all(Preds) :-
109 abolish(Preds, [force(true),tree(true)]).
110 :- endif.
111
112 open_xtl_file(File) :-
113 abolish_all([prop/2, trans/3, trans/4, trans_prop/2, symb_trans/3, symb_trans/4, symb_trans_enabled/2, start/1, start/2, nr_state_properties/1]),
114 abolish_all([animation_image/2,animation_function_result/2,
115 animation_image_click_transition/6,animation_image_right_click_transition/3,
116 animation_image_right_click_transition/4,
117 heuristic_function_active/0, heuristic_function_result/2,
118 prob_pragma_string/2,
119 prob_game_info/3]),
120 abolish_all([xtl_search_scope/1, xtl_main_file_name/1]),
121 abolish_all([disprover_po/6]),
122 assertz((prop(_,_) :- fail)),
123 assertz((trans(_,_,_) :- fail)),
124 assertz((trans(_,_,_,_) :- fail)),
125 assertz((trans_prop(_,_) :- fail)),
126 assertz((symb_trans(_,_,_) :- fail)),
127 assertz((symb_trans(_,_,_,_) :- fail)),
128 assertz((symb_trans_enabled(_,_) :- fail)),
129 assertz((start(_) :- fail)),
130 assertz((start(_,_) :- fail)),
131 assertz((nr_state_properties(_) :- fail)),
132 assertz((heuristic_function_active :- fail)),
133 assertz((heuristic_function_result(_,_) :- fail)),
134 assertz((animation_image(_,_) :- fail)),
135 assertz((animation_function_result(_,_) :- fail)),
136 assertz((animation_image_click_transition(_,_,_,_,_,_) :- fail)),
137 assertz((animation_image_right_click_transition(_,_,_) :- fail)),
138 assertz((animation_image_right_click_transition(_,_,_,_) :- fail)),
139 assertz((prob_pragma_string(_,_) :- fail)),
140 assertz((prob_game_info(_,_,_) :- fail)),
141 assertz((xtl_search_scope(_) :- fail)),
142 assertz(xtl_main_file_name(File)),
143 assertz((disprover_po(_,_,_,_,_,_) :- fail)), % to detect sequent_prover mode
144
145 debug_println(9,open_xtl_file(File)),
146 consult_without_redefine_warning(File),
147 check_and_load_po_file(File),
148 debug_println(9,new_xtl_file(File)),
149 (xtl_get_definition_string('SCOPE',ScopePredStr)
150 -> (xtl_set_search_scope(ScopePredStr) -> true
151 ; add_error(xtl,'Could not set the SCOPE of the XTL specification: ',ScopePredStr)) % do not fail loading the file
152 ; true).
153
154 xtl_transition(State,Operation,NewState) :-
155 xtl_transition(State,Operation,NewState,_).
156 xtl_transition(State,Operation,NewState,Infos) :-
157 (get_preference(xtl_safe_mode, true)
158 -> xtl_transition_safe(State,Operation,NewState,Infos)
159 ? ; xtl_transition_unsafe(State,Operation,NewState,Infos)).
160 ?xtl_transition_unsafe(root,start_xtl_system,NewState,Infos) :- get_start(NewState,Infos).
161 xtl_transition_unsafe(State,Operation,NewState,Infos) :-
162 ? State \= root, get_trans(Operation,State,NewState,Infos).
163 xtl_transition_safe(State,Operation,NewState,Infos) :-
164 (ground(State) -> true ; add_error(xtl,'Non-ground XTL state:',State),fail),
165 (State=root
166 -> Operation=start_xtl_system, get_start(NewState,Infos)
167 ; get_trans(Operation,State,NewState,Infos),
168 ((atom(Operation) ; compound(Operation)) -> true ; add_error(xtl,'Illegal XTL operation:',Operation),fail),
169 (ground(Operation) -> true ; add_error(xtl,'Non-ground XTL operation:',Operation),fail)
170 ),
171 (ground(NewState) -> true ; add_error(xtl,'Non-ground XTL destination state:',NewState), fail).
172
173 xtl_transition_with_symbolic(State,Operation,NewState) :-
174 xtl_transition_with_symbolic(State,Operation,NewState,_).
175 xtl_transition_with_symbolic(State,Operation,NewState,Infos) :-
176 ground(Operation),
177 get_symb_trans(Operation,State,NewState,Infos), !. % TODO: use symbolic trans only if NewState is ground/nonvar
178 % TODO safe ground check for states
179 xtl_transition_with_symbolic(State,Operation,NewState,Infos) :-
180 ? xtl_transition(State,Operation,NewState,Infos).
181
182 ?get_start(State,[]) :- start(State).
183 ?get_start(State,Infos) :- start(State,Infos),
184 (is_list(Infos) -> true ; add_error(xtl,'Transition info is not a list:',Infos), fail).
185 ?get_start(_,_) :- \+ start(_), \+start(_,_), add_error(xtl,'No XTL start state defined'), fail.
186
187 ?get_trans(Operation,State,NewState,[]) :- trans(Operation,State,NewState), check_trans_params(Operation).
188 ?get_trans(Operation,State,NewState,Infos) :- trans(Operation,State,NewState,Infos),
189 (is_list(Infos) -> true ; add_error(xtl,'Transition info is not a list:',Infos), fail),
190 check_trans_params(Operation).
191
192 get_symb_trans(Operation,State,NewState,[]) :- symb_trans(Operation,State,NewState), check_trans_params(Operation).
193 get_symb_trans(Operation,State,NewState,Infos) :- symb_trans(Operation,State,NewState,Infos),
194 (is_list(Infos) -> true ; add_error(xtl,'Transition info is not a list:',Infos), fail),
195 check_trans_params(Operation).
196
197 % check that number of specified parameters matches the arity of the transition term and
198 % that only one declaration of parameter names per name is provided
199 ?check_trans_params(_) :- \+ trans_prop(_,param_names(_)), !.
200 check_trans_params(OpTerm) :-
201 functor(OpTerm,Name,Ar),
202 (trans_prop(Name,param_names(_))
203 -> xtl_transition_parameters(Name,Paras), % fails with an error for conflicting declarations
204 length(Paras,NrP),
205 (Ar =:= NrP -> true ; add_error(xtl,'Number of specified parameter names does not match the arity of transition:',Name),fail)
206 ; true). % no params
207
208 :- assert_must_succeed((assertz(trans_prop(selfcheck_t,param_names([x]))), assertz(trans_prop(selfcheck_t,param_names([x]))),
209 findall(P,xtl_transition_parameters(selfcheck_t,P),L), retractall(trans_prop(selfcheck_t,_)), L == [[x]])).
210 :- assert_must_succeed((assertz(trans_prop(selfcheck_t,param_names([x]))), assertz(trans_prop(selfcheck_t,param_names([y]))),
211 (check_trans_params(selfcheck_t(1)) -> R=accepted ; R=rejected), retractall(trans_prop(selfcheck_t,_)),
212 error_manager:clear_errors_with_source(xtl), R == rejected)). % conflicting declarations
213 % succeeds at most once per transition name; identical duplicate declarations are allowed
214 xtl_transition_parameters(TransName,ParaNames) :-
215 (nonvar(TransName) -> Names=[TransName]
216 ; findall(N,trans_prop(N,param_names(_)),Ns), sort(Ns,Names)),
217 member(TransName,Names),
218 findall(P,trans_prop(TransName,param_names(P)),Ps),
219 sort(Ps,SPs),
220 (SPs = [ParaNames] -> true
221 ; SPs = [_,_|_] -> add_error(xtl,'Multiple parameter declarations for transition name:',TransName), fail).
222
223 get_xtl_paras_as_identifiers(OpName,ParaIds) :-
224 xtl_transition_parameters(OpName,ParaNames), !,
225 findall(b(identifier(Name),string,[]), member(Name,ParaNames), ParaIds).
226 get_xtl_paras_as_identifiers(_,[]).
227
228 xtl_symbolic_transition_potentially_enabled(TransName,State) :-
229 xtl_symbolic_transition_potentially_enabled(TransName,_,State).
230 xtl_symbolic_transition_potentially_enabled(TransName,ParamValues,State) :-
231 symb_trans_enabled(TransTerm,State),
232 TransTerm=..[TransName|Params], % 'none' is recognised by ProB2 as empty parameter value
233 (trans_prop(TransName,param_names(_))
234 -> xtl_transition_parameters(TransName,ParaNames),
235 (\+ ((Params=[] ; same_length(ParaNames,Params)), ground(Params))
236 -> add_message(xtl,'Number of parameters for symb_trans_enabled does not match or parameters are not ground, will be ignored:',TransTerm),
237 ParamValues=[]
238 ; ParamValues=Params)
239 ; ground(Params) -> ParamValues=Params % no declared parameter names: the default names para1,... are used
240 ; add_message(xtl,'Parameters for symb_trans_enabled are not ground, will be ignored:',TransTerm),
241 ParamValues=[]).
242
243 xtl_property(State,Property) :-
244 ? (get_preference(xtl_safe_mode, true) -> xtl_property_safe(State,Property) ; xtl_property_unsafe(State,Property)).
245 xtl_property_unsafe(State,Property) :-
246 ? State \= root, get_prop(State,Property).
247 xtl_property_safe(State,Property) :-
248 State \= root,
249 get_prop(State,Property),
250 (ground(Property)-> true ; add_error(xtl,'Non-ground XTL property:',Property), fail).
251
252 ?get_prop(State,Property) :- if(prop(State,Property), true, Property='No XTL properties defined').
253
254 % special Property is unsafe; see is_xtl_error_state in model_checker.pl
255 % Note for XTL we do not use not_invariant_checked/1 facts
256 ?xtl_invariant_violated(State) :- xtl_property(State,unsafe).
257 xtl_goal_found(State) :- xtl_property(State,goal).
258
259 xtl_nr_state_properties(Nr) :- nr_state_properties(Nr).
260
261 xtl_animation_function_result(State,AnimationMatrix) :- State \= root,
262 animation_function_result(State,AnimationMatrix).
263
264 xtl_animation_image(Nr,PathToGif) :-
265 %on_exception(error(existence_error(_,_),_),
266 animation_image(Nr,PathToGif).
267
268 % return a transition template to execute for simple clicks (From=To) or drags
269 % OperationTemplate can either be the template of an operation to match or a list of such templates
270 % (the operations will then be executed in order)
271 xtl_animation_image_click_transition(FromX,FromY,ToX,ToY,OperationTemplate,Image) :-
272 animation_image_click_transition(FromX,FromY,ToX,ToY,OperationTemplate,Image).
273
274 xtl_animation_image_right_click_transition(X,Y,OperationTemplate,State) :-
275 animation_image_right_click_transition(X,Y,OperationTemplate,State).
276 xtl_animation_image_right_click_transition(X,Y,OperationTemplate,_) :-
277 animation_image_right_click_transition(X,Y,OperationTemplate).
278
279 xtl_heuristic_function_active :-
280 heuristic_function_active.
281 xtl_heuristic_function_result(State,int(IntegerVal)) :- State \= root,
282 heuristic_function_result(State,Res),
283 (nonvar(Res), Res=int(R), integer(R) -> IntegerVal=R
284 ; integer(Res) -> IntegerVal=Res
285 ; add_error(xtl_heuristic_function_result,'heuristic_function_result must be integer: ',Res),fail
286 ).
287
288 xtl_game_info(Key,State,Value) :- prob_game_info(Key,State,Value).
289 %xtl_game_over(State) :- prob_game_info('GAME_OVER',State,true).
290 %xtl_game_value(State,Value) :- prob_game_info('GAME_VALUE',State,Value).
291 %xtl_game_player(State,Player) :- prob_game_info('GAME_PLAYER',State,Player).
292
293
294 % way to mimic DEFINITION Strings in XTL mode, such as ASSERT_LTL
295 xtl_get_definition_string(Def_Name,DefString) :-
296 ? prob_pragma_string(N,S),
297 get_atom_string(N,Def_Name),
298 get_atom_string(S,DefString).
299
300 :- use_module(tools,[safe_atom_codes/2]).
301 get_atom_string(Atom,Res) :- atom(Atom),!,Res=Atom.
302 get_atom_string([H|T],Res) :- safe_atom_codes(Atom,[H|T]), !, Res=Atom. % transform "abc" into 'abc'
303 get_atom_string(R,R).
304
305 % set search_scope, restricting model checking to states which satisfy the SCOPE DEFINITION predicate
306 xtl_set_search_scope(Goal) :-
307 (predicate_property(xtl_search_scope(_),dynamic) % can be static if it is defined by the XTL spec
308 -> bmachine:b_parse_machine_predicate(Goal,[prob_ids(visible),variables,external_library(all_available_libraries)],TypedPred),
309 retractall(xtl_search_scope(_)),
310 assertz(xtl_search_scope(TypedPred))
311 ; add_warning(xtl_set_search_scope,'Failed to set XTL search scope (xtl_search_scope is already defined): ',Goal)).
312
313 consult_without_redefine_warning(File) :-
314 get_set_optional_prolog_flag(redefine_warnings, Old, off),
315 get_set_optional_prolog_flag(single_var_warnings, Old2, off),
316 (catch(my_compile(File),
317 error(existence_error(_,_),_),
318 add_error_fail(xtl,'XTL File does not exist:',File))
319 -> OK=true ; OK=false),
320 get_set_optional_prolog_flag(redefine_warnings, _, Old),
321 get_set_optional_prolog_flag(single_var_warnings, _, Old2),
322 OK=true.
323
324 my_compile(F) :- % it seems it is ok to call compile also in probcli binary; it may do consult though
325 compile(F).
326
327 :- use_module(seqproversrc(sequent_prover),[initialise_for_po_file/1]).
328 :- use_module(tools,[get_filename_extension/2]).
329 check_and_load_po_file(File) :-
330 get_filename_extension(File,Ext),
331 check_is_po_file(File,Ext), !,
332 specfile:set_animation_minor_mode(sequent_prover),
333 initialise_for_po_file(File).
334 check_and_load_po_file(_).
335
336 check_is_po_file(_,probpo) :- !. % Disprover PO file
337 check_is_po_file(File,Ext) :-
338 open(File,read,Stream),
339 call_cleanup(stream_looks_like_po_file(Stream), close(Stream)),
340 ajoin(['File with extension .',Ext,' looks like a PO file and is loaded in sequent prover mode. Consider changing the extension to .probpo.'],Msg),
341 add_warning(open_xtl_file,Msg).
342
343 :- use_module(library(codesio),[open_codes_stream/2]).
344 :- assert_must_fail((open_codes_stream("foo(a b).\nstart(s0).\n",S), stream_looks_like_po_file(S))). % used to raise a syntax error
345 :- assert_must_succeed((open_codes_stream("generated(a,b).\nproject_name(p).\nmachine_name(m).\ndisprover_po(a,b,c,d,e,f).\n",S),
346 stream_looks_like_po_file(S))).
347 stream_looks_like_po_file(Stream) :-
348 catch(( read(Stream,generated(_,_)),
349 read(Stream,project_name(_)),
350 read(Stream,machine_name(_)),
351 read(Stream,disprover_po(_,_,_,_,_,_)) ),
352 error(syntax_error(_),_), fail). % not a PO file; the syntax error is reported when compiling the file
353
354 % see disprover_test_runner [load_po_file/1,get_disprover_po/6]
355 get_disprover_po(FName,Context,Goal,All,Sel,Status) :-
356 ? xtl_interface:disprover_po(Name,Context,Goal,All,Sel,Status),
357 fix_rodin_name(Name,FName).
358 linefeed(10).
359 linefeed(13).
360 % sometimes Rodin POs have a newline before the slash; we remove this (see e.g., test 2023)
361 fix_rodin_name(POName,FixedName) :- atom_codes(POName,Cs),
362 exclude(linefeed,Cs,NewCs),
363 atom_codes(FixedName,NewCs).
364
365 /* --------------- Promela ----------------- */
366
367 %:- use_module('promela/h_int').
368
369 /* --------------- SMV ----------------- */
370
371 % :- use_module('smv/smv_trans').
372
373
374 /* --------------- CSP-M ----------------- */
375
376 :- use_module(probcspsrc(haskell_csp),[parse_and_load_cspm_file/1,
377 cspm_trans_enum/3,
378 animatable_process/1, animatable_process_without_arguments/1,
379 get_symbol_span/2,force_evaluate_argument/2,normalise_cspm_state/2]).
380 :- use_module(probcspsrc(haskell_csp_analyzer),[cspPrintCompiled/2]).
381 :- use_module(probsrc(translate),[translate_cspm_state/2]).
382
383 :- dynamic last_opened_cspm_file/1. % useful for csp_and_b mode
384
385 open_cspm_file(File) :-
386 retractall(last_opened_cspm_file(_)),
387 debug_println(15,open_cspm_file(File)), flush_output(user_output),
388 parse_and_load_cspm_file(File),
389 assertz(last_opened_cspm_file(File)).
390
391 :- dynamic cspm_main_process/1.
392 cspm_main_process('MAIN').
393 set_cspm_main_process(M) :-
394 retractall(cspm_main_process(_)),
395 assertz(cspm_main_process(M)).
396
397 reset_xtl_interface :- retractall(last_opened_cspm_file(_)),
398 reset_cspm_main_process.
399 reset_cspm_main_process :- set_cspm_main_process('MAIN').
400
401 :- use_module(eventhandling,[register_event_listener/3]).
402 :- register_event_listener(clear_specification,reset_xtl_interface,
403 'Reset XTL Interface.').
404
405 cspm_transition(root,start_cspm_MAIN,NormalisedNewState) :-
406 cspm_main_process(MAIN),
407 ? animatable_process_without_arguments(MAIN),
408 get_start_expr(MAIN,NewState),
409 ? normalise_cspm_state(NewState,NormalisedNewState).
410 cspm_transition(root,start_cspm(X),NormalisedNewState) :- cspm_main_process(MAIN),
411 start_processes_without_arguments(MAIN),
412 ? animatable_process_without_arguments(X),
413 X\=MAIN,
414 get_start_expr(X,NewState),
415 ? normalise_cspm_state(NewState,NormalisedNewState).
416 cspm_transition(root,start_cspm(X),NormalisedNewState) :- cspm_main_process(MAIN),
417 get_preference(cspm_animate_all_processes,true),
418 animatable_process(X),
419 X\=MAIN,
420 \+ (start_processes_without_arguments(MAIN), % already provided by the clause above
421 animatable_process_without_arguments(X)),
422 get_start_expr(X,NewState),
423 normalise_cspm_state(NewState,NormalisedNewState).
424 cspm_transition(root,io([V1],print,no_loc_info_available),root) :-
425 ? cspPrintCompiled(Expr,CompiledExpr), debug_println(9,cspPrintCompiled(Expr,CompiledExpr)),
426 nl, translate:print_csp_value(Expr),
427 print(' == '), nl, print(' '),
428 ? force_evaluate_argument(CompiledExpr,V1),
429 translate:print_csp_value(V1),nl.
430 cspm_transition(root,no_process_to_animate,root) :-
431 ( get_preference(cspm_animate_all_processes,true) ->
432 \+ animatable_process(_)
433 ? ; \+ animatable_process_without_arguments(_)).
434 cspm_transition(State,Action,NormalisedNewState) :- State \= root,
435 %print(comp),nl,
436 ? cspm_trans_enum(State,Action,NewState),
437 ? normalise_cspm_state(NewState,NormalisedNewState).
438 %(ActionS = io(V,Ch,_Span) -> Action = io(V,Ch) ; Action=ActionS).
439 %print(new(NewState)),nl. /* TO DO: Normalise */
440
441 % true if all processes without arguments should get a start transition (succeeds at most once)
442 start_processes_without_arguments(MAIN) :-
443 (get_preference(cspm_animate_all_processes_without_arguments,true) -> true
444 ? ; \+ animatable_process_without_arguments(MAIN)).
445
446 cspm_property(State,Property) :-
447 translate_cspm_state(State,Property).
448
449 /* --------------- CSP ----------------- */
450
451
452 get_start_expr(Proc,val_of(Proc,Span)) :- get_symbol_span(Proc,Span).
453
454
455
456 csp_initialisation_for_b(NewState) :- cspm_main_process(MAIN),
457 ? (animatable_process_without_arguments(MAIN) -> get_start_expr(MAIN,NewState);
458 (animatable_process_without_arguments(X)
459 -> add_error(csp_initialisation_for_b,'No MAIN process in the CSP file! I am animating:',X),
460 get_start_expr(X,NewState) % val_of/2, as used by the CSP interpreter
461 ; add_error(csp_initialisation_for_b,'No animatable process in the CSP file!'),
462 NewState = stop(no_loc_info_available))
463 ).
464
465 csp_transition_for_b(State,Ch,Args,Action,NewState) :- State \= root,
466 % print(cspm_trans_enum(State,Action,NewState)),nl,
467 ? cspm_trans_enum(State,Action,NewState), %% TO DO: delay enumeration until B operation has been setup ?
468 % print(cspm_trans_enum(Action,NewState)),nl,
469 decompose_event(Action,Ch,Args).
470 % print(b(Ch,BArgs)),nl.
471
472
473 /* needed: an any operation: map any operation<------------- */
474
475 decompose_event(io(V,Ch,_Src),Ch,V).
476 decompose_event(tau(S),tau(S),[]).
477 %% decompose_event(i(S),i(S),[]). %% deprecated
478 decompose_event(tick(S),tick(S),[]).
479
480 generate_b_operationargs_from_csp(V,BArgs) :- l_copy_args_to_b(V,BArgs).
481
482
483 l_copy_args_to_b(tail_in(X),[Y]) :- translate_and_normalise_arg_to_b(X,Y).
484 l_copy_args_to_b([],[]).
485 l_copy_args_to_b([HCSP|T],[HB|TB]) :-
486 copy_args_to_b(HCSP,HB),
487 l_copy_args_to_b(T,TB).
488
489 copy_args_to_b(dot(X),Y) :- !,translate_and_normalise_arg_to_b(X,Y). /* is this still required with the new eval ?? */
490 copy_args_to_b(in(X),Y) :- !,translate_and_normalise_arg_to_b(X,Y).
491 copy_args_to_b(out(X),Y) :- !,translate_and_normalise_arg_to_b(X,Y).
492 copy_args_to_b(X,Y) :- translate_and_normalise_arg_to_b(X,Y).
493
494 :- use_module(store,[normalise_value_for_var/4]).
495
496 translate_and_normalise_arg_to_b(CSP,BN) :- translate_arg_to_b(CSP,B), normalise_value_for_var(csp,true,B,BN).
497
498 :- use_module(tools,[print_message/1, convert_list_into_pairs/2]).
499 :- use_module(custom_explicit_sets,[construct_avl_from_lists/2]).
500
501 %translate_arg_to_b(X,Y) :- print(translate_arg_to_b(X,Y)),nl,fail.
502 translate_arg_to_b(X,X) :- var(X),!.
503 translate_arg_to_b(X,int(X)) :- number(X),!.
504 translate_arg_to_b(fd(N,S),fd(N,S)) :- !. /* copy B SET element across */
505 translate_arg_to_b(string(S),string(S)) :- !. /* copy B STRING element across */
506 translate_arg_to_b(int(N),int(N)) :- !.
507 translate_arg_to_b(true,pred_true /* bool_true */) :- !.
508 translate_arg_to_b(false,pred_false /* bool_false */) :- !.
509 translate_arg_to_b(global_set(N),global_set(N)) :- !.
510 translate_arg_to_b(freetype(N),freetype(N)) :- !.
511 translate_arg_to_b(avl_set(N),avl_set(N)) :- !.
512 translate_arg_to_b(closure(A,B,C),closure(A,B,C)) :- !.
513 translate_arg_to_b(closure(A,B,C,E),closure(A,B,C,E)) :- !.
514 translate_arg_to_b(setValue(S),R) :- !, translate_arg_to_b(S,R1),
515 construct_avl_from_lists(R1,R).
516 %sort(R1,R). % IS SORTING NECESSARY?; we could translate to AVL
517 translate_arg_to_b(list(L),R) :- !, translate_list_to_b(L,1,R1),
518 custom_explicit_sets:construct_avl_from_lists(R1,R).
519 translate_arg_to_b([],[]) :- !.
520 translate_arg_to_b([H|T],[TH|TT]) :- !,translate_arg_to_b(H,TH), translate_arg_to_b(T,TT).
521 translate_arg_to_b((H,T),(TH,TT)) :- !,translate_arg_to_b(H,TH), translate_arg_to_b(T,TT).
522 translate_arg_to_b(na_tuple(L),Res) :- !,l_translate_arg_to_b(L,TL),
523 convert_list_into_pairs(TL,Res).
524 translate_arg_to_b(Constant,BRep) :- translate_b_constant(Constant,BRep),!. /* clause necessary?? */
525 translate_arg_to_b(term(Constant),BRep) :- translate_b_constant(Constant,BRep),!.
526 % TO DO: treat floats/reals
527 translate_arg_to_b(term(N),term(N)) :- !.
528 translate_arg_to_b(DeferredSetEl,FD) :-
529 is_deferred_set_element_name(DeferredSetEl,FD),!.
530 translate_arg_to_b(X,string(X)) :- %write(unknown_csp_term(X)),nl,
531 atomic(X),!. % if the identfier X is not known: translate it to a string
532 % 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
533 translate_arg_to_b(X,term(X)) :-
534 add_error(translate_arg_to_b,'Unknown CSP datatype, cannot convert to B:',X).
535 /* extend for other types */
536
537 translate_list_to_b([],_,[]).
538 translate_list_to_b([H|T],Nr,[(int(Nr),TH)|TT]) :- translate_arg_to_b(H,TH),
539 N1 is Nr+1, translate_list_to_b(T,N1,TT).
540
541 l_translate_arg_to_b([],[]).
542 l_translate_arg_to_b([H|T],[TH|TT]) :- translate_arg_to_b(H,TH),
543 l_translate_arg_to_b(T,TT).
544
545 :- use_module(self_check).
546 :- use_module(b_global_sets,[b_global_set/1, all_elements_of_type/2,
547 b_partially_deferred_set/1, b_global_deferred_set/1]).
548
549 :- assert_must_succeed( (xtl_interface:is_deferred_set_element_name('Code1',R),R=fd(1,'Code')) ).
550 :- assert_must_fail( xtl_interface:is_deferred_set_element_name('CodeXX',_R) ).
551 is_deferred_set_element_name(DeferredSetEl,fd(Nr,Set)) :- atomic(DeferredSetEl),
552 (b_global_deferred_set(Set) ; b_partially_deferred_set(Set)),
553 atom_codes(Set,SetCodes),
554 append(SetCodes,NrCodes,DC),
555 safe_atom_codes(DeferredSetEl,DC),
556 catch(number_codes(Nr,NrCodes),_,fail),
557 integer(Nr), Nr>0, number_codes(Nr,NrCodes). % only canonical positive numbers, not Code-1 or Code1.5
558
559
560 translate_b_constant(GS,BRep) :- nonvar(GS),b_global_set(GS),all_elements_of_type(GS,BRep),!.
561 translate_b_constant(Constant,BRep) :- nonvar(Constant),b_global_sets:lookup_global_constant(Constant,BRep),!.