JadeBehaviours

download JadeBehaviours

of 32

Transcript of JadeBehaviours

Using Jade BehavioursBy Kiran

Primitive Behaviours

Simple Behaviour: A basic class that can be extend in various ways and most often is the best solution when other promising behaviours are found to have some hidden quirks.

Cyclic Behaviour: This behaviour stays active as long as its agent is alive and is called repeatedly after every event. Quite useful to handle message reception. Ticker Behaviour: A cyclic behaviour which periodically executes some user-defined piece of code.One Shot Behaviour: This executes ONCE and dies. Waker Behaviour: Which executes some user code once at a specified time . Receiver Behaviour: Which triggers when a given type of message is received (or a timeout expires).

Composite Behaviours

Parallel Behaviour: 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. Sequential Behaviour: This behaviour executes its children behaviours one after the other and terminates when the last child has ended.

Behaviour Methods

Most of the time, added behaviours disappear automatically when their job is done; however, it is also possible to remove them explicitly. There are 2 versions of add and remove methods depending whether the behaviours are at the top level or part of a composite behaviour.in Agent void add Behaviour( Behaviour ) void remove Behaviour( Behaviour ) in Composite Behaviour void add SubBehaviour( Behaviour ) void remove SubBehaviour( Behaviour )

Two-step revisited

The problem of combining a recurrent action which prints a message every 300 ms with a sequence where we output 2 messages at specified times. First for the Looping, before we relied on block(dt) to wait for dt msec. Now we use Jade's Ticker Behaviour where the recurring actions is specified in the onTick method. Below we show how its use resolves concisely the Looping part of the problem:

First StepBehaviour loop = new TickerBehaviour( this, 300 ) { protected void onTick() { System.out.println("Looper:" + myAgent.getLocalName()); } }); addBehaviour( loop );

Outputjean% java jade.Boot fred:Complex1 324: fred 628: fred 932: fred 1242: fred 1546: fred 1849: fred 2153: fred 2457: fred .... stopped with CTL-C

Second Step

We want the agent to do a 4 things in order: wait 0.25 sec print out a first message wait 0.5 sec print out a second messageTwo simple solutions which don't use a SequentialBehaviour

Second Step - First Method

We create two parallel Wakers to trigger at 250 ms and 750 ms espectively; timeout specified for a WakerBehaviour represents a delay from the time the Behaviour is CREATED, not from the time it STARTS executing. Note that we use addBehaviour directly on the agent and not addSubBehaviour on a sequence.addBehaviour( new WakerBehaviour( this, 250 ) { protected void handleElapsedTimeout() { System.out.println( "... Message 2" ); addBehaviour( new WakerBehaviour( myAgent, 500 ) { protected void handleElapsedTimeout() { System.out.println( "... Message1" ); } }); } });

Second Step - Second Method

we achieve sequentiality by creating the second Waker when the first finishes. In this case the timeout, can be set to the delay (500ms) and not to the absolute time (750ms).addBehaviour( new WakerBehaviour( this, 250 ) { protected void handleElapsedTimeout() { System.out.println( "... Message 2" ); addBehaviour( new WakerBehaviour( myAgent, 500 ) { protected void handleElapsedTimeout() { System.out.println( "... Message1" ); } }); } });

Delay Behaviours

If you wish to introduce delays as steps in SequentialBehaviours, we need to apply delay from the time the behaviour starts. This can be achieved by modifying the code of the WakerBehaviour to obtain the DelayBehaviour class which computes the wakeup time in the onStart() method. Whenever the behaviour is activated, we compute dt, the time remaining to wakeup. If it is negative, we call handleElapsedTimeout and "finish"; otherwise we block for the remaining time

public class DelayBehaviour extends SimpleBehaviour { private long timeout, wakeupTime; private boolean finished = false; public DelayBehaviour(Agent a, long timeout) { super(a); this.timeout = timeout; } public void onStart() { wakeupTime = System.currentTimeMillis() + timeout; } public void action() { long dt = wakeupTime - System.currentTimeMillis(); if (dt