Class SubscriptionInitiator
- java.lang.Object
-
- jade.core.behaviours.Behaviour
-
- jade.core.behaviours.CompositeBehaviour
-
- jade.core.behaviours.SerialBehaviour
-
- jade.core.behaviours.FSMBehaviour
-
- jade.proto.SubscriptionInitiator
-
- All Implemented Interfaces:
Serializable,Serializable
- Direct Known Subclasses:
DFSubscriber
public class SubscriptionInitiator extends FSMBehaviour
This is a single homogeneous and effective implementation of the initiator role in all the FIPA-Subscribe-like interaction protocols defined by FIPA, that is all those protocols where the initiator sends a single "subscription" message and receives notifications each time a given condition becomes true. This implementation works both for 1:1 and 1:N conversations.FIPA has already specified a number of these interaction protocols, like FIPA-subscribe and FIPA-request-whenever.
The structure of these protocols is always the same. The initiator sends a "subscription" message (in general it performs a communicative act).
The responder can then reply by sending a
not-understood, arefuseor anagreemessage to communicate that the subscription has been agreed. This first category of reply messages has been here identified as a "response". Sending no response is allowed and is equivalent to sending anagree.Each time the condition indicated within the subscription message becomes true, the responder sends proper "notification" messages to the initiator.
This behaviour terminates if a) neither a response nor a notification has been received before the timeout set by the
reply-byof the subscription message has expired or b) all responders replied with REFUSE or NOT_UNDERSTOOD. Otherwise the behaviour will run forever.NOTE that this implementation is in an experimental state and the API will possibly change in next versions also taking into account that the FIPA specifications related to subscribe-like protocols are not yet stable.
Read carefully the section of the JADE programmer's guide that describes the usage of this class.
One message for every receiver is sent instead of a single message for all the receivers.- Author:
- Giovanni Caire - TILab
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jade.core.behaviours.Behaviour
Behaviour.RunnableChangedEvent
-
-
Field Summary
Fields Modifier and Type Field Description protected StringALL_INITIATIONS_KStringALL_RESPONSES_KEYkey to retrieve from the DataStore of the behaviour the vector of ACLMessage objects that have been received as responses.StringALL_SUBSCRIPTIONS_KEYkey to retrieve from the DataStore of the behaviour the vector of subscription ACLMessage objects that have been sent.protected static StringCHECK_IN_SEQprotected static StringCHECK_SESSIONSprotected static StringDUMMY_FINALprotected static StringHANDLE_FAILUREprotected static StringHANDLE_NOT_UNDERSTOODprotected static StringHANDLE_OUT_OF_SEQprotected StringINITIATION_Kprotected static StringPREPARE_INITIATIONSprotected static StringRECEIVE_REPLYprotected StringREPLY_KStringREPLY_KEYkey to retrieve from the DataStore of the behaviour the last ACLMessage object that has been received (null if the timeout expired).protected MsgReceiverreplyReceiverprotected MessageTemplatereplyTemplateprotected static StringSEND_INITIATIONSprotected MapsessionsStringSUBSCRIPTION_KEYkey to retrieve from the DataStore of the behaviour the subscription ACLMessage object passed in the constructor of the class.-
Fields inherited from class jade.core.behaviours.FSMBehaviour
currentName, lastStates
-
Fields inherited from class jade.core.behaviours.CompositeBehaviour
currentExecuted
-
Fields inherited from class jade.core.behaviours.Behaviour
myAgent, myEvent, NOTIFY_DOWN, NOTIFY_UP, parent, STATE_BLOCKED, STATE_READY, STATE_RUNNING
-
-
Constructor Summary
Constructors Constructor Description SubscriptionInitiator(Agent a, ACLMessage msg)Construct aSubscriptionInitiatorwith an empty DataStoreSubscriptionInitiator(Agent a, ACLMessage msg, DataStore store)Construct aSubscriptionInitiatorwith a given DataStore
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidadjustReplyTemplate(ACLMessage msg)voidcancel(AID receiver, boolean ignoreResponse)Cancel the subscription to agentreceiver.voidcancellationCompleted(AID receiver)This method should be called when the notification of a successful subscription cancellation is received from agentreceiverto terminate the session with him.protected booleancheckInSequence(ACLMessage reply)Check whether a reply is in-sequence and update the appropriate Session.protected intcheckSessions(ACLMessage reply)Check the status of the sessions after the reception of the last reply or the expiration of the timeout.protected StringcreateConvId(Vector msgs)Create a new conversation identifier to begin a new interaction.protected voidfillCancelContent(ACLMessage subscription, ACLMessage cancel)This method is used to fill the:contentslot of the CANCEL message that is being sent to an agent to cancel the subscription previously activated by means of thesubscriptionmessage.protected jade.proto.Initiator.ProtocolSessiongetSession(ACLMessage msg, int sessionIndex)Return a ProtocolSession object to manage replies to a given initiation messageprotected String[]getToBeReset()Return the states that must be reset before they are visited again.protected voidhandleAgree(ACLMessage agree)This method is called every time anagreemessage is received, which is not out-of-sequence according to the protocol rules.protected voidhandleAllResponses(Vector responses)This method is called when all the responses have been collected or when the timeout is expired.protected voidhandleFailure(ACLMessage failure)This method is called every time afailuremessage is received, which is not out-of-sequence according to the protocol rules.protected voidhandleInform(ACLMessage inform)This method is called every time ainformmessage is received, which is not out-of-sequence according to the protocol rules.protected voidhandleNotUnderstood(ACLMessage notUnderstood)This method is called every time anot-understoodmessage is received, which is not out-of-sequence according to the protocol rules.protected voidhandleOutOfSequence(ACLMessage msg)This method is called every time a message is received, which is out-of-sequence according to the protocol rules.protected voidhandleRefuse(ACLMessage refuse)This method is called every time arefusemessage is received, which is not out-of-sequence according to the protocol rules.protected voidinitializeDataStore(ACLMessage msg)Initialize the data store.voidonStart()Override the onStart() method to initialize the vectors that will keep all the replies in the data store.protected VectorprepareInitiations(ACLMessage initiation)This method is called internally by the framework and is not intended to be called by the userprotected VectorprepareSubscriptions(ACLMessage subscription)This method must return the vector of subscription ACLMessage objects to be sent.voidregisterHandleAgree(Behaviour b)This method allows to register a user definedBehaviourin the HANDLE_AGREE state.voidregisterHandleAllResponses(Behaviour b)This method allows to register a user definedBehaviourin the HANDLE_ALL_RESPONSES state.voidregisterHandleFailure(Behaviour b)This method allows to register a user definedBehaviourin the HANDLE_FAILURE state.voidregisterHandleInform(Behaviour b)This method allows to register a user definedBehaviourin the HANDLE_INFORM state.voidregisterHandleNotUnderstood(Behaviour b)This method allows to register a user definedBehaviourin the HANDLE_NOT_UNDERSTOOD state.voidregisterHandleOutOfSequence(Behaviour b)This method allows to register a user definedBehaviourin the HANDLE_OUT_OF_SEQ state.voidregisterHandleRefuse(Behaviour b)This method allows to register a user definedBehaviourin the HANDLE_REFUSE state.protected voidregisterPrepareInitiations(Behaviour b)Attach a behaviour to thePrepare-initiationsprotocol state.voidregisterPrepareSubscriptions(Behaviour b)This method allows to register a user definedBehaviourin the PREPARE_SUBSCRIPTIONS state.protected voidreinit()Re-initialize the internal state without performing a complete reset.voidreset()reset this behaviour by putting a null ACLMessage as message to be sentvoidreset(ACLMessage msg)reset this behaviourprotected voidsendInitiations(Vector initiations)Create and initialize the Sessions and sends the initiation messagesvoidsetDataStore(DataStore ds)Override the setDataStore() method to propagate this setting to all children.-
Methods inherited from class jade.core.behaviours.FSMBehaviour
checkTermination, deregisterDefaultTransition, deregisterState, deregisterTransition, forceTransitionTo, getChildren, getCurrent, getLastExitValue, getName, getPrevious, getState, handleInconsistentFSM, handleStateEntered, hasDefaultTransition, onEnd, registerDefaultTransition, registerDefaultTransition, registerFirstState, registerLastState, registerState, registerTransition, registerTransition, resetStates, scheduleFirst, scheduleNext, stringifyTransitionTable
-
Methods inherited from class jade.core.behaviours.SerialBehaviour
handle
-
Methods inherited from class jade.core.behaviours.CompositeBehaviour
action, done, handleBlockEvent, handleRestartEvent, registerAsChild, resetChildren, setAgent
-
Methods inherited from class jade.core.behaviours.Behaviour
actionWrapper, block, block, getAgent, getBehaviourName, getDataStore, getExecutionState, getParent, getRestartCounter, isRunnable, restart, root, setBehaviourName, setExecutionState
-
-
-
-
Field Detail
-
SUBSCRIPTION_KEY
public final String SUBSCRIPTION_KEY
key to retrieve from the DataStore of the behaviour the subscription ACLMessage object passed in the constructor of the class.
-
ALL_SUBSCRIPTIONS_KEY
public final String ALL_SUBSCRIPTIONS_KEY
key to retrieve from the DataStore of the behaviour the vector of subscription ACLMessage objects that have been sent.
-
REPLY_KEY
public final String REPLY_KEY
key to retrieve from the DataStore of the behaviour the last ACLMessage object that has been received (null if the timeout expired).
-
ALL_RESPONSES_KEY
public final String ALL_RESPONSES_KEY
key to retrieve from the DataStore of the behaviour the vector of ACLMessage objects that have been received as responses.
-
INITIATION_K
protected final String INITIATION_K
-
ALL_INITIATIONS_K
protected final String ALL_INITIATIONS_K
-
REPLY_K
protected final String REPLY_K
-
PREPARE_INITIATIONS
protected static final String PREPARE_INITIATIONS
- See Also:
- Constant Field Values
-
SEND_INITIATIONS
protected static final String SEND_INITIATIONS
- See Also:
- Constant Field Values
-
RECEIVE_REPLY
protected static final String RECEIVE_REPLY
- See Also:
- Constant Field Values
-
CHECK_IN_SEQ
protected static final String CHECK_IN_SEQ
- See Also:
- Constant Field Values
-
HANDLE_NOT_UNDERSTOOD
protected static final String HANDLE_NOT_UNDERSTOOD
- See Also:
- Constant Field Values
-
HANDLE_FAILURE
protected static final String HANDLE_FAILURE
- See Also:
- Constant Field Values
-
HANDLE_OUT_OF_SEQ
protected static final String HANDLE_OUT_OF_SEQ
- See Also:
- Constant Field Values
-
CHECK_SESSIONS
protected static final String CHECK_SESSIONS
- See Also:
- Constant Field Values
-
DUMMY_FINAL
protected static final String DUMMY_FINAL
- See Also:
- Constant Field Values
-
sessions
protected Map sessions
-
replyReceiver
protected MsgReceiver replyReceiver
-
replyTemplate
protected MessageTemplate replyTemplate
-
-
Constructor Detail
-
SubscriptionInitiator
public SubscriptionInitiator(Agent a, ACLMessage msg)
Construct aSubscriptionInitiatorwith an empty DataStore
-
SubscriptionInitiator
public SubscriptionInitiator(Agent a, ACLMessage msg, DataStore store)
Construct aSubscriptionInitiatorwith a given DataStore- Parameters:
a- The agent performing the protocolmsg- The message that must be used to initiate the protocol. Notice that the default implementation of theprepareSubscription()method returns an array composed of only this message. The values of the slotreply-withis ignored and a different value is assigned automatically by this class for each receiver.store- TheDataStorethat will be used by thisSubscriptionInitiator
-
-
Method Detail
-
prepareInitiations
protected Vector prepareInitiations(ACLMessage initiation)
This method is called internally by the framework and is not intended to be called by the user
-
checkInSequence
protected boolean checkInSequence(ACLMessage reply)
Check whether a reply is in-sequence and update the appropriate Session. This method is called internally by the framework and is not intended to be called by the user
-
checkSessions
protected int checkSessions(ACLMessage reply)
Check the status of the sessions after the reception of the last reply or the expiration of the timeout. This method is called internally by the framework and is not intended to be called by the user
-
getToBeReset
protected String[] getToBeReset()
Return the states that must be reset before they are visited again. Note that resetting a state before visiting it again is required only if - The onStart() method is redefined - The state has an "internal memory"
-
prepareSubscriptions
protected Vector prepareSubscriptions(ACLMessage subscription)
This method must return the vector of subscription ACLMessage objects to be sent. It is called in the first state of this protocol. This default implementation just returns the ACLMessage object passed in the constructor. Programmers might prefer to override this method in order to return a vector of objects for 1:N conversations or also to prepare the messages during the execution of the behaviour.- Parameters:
subscription- the ACLMessage object passed in the constructor- Returns:
- a Vector of ACLMessage objects.
The values of the slot
reply-withis ignored and a different value is assigned automatically by this class for each receiver.
-
handleAgree
protected void handleAgree(ACLMessage agree)
This method is called every time anagreemessage is received, which is not out-of-sequence according to the protocol rules. This default implementation does nothing; programmers might wish to override the method in case they need to react to this event.- Parameters:
agree- the received agree message
-
handleRefuse
protected void handleRefuse(ACLMessage refuse)
This method is called every time arefusemessage is received, which is not out-of-sequence according to the protocol rules. This default implementation does nothing; programmers might wish to override the method in case they need to react to this event.- Parameters:
refuse- the received refuse message
-
handleInform
protected void handleInform(ACLMessage inform)
This method is called every time ainformmessage is received, which is not out-of-sequence according to the protocol rules. This default implementation does nothing; programmers might wish to override the method in case they need to react to this event.- Parameters:
inform- the received inform message
-
handleAllResponses
protected void handleAllResponses(Vector responses)
This method is called when all the responses have been collected or when the timeout is expired. The used timeout is the minimum value of the slotreplyByof all the sent messages. By response message we intend here all theagree, not-understood, refuse, failurereceived messages, which are not out-of-sequence according to the protocol rules. This default implementation does nothing; programmers might wish to override the method in case they need to react to this event by analysing all the messages in just one call.- Parameters:
responses- the Vector of ACLMessage objects that have been received
-
registerPrepareSubscriptions
public void registerPrepareSubscriptions(Behaviour b)
This method allows to register a user definedBehaviourin the PREPARE_SUBSCRIPTIONS state. This behaviour would override the homonymous method. This method also sets the data store of the registeredBehaviourto the DataStore of this current behaviour. It is responsibility of the registered behaviour to put the Vector of ACLMessage objects to be sent into the datastore at theALL_SUBSCRIPTIONS_KEYkey. The values of the slotreply-withis ignored and a different value is assigned automatically by this class for each receiver.- Parameters:
b- the Behaviour that will handle this state
-
registerHandleAgree
public void registerHandleAgree(Behaviour b)
This method allows to register a user definedBehaviourin the HANDLE_AGREE state. This behaviour would override the homonymous method. This method also sets the data store of the registeredBehaviourto the DataStore of this current behaviour. The registered behaviour can retrieve theagreeACLMessage object received from the datastore at theREPLY_KEYkey.- Parameters:
b- the Behaviour that will handle this state
-
registerHandleInform
public void registerHandleInform(Behaviour b)
This method allows to register a user definedBehaviourin the HANDLE_INFORM state. This behaviour would override the homonymous method. This method also set the data store of the registeredBehaviourto the DataStore of this current behaviour. The registered behaviour can retrieve theinformACLMessage object received from the datastore at theREPLY_KEYkey.- Parameters:
b- the Behaviour that will handle this state
-
registerHandleRefuse
public void registerHandleRefuse(Behaviour b)
This method allows to register a user definedBehaviourin the HANDLE_REFUSE state. This behaviour would override the homonymous method. This method also set the data store of the registeredBehaviourto the DataStore of this current behaviour. The registered behaviour can retrieve therefuseACLMessage object received from the datastore at theREPLY_KEYkey.- Parameters:
b- the Behaviour that will handle this state
-
registerHandleAllResponses
public void registerHandleAllResponses(Behaviour b)
This method allows to register a user definedBehaviourin the HANDLE_ALL_RESPONSES state. This behaviour would override the homonymous method. This method also sets the data store of the registeredBehaviourto the DataStore of this current behaviour. The registered behaviour can retrieve the vector of ACLMessage objects, received as a response, from the datastore at theALL_RESPONSES_KEYkey.- Parameters:
b- the Behaviour that will handle this state
-
cancel
public void cancel(AID receiver, boolean ignoreResponse)
Cancel the subscription to agentreceiver. This method retrieves the subscription message sent toreceiverand sends a suitable CANCEL message with the conversationID and all other protocol fields appropriately set. The:contentslot of this CANCEL message is filled in by means of thefillCancelContent()method. The way the CANCEL content is set in fact is application specific.- Parameters:
receiver- The agent to whom we are cancelling the subscription.ignoreResponse- When receiving a CANCEL, the responder may send back a response to notify that the subscription has been cancelled (INFORM) or not (FAILURE). If this parameter is set totruethis response is ignored and the session with agentreceiveris immediately terminated. WhenignoreResponseis set tofalse, on the other hand, the session with agentreceiverremains active and the INFORM or FAILURE massage (if any) will be handled by theHANDLE_INFORMandHANDLE_FAILUREstates as if they were normal notifications. It is responsibility of the programmer to distinguish them and actually terminate the session with agentreceiverby calling thecancellationCompleted()method.- See Also:
fillCancelContent(ACLMessage, ACLMessage),cancellationCompleted(AID)
-
fillCancelContent
protected void fillCancelContent(ACLMessage subscription, ACLMessage cancel)
This method is used to fill the:contentslot of the CANCEL message that is being sent to an agent to cancel the subscription previously activated by means of thesubscriptionmessage. Note that all other relevant fields of thecancelmessage have already been set appropriately and the programmer should not modify them. The default implementation just sets a null content (the responder should be able to identify the subscription that has to be cancelled on the basis of the sender and conversationID fields of the CANCEL message). Programmers may override this method to create an appropriate content as exemplified in the code below.try { AID receiver = (AID) cancel.getAllReceiver().next(); Action a = new Action(receiver, OntoACLMessage.wrap(subscription)); getContentManager.fillContent(cancel, a); } catch (Exception e) { e.printStackTrace(); }- See Also:
cancel(AID, boolean)
-
cancellationCompleted
public void cancellationCompleted(AID receiver)
This method should be called when the notification of a successful subscription cancellation is received from agentreceiverto terminate the session with him. This method has some effect only if a cancellation for agentreceiverwas previously activated by means of thecancel()method.- See Also:
cancel(AID, boolean)
-
reinit
protected void reinit()
Re-initialize the internal state without performing a complete reset.
-
initializeDataStore
protected void initializeDataStore(ACLMessage msg)
Initialize the data store. This method is called internally by the framework and is not intended to be called by the user
-
getSession
protected jade.proto.Initiator.ProtocolSession getSession(ACLMessage msg, int sessionIndex)
Return a ProtocolSession object to manage replies to a given initiation message
-
sendInitiations
protected void sendInitiations(Vector initiations)
Create and initialize the Sessions and sends the initiation messages
-
handleNotUnderstood
protected void handleNotUnderstood(ACLMessage notUnderstood)
This method is called every time anot-understoodmessage is received, which is not out-of-sequence according to the protocol rules. This default implementation does nothing; programmers might wish to override the method in case they need to react to this event.- Parameters:
notUnderstood- the received not-understood message
-
handleFailure
protected void handleFailure(ACLMessage failure)
This method is called every time afailuremessage is received, which is not out-of-sequence according to the protocol rules. This default implementation does nothing; programmers might wish to override the method in case they need to react to this event.- Parameters:
failure- the received failure message
-
handleOutOfSequence
protected void handleOutOfSequence(ACLMessage msg)
This method is called every time a message is received, which is out-of-sequence according to the protocol rules. This default implementation does nothing; programmers might wish to override the method in case they need to react to this event.- Parameters:
msg- the received message
-
registerPrepareInitiations
protected void registerPrepareInitiations(Behaviour b)
Attach a behaviour to thePrepare-initiationsprotocol state.- Parameters:
b- The behaviour object to be executed in thePrepare-initiationsstate.
-
registerHandleNotUnderstood
public void registerHandleNotUnderstood(Behaviour b)
This method allows to register a user definedBehaviourin the HANDLE_NOT_UNDERSTOOD state. This behaviour would override the homonymous method. This method also set the data store of the registeredBehaviourto the DataStore of this current behaviour. The registered behaviour can retrieve thenot-understoodACLMessage object received from the datastore at theREPLY_KEYkey.- Parameters:
b- the Behaviour that will handle this state
-
registerHandleFailure
public void registerHandleFailure(Behaviour b)
This method allows to register a user definedBehaviourin the HANDLE_FAILURE state. This behaviour would override the homonymous method. This method also set the data store of the registeredBehaviourto the DataStore of this current behaviour. The registered behaviour can retrieve thefailureACLMessage object received from the datastore at theREPLY_KEYkey.- Parameters:
b- the Behaviour that will handle this state
-
registerHandleOutOfSequence
public void registerHandleOutOfSequence(Behaviour b)
This method allows to register a user definedBehaviourin the HANDLE_OUT_OF_SEQ state. This behaviour would override the homonymous method. This method also set the data store of the registeredBehaviourto the DataStore of this current behaviour. The registered behaviour can retrieve theout of sequenceACLMessage object received from the datastore at theREPLY_KEYkey.- Parameters:
b- the Behaviour that will handle this state
-
reset
public void reset()
reset this behaviour by putting a null ACLMessage as message to be sent- Overrides:
resetin classFSMBehaviour
-
reset
public void reset(ACLMessage msg)
reset this behaviour- Parameters:
msg- is the ACLMessage to be sent
-
onStart
public void onStart()
Override the onStart() method to initialize the vectors that will keep all the replies in the data store.
-
setDataStore
public void setDataStore(DataStore ds)
Override the setDataStore() method to propagate this setting to all children.- Overrides:
setDataStorein classBehaviour- Parameters:
ds- theDataStorethat thisBehaviourwill use as its private data store
-
createConvId
protected String createConvId(Vector msgs)
Create a new conversation identifier to begin a new interaction.- Parameters:
msgs- A vector of ACL messages. If the first one has a non-empty:conversation-idslot, its value is used, else a new conversation identifier is generated.
-
adjustReplyTemplate
protected void adjustReplyTemplate(ACLMessage msg)
-
-