jade.core.behaviours
Class ComplexBehaviour

java.lang.Object
  |
  +--jade.core.behaviours.Behaviour
        |
        +--jade.core.behaviours.ComplexBehaviour
Direct Known Subclasses:
NonDeterministicBehaviour, SequentialBehaviour

public abstract class ComplexBehaviour
extends Behaviour

An abstract superclass for behaviours composed by many parts. This class holds inside a list of children behaviours, to which elements can be aded or emoved dynamically. When a ComplexBehaviour receives it execution quantum from the agent scheduler, it executes one of its children according to some policy. This class must be extended to provide the actual scheduling policy to apply when running children behaviours.

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

Inner Class Summary
protected  class ComplexBehaviour.BehaviourList
          Inner class to implement a list of behaviours.
 
Inner classes inherited from class jade.core.behaviours.Behaviour
Behaviour.RunnableChangedEvent
 
Field Summary
protected  ComplexBehaviour.BehaviourList subBehaviours
          The children list for this behaviour.
 
Fields inherited from class jade.core.behaviours.Behaviour
myAgent, myEvent, NOTIFY_DOWN, NOTIFY_UP, parent
 
Constructor Summary
ComplexBehaviour()
          Default constructor, does not set the owner agent.
ComplexBehaviour(Agent a)
          This constructor sets the owner agent.
 
Method Summary
 void action()
          Executes this ComplexBehaviour.
 void addSubBehaviour(Behaviour b)
          Adds a behaviour to the children list.
protected abstract  boolean bodyAction()
          Abstract policy method for children execution.
 boolean done()
          Checks whether this behaviour has terminated.
protected  void handle(Behaviour.RunnableChangedEvent rce)
          Handle block/restart notifications.
protected  void postAction()
          This method is just an empty placeholder for subclasses.
protected  void preAction()
          This method is just an empty placeholders for subclasses.
 void removeSubBehaviour(Behaviour b)
          Removes a behaviour from the children list.
 void reset()
          Puts a ComplexBehaviour back in initial state.
 void restart()
          Restarts this behaviour.
 
Methods inherited from class jade.core.behaviours.Behaviour
block, block, isRunnable, root
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

subBehaviours

protected ComplexBehaviour.BehaviourList subBehaviours
The children list for this behaviour. It can be accessed by subclasses to implement their scheduling policies.
Constructor Detail

ComplexBehaviour

public ComplexBehaviour()
Default constructor, does not set the owner agent.

ComplexBehaviour

public ComplexBehaviour(Agent a)
This constructor sets the owner agent.
Parameters:
a - The agent this behaviour belongs to.
Method Detail

preAction

protected void preAction()
This method is just an empty placeholders for subclasses. It is executed just once before starting children scheduling. Therefore, it acts as a prolog to the composite action represented by this ComplexBehaviour.

bodyAction

protected abstract boolean bodyAction()
Abstract policy method for children execution. Different subclasses will implement this method to run children according to some policy (sequentially, round robin, priority based, ...). This abstract method is the policy routine to be used by subclasses to schedule children behaviours. This method must be used by application programmer only to create new kinds of ComplexBehaviour with custom children scheduling. In this case, the method must return true when the composite behaviour has ended and false otherwise. Typically, the value returned will be some function of all termination statuses of children behaviours.
Returns:
true when done, false when children behaviours still need to be run.
See Also:
SequentialBehaviour, NonDeterministicBehaviour

postAction

protected void postAction()
This method is just an empty placeholder for subclasses. It is invoked just once after children scheduling has ended. Therefore, it acts as an epilog for the composite task represented by this ComplexBehaviour. Overriding this method, application programmers can build fork()/join() execution structures. An useful idiom can be used to implement composite cyclic behaviours (e.g. a behaviour that continuously follows a specific interaction protocol): puttng a reset() call into postAction() method makes a complex behaviour restart as soon as it terminates, thereby turning it into a cyclic composite behaviour.

action

public final void action()
Executes this ComplexBehaviour. This method starts by executing preAction(); then bodyAction() is called once per scheduling turn until it returns true. Eventually, postAction() is called.
Overrides:
action in class Behaviour

done

public boolean done()
Checks whether this behaviour has terminated.
Returns:
true if this ComplexBehaviour has finished executing, falseotherwise.
Overrides:
done in class Behaviour

reset

public void reset()
Puts a ComplexBehaviour back in initial state. The internal state is cleaned up and reset() is recursively called for each child behaviour.
Overrides:
reset in class Behaviour

addSubBehaviour

public void addSubBehaviour(Behaviour b)
Adds a behaviour to the children list. This method can be called whenever deemed useful, so that dynamic activation of behaviours is fully supported.
Parameters:
b - The behaviour to add.

removeSubBehaviour

public void removeSubBehaviour(Behaviour b)
Removes a behaviour from the children list. This method can be called whenever deemed useful, so that dynamic removal of behaviour is fully supported.
Parameters:
b - The behaviour to remove. If it's not present in the list, nothing happens.

handle

protected void handle(Behaviour.RunnableChangedEvent rce)
Handle block/restart notifications. This method handles notifications by simply forwarding them according to their original direction.
Parameters:
rce - The event to handle
Overrides:
handle in class Behaviour

restart

public void restart()
Restarts this behaviour. A ComplexBehaviour blocks just like its Behaviour superclass, but when restart() is called all its children behaviours are notified, too.
Overrides:
restart in class Behaviour