JADE中的ACLMessages问题

时间:2018-10-15 13:07:12

标签: java agents-jade

我已经编写了一个程序,一切看起来都很好。例如,我有以下代码:

ACLMessage msg = new ACLMessages (ACLMessage.INFORM);
msg.setContent = ("G" + groupID);
for(int i =0 ; i<50 ; i++){
    msg.addReceiver(new AID("MyClass" + i, AID.ISLOCALNAME));
}
send (msg);

并假设我这样收到它:

ACLMessage rcv = myAgent.receive();

并假设我在程序的另一部分中定义了另一个ACLMessage,例如,在另一个块中名为msg2 ..,其内容为“ T” + temp。

当我收到下一条消息时,我意识到这些消息很混乱……它们没有被正确接收。我的意思是运行下面的代码有2个不同的结果:

System.out.println("rcv Content is: " + rcv.getContent());

,结果将是:G1 有时是:T34

此错误消息使我的程序无法正常运行...我更改了消息格式,例如:“ T” + groupID +“ T”或其他形式...但是没有用。.

///////////////////////////////////////////////// //// 在我学会了使用消息模板之后:

 case 17:{// in this case deligates send the avg to the supervisor
                if(!deligateFlag){
                    state++;
                    break;
                }

                ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
                msg.setConversationId("A");
                msg.setContent("V" + avg);
                //System.err.println("Content of rcv is: " + msg.getContent());
                msg.addReceiver(mySupervisor);
                send(msg);
                System.out.println(myAgent.getLocalName() 
                        + " Says: I am deligate of group " 
                        + group 
                        + " And I sent the average temp of my followers "
                        + "to the supervisor which is: " 
                        + mySupervisor.getLocalName());
                state++;
                break;
            }
            case 18:{/* in this case supervisor receives the avg temp of 
                each group and calculates the avg of averages and then 
                decides what to do*/
                if(!supervisorFlag){
                    n=1;
                    state++;
                    break;
                }
                //System.err.println("This is Beginning of case 18");
                if(supervisorFlag){
                    MessageTemplate mt = MessageTemplate.MatchConversationId("A");
                    ACLMessage msg = myAgent.receive(mt);
                    if (msg != null) { System.err.println("TContent is: " + msg.getContent());
                        dAvg += Character.getNumericValue(msg.getContent().charAt(1));

                        if(msg.getContent().charAt(0) == 'V'){
                            n++;
                            System.err.println("N is: " + n);
                        }
                    }
                    if(n > 4){

                                dAvg /= 4;
                                totalAvg = dAvg;
                                System.out.println("Supervisor " 
                                        + myAgent.getLocalName() 
                                        + "Says: The total average of whole system is: " 
                                        + totalAvg);
                        }
                        state++;
                        break;

问题是,在最佳情况下,程序运行到if (n>4) ..并且一切都停止了..没有错误,没有警告..它只是停止了..即使n变为5但什么也没发生...我不知道可能是确切的问题..是ACL消息还是我不知道...通常我不知道为什么在90%的程序中不打印TContent。消息将发生什么。.

1 个答案:

答案 0 :(得分:1)

如果要接收特定的aclMessage,可以使用myAgent.receive(MessageTemplate t)。

例如,您要发送消息

Agent1:

ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
...     
request.setConversationId("G");
myAgent.send(request)

并且您希望您的Agent2仅接收ConversationId =“ G”的消息

Agent2:

MessageTemplate mt = MessageTemplate.MatchConversationId("G");;
ACLMessage msg = myAgent.receive(mt);
if (msg != null) {
  // Process it
  ...
} else {
   block();
}