三.ACL(agent communication language)
Jade的agent之间进行通信使用的acl语言遵循fipa acl规范。一个acl消息通常包含这些参数:sende:消息的发送者,用agent标志AID表示; receivers,接受agent消息的agent可以是多个;Reply-to,应受到回应的接受者;Performative:标志该消息的目的,即发送者想要通过发送消息获取什么,通常有这样一些常值:REQUEST, INFORM, ACCEPT_PROPOSAL, REJECT_PROPOSAL, PROPOSE;Content,消息的内容;语言,比如内容的编码格式;ontology,双方都能够理解的消息内容的概念说明和语义描述。
实例:
在netbeans中创建常规项目:其代码文件有两个,分别为
package ips;
/**
*
* @author Administrator
*/
import java.io.InterruptedIOException;
import java.io.IOException;
import jade.core.*;
import jade.core.behaviours.*;
import jade.lang.acl.*;
import jade.domain.FIPANames.InteractionProtocol;
import jade.proto.SimpleAchieveREInitiator;
import java.util.Vector;
import java.util.Enumeration;
public class SimpleRequestInitiator extends Agent{
static class MarriageProposer extends SimpleAchieveREInitiator{
protected MarriageProposer(Agent agent, ACLMessage msg){
super(agent, msg);
}
protected void handleAgree(ACLMessage msg) {
System.out.println(myAgent.getLocalName() + ": 吼吼! " +
msg.getSender().getLocalName() +" 已经同意嫁给我了, I'm so excited!");
}
protected void handleRefuse(ACLMessage msg) {
System.out.println(myAgent.getLocalName() + ": Oh no! " +
msg.getSender().getLocalName() +" 拒绝了我, i feel sad.");
}
protected void handleInform(ACLMessage msg) {
System.out.println(myAgent.getLocalName() + ":" +
msg.getSender().getLocalName() +" has informed me of the status of my request." +
" They said : " + msg.getContent());
}
protected void handleNotUnderstood(ACLMessage msg){
System.out.println(myAgent.getLocalName() + ":"
+ msg.getSender().getLocalName() +
" has indicated that they didn't understand.");
}
protected void handleOutOfSequence(ACLMessage msg) {
System.out.println(myAgent.getLocalName() + ":"
+ msg.getSender().getLocalName() +
"has sent me a message which I wasn't" +
" expecting in this conversation");
}
}
protected void setup() {
System.out.println(getLocalName() +": about to propose marriage to bob ");
doWait(5000); //wait for bob to be started.
ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
AID to = new AID();
to.setLocalName("bob");
msg.setSender(getAID());
msg.addReceiver(to);
msg.setContent("Marry Me!");
msg.setProtocol(InteractionProtocol.FIPA_REQUEST);
addBehaviour(new MarriageProposer(this, msg));
}
}
还有:
package ips;
import java.io.InterruptedIOException;
import java.io.IOException;
import jade.core.*;
import jade.core.behaviours.*;
import jade.lang.acl.*;
import jade.domain.FIPANames.InteractionProtocol;
import jade.proto.SimpleAchieveREResponder;
import java.util.Vector;
import java.util.Enumeration;
public class SimpleRequestResponder extends Agent {
static class MarriageResponder extends SimpleAchieveREResponder{
public MarriageResponder(Agent agent){
super(agent,createMessageTemplate(InteractionProtocol.FIPA_REQUEST));
}
protected ACLMessage prepareResponse(ACLMessage msg) {
ACLMessage response = msg.createReply();
if(msg.getContent()!=null && msg.getContent().equals("Marry Me!")){
System.out.println(myAgent.getLocalName() + ":" +
msg.getSender().getLocalName() +
" has asked me to marry him!");
AID sender;
sender = msg.getSender();
if(sender.getLocalName().equals("baz")){
response.setPerformative(ACLMessage.AGREE);
System.out.println(myAgent.getLocalName() +
":I'm going to agree.");
}else{
response.setPerformative(ACLMessage.REFUSE);
System.out.println(myAgent.getLocalName() +
":I'm going to turn him down.");
}
}else{
response.setPerformative(ACLMessage.NOT_UNDERSTOOD);
System.out.println(myAgent.getLocalName() +
":I didn't understand what " +
msg.getSender().getLocalName() +
" just said to me.");
}
return response;
}
protected ACLMessage prepareResultNotification(ACLMessage inmsg,
ACLMessage outmsg) {
//what they have asked is now complete (or if it failed)
ACLMessage msg = inmsg.createReply();
msg.setPerformative(ACLMessage.INFORM);
msg.setContent("I Do!");
return msg;
}
}
protected void setup() {
System.out.println(getLocalName() +
": I wonder if anybody wants to marry me?");
addBehaviour(new MarriageResponder(this));
}//
}
按照以前blog中记载,在netbeans属性中加入库,然后再运行参数中,设置,主类依然为jade.Boot,参数为baz:ips.SimpleRequestInitiator bob:ips.SimpleRequestResponder,运行成功后,以下是输出结果:
baz: about to propose marriage to bob
bob: I wonder if anybody wants to marry me?
bob:baz has asked me to marry him!
bob:I'm going to agree.
baz: 吼吼! bob 已经同意嫁给我了, I'm so excited!
baz:bob has informed me of the status of my request. They said : I Do!