changeset 3:13b598d91c15

get_message_or_timeout/2 now uses thread_get_message/3 if available; player now supports optional event transform between source and destination; removed deprecated index declarations.
author samer
date Tue, 07 Feb 2012 14:49:28 +0000
parents b39711ae9035
children 6beef97eda32
files qutils.pl reactive.pl recorder.pl
diffstat 3 files changed, 35 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/qutils.pl	Wed Jan 25 19:41:35 2012 +0000
+++ b/qutils.pl	Tue Feb 07 14:49:28 2012 +0000
@@ -18,6 +18,19 @@
 %  then Msg is unified  with timeout. 
 %  NB: may fail if Msg is bound on input.
 get_message_or_timeout(inf,Msg) :- !, thread_get_message(Msg).
+
+:- if(current_predicate(thread_get_message/3)).
+
+get_message_or_timeout(Deadline,Msg) :- 
+	get_time(Now), 
+	(	Now>=Deadline -> Msg=timeout %, writeln(elapsed)
+	;	Timeout is Deadline-Now,
+		thread_self(Thread),
+		thread_get_message(Thread,Msg,[timeout(Timeout)])
+	).	
+
+:- else.
+
 get_message_or_timeout(Deadline,Msg) :- 
 	get_time(Now), 
 	(	Now>=Deadline -> Msg=timeout %, writeln(elapsed)
@@ -29,6 +42,8 @@
 		%,(Msg\=timeout -> writeln(msg:Msg); true)
 	).	
 
+:-endif.
+
 /*
 get_message_or_timeout(Deadline,Msg) :- 
 	get_time(Now), 
--- a/reactive.pl	Wed Jan 25 19:41:35 2012 +0000
+++ b/reactive.pl	Tue Feb 07 14:49:28 2012 +0000
@@ -188,7 +188,7 @@
 	get_timeout(P1,T1), 
 	(leq(T1,T) -> step_timeout(P1,P2); P1=P2).
 
-:- index(step_event(0,1,0)).
+%:- index(step_event(0,1,0)).
 
 %% step_event(+E:event, +P1:process, -P2:process) is det.
 %
--- a/recorder.pl	Wed Jan 25 19:41:35 2012 +0000
+++ b/recorder.pl	Tue Feb 07 14:49:28 2012 +0000
@@ -1,10 +1,16 @@
-:- module(recorder, [recorder/2, save_events/1, load_events/1, player/2]).
+:- module(recorder, 
+	[	recorder/2
+	,	save_events/1
+	,	load_events/1
+	,	player/2
+	,	player/3
+	]).
 /** <module> event recording
 
 This module provides a way to capture and record events processed by
 the reactive programming framework of reactive.pl.
 */
-:- meta_predicate recorder(1,?), player(1,?).
+:- meta_predicate recorder(1,?), player(1,?), player(2,2,?).
 :- dynamic start_time/1, event/2.
 
 :- use_module(library(fileutils)).
@@ -46,6 +52,7 @@
 			E^recorder_on_event(E,C1), Proc)
 	).
 
+%% player( +Client:ptail, +Trans:pred(A,A), -Proc:process) is det.
 %% player( +Client:ptail, -Proc:process) is det.
 %
 %  This predicate represents a reactive process that behaves as Client,
@@ -53,6 +60,9 @@
 %  time shifted by the difference between the recorded start time and the
 %  time at which player/2 is called. The type signature
 %  implies that the term recorder(Client) is of type =|ptail|=.
+%
+%  player/3 allows an event transformer pred(+EventIn:A,-EventOut:A) to be
+%  specified. player/2 is equivalent to using=/2 as the transformer.
 
 player(Client,Proc) :-
 	get_time(Now), 
@@ -61,6 +71,13 @@
 	call(Client,C1),
 	player_cont(Events,C1,Proc).
 
+player(Client,Trans,Proc) :-
+	get_time(Now), 
+	start_time(T0), DT is Now-T0,
+	setof(event(T,Msg),T1^Msg1^(event(T1,Msg1),T is DT+T1, call(Trans,Msg1,Msg)),Events),
+	call(Client,C1),
+	player_cont(Events,C1,Proc).
+
 player_on_event(Events,C1,Proc) :-
 	player_cont(Events,C1,Proc).