|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object gov.nasa.arc.brahms.vm.api.jagt.AbstractExternalAgent brahms.communication.AbstractCommunicationAgent
public abstract class AbstractCommunicationAgent
The AbstractCommunicationAgent is a base class for external agents that communicate with other agents solely using CommunicativeActs. This agent supports simulation mode, realtime mode and distributed realtime mode.
The minimum set of methods to be implemented by an implementation are:
getConfigurationFile
onInitialize
onStart
onStop
onMessage
getName
getQualifiedName
getConfigurationFile
onPause
onResume
sendMessage(CommunicativeAct message, IActiveInstance recipient)
sendMessage(IObject message, IActiveInstance recipient)
The agent automatically loads the configuration properties for
the agent if a configuration file is specified getConfigurationFile
.
These properties are available through getProperties
.
The agent automatically enqueues any incoming CommunicativeActs
in the agent's onReceive method. The messages are processed in
onMessage. The agent calls onMessage for incoming message at the
appropriate time depending on the mode of operation (either in doWork
in the agent's thread when in a realtime mode or in simulation mode
when isAutoReady() returns false, or in onProcess when in
simulation mode and isAutoReady() returns true). Methods are available
to manipulate the incoming message queue
(enqueue/dequeueIncomingMessage
).
The agent should send out messages using any of the sendMessage
methods. This ensures that outgoing communications are sent out
using the appropriate methods and that the scheduler is made aware
of the communication when in simulation mode. The agent can register
an IReplyHandler for a message that is sent out if responses are
expected for that message. In that case the agent will forward
responses to the appropriate IReplyHandler and will not call
onMessage for those responses. Only one IReplyHandler can be registered
for a CommunicativeAct, if a new IReplyHandler is registered it
replaces the previously registered IReplyHandler. Use
removeReplyHandler(CommunicativeAct)
to remove
the reply handler when it is no longer needed. NOTE: Be very careful
with the reply handlers when the agent is run in simulation
mode and isAutoReady returns true. The reply handler must not
block or it will prevent the transmission of any outgoing
messages and prevent any responses for the message from being
delivered.
If time synchronization is of importance in simulation mode,
i.e. the agent must ensure that that simulation does not advance
until it completes its incoming message processing, then the
agent must set the auto-ready flag to false setAutoReady(false)
in its initialization method onInitialize
. By
default auto-ready is true.
Having auto-ready set to false makes message processing more
efficient in that all incoming
messages are processed on the agent's thread of execution
instead of on the scheduler's thread of execution. However
the agent ready()
when it finishes doing all of its needed processing and
wants the simulation to resume. This notifies the scheduler
to distribute any outgoing messages and to notify this agent
of any messages that were queued for delivery. If no events
are delivered to the agent during simulation cycles or
any delivered events do not result in messages to be
processed then automatically a ready() is invoked by this
agent when onProcess is called. So only when there are
incoming CommunicativeActs the agent must call ready() to
resume the simulation. Note that setAutoReady, isAutoReady
and ready have no influence when the agent is run in
real-time mode.
The agent automatically acquires a memory reference for every CommunicativeAct it creates and receives. By default the agent will automatically release the reference to the CommunicativeAct:
setAutoMemoryReferenceRelease(false)
. In this case
only when the agent is stopped will this implementation release
all references to CommunicativeActs that are queued in the
incoming and outgoing message queues for CommunicativeActs that
have not yet been processed and release the references for
any CommunicativeActs for which reply handlers are registered.
Constructor Summary | |
---|---|
AbstractCommunicationAgent()
|
Method Summary | |
---|---|
CommunicativeAct |
dequeueIncomingMessage()
Removes the first CommunicativeAct from the incoming queue and blocks the calling thread if no messages are queued. |
CommunicativeAct |
dequeueIncomingMessage(boolean block)
Removes the first CommunicativeAct from the incoming queue and blocks the calling thread if no data is queued and block = true or returns null if block = false. |
void |
doWork()
The virtual machine takes care of having the agent run in its own thread of execution. |
void |
enqueueIncomingMessage(CommunicativeAct message)
Adds the specified CommunicativeAct to the incoming message queue. |
java.lang.String |
getConfigurationFile()
Returns the name of the file used to load the agent's configuration properties. |
java.lang.String |
getName()
Returns this communication agent's simple name. |
java.util.Properties |
getProperties()
Returns the configuration properties for this agent. |
java.lang.String |
getQualifiedName()
Returns this communication agent's fully qualified name. |
RunState |
getRunState()
Returns the RunState for the agent. |
int[] |
getStateLock()
Returns the lock used to make access to the RunState thread safe. |
boolean |
hasIncomingMessage()
Indicates whether one or more messages are still in the queue. |
void |
initialize()
Initialize is used to initialize the external agent after it has been loaded into the virtual machine. |
boolean |
isAutoMemoryReferenceRelease()
Indicates whether the agent should automatically release the memory references obtained to the CommunicativeActs it creates and/or receives. |
protected java.util.Properties |
loadConfigurationProperties()
Loads the configuration properties for the agent. |
void |
onAssert(IFact fact)
onAssert is called by the virtual machine whenever a new fact is asserted in the world state. |
abstract void |
onInitialize()
Called when the agent is being initialized after it has been loaded into virtual machine. |
abstract void |
onMessage(CommunicativeAct message)
Processes the specified message received by this agent. |
void |
onPause()
Called when the agent is being paused. |
void |
onProcess(long time)
onProcess is called by the virtual machine when the virtual machine is in simulation mode and the agent is notified by the scheduler that it is allowed to process any events for the specified time. |
void |
onReceive(IBelief belief)
onReceive is called by the virtual machine whenever a new belief is communicated to the external agent by another agent or object. |
void |
onReset()
Called when the agent is being reset. |
void |
onResume()
Called when the agent is being resumed from a pause. |
void |
onRetract(IFact fact)
onRetract is called by the virtual machine whenever a fact is retracted from the world state. |
abstract void |
onStart()
Called when the agent is being started. |
abstract void |
onStop()
Called when the agent is being stopped. |
void |
pause()
Pauses the external agent. |
void |
ready()
Notifies the scheduler that the agent completed its processing and is ready to receive more events. |
void |
removeReplyHandler(CommunicativeAct message)
Removes the IReplyHandler that was registered for the specified CommunicativeAct. |
void |
removeReplyHandler(IObject message)
Removes the IReplyHandler that was registered for the specified CommunicativeAct. |
void |
reset()
Resets the external agent to the initialized state as it was placed in using the initialize method, performing any additional clean-up if necessary. |
void |
resume()
Resumes the external agent from a pause. |
void |
sendMessage(CommunicativeAct message,
IActiveInstance recipient)
Sends the specified message to the specified recipient. |
void |
sendMessage(CommunicativeAct message,
IActiveInstance recipient,
IReplyHandler replyHandler)
Sends the specified message to the specified recipient and has responses for the specified message sent to the specified reply handler. |
void |
setAutoMemoryReferenceRelease(boolean autoRelease)
Sets whether the agent should automatically release the memory references obtained to the CommunicativeActs it creates and/or receives. |
void |
setRunState(RunState state)
Sets the RunState for the agent. |
void |
start()
Starts the external agent. |
void |
stop()
Stops the external agent. |
Methods inherited from class gov.nasa.arc.brahms.vm.api.jagt.AbstractExternalAgent |
---|
assertFact, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, conclude, deregisterForMonitoring, getBrahmsAgent, getContext, getModel, getModeOfOperation, getVMController, getWorldState, isAcceptingFacts, isAutoReady, isValidTruthValue, notifyNewEvents, onThreadEnd, registerForMonitoring, retractFact, send, send, send, send, setAcceptingFacts, setAgentCtx, setAutoReady, setBrahmsAgent, setProperties, setVMController, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public AbstractCommunicationAgent()
Method Detail |
---|
public final java.lang.String getName()
public final java.lang.String getQualifiedName()
public java.lang.String getConfigurationFile()
getName()+".cfg"
public java.util.Properties getProperties()
public final int[] getStateLock()
public final RunState getRunState()
Note: This method is not threadsafe, use the state lock to enforce a thread safe retrieval of the RunState.
getStateLock()
public final void setRunState(RunState state)
Note: This method is not threadsafe, use the state lock to enforce a thread safe modification of the RunState.
state
- RunStategetStateLock()
public final boolean isAutoMemoryReferenceRelease()
public final void setAutoMemoryReferenceRelease(boolean autoRelease)
autoRelease
- true if the agent is to automatically release the
memory references, false to have the agent implementation
take care of it.public final void initialize() throws ExternalException
initialize
in interface IExternalAgent
initialize
in class AbstractExternalAgent
ExternalException
- if an internal error occurs.protected java.util.Properties loadConfigurationProperties()
public abstract void onInitialize() throws ExternalException
ExternalException
- if an internal error occurs.public final void start() throws ExternalException
start
in interface IExternalAgent
start
in class AbstractExternalAgent
ExternalException
- if an internal error occurs.public abstract void onStart() throws ExternalException
ExternalException
- if an internal error occurspublic final void pause() throws ExternalException
pause
in interface IExternalAgent
pause
in class AbstractExternalAgent
ExternalException
- if an internal error occurs.public void onPause() throws ExternalException
ExternalException
- if an internal error occurspublic final void resume() throws ExternalException
resume
in interface IExternalAgent
resume
in class AbstractExternalAgent
ExternalException
- if an internal error occurs.public void onResume() throws ExternalException
ExternalException
- if an internal error occurspublic final void stop() throws ExternalException
stop
in interface IExternalAgent
stop
in class AbstractExternalAgent
ExternalException
- if an internal error occurs.public abstract void onStop() throws ExternalException
ExternalException
- if an internal error occurspublic final void reset() throws ExternalException
reset
in interface IExternalAgent
reset
in class AbstractExternalAgent
ExternalException
- if an internal error occurs.public void onReset() throws ExternalException
ExternalException
- if an internal error occurspublic final void doWork() throws ExternalException
doWork
in interface IExternalAgent
doWork
in class AbstractExternalAgent
ExternalException
- if an unrecoverable error occurs.public final void onProcess(long time) throws ExternalException
This method first send out any scheduled outgoing messages, scheduled
by calls to sendMessage
, then processes any messages
that were received by this agent calling onMessage
for
each incoming message, lastly it sends out any new scheduled outgoing
messages, scheduled as a result of processing the incoming messages.
onProcess
in interface IExternalAgent
onProcess
in class AbstractExternalAgent
time
- the time for which agents can process their events.
ExternalException
- if an internal error occursAbstractExternalAgent.onProcess(long)
public void enqueueIncomingMessage(CommunicativeAct message) throws QueueException
message
- the CommunicativeAct to be enqueued
QueueException
- if the queue was closed.public CommunicativeAct dequeueIncomingMessage() throws QueueException
QueueException
- if the queue was closed while waiting
for new messages.public CommunicativeAct dequeueIncomingMessage(boolean block) throws QueueException
block
- true to have the method block until a message is enqueued,
false to not block and return null if no messages are present
QueueException
- if the queue was closed while waiting
for new messages.public boolean hasIncomingMessage()
public abstract void onMessage(CommunicativeAct message)
message
- the CommunicativeAct with the message detailspublic void sendMessage(CommunicativeAct message, IActiveInstance recipient) throws ExternalException
message
- the CommunicativeAct to be sentrecipient
- the IActiveInstance to deliver the message to
ExternalException
- if message is not a CommunicativeAct or
because of an internal error or in case of a communication
failure the linked exception will be one of
ServiceException, NoSuchActorException, ActorNotActiveException,
NotLocatableException, TransportFailure, or SecurityFailureServiceException
,
SecurityFailure
,
NoSuchActorException
,
ActorNotActiveException
,
NotLocatableException
,
TransportFailure
public void sendMessage(CommunicativeAct message, IActiveInstance recipient, IReplyHandler replyHandler) throws ExternalException
message
- the CommunicativeAct to be sentrecipient
- the IActiveInstance to deliver the message toreplyHandler
- the IReplyHandler to notify of any responses to
the specified message
ExternalException
- if message is not a CommunicativeAct or
because of an internal error or in case of a communication
failure the linked exception will be one of
ServiceException, NoSuchActorException, ActorNotActiveException,
NotLocatableException, TransportFailure, or SecurityFailureServiceException
,
SecurityFailure
,
NoSuchActorException
,
ActorNotActiveException
,
NotLocatableException
,
TransportFailure
public final void ready()
isAutoReady
this method is a no-op, if
not isAutoReady
the agent must notify the
scheduler when it is ready to receive new events after
onProcess has been called. Only when ready is called
will the simulation proceed. The simulation waits after
onProcess is called for all agents until all agents are
ready to receive more events before the simulation
distributes the new events and advances time.
Note: Only relevant when the agent is used in simulation mode a no-op in real-time mode.
ready
in interface IExternalAgent
ready
in class AbstractExternalAgent
public void removeReplyHandler(CommunicativeAct message)
message
- the CommunicativeAct for which to remove the IReplyHandlerpublic void removeReplyHandler(IObject message)
message
- the Brahms CommunicativeAct for which to remove the IReplyHandlerpublic void onReceive(IBelief belief) throws ExternalException
onReceive
in interface IExternalAgent
onReceive
in class AbstractExternalAgent
belief
- the IBelief communicated to this external agent
ExternalException
- if an error occurs handling this beliefpublic void onAssert(IFact fact) throws ExternalException
onAssert
in interface IExternalAgent
onAssert
in class AbstractExternalAgent
fact
- the IFact asserted in the world state
ExternalException
- if an error occurs handling this factpublic void onRetract(IFact fact) throws ExternalException
onRetract
in interface IExternalAgent
onRetract
in class AbstractExternalAgent
fact
- the IFact retracted from the world state
ExternalException
- if an error occurs handling this fact
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |