分类:
2007-10-12 08:46:16
MessageTemplate
class 利用MessageTemplate可以针对ACLMessage的每个属性设置模式,以达到过滤消息的目的。为了可以构建更复杂的匹配规则,多个模式也可以进行and,or,not运算。最有用的一些规则或方法包括:通信行为匹配,发送者匹配,会话ID匹配。
比如MatchPerformative( performative ) 是通信行为的匹配。
这里 performative 可能是:
ACLMessage.INFORM
ACLMessage.PROPOSE
ACLMessage.AGREE
还有发送者匹配MatchSender( AID ),会话匹配MatchConversationID( String ),通信协议匹配MatchProtocol( String ) ,本体匹配MatchOntology( String)。
比如:MessageTemplate mt = MessageTemplate.and(
MessageTemplate.MatchPerformative( ACLMessage.INFORM ),
MessageTemplate.MatchSender( new AID( "a1",
AID.ISLOCALNAME))) ;
相当于建立了一个模板,表示消息规则为INFORM行为并且发送者为“a1”。
接收过程如下:ACLMessage msg = receive( mt );
if (msg != null) { ... handle message }
block();
示例:package jadePrime.acl;
import
jade.core.Agent;
import
jade.core.AID;
import
jade.core.behaviours.*;
import
jade.lang.acl.*;
public class Template extends
Agent {
MessageTemplate mt1
= MessageTemplate.and(
MessageTemplate.MatchPerformative( ACLMessage.INFORM ),
MessageTemplate.MatchSender( new
AID( "a1",AID.ISLOCALNAME))) ;
protected void setup()
{
// Send messages to
"a1" and "a2"
ACLMessage msg = new
ACLMessage(ACLMessage.INFORM);
msg.setContent( "
for (int i = 1; i<=2; i++)
msg.addReceiver( new AID( "a"
+ i, AID.ISLOCALNAME) );
send(msg);
// Set-up Behaviour
1
addBehaviour(new
CyclicBehaviour(this)
{
public
void action( )
{
System.out.print("Behaviour
ONE: ");
ACLMessage msg= receive( mt1 );
if
(msg!=null
System.out.println( "gets
" + msg.getPerformative() + " from " + msg.getSender().getLocalName() + "=" +
msg.getContent() );
else
System.out.println( "gets
NULL" );
block();
}
});
// Set-up Behaviour
2
addBehaviour(new
CyclicBehaviour(this)
{
public
void action()
{
System.out.print("Behaviour TWO: ");
ACLMessage msg= receive();
if
(msg!=null)
System.out.println( "gets
"
+ msg.getPerformative() + " from "
+ msg.getSender().getLocalName() + "="
+ msg.getContent() );
else
System.out.println( "gets
NULL" );
block();
}
});
}
}
package
jadePrime.acl;
import
jade.core.Agent;
import
jade.core.behaviours.*;
import
jade.lang.acl.*;
public class Responder extends
Agent {
protected void
setup()
{
addBehaviour(new CyclicBehaviour(this)
{
public
void action()
{
ACLMessage msg = receive();
if
(msg!=null) {
ACLMessage reply =
msg.createReply();
reply.setPerformative(
ACLMessage.INFORM );
reply.setContent(" Gossip....." );
send(reply);
reply.setPerformative(
ACLMessage.PROPOSE );
reply.setContent(" Really sexy stuff... cheap! " );
send(reply);
}
block();
}
});
}
}
liyifarob2008-11-20 20:38:37
在么???public class DeicingStationAgent extends Agent{ int resource[][]=new int[10][]; private MessageTemplate mt; // 接受消息的模板 public void action() { mt = MessageTemplate.MatchPerformative(ACLMessage.QUERY_IF); ACLMessage msg = DeicingStationAgent.receive(mt); if (msg != null) { // 收到请求消息