MessageTemplate
ACL是Aget通信的协议,它用功能或行为来定义通信,我们称之为通信行为。
接收消息时,MessageTemplate class 利用MessageTemplate可以针对ACLMessage的每个属性设置模式,以达到过滤消息的目的。为了可以构建更复杂的匹配规则,多个模式也可以进行and,or,not运算。最有用的一些规则或方法包括:通信行为匹配,发送者匹配,会话ID匹配。
由消息的目的、意图等不同,我们又将通信行为细分为多种,常见的有:
ACLMessage.INFORM:通知接收者
ACLMessage.REQUEST:请求接收者执行某些动作
ACLMessage.AGREE:同意执行某些动作
ACLMessage.FAILURE:通知另一个Agent尝试执某一行动作,但是失败
ACLMessage.PROPOSE:在一定前提下,接收执行某一特定动作的建议
还有发送者匹配MatchSender( AID ),会话匹配MatchConversationID( String ),通信协议匹配MatchProtocol( String ) ,本体匹配MatchOntology( String)。
比如如下模板,表示消息规则为INFORM的行为并且设置发送者为Sender。
MessageTemplate mt = MessageTemplate.and(
MessageTemplate.MatchPerformative(ACLMessage.INFORM�),
MessageTemplate.MatchSender(new AID("Sender", AID.ISLOCALNAME));
接收信息过程:
ACLMessage msg = receive(mt);
if (msg != null) {
//... handle message
}
block();
简单例子:
package cn.bcrab.agent;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
public class Template extends Agent{
MessageTemplate mt = MessageTemplate.and(
MessageTemplate.MatchPerformative(ACLMessage.INFORM),
MessageTemplate.MatchSender(new AID("user1", AID.ISLOCALNAME))
);
@Override
protected void setup() {
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
msg.setContent("有内鬼,终止交易!");
// create two receivers
msg.addReceiver(new AID("user1" ,AID.ISLOCALNAME ));
msg.addReceiver(new AID("user2" ,AID.ISLOCALNAME ));
send(msg);
// set up behaviour 1
Behaviour b1 = new CyclicBehaviour(this) {
@Override
public void action() {
System.out.println("----------------Behaviour1---------------");
ACLMessage msg = receive(mt);
if (msg!=null){
System.out.println("Performative: "+msg.getPerformative());
System.out.println("Sender: "+msg.getSender().getLocalName());
System.out.println("Content: "+msg.getContent());
}else{
System.out.println("Nothing");
}
System.out.println("--------------------Behaviour1 end----------------------");
block();
}
};
// set up behaviour 2
Behaviour b2 = new CyclicBehaviour(this) {
@Override
public void action() {
System.out.println("----------------Behaviour2---------------");
ACLMessage msg = receive();
if (msg!=null){
System.out.println("Performative: "+msg.getPerformative());
System.out.println("Sender: "+msg.getSender().getLocalName());
System.out.println("Content: "+msg.getContent());
}else{
System.out.println("Nothing");
}
System.out.println("----------------Behaviour2 end---------------");
block();
}
};
this.addBehaviour(b1);
this.addBehaviour(b2);
}
}
package cn.bcrab.agent;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
public class Responder extends Agent {
@Override
protected void setup() {
Behaviour b = new CyclicBehaviour(){
@Override
public void action() {
ACLMessage msg = receive();
if (msg != null){
System.out.println("xxxx");
ACLMessage reply = msg.createReply();
reply.setPerformative(ACLMessage.INFORM);
reply.setContent("收到!");
send(reply);
reply.setPerformative(ACLMessage.PROPOSE);
reply.setContent("Copy that!");
send(reply);
}
block();
}
};
this.addBehaviour(b);
}
}
相关代码
https://gitee.com/2010jing/JADE-tutorial/tree/tutorial05/
Reference
网友评论