如何使代理商在玉中交流?
我希望我的第一个代理商向第二代理发送消息,这是第一个代理的代码:
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);
}
});
}}
对于第二类:
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();
}
}
});
}}
但是第二代理总是执行else语句,而msg似乎是无效的,我不能图为什么。
有建议吗?
谢谢
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 :
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.
Any suggestion ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的这一行
仅读取来自AGNET的消息队列的消息。它不会等待收到一条消息。因此,您的第二个代理在收到消息之前正在执行,这就是为什么
else
首先被执行的原因。然而,
这是不正确的,Else语句首先是执行的,然后代理打印其消息,然后再次将其他语句删除,然后将代理被阻止。
说明:您的第一个代理具有
onshotbehaviour
,这意味着他将一次执行他的代码。话虽如此,他只会发送一次信息。虽然您的第二代理具有
cyclakhaviour
,但这意味着他会继续重复自己。在此行为的第一次运行中,执行else
,因为尚未收到消息。然后,有块
,它将阻止此行为,直到收到消息为止。当收到消息时,行为将重复,但是这次他将打印收到的消息。然后,block
再次...如果您希望第二个代理商第一次打印消息,则需要使代理等待接收消息。您可以在上面的行之前使用方法
dowait()
。这样,您的第二代理将继续等到他收到消息。This line below
only reads a message from the messages queue of the agnet. 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.However,
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.
The explanation: your first agent has a
OneShotBehaviour
, that means that he will execute his code once and then quite. 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. In the first run of this behaviour, theelse
is executed because the message was not received yet. Then, there is theblock
which will block this behaviour until a message is received. When the message is received, the behaviour will repeat itself, but this time he will print the received message. Then theblock
again...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. This way, your second agent will keep waiting until he receives a message.