Package jade.core.behaviours
Class Behaviour
- java.lang.Object
-
- jade.core.behaviours.Behaviour
-
- All Implemented Interfaces:
Serializable,Serializable
- Direct Known Subclasses:
CompositeBehaviour,LoaderBehaviour,ReceiverBehaviour,SimpleBehaviour,ThreadedBehaviourFactory.ThreadedBehaviourWrapper,WrapperBehaviour
public abstract class Behaviour extends Object implements 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.- Version:
- $Date$ $Revision$
- Author:
- Giovanni Rimassa - Universita' di Parma
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classBehaviour.RunnableChangedEventEvent class for notifying blocked and restarted behaviours.
-
Field Summary
Fields Modifier and Type Field Description protected AgentmyAgentThe agent this behaviour belongs to.protected Behaviour.RunnableChangedEventmyEventThis event object will be re-used for every state change notification.protected static intNOTIFY_DOWNA constant for parent-to-child notifications.protected static intNOTIFY_UPA constant for child-to-parent notifications.protected CompositeBehaviourparentstatic StringSTATE_BLOCKEDA constant identifying the blocked state.static StringSTATE_READYA constant identifying the runnable state.static StringSTATE_RUNNINGA constant identifying the running state.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidaction()Runs the behaviour.voidactionWrapper()This method is called internally by the JADE framework and should not be called by the user.voidblock()Blocks this behaviour.voidblock(long millis)Blocks this behaviour for a specified amount of time.abstract booleandone()Check if this behaviour is done.AgentgetAgent()StringgetBehaviourName()Retrieve the name of this behaviour object.DataStoregetDataStore()Return the private data store of thisBehaviour.StringgetExecutionState()protected CompositeBehaviourgetParent()Retrieve the enclosing CompositeBehaviour (if present).longgetRestartCounter()This method is used internally by the framework.protected voidhandle(Behaviour.RunnableChangedEvent rce)Handler for block/restart events.protected voidhandleBlockEvent()This method is used internally by the framework.voidhandleRestartEvent()This method is used internally by the framework.booleanisRunnable()Returns whether thisBehaviourobject is blocked or not.intonEnd()This method is just an empty placeholder for subclasses.voidonStart()This method is just an empty placeholders for subclasses.voidreset()Restores behaviour initial state.voidrestart()Restarts a blocked behaviour.Behaviourroot()Returns the root for thisBehaviourobject.voidsetAgent(Agent a)Associates this behaviour with the agent it belongs to.voidsetBehaviourName(String name)Give a name to this behaviour object.voidsetDataStore(DataStore ds)Set the private data store of thisBehaviourvoidsetExecutionState(String s)
-
-
-
Field Detail
-
NOTIFY_UP
protected static final int NOTIFY_UP
A constant for child-to-parent notifications.- See Also:
- Constant Field Values
-
NOTIFY_DOWN
protected static final int NOTIFY_DOWN
A constant for parent-to-child notifications.- See Also:
- Constant Field Values
-
STATE_READY
public static final String STATE_READY
A constant identifying the runnable state.- See Also:
- Constant Field Values
-
STATE_BLOCKED
public static final String STATE_BLOCKED
A constant identifying the blocked state.- See Also:
- Constant Field Values
-
STATE_RUNNING
public static final String STATE_RUNNING
A constant identifying the running state.- See Also:
- Constant Field Values
-
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 classBehaviouris 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 tonull.
-
myEvent
protected Behaviour.RunnableChangedEvent myEvent
This event object will be re-used for every state change notification.
-
parent
protected CompositeBehaviour parent
-
-
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
-
getParent
protected CompositeBehaviour getParent()
Retrieve the enclosing CompositeBehaviour (if present). In order to access the parent behaviour it is strongly suggested to use this method rather than theparent member variable directly. In case of threaded or wrapped behaviour in facts the latter may have unexpected values. - Returns:
- The enclosing CompositeBehaviour (if present).
- See Also:
CompositeBehaviour
-
setBehaviourName
public final void setBehaviourName(String name)
Give a name to this behaviour object.- Parameters:
name- The name to give to this behaviour.
-
getBehaviourName
public final String getBehaviourName()
Retrieve the name of this behaviour object. If no explicit name was set, a default one is given, based on the behaviour class name.- Returns:
- The name of this behaviour.
-
action
public abstract void action()
Runs the behaviour. This abstract method must be implemented byBehavioursubclasses to perform ordinary behaviour duty. An agent schedules its behaviours calling theiraction()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:
CompositeBehaviour
-
done
public abstract boolean done()
Check if this behaviour is done. The agent scheduler calls this method to see whether aBehaviourstill 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:
trueif the behaviour has completely executed.
-
onEnd
public int onEnd()
This method is just an empty placeholder for subclasses. It is invoked just once after this behaviour has ended. Therefore, it acts as an epilog for the task represented by thisBehaviour.
Note thatonEndis called after the behaviour has been removed from the pool of behaviours to be executed by an agent. Therefore callingreset()is not sufficient to cyclically repeat the task represented by thisBehaviour. In order to achieve that, thisBehaviourmust be added again to the agent (usingmyAgent.addBehaviour(this)). The same applies to in the case of aBehaviourthat is a child of aParallelBehaviour.- Returns:
- an integer code representing the termination value of the behaviour.
-
onStart
public void onStart()
This method is just an empty placeholders for subclasses. It is executed just once before starting behaviour execution. Therefore, it acts as a prolog to the task represented by thisBehaviour.
-
actionWrapper
public final void actionWrapper()
This method is called internally by the JADE framework and should not be called by the user.
-
setExecutionState
public final void setExecutionState(String s)
-
getExecutionState
public final String getExecutionState()
-
reset
public void reset()
Restores behaviour initial state. This method must be implemented by concrete subclasses in such a way that callingreset()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. Remind to call super.reset() from the sub-classes.
-
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 traveling upwards and by doing nothing when it is traveling downwards, since an ordinary behaviour has no children.- Parameters:
rce- The event to handle
-
root
public Behaviour root()
Returns the root for thisBehaviourobject. That is, the top-level behaviour this one is a part of. Agents apply scheduling only to top-level behaviour objects, so they just callrestart()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
thisis returned. - See Also:
restart()
-
isRunnable
public boolean isRunnable()
Returns whether thisBehaviourobject is blocked or not.- Returns:
truewhen this behaviour is not blocked,falsewhen it is.
-
getRestartCounter
public final long getRestartCounter()
This method is used internally by the framework. Developer should not call or redefine it.
-
block
public void block()
Blocks this behaviour. It should be noticed that this method is NOT a blocking call: when it is invoked, the internal behaviour state is set to Blocked so that, as soon as theaction()method returns, the behaviour is put into a blocked behaviours queue so that it will not be scheduled anymore.
The behaviour is moved back in the pool of active behaviours when either a message is received or the behaviour is explicitly restarted by means of itsrestart()method.
If this behaviour is a child of aCompositeBehavioura suitable event is fired to notify its parent behaviour up to the behaviour composition hierarchy root.- See Also:
restart()
-
handleBlockEvent
protected void handleBlockEvent()
This method is used internally by the framework. Developer should not call or redefine it.
-
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.- A time of
millismilliseconds has passed since the call toblock(). - An ACL message is received by the agent this behaviour belongs to.
- Method
restart()is called explicitly on this behaviour object.
- Parameters:
millis- The amount of time to block, in milliseconds. Notice: a value of 0 formillisis equivalent to a call toblock()without arguments.- See Also:
block()
- A time of
-
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()
-
handleRestartEvent
public void handleRestartEvent()
This method is used internally by the framework. Developer should not call or redefine it.
-
setAgent
public void setAgent(Agent a)
Associates this behaviour with the agent it belongs to. There is no need to call this method explicitly, since theaddBehaviour()call takes care of the association transparently.- Parameters:
a- The agent this behaviour belongs to.- See Also:
Agent.addBehaviour(Behaviour b)
-
getAgent
public Agent getAgent()
-
getDataStore
public DataStore getDataStore()
Return the private data store of thisBehaviour. If it was null, a new DataStore is created and returned.- Returns:
- The private data store of this
Behaviour
-
setDataStore
public void setDataStore(DataStore ds)
Set the private data store of thisBehaviour- Parameters:
ds- theDataStorethat thisBehaviourwill use as its private data store
-
-