| 1 | % (c) 2016-2022 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(trace_generator,[generate_all_traces_until/4, generate_traces/0]). | |
| 6 | ||
| 7 | :- use_module(library(lists)). | |
| 8 | :- use_module(probsrc(error_manager)). | |
| 9 | :- use_module(probsrc(state_space),[visited_expression/2, | |
| 10 | try_set_trace_by_transition_ids/1, transition/4]). | |
| 11 | ||
| 12 | :- use_module(probltlsrc(ltl_propositions),[check_atomic_property_formula/2]). | |
| 13 | :- use_module(probsrc(tcltk_interface),[compute_all_transitions_if_necessary/2]). | |
| 14 | :- use_module(probsrc(b_trace_checking),[tcltk_save_history_as_trace_file/3]). | |
| 15 | :- use_module(probsrc(tools_strings),[ajoin/2]). | |
| 16 | :- use_module(probsrc(debug),[formatsilent/2]). | |
| 17 | :- use_module(probsrc(specfile),[b_or_z_mode/0,currently_opened_file/1]). | |
| 18 | ||
| 19 | :- use_module(ltl_tools,[temporal_parser/3]). | |
| 20 | :- use_module(probsrc(preferences), [get_preference/2]). | |
| 21 | :- use_module(probsrc(tools_meta),[safe_time_out/3]). | |
| 22 | :- use_module(visbsrc(visb_visualiser),[load_default_visb_file_if_necessary/0, get_default_visb_file/2]). | |
| 23 | :- use_module(visbsrc(visb_html_export),[generate_visb_html_for_history_with_vars/1]). | |
| 24 | ||
| 25 | :- use_module(probsrc(module_information),[module_info/2]). | |
| 26 | :- module_info(group,ltl). | |
| 27 | :- module_info(description,'This module generates all traces under a certain condition.'). | |
| 28 | ||
| 29 | generate_all_traces_until(LTL_Stop_AsAtom,FilePrefix,Result,NrTracesGenerated) :- | |
| 30 | ( temporal_parser(LTL_Stop_AsAtom,ltl,LtlStopFormula) -> | |
| 31 | FromID=root, | |
| 32 | generate_traces(FromID,LtlStopFormula,FilePrefix,Result), | |
| 33 | bb_get(traces_generated,NrTracesGenerated) | |
| 34 | ; | |
| 35 | add_error(ltl,'LTL Parser failed:',LTL_Stop_AsAtom), | |
| 36 | Result = ltl_parse_error, NrTracesGenerated=0 | |
| 37 | ). | |
| 38 | ||
| 39 | % generate a single trace until either stop condition or deadlock reached | |
| 40 | % only simple paths are generated: a trace never visits a state twice; otherwise generation would not | |
| 41 | % terminate for cycles, and self-loops would produce the same trace again and again with more steps | |
| 42 | generate_trace(StateId,AtomicLTL_StopCond,Trace,EndResult) :- | |
| 43 | generate_trace(StateId,AtomicLTL_StopCond,[StateId],Trace,EndResult). | |
| 44 | generate_trace(StateId,AtomicLTL_StopCond,_,[],stop_condition) :- | |
| 45 | compute_all_transitions_if_necessary(StateId,false), % could be made optional | |
| 46 | check_atomic_property_formula(AtomicLTL_StopCond,StateId),!, | |
| 47 | formatsilent('Trace stop condition found in state ~w~n',[StateId]). | |
| 48 | generate_trace(StateId,_,_,[],deadlock) :- | |
| 49 | \+ transition(StateId,_,_,_), | |
| 50 | formatsilent('Deadlock in state ~w~n',[StateId]). | |
| 51 | generate_trace(StateId,AtomicLTL_StopCond,Visited,[TransID|T],EndResult) :- | |
| 52 | transition(StateId,_TransitionTerm,TransID,ToID), | |
| 53 | \+ memberchk(ToID,Visited), | |
| 54 | generate_trace(ToID,AtomicLTL_StopCond,[ToID|Visited],T,EndResult). | |
| 55 | ||
| 56 | % unit test on a small hand-made state space: tgs0 has a self-loop a and goes to tgs1 with b; | |
| 57 | % tgs1 goes back to tgs0 with c and to the deadlock tgs2 with d: only one simple trace (b,d) | |
| 58 | :- use_module(probsrc(self_check)). | |
| 59 | with_trace_test_transitions(Goal) :- | |
| 60 | Ts = [transition(tgs0,a,tga,tgs0),transition(tgs0,b,tgb,tgs1), | |
| 61 | transition(tgs1,c,tgc,tgs0),transition(tgs1,d,tgd,tgs2)], | |
| 62 | call_cleanup((maplist(assert_test_transition,Ts), call(Goal)), | |
| 63 | maplist(retract_test_transition,Ts)). | |
| 64 | assert_test_transition(transition(A,B,C,D)) :- assertz(state_space:transition(A,B,C,D)). | |
| 65 | retract_test_transition(transition(A,B,C,D)) :- retract(state_space:transition(A,B,C,D)). | |
| 66 | :- assert_must_succeed(with_trace_test_transitions((findall(T/R,generate_trace(tgs0,false,T,R),L), | |
| 67 | L == [[tgb,tgd]/deadlock]))). | |
| 68 | ||
| 69 | generate_traces :- generate_traces(root). | |
| 70 | generate_traces(FromID) :- generate_traces(FromID,false,'Trace_',_). % only generate deadlocking traces | |
| 71 | ||
| 72 | generate_traces(FromID,AtomicLTL_StopCond,FilePrefix,Res) :- | |
| 73 | load_default_visb_file_if_necessary, | |
| 74 | get_preference(trace_generation_time_out,TO), | |
| 75 | safe_time_out(generate_traces2(FromID,AtomicLTL_StopCond,FilePrefix,Res), | |
| 76 | TO, TORes), | |
| 77 | (TORes = time_out | |
| 78 | -> add_message(trace_generator,'Time-out, increase TRACES_TIME_OUT preference: ',TO), | |
| 79 | Res=time_out | |
| 80 | ; true). | |
| 81 | ||
| 82 | generate_traces2(FromID,AtomicLTL_StopCond,FilePrefix,Res) :- | |
| 83 | get_preference(trace_generation_limit,Limit), | |
| 84 | bb_put(traces_generated,0), | |
| 85 | generate_trace(FromID,AtomicLTL_StopCond,TraceIDs,EndResult), | |
| 86 | bb_inc(Nr), | |
| 87 | save_trace(FilePrefix,json,Nr,EndResult,TraceIDs), | |
| 88 | (Nr >= Limit -> !,add_message(trace_generator,'Limit of number of traces reached, increase MAX_TRACES preference: ',Nr), Res=limit_reached | |
| 89 | ; fail). | |
| 90 | generate_traces2(_,_,_,all_traces_generated). % we could check max_reached, ... | |
| 91 | ||
| 92 | bb_inc(Nr) :- bb_get(traces_generated,Old), | |
| 93 | New is Old+1, bb_put(traces_generated,New), Nr=New. | |
| 94 | ||
| 95 | save_trace(FilePrefix,Format,Nr,EndResult,TraceIDs) :- | |
| 96 | try_set_trace_by_transition_ids(TraceIDs), | |
| 97 | %loaded_main_file(_,MainFile), | |
| 98 | % currently_opened_file(File), | |
| 99 | % (b_or_z_mode -> b_machine_name(Model) ; get_modulename_filename(File,Model)), | |
| 100 | (EndResult = deadlock | |
| 101 | -> DLK = '_DLK' % we could make adding this to the name optional | |
| 102 | ; DLK='' | |
| 103 | ), | |
| 104 | ajoin([FilePrefix,Nr,DLK,'.prob2trace'],HistFile), % If FilePrefix ends in a / we could add Trace? | |
| 105 | format('% Saving history to JSON ProB2-UI trace file: ~w~n',[HistFile]), | |
| 106 | ajoin(['Trace ',Nr,' generated ending in ',EndResult],Desc), | |
| 107 | tcltk_save_history_as_trace_file(Format,[description/Desc],HistFile), | |
| 108 | ajoin([FilePrefix,Nr,DLK,'.html'],VisBHTMLFile), | |
| 109 | save_visb_html_his(VisBHTMLFile). | |
| 110 | ||
| 111 | save_visb_html_his(File) :- | |
| 112 | get_default_visb_file(_,_),!, | |
| 113 | format('% Saving history to VisB HTML file: ~w~n',[File]), | |
| 114 | generate_visb_html_for_history_with_vars(File). | |
| 115 | save_visb_html_his(_). % No file provided; do not generate: we could foresee another option for this | |
| 116 | ||
| 117 | ||
| 118 | % :- use_module(probltlsrc(trace_generator)), generate_traces. |