% This example demonstrates the use of CUT (!) after rcvMsg/rcvMult
%    inline reactions.
% This functionality is different from the usual CUT because it cuts
%    through all reactions to the matching message regardless
%    of the predicate signature in the head of the clause. 

:- eval(ex020()).

ex020() :-
	println(["==========ex020=========="]),
	iam(Me),
	sendMsg(XID,self,Me,reply,a(1)),
	ex020_inner(XID,Me).

ex020_inner(XID,Me) :-
	ex020_inner_a(XID,Me).
ex020_inner(XID,Me) :-
	ex020_inner_b(XID,Me).

ex020_inner_a(XID,Me) :-
	rcvMsg(XID,self,Me,reply,a(1)),
	% This CUT stops all further INLINE reaction rules that may be matching
	%    the message intercepted in the above line.
	% However, STANDALONE reaction rules are not CUT and are always processed.
	% If these standalone reaction rules have a further CUT, subsequent
	%    standalone reaction rules are cut.
	!,
	println(["Inline reaction 1: ",rcvMsg(XID,self,Me,reply,a(1))]).
ex020_inner_a(XID,Me) :-
	rcvMsg(XID,self,Me,reply,a(1)),
	println(["Inline reaction 2: ",rcvMsg(XID,self,Me,reply,a(1))]).

ex020_inner_b(XID,Me) :-
	rcvMsg(XID,self,Me,reply,a(1)),
	println(["Inline reaction 3: ",rcvMsg(XID,self,Me,reply,a(1))]).

% A testing harness for printing all incoming 'reply' messages.
rcvMsg(XID,Protocol,From,reply,[X|Xs]|Extra) :-
	println(["reply for conversation-id ",XID,": ",[X|Xs],","|Extra]).
