1 | % (c) 2009-2019 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 | ||
6 | :- module(tools_timeout, [ | |
7 | time_out_call/2, time_out_call/1, | |
8 | time_out_with_factor_call/3, time_out_with_factor_call/4, | |
9 | get_time_out_with_factor/2 | |
10 | ]). | |
11 | ||
12 | :- use_module(module_information). | |
13 | ||
14 | :- module_info(group,infrastructure). | |
15 | :- module_info(description,'This module contains higher-level timeout helper predicates.'). | |
16 | ||
17 | ||
18 | :- meta_predicate time_out_with_factor_call(0,*,0). | |
19 | :- meta_predicate time_out_call(0,0). | |
20 | :- meta_predicate time_out_call(0). | |
21 | ||
22 | :- use_module(tools_meta,[safe_time_out/3]). | |
23 | :- use_module(tools_printing,[print_term_summary/1]). | |
24 | :- use_module(error_manager,[add_message/3, add_error_and_fail/3]). | |
25 | :- use_module(tools_strings,[predicate_functor/3]). | |
26 | :- use_module(preferences,[get_preference/2]). | |
27 | ||
28 | time_out_call(Call,Res) :- | |
29 | get_preference(time_out,CurTO), | |
30 | safe_time_out(Call,CurTO,TimeOutRes), | |
31 | (TimeOutRes=time_out | |
32 | -> predicate_functor(Call,F,N), | |
33 | add_message(self_check,'Time out occurred: ',F/N), | |
34 | print_term_summary(Call), | |
35 | call(Res) | |
36 | ; true). | |
37 | ||
38 | time_out_call(Call) :- | |
39 | time_out_call(Call,add_error_and_fail(time_out_call,'*** TIMEOUT occurred: ',Call)). | |
40 | ||
41 | :- use_module(preferences,[get_time_out_preference_with_factor/2]). | |
42 | get_time_out_with_factor(fixed_time_out(Fixedms),TORes) :- % also allow to specify a hard time out value | |
43 | !, TORes = Fixedms. | |
44 | get_time_out_with_factor(Factor,Res) :- | |
45 | get_time_out_preference_with_factor(Factor,Res). | |
46 | ||
47 | time_out_with_factor_call(Call,Factor,Res) :- | |
48 | time_out_with_factor_call(Call,Factor,[],Res). | |
49 | time_out_with_factor_call(Call,Factor,Options,Res) :- | |
50 | get_time_out_with_factor(Factor,TO), | |
51 | %%print(time_out(Call,TO)),nl, | |
52 | statistics(runtime,[Start,_]), | |
53 | on_exception(enumeration_warning(_,_,_,_,_), | |
54 | safe_time_out(Call,TO,TimeOutRes), | |
55 | (TimeOutRes = time_out,EnumWarning=true)), | |
56 | (TimeOutRes=time_out | |
57 | -> (member(silent,Options) -> true | |
58 | ; statistics(runtime,[Stop,_]), | |
59 | predicate_functor(Call,F,N), | |
60 | (EnumWarning==true | |
61 | -> add_message(self_check,'Time-out forced by enumeration warning: ',F/N) | |
62 | ; add_message(self_check,'Time-out occurred: ',F/N) | |
63 | ), | |
64 | print_term_summary(Call), | |
65 | Diff is Stop-Start, | |
66 | print(Diff), print(' ms runtime (time_out = '), print(TO), print(' ms)'),nl | |
67 | ), | |
68 | call(Res) | |
69 | ; true). |