Agent Behaviors

MyAgent.java (1.0)

package demos;

import jade.core.*;
import jade.core.behaviours.*;

public class MyAgent extends Agent {

  protected void setup() {
    // getting my Agent ID (AID) and local name:
   System.out.println("Agent " + getAID().getName() + " is ready!");
   System.out.println("My local name is " + getLocalName());
   // fetching my command line arguments:
    Object[] args = getArguments();
    if (args != null && args.length > 0) {
       System.out.println("My command line arguments are: ");
       for(int i = 0; i < args.length; i++) {
         System.out.println(args[i]);
       }
    } else {
       System.out.println("No command line argments specified");
    }
   // terminating:
   doDelete();
  }

  // put any agent cleanup operations here:
  protected void takeDown() {
     System.out.println(
      "Agent " + getLocalName() + " is shutting down");
  }
}

Starting a new agent using RMA:

Console output:

Agent mimi@DELL-LAPTOP2:1099/JADE is ready!
My local name is mimi
My command line arguments are:
a
bb
ccc
dddd
Agent mimi is shutting down

MyAgent.java (2.0)

protected void setup() {
   addBehaviour(new Behavior1(this));
   addBehaviour(new Behavior2());
   addBehaviour(new Behavior3());

   // etc.
}

Generic Behavior

class Behavior1 extends Behaviour {
   private int count = 0;
   public Behavior1(Agent a) { super(a); } // myAgent = a
   public void action() {
      System.out.println("Count = " + count++);
   }
   public boolean done() {
      return 10 < count;
   }
   public int onEnd() {
      myAgent.doDelete();
      return super.onEnd();
   }
}

One Shot Behavior

class Behavior2 extends OneShotBehaviour {
   // done() = true
   public void action() {
      System.out.println("This message only appears once");
   }
}

Cyclic Behavior

class Behavior3 extends CyclicBehaviour {
   // done() = false
   public void action() {
      System.out.println("This message appears many times");
   }
}

Console Output

Agent alf@DELL-LAPTOP2:1099/JADE is ready!
My local name is alf
No command line argments specified
Count = 0
This message only appears once
This message appears many times
Count = 1
This message appears many times
Count = 2
This message appears many times
Count = 3
This message appears many times
Count = 4
This message appears many times
Count = 5
This message appears many times
Count = 6
This message appears many times
Count = 7
This message appears many times
Count = 8
This message appears many times
Count = 9
This message appears many times
Count = 10
Agent alf is shutting down