简体   繁体   English

如何使用JADE在另一个代理中添加来自多个代理的消息(整数值)

[英]How add messages (integer values) from multiple agents in another agent using JADE

I have the following requirement in JADE. 我在JADE中具有以下要求。 I have to create multiple agents whose task is to generate random numbers. 我必须创建多个代理,其任务是生成随机数。 Another receiver agent collects all the random numbers generated and sums them up to make a decision. 另一个接收器代理收集所有生成的随机数,并将它们加起来以做出决定。 This is the sender agent class extends tickerbehaviour and its tick onTick method is as follows 这是发件人代理类扩展的tickerbehaviour,其tick onTick方法如下

    protected void onTick()
            {
                ACLMessage msg_LoadDetails = new ACLMessage(ACLMessage.INFORM);
                msg_LoadDetails.addReceiver(new AID("LoadCoordinator", AID.ISLOCALNAME));
                msg_LoadDetails.setContent(String.valueOf(getLoad()));
                LoadConv.send(msg_LoadDetails);
                //load = (int)(Math.random()*1000+1);
            }

The receiver class extend cyclic behaviour and its action method is as follows 接收器类扩展循环行为,其作用方法如下

public void action()
    {
        ACLMessage msg_IncomingLoadDetails = LoadCoordinator.receive();
        if(msg_IncomingLoadDetails!=null)
        totalLoad = Integer.parseInt(msg_IncomingLoadDetails.getContent());

        if(totalLoad>500)
        {actioncommand = "off";}
        else
        {actioncommand = "on";}

        System.out.println("The current load is:" +totalLoad+ "; the load will be switched " +actioncommand);
        block();
    }

The issue here is that the received values are just for one agent which i create (from console). 这里的问题是,接收到的值仅适用于我从控制台创建的一个代理。 I want to receive values for all created agents. 我想接收所有创建的代理的值。 Does this require me create an array of agents? 这是否需要我创建一系列代理? How do i read values from many agent messages? 我如何从许多座席消息中读取值? Could some one please help with the code to do so as I am new to JAVA and JADE? 当我是JAVA和JADE的新手时,有人可以帮助提供代码吗?

I tested your code and receiver agent gets message from all sender agents. 我测试了您的代码,接收者代理从所有发送者代理获取消息。

I have some questions and remarks: 我有一些问题和评论:

  1. How often sender agents should send their number? 发件人代理应多久发送一次其号码? Only once, or cyclically, after given time? 在给定的时间之后只有一次或周期性地?

  2. Your receiver agent doesn't sum received values, he always compares last received value. 您的接收者代理不会汇总接收到的值,而是始终比较上次接收到的值。

  3. Better use this structure for logic, after receiving message: 收到消息后,最好将此结构用于逻辑:

     if (msg_IncomingLoadDetails != null) { // your logic } else { block(); } 

    it can help to prevent some problems like NullPointerException 它可以帮助防止某些问题,例如NullPointerException

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

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