My first SPADE agent

It's time for us to build our first SPADE agent. We'll assume that we have a working SPADE platform at myhost.myprovider.com with the standard configuration. A basic SPADE agent is really a Python script that imports the spade module and that uses the constructs defined therein. For starters, fire up you favorite Python editor, create a file called myagent.py and write this:

import spade	
		

With this sentence, the SPADE Agent Library is imported and all its features become available. Let's start defining the base class for the agent:

class MyAgent(spade.Agent.Agent):
        def _setup(self):
                print "MyAgent starting . . ."
		

As you can see, we have derived a class from spade.Agent.Agent, which is the base class for all SPADE normal agents. Also, note that we have defined a method called _setup() . Every SPADE agent should override this method. It is where the initialization (or setup) code of the agent must be placed.

As this is a toy example, we don't need anything else. We complete the script to execute the agent:

if __name__ == "__main__":
	a = MyAgent("agent@myhost.myprovider.com", "secret")
	a.start()
		

The first thing the script does is to make an instance of our agent class. Mind the two parameters we provide: First is the JID of the agent, it contains its name (before the @) and the name of the agent platform (after the @). Second is the Jabber password for this particular agent. Note that an agent has to coherently use the same JID and password combination in order to succesfully connect to a platform over time, just like a human user has to use the same username and password combination over time to access a Jabber server.

The next thing the script does is to actually start the agent with the start() method. It is very important to understand that when an agent is created, it does not start working automatically. The start() method must be called first in order to trigger the agent execution. Actually, it is in that moment when the _setup() method will be called.

To finish the example, just execute the script and see the results:

$ python myagent.py
MyAgent starting . . .
Agent: agent@myhost.myprovider.com registered correctly (inform)
		

As you can see, the line we put in _setup() is printed first, before the agent actually connects to the platform. Then, the agent informs that it has registered correctly in the platform.

And that's it! We have built our first SPADE Agent in 7 lines of code. Easy, isn't it? Of course, this is a very very dumb agent that does nothing, but it serves well as a starting point to understand the logics behind the SPADE platform.