HelloWorldAgent.java

[java]
/**
* SECTION 3.7 OF THE BOOK
* To help explain the platform tools, this code illustrates a simple
* HelloWorldAgent that implements the following cyclic behaviour: each time
* a message is received, it prints the message onto the standard output and
* replies to the sender with a "Hello" message
**/
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;

public class HelloWorldAgent extends Agent {

public void setup() {
System.out.println("Hello. My name is "+getLocalName());
addBehaviour(new CyclicBehaviour() {
public void action() {
ACLMessage msgRx = receive();
if (msgRx != null) {
System.out.println(msgRx);
ACLMessage msgTx = msgRx.createReply();
msgTx.setContent("Hello!");
send(msgTx);
} else {
block();
}
}
});
}
}
[/java]