2

I have the following requirement in 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

    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?

EyesOfÖzil
  • 309
  • 1
  • 9

1 Answers1

3

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

  • The tick behaviour executes onTick method every 5s. I know it does not sum the values. I would like to know how I can receive the values from all agents and then sum them – EyesOfÖzil May 09 '16 at 19:58
  • Agent.receive() method receives the oldest message in agent's queue. I created 2 sender agents, each sends random integer every 5 seconds. And receiver receives from both senders, here's log from my console: http://pastebin.com/D8sN1fUY – Wojciech Ćwiek May 09 '16 at 20:09
  • 1
    This works in this way: senders and receiver agent start, receiver tries to get message, but queue is empty. So his behaviour blocks and is waiting for signal. Then both senders send their numbers. Receiver gets signal about new messages and triggers his behaviour. It finds message in queue, gets it and shows it. Then again, triggers behaviour, finds second message in queue, gets it and shows it. – Wojciech Ćwiek May 09 '16 at 20:24