HOWTO Prolog timeout exception exception

I want to limit the "execution" of the algorithm in the prolog. Can you give me a hint how to do this? I found this predicate: call_with_time_limit How can I catch the time_limit_exceeded exception? Thanks

UPDATE:

I try like this:

timeout(t) :- catch(call_with_time_limit(t, sleep(5)), X, error_process(X)). error_process(time_limit_exceeded) :- write('Timeout exceeded'), nl, halt. error_process(X) :- write('Unknown Error' : X), nl, halt. 

but note that when I call the timeout (1):

 prolog :- timeout(1), 

but when I do it like this:

 runStart :- call_with_time_limit(1, sleep(5)). timeout(1) :- catch(runStart, X, error_process(X)). error_process(time_limit_exceeded) :- write('Timeout exceeded'), nl, halt. error_process(X) :- write('Unknown Error' : X), nl, halt. 

and again the call waiting time (1) is all right. What for? thanks UPDATE 2:

The problem is solved, it is necessary to provide an "argument" with upper case ...

+4
source share
2 answers

Use catch/3 . Example:

 catch(call_with_time_limit(1, sleep(5)), time_limit_exceeded, writeln('overslept!')). 

More practical:

 catch(call_with_time_limit(T, heavy_computation(X)), time_limit_exceeded, X = no_answer). % or just fail 
+5
source
 loop :- loop. loop_for_n_sec(N, Catcher) :- catch( call_with_time_limit(N, loop), Catcher, true ). 

Using:

 ?- loop_for_n_sec(1, Catcher). Catcher = time_limit_exceeded 
+3
source

All Articles