简体   繁体   English

如何让座席在翡翠中交流?

[英]How to make agents communicate in jade?

I want my first Agent to send a message to the second Agent, this is the code for the First agent:我希望我的第一个代理向第二个代理发送消息,这是第一个代理的代码:

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

public class First extends Agent {  

@Override
protected void setup() {
    addBehaviour(new OneShotBehaviour() {
        
        @Override
        public void action() {
            System.out.println("I'm sending");
            ACLMessage msg =new ACLMessage(ACLMessage.INFORM);
            msg.addReceiver(new AID("second",AID.ISLOCALNAME));
            msg.setContent("HELLO");
            send(msg);              
        }
    });
}}

And for the second class:对于第二个 class:

import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;

public class Second extends Agent {
 
@Override
protected void setup() {
    addBehaviour(new CyclicBehaviour() {
        
         @Override
         public void action() {
             ACLMessage msg = receive();
             if(msg != null) {
                 System.out.print("i received "+msg.getContent());
             } else {
                 System.out.print("i didn't receive msg ");
                 block();
             }               
         }
    });
}}

But the second agent always execute the else statement and msg seems to be null, and i can't figure why.但是第二个代理总是执行 else 语句并且 msg 似乎是 null,我不明白为什么。

Any suggestion?有什么建议吗?

Thanks谢谢

This line below下面这一行


ACLMessage msg = receive();

only reads a message from the messages queue of the a.net.只从 a.net 的消息队列中读取一条消息。 It does not wait to receive a message.它不会等待接收消息。 So, your second agent is being executed before that he receives the message, that's why the else is executed at first.所以,你的第二个代理在他收到消息之前被执行,这就是为什么首先执行else的原因。

However,然而,

But the second agent always execute the else statement and msg seems to be null, and i can't figure why.但是第二个代理总是执行 else 语句并且 msg 似乎是 null,我不明白为什么。

This is not correct, the else statement is executed at first, then the agent prints its message, then the else is exectued again, then the agents is blocked.这是不正确的,先执行else语句,然后代理打印它的消息,然后再次执行else,然后代理被阻塞。

The explanation: your first agent has a OneShotBehaviour , that means that he will execute his code once and then quite.解释:您的第一个代理有一个OneShotBehaviour ,这意味着他将执行他的代码一次然后完全执行。 That being said, he will send his message only one time.话虽这么说,他只会发送一次消息。

While your second agent has a CyclicBehaviour , that means that he will keep repeating himself.虽然您的第二个代理有CyclicBehaviour ,这意味着他会不断重复自己。 In the first run of this behaviour, the else is executed because the message was not received yet.在此行为的第一次运行中, else被执行,因为尚未收到消息。 Then, there is the block which will block this behaviour until a message is received.然后,有一个block将阻止此行为,直到收到消息。 When the message is received, the behaviour will repeat itself, but this time he will print the received message.当收到消息时,行为会重复,但这次他会打印收到的消息。 Then the block again...然后又是block ...

If you want your second agent to print the message from the first time, you need to make your agent waits to receive a message.如果你想让你的第二个代理从第一次打印消息,你需要让你的代理等待接收消息。 You can use the method doWait() before the line above.您可以在上面的行之前使用方法doWait() This way, your second agent will keep waiting until he receives a message.这样,您的第二个代理将一直等待,直到他收到一条消息。


doWait();

ACLMessage msg = receive();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM