jade.core.behaviours
Class Behaviour

java.lang.Object
  |
  +--jade.core.behaviours.Behaviour
Direct Known Subclasses:
ComplexBehaviour, FipaRequestResponderBehaviour.ActionHandler, ReceiverBehaviour, SimpleBehaviour

public abstract class Behaviour
extends java.lang.Object
implements java.io.Serializable

Abstract base class for JADE behaviours. Extending this class directly should only be needed for particular behaviours with special synchronization needs; this is because event based notification used for blocking and restarting behaviours is directly accessible at this level.

Author:
Giovanni Rimassa - Universita` di Parma
See Also:
Serialized Form

Inner Class Summary
protected  class Behaviour.RunnableChangedEvent
          Event class for notifying blocked and restarted behaviours.
 
Field Summary
protected  Agent myAgent
          The agent this behaviour belongs to.
protected  Behaviour.RunnableChangedEvent myEvent
          This event object will be re-used for every state change notification.
protected  int NOTIFY_DOWN
          A constant for parent-to-child notifications.
protected  int NOTIFY_UP
          A constant for child-to-parent notifications.
protected  ComplexBehaviour parent
          Back pointer to the enclosing Behaviour (if present).
 
Constructor Summary
Behaviour()
          Default constructor.
Behaviour(Agent a)
          Constructor with owner agent.
 
Method Summary
abstract  void action()
          Runs the behaviour.
 void block()
          Blocks this behaviour.
 void block(long millis)
          Blocks this behaviour for a specified amount of time.
abstract  boolean done()
          Check if this behaviour is done.
protected  void handle(Behaviour.RunnableChangedEvent rce)
          Handler for block/restart events.
 boolean isRunnable()
          Returns whether this Behaviour object is blocked or not.
abstract  void reset()
          Restores behaviour initial state.
 void restart()
          Restarts a blocked behaviour.
 Behaviour root()
          Returns the root for this Behaviour object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOTIFY_UP

protected final int NOTIFY_UP
A constant for child-to-parent notifications.

NOTIFY_DOWN

protected final int NOTIFY_DOWN
A constant for parent-to-child notifications.

myAgent

protected Agent myAgent
The agent this behaviour belongs to. This is an instance variable that holds a reference to the Agent object and allows the usage of its methods within the body of the behaviour. As the class Behaviour is the superclass of all the other behaviour classes, this variable is always available. Of course, remind to use the appropriate constructor, i.e. the one that accepts an agent object as argument; otherwise, this variable is set to null.

myEvent

protected Behaviour.RunnableChangedEvent myEvent
This event object will be re-used for every state change notification.

parent

protected ComplexBehaviour parent
Back pointer to the enclosing Behaviour (if present).
See Also:
ComplexBehaviour
Constructor Detail

Behaviour

public Behaviour()
Default constructor. It does not set the agent owning this behaviour object.

Behaviour

public Behaviour(Agent a)
Constructor with owner agent.
Parameters:
a - The agent owning this behaviour.
Method Detail

action

public abstract void action()
Runs the behaviour. This abstract method must be implemented by Behavioursubclasses to perform ordinary behaviour duty. An agent schedules its behaviours calling their action() method; since all the behaviours belonging to the same agent are scheduled cooperatively, this method must not enter in an endless loop and should return as soon as possible to preserve agent responsiveness. To split a long and slow task into smaller section, recursive behaviour aggregation may be used.
See Also:
ComplexBehaviour

done

public abstract boolean done()
Check if this behaviour is done. The agent scheduler calls this method to see whether a Behaviour still need to be run or it has completed its task. Concrete behaviours must implement this method to return their completion state. Finished behaviours are removed from the scheduling queue, while others are kept within to be run again when their turn comes again.
Returns:
true if the behaviour has completely executed.

reset

public abstract void reset()
Restores behaviour initial state. This method must be implemented by concrete subclasses in such a way that calling reset() on a behaviour object is equivalent to destroying it and recreating it back. The main purpose for this method is to realize multistep cyclic behaviours without needing expensive constructions an deletion of objects at each loop iteration.

handle

protected void handle(Behaviour.RunnableChangedEvent rce)
Handler for block/restart events. This method handles notification by copying its runnable state and then by simply forwarding the event when it is travelling upwards and by doing nothing when it is travelling downwards, since an ordinary behaviour has no children.
Parameters:
rce - The event to handle

root

public Behaviour root()
Returns the root for this Behaviour object. That is, the top-level behaviour this one is a part of. Agents apply scheduling only to top-level behaviour objects, so they just call restart() on root behaviours.
Returns:
The top-level behaviour this behaviour is a part of. If this one is a top level behaviour itself, then simply this is returned.
See Also:
restart()

isRunnable

public boolean isRunnable()
Returns whether this Behaviour object is blocked or not.
Returns:
true when this behaviour is not blocked, false when it is.

block

public void block()
Blocks this behaviour. When this method is called, the behaviour state is set to Blocked and a suitable event is fired to notify its parent behaviour. Then the behaviour is put into a blocked behaviours queue by the agent scheduler. If this method is called from within action() method, behaviour suspension occurs as soon as action() returns.
See Also:
restart()

block

public void block(long millis)
Blocks this behaviour for a specified amount of time. The behaviour will be restarted when among the three following events happens.
Parameters:
millis - The amount of time to block, in milliseconds. Notice: a value of 0 for millis is equivalent to a call to block() without arguments.
See Also:
block()

restart

public void restart()
Restarts a blocked behaviour. This method fires a suitable event to notify this behaviour's parent. When the agent scheduler inserts a blocked event back into the agent ready queue, it restarts it automatically. When this method is called, any timer associated with this behaviour object is cleared.
See Also:
block()