JADE Agent Behaviours - II part
CyclicBehaviour: This behaviour stays active as long as its agent is alive and will be called
repeatedly after every event. Quite useful to handle message reception.
Exercise 1: CyclicBehaviourAgent.java
package cse5610.tutorial.behaviours;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
public class CyclicBehaviourAgent extends Agent {
protected void setup() {
addBehaviour(new MyCyclicBehaviour(this));
}
class MyCyclicBehaviour extends CyclicBehaviour {
private int count = 0;
public MyCyclicBehaviour(Agent a)
{
super(a);
}
@Override
public void action() {
System.out.println("Round " + count++);
}
}
}
Exercise 2: Extended CyclicBehaviourAgent.java
package cse5610.tutorial.behaviours;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
public class CyclicBehaviourAgent extends Agent {
private int count = 0;
protected void setup() {
addBehaviour(new MyCyclicBehaviour(this));
addBehaviour(new CounterBehaviour(this));
}
protected void takeDown() {
System.out.println("Disposing of agent");
}
class CounterBehaviour extends CyclicBehaviour {
public CounterBehaviour(Agent a)
{
super(a);
}
@Override
public void action() {
if(count == 10)
myAgent.doDelete();
}
}
class MyCyclicBehaviour extends CyclicBehaviour {
public MyCyclicBehaviour(Agent a)
{
super(a);
}
@Override
public void action() {
System.out.println("Round " + count++);
}
}
}
Exercise 3: Generic Behaviour
package cse5610.tutorial.behaviours;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
public class GenericBehaviourAgent extends Agent {
protected void setup()
{
addBehaviour(new ThreeStepBehaviour());
}
class ThreeStepBehaviour extends Behaviour {
private int step = 0;
public void action() {
switch(step) {
case 0:
// do something
step++;
System.out.println("Step " + step);
break;
case 1:
// do something
step++;
System.out.println("Step " + step);
break;
case 2:
// do something
step++;
System.out.println("Step " + step);
break;
}
}
@Override
public boolean done() {
return step == 3;
}
}
}
Exercise 4: Expand the previous exercise so that at the end of the
ThreeStepBehaviour, the agent starts a new OneShotBehaviour
Composite Behaviours
*
ParallelBehaviour: controls a set of children behaviours that execute in parallel. The
important thing is the termination condition: we can specify that the group terminates
when ALL children are done, N children are done or ANY child is done.
*
SequentialBehaviour: this behaviour executes its children behaviours one after the other
and terminates when the last child has ended.
SequentialBehaviour & ParallelBehaviour
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.ParallelBehaviour;
import jade.core.behaviours.SequentialBehaviour;
public class CompositeBehaviourAgent extends Agent {
protected void setup()
{
SequentialBehaviour seq1 = new SequentialBehaviour(this);
seq1.setBehaviourName("SB1");
seq1.addSubBehaviour(
new OneShotBehaviour(this) {
public void action() {
System.out.println(this.getParent().getBehaviourName() + " : First");
}
});
seq1.addSubBehaviour(
new OneShotBehaviour(this) {
public void action() {
System.out.println(this.getParent().getBehaviourName() + " : Second");
}
});
seq1.addSubBehaviour(
new OneShotBehaviour(this) {
public void action() {
System.out.println(this.getParent().getBehaviourName() + " : Third");
}
});
SequentialBehaviour seq2 = new SequentialBehaviour(this);
seq2.setBehaviourName("SB2");
seq2.addSubBehaviour(
new OneShotBehaviour(this) {
public void action() {
System.out.println(this.getParent().getBehaviourName() + " : First");
}
});
seq2.addSubBehaviour(
new OneShotBehaviour(this) {
public void action() {
System.out.println(this.getParent().getBehaviourName() + " : Second");
}
});
seq2.addSubBehaviour(
new OneShotBehaviour(this) {
public void action() {
System.out.println(this.getParent().getBehaviourName() + " : Third");
}
});
ParallelBehaviour p = new ParallelBehaviour(this,
ParallelBehaviour.WHEN_ALL);
p.addSubBehaviour(seq1);
p.addSubBehaviour(seq2);
addBehaviour(p);
}
}
Add New Comment