Subject: [SWIPL] FW: call_with_time_limit/2

From: "Douglas R. Miles" <[email-masked]>
Date: Tue, 4 Dec 2001 13:07:47 -0800

Corrected some things bellow, sorry for two messsages

changed show_bug/0 and added show_work/0.

-----Original Message-----
From: Douglas R. Miles 
Sent: Tuesday, December 04, 2001 1:03 PM
To: [email-masked]
Cc: John Li
Subject: FW: call_with_time_limit/2

Hi all on the SWI-Prolog list,

I am geting a PrologCall() Error,

Here is my program (We are using the Multithread SWI-Prolog 4.0.10):
 
/*

Included email thread to give background about need for
call_with_time_limit/2 in a thread context.

> Hi Douglas,
> 
> I just found there is a call_with_time_limit/2 predicate in 
> SWI manual (p. 60) which uses a small C program (alarm timer) to signal
and 
> control then execution time.  Have you tried it before?

John,

Yes, I was using it a while in linux sigma.. The only problem was it is not
compatible with windows.  So I started working on a win32 version (a dll)
but I wasn't able to accomplish, it since it required an extra thread
background thread to 'tick' in (after spending 2 hours trying I gave up)  

But back to the unix version,  It sends a global interupt (SIGTIME) to all
prolog threads, so one query thread could not get a personalized timer.  But
it certainly worked just as it was in the manual! And is extremely usefull. 

> This can be a probelm for using it, since queries are executed at
different
> time periods by different threads.  They should not be terminated at the
> same time.  Is there a way to personalize it?

I am hoping this Prolog code version of call_with_time_limit/2 should work
to run on personal threads.

-Douglas

*/

show_work:-invoke_cmd2(user_output,d(_,_,'ADAM',_,type,_,X),['X'=X]).

% Run this to see error

show_bug:-invoke_cmd2(user_output,
		call_with_time_limit(d(_,_,'ADAM',_,type,_,X),1),
		['X'=X]).

/*

We are getting a C based error about 'PrologCall() error'

*/

% just made up a fact John.
d('http://localhost','#peoples','ADAM',apease,type,daml,'Human').   

:-dynamic answer_yes/1.

invoke_cmd2(Out,CMD,Vars):-
	thread_self(Self),!,
	invoke_cmd2(Self,Out,CMD,Vars).

invoke_cmd2(Self,Out,CMD,Vars):-
      tell(Out),
      retract_all(answer_yes(Self)),
      catch(CMD,Err,(format('<error description="~w"/>',[Err]),!,fail)),
      assert(answer_yes(Self)),
      write_swi_vars_proc(Out,Vars),
      fail.

invoke_cmd2(Self,Out,_,_):-   
         (retract(answer_yes(Self)) -> 
                format(Out,'<prolog:yes/>\n',[]) 
                 ;
                format(Out,'<prolog:no/>\n',[])),!. 

write_swi_vars_proc(Out,[]):-!.
write_swi_vars_proc(Out,Vars):-
         write(Out,'<prolog:result>'),
         write_swi_vars(Out,Vars),
         write(Out,'</prolog:result>\n').
                              
write_swi_vars(Out,[]):-!.
write_swi_vars(Out,[Term|REST]):-  
	   !,Term=..[_,N,V],
         format(Out,'<prolog:var name="~w">~q</var>',[N,V]),
         write_swi_vars(Out,REST).

:- module_transparent call_with_time_limit/2. 
% Max time in seconds 
	call_with_time_limit(Goal, MaxTime) :- 
      	  thread_self(Target), 
	        thread_create(Timer, 
                (( 
                       sleep(MaxTime), 
	
catch(ignore(thread_signal(Target,throw(giveup))),_,true), 
                        thread_exit(completed) 
               )),[detatched]), 
	  retractall(timer_thread(Targer,_)), 
        asserta(timer_thread(Targer,Timer)),
	catch(Goal, giveup, fail), !. 

call_with_time_limit(_, _) :- 
      etract(timer_thread(Targer,Timer)), 
	
catch(ignore(thread_signal(Timer,thread_exit(iWasDoneFirst))),_,true), 
      fail. 

% End program

What changes need to be made to this prolog call_with_time_limit/2 to make
it work?

Thank you,
 Douglas

---Original Message-----
From: John Li [mailto:[email-masked]]
Sent: Tuesday, December 04, 2001 11:04 AM
To: Douglas R. Miles; Douglas R. Miles
Subject: RE: call_with_time_limit/2

Douglas, 

I got some error message 'PrologCall() error' or something like 'can not
display' on the server console and then the system hang up, after I telnet
the following goal to our MY server: 

$ telnet 192.168.200.96 7000 
Trying 192.168.200.96... 
Connected to 192.168.200.96. 
Escape character is '^]'. 
<?xml version="1.0" encoding="ISO-8859-1"?> 
<answer thread="5"> 
call_with_time_limit(d(_,_,'ADAM',_,type,_,X), 1). 

d(_,_,'ADAM',_,type,_,X) is a legitimate query. 

I checked the server code and found the query
call_with_time_limit(d(_,_,'ADAM',_,type,_,X), 1) was executed as CMD in
invoke_cmd2/4. Do you see any violations/conflicts between the invoke_cmd2/4
and call_with_time_limit/2 there that might have been the cause of the error
message? 

Thanks, 
John