/*			APPENDIX  B
		Section B.3   Motivating Examples
		B.3.2  Execution related predicates.
*/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  EXECUTION 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/* Use this version to run examples and simulation */
/*
execute(A,T) :- 
     nl, write('>> Im doing the action '), write(A),
     write(' at time '), write(T), nl,
     doReally(A).

wait( Time ) :-
        nl, printf( "*-> wait for %w seconds\n", [ Time ] ), nl,
        flush(output).  
*/

/* Use this version to run the robot in the real corridor */

execute(A,T) :- 
     nl, write('>> Im doing the action '), write(A),
     write(' at time '), write(T), nl,
        doReally(A),
        current_stream(reports, X, Stream),
        printf(Stream, "\n >> Im doing the action %w at time %w\n", [A,T]).

myWait( Time, S ) :-
        Time > 10,
        sense(BeginTime, S),    /* watch the time before waiting */
        current_stream(reports, X, Stream),
        printf(Stream, "       *-> Time before waiting = %w \n", [ BeginTime ] ),
        nl, printf( "*-> start waiting for %w seconds...\n", [ Time ] ), nl,
        flush(output),
        printf(Stream, "       *-> start waiting for %w seconds... \n", [ Time ] ),
        doWait( Time ),
        sense(EndTime, S),    /* watch the time after waiting */
        DiffTime is EndTime - BeginTime,
        ( DiffTime < Time -> myWait(DiffTime, S) ; true).

/*
   We do not wait for less than 10 seconds. However, waiting for 
  small amounts of time can be included, see the next clause below. */

myWait( Time, S ) :-
        nl, 
      printf( "*-> I'm too busy: no time to wait for %w seconds\n", [ Time ] ), nl,
        flush(output),
        sense(EndTime, S),    /* watch the time after waiting */
        current_stream(reports, X, Stream),
        printf(Stream, "       *-> Time after waiting = %w \n", [ EndTime ] ).


myWait( Time, S ) :-  Time < 0,
            /* remove  Time < 0,  to allow waiting for small amount of time */
        sense(BeginTime, S),    /* watch the time before waiting */
        nl, printf( "*-> ok, wait for %w seconds ?\n", [ Time ] ), nl,
        flush(output),
        doWait( Time ),
        sense(EndTime, S),    /* watch the time after waiting */
        DiffTime is EndTime - BeginTime,
        nl, printf( "*-> I waited for %w seconds, indeed\n", [ DiffTime ] ), nl,
        flush(output).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

doReally( pickupCoffee( T ) ) :-
	printf( "  *-> give some coffee please at time %w\n", [T] ),
        flush(output).

doReally( giveCoffee( Person, T ) ) :-
	printf( "  *-> %w, take your coffee please at time %w\n", [Person,T] ),
        flush(output),
	takeYourCoffee.

doReally( startGo( From, To, T ) ) :-
	printf( "  *-> start going from %w to %w at time %w\n", [From, To, T] ),
        flush(output),
        driveReal( From, To ).

doReally( endGo( From, To, T) ) :-
	printf( "  *-> end going from %w to %w at time %w\n", [From, To, T] ),
        flush(output).

doReally( Action ) :-
	printf( "  *-> error of action %w\n", [ Action ] ),
        flush(output).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/*  askForCoffee :-
	hli_sound_stop,
	%     hli_show_picture("give"),
	hli_sound("some_coffee"),
	hli_buttons( 3, 3, 3, 3, 20, Answer ),
	( Answer=0 -> 
	    askForCoffee 
	; 
	    hli_show_picture("thanks"),
	    hli_sound("thank_you") 
	). 
*/
askForCoffee.

/*  takeYourCoffee :-
	hli_sound_stop,
	%     hli_show_picture("take"),	
	hli_sound("take_coffee"),
	hli_buttons( 3, 3, 3, 3, 20, Answer ),
	( Answer=0 -> 
	    takeYourCoffee 
	; 
	    hli_show_picture("thanks"),
	    hli_sound("thank_you") 
	).
*/
takeYourCoffee.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TOOLS: hacks to fight with bugs in Eclipse Prolog.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

doWait( Time ) :-
        Wait is Time*2 +1,
        doWait2( Wait ).

doWait2( Time ) :-
        Time =< 0.
doWait2( Time ) :-
        Time2 is Time-1,
        sleep( 0.5 ),
        doWait2( Time2 ).