-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimeout.pl
66 lines (60 loc) · 1.88 KB
/
timeout.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
:- module(timeout, [timeout/1, reply_with_timeout/2, call_with_timeout/5, call_with_timeout/6]).
:- use_module(functional).
% predicate for timeout constant (in seconds)
timeout(2).
small_timeout(0.1).
% ========== timeout ==========
% simple request with timeout
reply_with_timeout(Request, Pred) :-
timeout(T),
catch(
call_with_time_limit(T, (call(Pred, Request, Reply), HadTimeout = false)),
time_limit_exceeded, % exception type
(HadTimeout = true) % catch branch
),
( HadTimeout = true -> Reply = '[timeout]~n'
; true
),
reply(Reply).
% call functions with one argument, fallback to single solution
call_with_timeout(A, Solution, PredNormal, PredTimeout, PredMapping) :-
small_timeout(T), % using the _small_ timeout constant
catch(
call_with_time_limit(
T,
(
call(PredNormal, A, Solution0),
map(Solution0, PredMapping, Solution1),
Solution = Solution1
)
),
time_limit_exceeded, % exception type
( % catch branch
call(PredTimeout, A, Solution0),
call(PredMapping, Solution0, Solution1),
Solution2 = [Solution1],
Solution = ['[timeout, only listing the first solution]'|Solution2],
!
)
).
% call functions with two arguments
call_with_timeout(A, B, Solution, PredNormal, PredTimeout, PredMapping) :-
small_timeout(T), % using the _small_ timeout constant
catch(
call_with_time_limit(
T,
(
call(PredNormal, A, B, Solution0),
map(Solution0, PredMapping, Solution1),
Solution = Solution1
)
),
time_limit_exceeded, % exception type
( % catch branch
call(PredTimeout, A, B, Solution0),
call(PredMapping, Solution0, Solution1),
Solution2 = [Solution1],
Solution = ['[timeout, only listing the first solution]'|Solution2],
!
)
).