📄 fiparequestinitiatoragent.java
字号:
/**
* ***************************************************************
* JADE - Java Agent DEvelopment Framework is a framework to develop
* multi-agent systems in compliance with the FIPA specifications.
* Copyright (C) 2000 CSELT S.p.A.
*
* GNU Lesser General Public License
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation,
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
* **************************************************************
*/
package examples.protocols;
import jade.core.Agent;
import jade.core.AID;
import jade.lang.acl.ACLMessage;
import jade.proto.AchieveREInitiator;
import jade.domain.FIPANames;
import java.util.Date;
import java.util.Vector;
/**
This example shows how to implement the initiator role in
a FIPA-request interaction protocol. In this case in particular
we use an <code>AchieveREInitiator</code> ("Achieve Rational effect")
to request a dummy action to a number of agents (whose local
names must be specified as arguments).
@author Giovanni Caire - TILAB
*/
public class FIPARequestInitiatorAgent extends Agent {
private int nResponders;
protected void setup() {
// Read names of responders as arguments
Object[] args = getArguments();
if (args != null && args.length > 0) {
nResponders = args.length;
System.out.println("Requesting dummy-action to "+nResponders+" responders.");
// Fill the REQUEST message
ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
for (int i = 0; i < args.length; ++i) {
msg.addReceiver(new AID((String) args[i], AID.ISLOCALNAME));
}
msg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
// We want to receive a reply in 10 secs
msg.setReplyByDate(new Date(System.currentTimeMillis() + 10000));
msg.setContent("dummy-action");
addBehaviour(new AchieveREInitiator(this, msg) {
protected void handleInform(ACLMessage inform) {
System.out.println("Agent "+inform.getSender().getName()+" successfully performed the requested action");
}
protected void handleRefuse(ACLMessage refuse) {
System.out.println("Agent "+refuse.getSender().getName()+" refused to perform the requested action");
nResponders--;
}
protected void handleFailure(ACLMessage failure) {
if (failure.getSender().equals(myAgent.getAMS())) {
// FAILURE notification from the JADE runtime: the receiver
// does not exist
System.out.println("Responder does not exist");
}
else {
System.out.println("Agent "+failure.getSender().getName()+" failed to perform the requested action");
}
}
protected void handleAllResultNotifications(Vector notifications) {
if (notifications.size() < nResponders) {
// Some responder didn't reply within the specified timeout
System.out.println("Timeout expired: missing "+(nResponders - notifications.size())+" responses");
}
}
} );
}
else {
System.out.println("No responder specified.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -