Pausing event listeners
You can also specify and not when you want to listen for a pattern, pause when a particular condition occurs, and resume listening for that pattern when some other condition occurs. Consider the example that terminates the event listener after the market closes. Suppose instead that you want to listen for increases in stock prices only when there is no auction. When the correlator receives an InAuction event, you want to pause the event listener and when the correlator receives an AuctionClosed event you want the event listener to become active again. To do this, you can write something like the following:
action initialize() {
on EndAuction() and not BeginAuction() notInAuctionLogic();
on BeginAuction() and not EndAuction() inAuctionLogic();
route RequestAuctionPhase();
}
action inAuctionLogic() {
on EndAuction() notInAuctionLogic();
}
action notInAuctionLogic() {
on all Price("IBM",>targetPrice):p and not BeginAuction()
sellStock();
on BeginAuction() inAuctionLogic();
}
The initialize() action sets up two event listeners that determine whether to start with the inAuctionLogic() action or the notInAuctionLogic() action. The response to the routed RequestAuctionPhase event is an EndAuction event or a BeginAuction event. As soon as one of these events arrive, both event listeners terminate. For example, if an EndAuction event arrives, the first event listener terminates because its EndAuction() event template evaluates to true and its not BeginAuction() event template also evaluates to true. The second event listener terminates because its not EndAuction() event template evaluates to false and so the event expression can never evaluate to true.