Package jade.core
Class Agent
- java.lang.Object
-
- jade.core.Agent
-
- All Implemented Interfaces:
TimerListener,Serializable,Serializable,Runnable
- Direct Known Subclasses:
ams,BackEndContainer.AgentImage,DummyAgent,GatewayAgent,GuiAgent,LogHelperAgent,LogManagerAgent,ScalabilityTest.BitrateReceiverAgent,ScalabilityTest.BitrateSenderAgent,ScalabilityTest.RTTReceiverAgent,ScalabilityTest.RTTSenderAgent,SocketProxyAgent,TestAgent,TestAgent,ToolAgent
public class Agent extends Object implements Runnable, Serializable, TimerListener
TheAgentclass is the common superclass for user defined software agents. It provides methods to perform basic agent tasks, such as:- Message passing using
ACLMessageobjects, both unicast and multicast with optional pattern matching. - Complete Agent Platform life cycle support, including starting, suspending and killing an agent.
- Scheduling and execution of multiple concurrent activities.
Agentsubclasses, adding specific behaviours as needed and exploitingAgentclass capabilities.- Version:
- $Date$ $Revision$
- Author:
- Giovanni Rimassa - Universita' di Parma, Giovanni Caire - TILAB
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAgent.InterruptedInner class Interrupted.
-
Field Summary
Fields Modifier and Type Field Description static intAP_ACTIVERepresents the active agent state.static intAP_DELETEDRepresents the deleted agent state.static intAP_IDLERepresents the idle agent state.static intAP_INITIATEDRepresents the initiated agent state.static intAP_MAXOut of band value for Agent Platform Life Cycle states.static intAP_MINOut of band value for Agent Platform Life Cycle states.static intAP_SUSPENDEDRepresents the suspended agent state.static intAP_WAITINGRepresents the waiting agent state.static intD_ACTIVERepresents the active agent state.static intD_MAXOut of band value for Domain Life Cycle states.static intD_MINOut of band value for Domain Life Cycle states.static intD_RETIREDRepresents the retired agent state.static intD_SUSPENDEDRepresents the suspended agent state.static intD_UNKNOWNRepresents the unknown agent state.static StringMSG_QUEUE_CLASS
-
Constructor Summary
Constructors Constructor Description Agent()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBehaviour(Behaviour b)This method adds a new behaviour to the agent.protected voidafterClone()Actions to perform after cloning.protected voidafterMove()Actions to perform after moving.protected voidbeforeClone()This empty placeholder method shall be overridden by user defined agents to execute some actions before copying an agent to another agent container.protected voidbeforeMove()This empty placeholder shall be overridden by user defined agents to execute some actions before the original agent instance on the source container is stopped (e.g.ACLMessageblockingReceive()Receives an ACL message from the agent message queue.ACLMessageblockingReceive(long millis)Receives an ACL message from the agent message queue, waiting at most a specified amount of time.ACLMessageblockingReceive(MessageTemplate pattern)Receives an ACL message matching a given message template.ACLMessageblockingReceive(MessageTemplate pattern, long millis)Receives an ACL message matching a given message template, waiting at most a specified time.voidchangeStateTo(LifeCycle newLifeCycle)voidclean(boolean ok)protected MessageQueuecreateMessageQueue()Developer can override this method to provide an alternative message queue creation mechanismvoiddoActivate()Make a state transition from suspended to active or waiting (whichever state the agent was in whendoSuspend()was called) within Agent Platform Life Cycle.voiddoClone(Location destination, String newName)Make this agent be cloned on another location.voiddoDelete()Make a state transition from active, suspended or waiting to deleted state within Agent Platform Life Cycle, thereby destroying the agent.voiddoMove(Location destination)Make this agent move to a remote location.voiddoSuspend()Make a state transition from active or waiting to suspended within Agent Platform Life Cycle; the original agent state is saved and will be restored by adoActivate()call.voiddoTimeOut(Timer t)Restarts the behaviour associated with t.voiddoWait()Make a state transition from active to waiting within Agent Platform Life Cycle.voiddoWait(long millis)Make a state transition from active to waiting within Agent Platform Life Cycle.voiddoWake()Make a state transition from waiting to active within Agent Platform Life Cycle.AgentStategetAgentState()AIDgetAID()Method to query the private Agent ID.AIDgetAID(String name)Method to build a complete agent GUID belonging to the same platform of the current agentAIDgetAMS()Get the Agent ID for the platform AMS.Object[]getArguments()Get the array of arguments passed to this agent.intgetBehavioursCnt()Return the number of behaviours currently included in the agent pool (both runnable and blocked).PropertiesgetBootProperties()Return the configuration properties exactly as they were passed to the Profile before starting the local JADE container.AgentContainergetContainerController()Return a controller for the container this agent lives in.ContentManagergetContentManager()Retrieves the agent's content managerintgetCurQueueSize()This method retrieves the current length of the message queue of this agent.AIDgetDefaultDF()Get the Agent ID for the platform default DF.longgetExecutedBehavioursCnt()booleangetGenerateBehaviourEvents()booleangetGenerateMessageEvents()StringgetHap()Method to query the Home Agent Platform.ServiceHelpergetHelper(String serviceName)Retrieves the agent's service helperStringgetLocalName()Method to query the agent local name.StringgetName()Method to query the agent complete name (GUID).<T> TgetO2AInterface(Class<T> theInterface)Used internally by the frameworkObjectgetO2AObject()This method picks an object (if present) from the internal object-to-agent communication queue.longgetPostedMessagesCnt()StringgetProperty(String key, String aDefault)Retrieve a configuration property set in theProfileof the local container (first) or as a System property.intgetQueueSize()Reads message queue size.longgetReceivedMessagesCnt()longgetSentMessagesCnt()intgetState()Read current agent state.Locationhere()Method to retrieve the location this agent is currently at.booleanisAlive()Returns true if the agent implemented by this Agent object is alive (regardless of the actual agent internal state).booleanisRestarting()This method returnstruewhen this agent is restarting after a crash.booleanjoin()This method is used internally by the framework and should NOT be used by programmers.voidnotifyChangeBehaviourState(Behaviour b, String from, String to)voidnotifyRestarted(Behaviour b)Notifies this agent that one of its behaviours has been restarted for some reason.voidpostMessage(ACLMessage msg)Put a received message into the agent message queue.voidputBack(ACLMessage msg)Puts a received ACL message back into the message queue.voidputO2AObject(Object o, boolean blocking)This method should not be used by application code.ACLMessagereceive()Receives an ACL message from the agent message queue.ACLMessagereceive(MessageTemplate pattern)Receives an ACL message matching a given template.List<ACLMessage>receive(MessageTemplate pattern, int max)Read up to max messages from the agent message queue.<T> voidregisterO2AInterface(Class<T> theInterface, T implementation)Registers an implementation for a given O2A interface.voidremoveBehaviour(Behaviour b)This method removes a given behaviour from the agent.voidremoveTimer(Behaviour b)voidrestartLater(Behaviour b, long millis)Schedules a restart for a behaviour, after a certain amount of time has passed.voidrestoreBufferedState()voidrestoreFields(InputStream s)Reads a previously saved agent state, and restore the agent user-defined fields replacing their current values.voidrun()This method is the main body of every agent.voidsend(ACLMessage msg)Send an ACL message to another agent.voidsetArguments(Object[] args)Called by AgentContainerImpl in order to pass arguments to a just created Agent.voidsetEnabledO2ACommunication(boolean enabled, int queueSize)This method declares this agent attitude towards object-to-agent communication, that is, whether the agent accepts to communicate with other non-JADE components living within the same JVM.voidsetGenerateBehaviourEvents(boolean b)Allow enabling/disabling (default: disabled) the generation of message-related events (such as NOTIFY_BEHAVIOUR_ADDED or NOTIFY_CHANGED_BEHAVIOUR_STATE)voidsetGenerateMessageEvents(boolean b)Allow disabling/enabling (default: enabled) the generation of message-related events (such as NOTIFY_POSTED or NOTIFY_RECEIVED)voidsetO2AManager(Behaviour b)Sets the behaviour responsible for managing objects passed to the agent by means of the Object-To-Agent (O2A) communication mechanism.voidsetQueueSize(int newSize)Set message queue size.protected voidsetup()This protected method is an empty placeholder for application specific startup code.protected voidtakeDown()This protected method is an empty placeholder for application specific cleanup code.voidwaitUntilStarted()This method blocks until the agent has finished its start-up phase (i.e.voidwrite(OutputStream s)Write this agent to an output stream; this method can be used to record a snapshot of the agent state on a file or to send it through a network connection.
-
-
-
Field Detail
-
AP_MIN
public static final int AP_MIN
Out of band value for Agent Platform Life Cycle states.- See Also:
- Constant Field Values
-
AP_INITIATED
public static final int AP_INITIATED
Represents the initiated agent state.- See Also:
- Constant Field Values
-
AP_ACTIVE
public static final int AP_ACTIVE
Represents the active agent state.- See Also:
- Constant Field Values
-
AP_IDLE
public static final int AP_IDLE
Represents the idle agent state.- See Also:
- Constant Field Values
-
AP_SUSPENDED
public static final int AP_SUSPENDED
Represents the suspended agent state.- See Also:
- Constant Field Values
-
AP_WAITING
public static final int AP_WAITING
Represents the waiting agent state.- See Also:
- Constant Field Values
-
AP_DELETED
public static final int AP_DELETED
Represents the deleted agent state.- See Also:
- Constant Field Values
-
AP_MAX
public static final int AP_MAX
Out of band value for Agent Platform Life Cycle states.- See Also:
- Constant Field Values
-
D_MIN
public static final int D_MIN
Out of band value for Domain Life Cycle states.- See Also:
- Constant Field Values
-
D_ACTIVE
public static final int D_ACTIVE
Represents the active agent state.- See Also:
- Constant Field Values
-
D_SUSPENDED
public static final int D_SUSPENDED
Represents the suspended agent state.- See Also:
- Constant Field Values
-
D_RETIRED
public static final int D_RETIRED
Represents the retired agent state.- See Also:
- Constant Field Values
-
D_UNKNOWN
public static final int D_UNKNOWN
Represents the unknown agent state.- See Also:
- Constant Field Values
-
D_MAX
public static final int D_MAX
Out of band value for Domain Life Cycle states.- See Also:
- Constant Field Values
-
MSG_QUEUE_CLASS
public static final String MSG_QUEUE_CLASS
- See Also:
- Constant Field Values
-
-
Method Detail
-
restartLater
public void restartLater(Behaviour b, long millis)
Schedules a restart for a behaviour, after a certain amount of time has passed.- Parameters:
b- The behaviour to restart later.millis- The amount of time to wait before restartingb.- See Also:
Behaviour.block(long millis)
-
doTimeOut
public void doTimeOut(Timer t)
Restarts the behaviour associated with t. This method runs within the time-critical Timer Dispatcher thread and is not intended to be called by users. It is defined public only because is part of theTimerListenerinterface.- Specified by:
doTimeOutin interfaceTimerListener
-
notifyRestarted
public void notifyRestarted(Behaviour b)
Notifies this agent that one of its behaviours has been restarted for some reason. This method clears any timer associated with behaviour objectb, and it is unneeded by application level code. To explicitly schedule behaviours, useblock()andrestart()methods.- Parameters:
b- The behaviour object which was restarted.- See Also:
Behaviour.restart()
-
removeTimer
public void removeTimer(Behaviour b)
-
createMessageQueue
protected MessageQueue createMessageQueue()
Developer can override this method to provide an alternative message queue creation mechanism- Returns:
- The MessageQueue to be used by this agent or null if the internal message queue must be used
-
getContainerController
public AgentContainer getContainerController()
Return a controller for the container this agent lives in.
NOT available in MIDP- Returns:
- jade.wrapper.AgentContainer a controller for the container this agent lives in.
-
setArguments
public final void setArguments(Object[] args)
Called by AgentContainerImpl in order to pass arguments to a just created Agent.Usually, programmers do not need to call this method in their code.
-
getArguments
public Object[] getArguments()
Get the array of arguments passed to this agent.Take care that the arguments are transient and they do not migrate with the agent neither are cloned with the agent!
- Returns:
- the array of arguments passed to this agent.
- See Also:
- How to use arguments or properties to configure your agent.
-
isRestarting
public final boolean isRestarting()
This method returnstruewhen this agent is restarting after a crash. The restarting indication is automatically reset as soon as thesetup()method of this agent terminates.- Returns:
truewhen this agent is restarting after a crash.falseotherwise.
-
getAMS
public final AID getAMS()
Get the Agent ID for the platform AMS.- Returns:
- An
AIDobject, that can be used to contact the AMS of this platform.
-
getDefaultDF
public AID getDefaultDF()
Get the Agent ID for the platform default DF.- Returns:
- An
AIDobject, that can be used to contact the default DF of this platform.
-
getLocalName
public final String getLocalName()
Method to query the agent local name.- Returns:
- A
Stringcontaining the local agent name (e.g. peter).
-
getName
public final String getName()
Method to query the agent complete name (GUID).- Returns:
- A
Stringcontaining the complete agent name (e.g. peter@fipa.org:50).
-
getHap
public final String getHap()
Method to query the Home Agent Platform. This is the name of the platform where the agent has been created, therefore it will never change during the entire lifetime of the agent. In JADE the name of an agent by default is composed by the concatenation (using '@') of the agent local name and the Home Agent Platform name- Returns:
- A
Stringcontaining the name of the home agent platform (e.g. myComputerName:1099/JADE).
-
getAID
public final AID getAID()
Method to query the private Agent ID. Note that this Agent ID is different from the one that is registered with the platform AMS.- Returns:
- An
Agent IDobject, containing the complete agent GUID, addresses and resolvers.
-
getAID
public final AID getAID(String name)
Method to build a complete agent GUID belonging to the same platform of the current agent- Parameters:
name- Agent local name.- Returns:
- An
Agent IDobject, containing the complete agent GUID, addresses and resolvers.
-
isAlive
public boolean isAlive()
Returns true if the agent implemented by this Agent object is alive (regardless of the actual agent internal state). This means that the internal Thread of this Agent object exists and is alive.- Returns:
- true if the agent implemented by this Agent object is alive
-
here
public Location here()
Method to retrieve the location this agent is currently at.- Returns:
- A
Locationobject, describing the location where this agent is currently running.
-
join
public boolean join()
This method is used internally by the framework and should NOT be used by programmers. This is used by the agent container to wait for agent termination. We have already called doDelete on the thread which would have issued an interrupt on it. However, it still may decide not to exit. So we will wait no longer than 5 seconds for it to exit and we do not care of this zombie agent. FIXME: we must further isolate container and agents, for instance by using custom class loader and dynamic proxies and JDK 1.3. FIXME: the timeout value should be got by Profile
-
setQueueSize
public void setQueueSize(int newSize) throws IllegalArgumentExceptionSet message queue size. This method allows to change the number of ACL messages that can be buffered before being actually read by the agent or discarded.- Parameters:
newSize- A non negative integer value to set message queue size to. Passing 0 means unlimited message queue. When the number of buffered messages exceeds this value, older messages are discarded according to a FIFO replacement policy.- Throws:
IllegalArgumentException- IfnewSizeis negative.- See Also:
getQueueSize()
-
getCurQueueSize
public int getCurQueueSize()
This method retrieves the current length of the message queue of this agent.- Returns:
- The number of messages that are currently stored into the message queue.
-
getQueueSize
public int getQueueSize()
Reads message queue size. A zero value means that the message queue is unbounded (its size is limited only by amount of available memory).- Returns:
- The actual size of the message queue (i.e. the max number of messages that can be stored into the queue)
- See Also:
setQueueSize(int newSize),getCurQueueSize()
-
getSentMessagesCnt
public long getSentMessagesCnt()
-
getPostedMessagesCnt
public long getPostedMessagesCnt()
-
getReceivedMessagesCnt
public long getReceivedMessagesCnt()
-
getExecutedBehavioursCnt
public long getExecutedBehavioursCnt()
-
changeStateTo
public void changeStateTo(LifeCycle newLifeCycle)
-
restoreBufferedState
public void restoreBufferedState()
-
getState
public int getState()
Read current agent state. This method can be used to query an agent for its state from the outside.- Returns:
- the Agent Platform Life Cycle state this agent is currently in.
-
getAgentState
public AgentState getAgentState()
-
doMove
public void doMove(Location destination)
Make this agent move to a remote location. This method is intended to support agent mobility and is called either by the Agent Platform or by the agent itself to start a migration process. It should be noted that this method just changes the agent state toAP_TRANSIT. The actual migration takes place asynchronously.
NOT available in MIDP- Parameters:
destination- TheLocationto migrate to.
-
doClone
public void doClone(Location destination, String newName)
Make this agent be cloned on another location. This method is intended to support agent mobility and is called either by the Agent Platform or by the agent itself to start a clonation process. It should be noted that this method just changes the agent state toAP_COPY. The actual clonation takes place asynchronously.
NOT available in MIDP- Parameters:
destination- TheLocationwhere the copy agent will start.newName- The name that will be given to the copy agent.
-
doSuspend
public void doSuspend()
Make a state transition from active or waiting to suspended within Agent Platform Life Cycle; the original agent state is saved and will be restored by adoActivate()call. This method can be called from the Agent Platform or from the agent itself and stops all agent activities. Incoming messages for a suspended agent are buffered by the Agent Platform and are delivered as soon as the agent resumes. CallingdoSuspend()on a suspended agent has no effect.
NOT available in MIDP- See Also:
doActivate()
-
doActivate
public void doActivate()
Make a state transition from suspended to active or waiting (whichever state the agent was in whendoSuspend()was called) within Agent Platform Life Cycle. This method is called from the Agent Platform and resumes agent execution. CallingdoActivate()when the agent is not suspended has no effect.
NOT available in MIDP- See Also:
doSuspend()
-
doWait
public void doWait()
Make a state transition from active to waiting within Agent Platform Life Cycle. This method has only effect when called by the agent thread and causes the agent to block, stopping all its activities until a message arrives or thedoWake()method is called.- See Also:
doWake()
-
doWait
public void doWait(long millis)
Make a state transition from active to waiting within Agent Platform Life Cycle. This method adds a timeout to the otherdoWait()version.- Parameters:
millis- The timeout value, in milliseconds.- See Also:
doWait()
-
doWake
public void doWake()
Make a state transition from waiting to active within Agent Platform Life Cycle. This method is called from Agent Platform and resumes agent execution. CallingdoWake()when an agent is not waiting has no effect.- See Also:
doWait()
-
doDelete
public void doDelete()
Make a state transition from active, suspended or waiting to deleted state within Agent Platform Life Cycle, thereby destroying the agent. This method can be called either from the Agent Platform or from the agent itself. CallingdoDelete()on an already deleted agent has no effect.
-
write
public void write(OutputStream s) throws IOException
Write this agent to an output stream; this method can be used to record a snapshot of the agent state on a file or to send it through a network connection. Of course, the whole agent must be serializable in order to be written successfully. The saved state of the agent can later be restored via the restoreFields() method
NOT available in MIDP- Parameters:
s- The stream this agent will be sent to. The stream is not closed on exit.- Throws:
IOException- Thrown if some I/O error occurs during writing.- See Also:
restoreFields(InputStream s)
-
restoreFields
public void restoreFields(InputStream s) throws Exception
Reads a previously saved agent state, and restore the agent user-defined fields replacing their current values. Only non-transient and non-static fields declared in the class of the agent will be restored. Fields declared in super-classes are left unchanged. The jade.core.Restore annotation can be used to skip the restoration of a field or to specify a user-defined method to perform the restoration.
NOT available in MIDP- Parameters:
s- The input stream the agent state will be read from.- Throws:
Exception
-
putO2AObject
public void putO2AObject(Object o, boolean blocking) throws InterruptedException
This method should not be used by application code. Use the same-named method ofjade.wrapper.Agentinstead.
NOT available in MIDP- Throws:
InterruptedException- See Also:
AgentController.putO2AObject(Object o, boolean blocking)
-
getO2AObject
public Object getO2AObject()
This method picks an object (if present) from the internal object-to-agent communication queue. In order for this method to work, the agent must have declared its will to accept objects from other software components running within its JVM. This can be achieved by calling thejade.core.Agent.setEnabledO2ACommunication()method. If the retrieved object was originally inserted by an external component using a blocking call, that call will return during the execution of this method.
NOT available in MIDP- Returns:
- the first object in the queue, or
nullif the queue is empty. - See Also:
AgentController.putO2AObject(Object o, boolean blocking),setEnabledO2ACommunication(boolean enabled, int queueSize)
-
setEnabledO2ACommunication
public void setEnabledO2ACommunication(boolean enabled, int queueSize)This method declares this agent attitude towards object-to-agent communication, that is, whether the agent accepts to communicate with other non-JADE components living within the same JVM.
NOT available in MIDP- Parameters:
enabled- Tells whether Java objects inserted withputO2AObject()will be accepted.queueSize- If the object-to-agent communication is enabled, this parameter specifiies the maximum number of Java objects that will be queued. If the passed value is 0, no maximum limit is set up for the queue.- See Also:
AgentController.putO2AObject(Object o, boolean blocking),getO2AObject()
-
setO2AManager
public void setO2AManager(Behaviour b)
Sets the behaviour responsible for managing objects passed to the agent by means of the Object-To-Agent (O2A) communication mechanism. If the O2A manager behaviour is set, whenever an object is inserted in the O2A queue by means of theputO2AObject()method, only the manager is waken up. This improves the efficiency since all behaviours not interested in O2A communication remain sleeping.
NOTE that this method only declares a behaviour as being responsible for managing objects received by the agent by means of the O2A communication channel; in order to correctly run, the behaviour must be added to the agent by means of theaddBehaviour()method as usual.
NOT available in MIDP- Parameters:
b- The behaviour that will act as O2A manager.- See Also:
AgentController.putO2AObject(Object o, boolean blocking),getO2AObject()
-
getO2AInterface
public <T> T getO2AInterface(Class<T> theInterface)
Used internally by the framework
-
registerO2AInterface
public <T> void registerO2AInterface(Class<T> theInterface, T implementation)
Registers an implementation for a given O2A interface. All invocations to methods of the O2A interface will be redirected to the registered implementation object.- Parameters:
theInterface- The O2A interface the implementation is registered for.implementation- The object providing an implementation for the given O2A interface.
-
run
public final void run()
This method is the main body of every agent. It provides startup and cleanup hooks for application programmers to put their specific code into.- Specified by:
runin interfaceRunnable- See Also:
setup(),takeDown()
-
clean
public void clean(boolean ok)
-
setup
protected void setup()
This protected method is an empty placeholder for application specific startup code. Agent developers can override it to provide necessary behaviour. When this method is called the agent has been already registered with the Agent Platform AMS and is able to send and receive messages. However, the agent execution model is still sequential and no behaviour scheduling is active yet. This method can be used for ordinary startup tasks such as DF registration, but is essential to add at least aBehaviourobject to the agent, in order for it to be able to do anything.- See Also:
addBehaviour(Behaviour b),Behaviour
-
takeDown
protected void takeDown()
This protected method is an empty placeholder for application specific cleanup code. Agent developers can override it to provide necessary behaviour. When this method is called the agent has not deregistered itself with the Agent Platform AMS and is still able to exchange messages with other agents. However, no behaviour scheduling is active anymore and the Agent Platform Life Cycle state is already set to deleted. This method can be used for ordinary cleanup tasks such as DF deregistration, but explicit removal of all agent behaviours is not needed.
-
beforeMove
protected void beforeMove()
This empty placeholder shall be overridden by user defined agents to execute some actions before the original agent instance on the source container is stopped (e.g. releasing local resources such as a GUI).
IMPORTANT: At this point, it is ensured that the move process is successful and that a moved agent instance has been created on the destination container Therefore setting the value of a class field in this method will have no impact on the moved agent instance. Such parameters must indeed be set before thedoMove()method is called.
NOT available in MIDP
-
afterMove
protected void afterMove()
Actions to perform after moving. This empty placeholder method can be overridden by user defined agents to execute some actions just after arriving to the destination agent container for a migration.
NOT available in MIDP
-
beforeClone
protected void beforeClone()
This empty placeholder method shall be overridden by user defined agents to execute some actions before copying an agent to another agent container.
NOT available in MIDP- See Also:
beforeMove(),afterClone()
-
afterClone
protected void afterClone()
Actions to perform after cloning. This empty placeholder method can be overridden by user defined agents to execute some actions just after creating an agent copy to the destination agent container.
NOT available in MIDP
-
addBehaviour
public void addBehaviour(Behaviour b)
This method adds a new behaviour to the agent. This behaviour will be executed concurrently with all the others, using a cooperative round robin scheduling. This method is typically called from an agentsetup()to fire off some initial behaviour, but can also be used to spawn new behaviours dynamically.
-
removeBehaviour
public void removeBehaviour(Behaviour b)
This method removes a given behaviour from the agent. This method is called automatically when a top level behaviour terminates, but can also be called from a behaviour to terminate itself or some other behaviour.- Parameters:
b- The behaviour to remove.- See Also:
Behaviour
-
getBehavioursCnt
public int getBehavioursCnt()
Return the number of behaviours currently included in the agent pool (both runnable and blocked).
-
send
public final void send(ACLMessage msg)
Send an ACL message to another agent. This methods sends a message to the agent specified in:receivermessage field (more than one agent can be specified as message receiver).- Parameters:
msg- An ACL message object containing the actual message to send.- See Also:
ACLMessage
-
receive
public final ACLMessage receive()
Receives an ACL message from the agent message queue. This method is non-blocking and returns the first message in the queue, if any. Therefore, polling and busy waiting is required to wait for the next message sent using this method.- Returns:
- A new ACL message, or
nullif no message is present. - See Also:
ACLMessage
-
receive
public final ACLMessage receive(MessageTemplate pattern)
Receives an ACL message matching a given template. This method is non-blocking and returns the first matching message in the queue, if any. Therefore, polling and busy waiting is required to wait for a specific kind of message using this method.- Parameters:
pattern- A message template to match received messages against.- Returns:
- A new ACL message matching the given template, or
nullif no such message is present. - See Also:
ACLMessage,MessageTemplate
-
receive
public final List<ACLMessage> receive(MessageTemplate pattern, int max)
Read up to max messages from the agent message queue. This can be useful for performance reasons in situations where an agent has to deal with a heavy load of messages.- Parameters:
pattern- The MessageTemplate specifying which messages must be read. Setting this to null is equivalent to a MatchAll templatemax- The maximum number of messages to read- Returns:
- The read messages in form of a List or null if no message matching the specified template is currently present in the agent message queue
-
blockingReceive
public final ACLMessage blockingReceive()
Receives an ACL message from the agent message queue. This method is blocking and suspends the whole agent until a message is available in the queue.- Returns:
- A new ACL message, blocking the agent until one is available.
- See Also:
receive(),ACLMessage
-
blockingReceive
public final ACLMessage blockingReceive(long millis)
Receives an ACL message from the agent message queue, waiting at most a specified amount of time.- Parameters:
millis- The maximum amount of time to wait for the message.- Returns:
- A new ACL message, or
nullif the specified amount of time passes without any message reception.
-
blockingReceive
public final ACLMessage blockingReceive(MessageTemplate pattern)
Receives an ACL message matching a given message template. This method is blocking and suspends the whole agent until a message is available in the queue.- Parameters:
pattern- A message template to match received messages against.- Returns:
- A new ACL message matching the given template, blocking until such a message is available.
- See Also:
receive(MessageTemplate),ACLMessage,MessageTemplate
-
blockingReceive
public final ACLMessage blockingReceive(MessageTemplate pattern, long millis)
Receives an ACL message matching a given message template, waiting at most a specified time.- Parameters:
pattern- A message template to match received messages against.millis- The amount of time to wait for the message, in milliseconds.- Returns:
- A new ACL message matching the given template, or
nullif no suitable message was received withinmillismilliseconds. - See Also:
blockingReceive()
-
putBack
public final void putBack(ACLMessage msg)
Puts a received ACL message back into the message queue. This method can be used from an agent behaviour when it realizes it read a message of interest for some other behaviour. The message is put in front of the message queue, so it will be the first returned by areceive()call.- See Also:
receive()
-
waitUntilStarted
public void waitUntilStarted()
This method blocks until the agent has finished its start-up phase (i.e. until just before its setup() method is called. When this method returns, the target agent is registered with the AMS and the JADE platform is aware of it.
-
notifyChangeBehaviourState
public void notifyChangeBehaviourState(Behaviour b, String from, String to)
-
setGenerateMessageEvents
public void setGenerateMessageEvents(boolean b)
Allow disabling/enabling (default: enabled) the generation of message-related events (such as NOTIFY_POSTED or NOTIFY_RECEIVED)
-
getGenerateMessageEvents
public boolean getGenerateMessageEvents()
-
setGenerateBehaviourEvents
public void setGenerateBehaviourEvents(boolean b)
Allow enabling/disabling (default: disabled) the generation of message-related events (such as NOTIFY_BEHAVIOUR_ADDED or NOTIFY_CHANGED_BEHAVIOUR_STATE)
-
getGenerateBehaviourEvents
public boolean getGenerateBehaviourEvents()
-
postMessage
public final void postMessage(ACLMessage msg)
Put a received message into the agent message queue. The message is put at the back end of the queue. This method is called by JADE runtime system when a message arrives, but can also be used by an agent, and is just the same as sending a message to oneself (though slightly faster).- Parameters:
msg- The ACL message to put in the queue.- See Also:
send(ACLMessage msg)
-
getContentManager
public ContentManager getContentManager()
Retrieves the agent's content manager- Returns:
- The content manager.
-
getHelper
public ServiceHelper getHelper(String serviceName) throws ServiceException
Retrieves the agent's service helper- Returns:
- The service helper.
- Throws:
ServiceException
-
getProperty
public String getProperty(String key, String aDefault)
Retrieve a configuration property set in theProfileof the local container (first) or as a System property.- Parameters:
key- the key that maps to the property that has to be retrieved.aDefault- a default value to be returned if there is no mapping forkey
-
getBootProperties
public Properties getBootProperties()
Return the configuration properties exactly as they were passed to the Profile before starting the local JADE container.
-
-