An Event Behaviour is a special kind of behaviour designed to be spawned as a response to an event occurring on an agent. As of version 2.0, SPADE agents support events in the form of incoming messages, and thus, Event Behaviours are associated with a message template as any other behaviour type. The main difference between an Event Behaviour and, say, a One-Shot Behaviour is that the Event Behaviour is not instanced nor is it running until the trigger event happens.
In order to register an Event Behaviour as the response to some event, SPADE offers the same uniform API as for the rest of behaviours. Once the behaviour is implemented, you have to define a message template and then call the addBehaviour() method of the agent, as you would do with any other type of behaviour. What's the difference? The difference is that the Event Behaviour doesn't get started right away when you call the start() method of the agent. When the trigger for an Event Behaviour happens, the agent automatically spawns the behaviour.
Following, there's an example of an agent that registers an Event Behaviour whenever a message that comes from itself arrives. You know, it is a lonely agent, nobody writes messages for it, so it forwards letters to itself. Poor agent:
class Sender(spade.Agent.Agent): def _setup(self): self.addBehaviour(self.SendMsgBehav()) print "Agent started!" class SendMsgBehav(spade.Behaviour.OneShotBehaviour): """ This behaviour sends a message to this same agent to trigger an EventBehaviour """ def _process(self): msg = spade.ACLMessage.ACLMessage() msg.setPerformative("inform") msg.addReceiver(spade.AID.aid("a@"+host,["xmpp://a@"+host])) msg.setContent("testSendMsg") print "Sending message in 3 . . ." time.sleep(1) print "Sending message in 2 . . ." time.sleep(1) print "Sending message in 1 . . ." time.sleep(1) self.myAgent.send(msg) print "I sent a message" #print str(msg) class RecvMsgBehav(spade.Behaviour.EventBehaviour): """ This EventBehaviour gets launched when a message that matches its template arrives at the agent """ def _process(self): print "This behaviour has been triggered by a message!" def _setup(self): # Create the template for the EventBehaviour: a message from myself template = spade.Behaviour.ACLTemplate() template.setSender(spade.AID.aid("a@"+host,["xmpp://a@"+host])) t = spade.Behaviour.MessageTemplate(template) # Add the EventBehaviour with its template self.addBehaviour(self.RecvMsgBehav(),t) # Add the sender behaviour self.addBehaviour(self.SendMsgBehav())