% Demonstrate how
%    - an inline reaction with a free conversation-id in the body
%      of a queryref reaction releases a postponed end-of-transmission
%      that thereby discharges an inline rcvMult in the caller in test048(). 

:- eval(test048()).

% This is the caller that sends a queryref
%    then waits in a rcvMult inline reaction,
%    and also sends a message that is expected by the queryref callee
%    before the latter sends back a reply.
test048() :-
	println(["==========test048=========="]),
	iam(Me),
	sendMsg(XID1,self,Me,queryref,test(R),id0),
	sendMsg(XID2,self,Me,data,result("toto"),id0),
	% end_of_transmission sent when rcvMsg in test(R) is discharged
	%    will discharge the following inline reaction.
	rcvMult(XID1,self,Me,inform,test(R),id0),
	println(["Result2=",R]).

test(R) :-
	rcvMsg(XID,Protocol,From,data,result(R),ID),
	% When the waiting for this message is over, the end_of_transmission
	%    for the queryref will be sent back to be intercepted in test048().  
	println(["Result1=",R]).

%====================================
% HELPER PREDICATES (part of toolkit)
%====================================

% Reaction rule to general queryref
rcvMsg(XID,Protocol,From,queryref,[X|Xs]|LocalContext) :-
	println(["Rule reaction 1 ",rcvMsg(XID,Protocol,From,queryref,[X|Xs]|LocalContext)]),
	derive([X|Xs]),
	sendMsg(XID,Protocol,From,inform,[X|Xs]|LocalContext).
rcvMsg(XID,Protocol,From,queryref,[X|Xs]|LocalContext) :-
	println(["Rule reaction 2 ",rcvMsg(XID,Protocol,From,queryref,[X|Xs]|LocalContext)]),
	sendMsg(XID,Protocol,From,end_of_transmission,[X|Xs]|LocalContext).
