create array of agents in jade

2.9k views Asked by At

good morning, i would like to create an array that contain a multiple instances of an agent (many agents that have a same behaviour), so i used netbeans to create firstly an agent management who create the others agents and draw a circle that represent every agent in a frame. this the code for the main agent:

package jade;

/**
 *
 * @author walid
 */
import jade.core.Agent;
import jade.core.Runtime;
import jade.core.ProfileImpl;
import jade.wrapper.*;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;

import java.util.logging.Level;
import java.util.logging.Logger;
import jade.Agent1;
public class AgentEnvironement extends Agent{
    private JFrame jFrame = null;
    private Agent1 []tab;       
    @Override
             protected void setup() {
        try {
            getJFrame().setVisible(true);
        } catch (StaleProxyException ex) {
            Logger.getLogger(AgentEnvironement.class.getName()).log(Level.SEVERE, null, ex);
        }
}           

public JFrame getJFrame() throws StaleProxyException {
        if (jFrame == null) {
        jFrame = new JFrame();
        jFrame.setSize(new java.awt.Dimension(500,350));
        Dimension tailleEcran =Toolkit.getDefaultToolkit().getScreenSize();
        int largeurEcran = tailleEcran.width;
        int hauteurEcran = tailleEcran.height;
        jFrame.setLocation((largeurEcran-500)/2,(hauteurEcran-350)/2);
        jFrame.setTitle("Environement des agents rumeurs.");
        jFrame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);                  
        //jFrame.setContentPane(getJContentPane());
                Runtime rt = Runtime.instance();
        // Création du profil par défault
    ProfileImpl p = new ProfileImpl(false);
    AgentContainer container =rt.createMainContainer(p);
        // Agent controleur pour permettre la création des agents 
    AgentController Agent=null;
        Agent = container.createNewAgent("Agent1", "jade.Agent1", null);
    Agent.start();      
    }
    return jFrame;
}
}

and this the code for the agent class that i would like to create many instance of him package jade;

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


/**
 *
 * @author walid
 */
public class Agent1 extends Agent {

    /**
     */
    @Override
    public void setup() {
addBehaviour(new comportement());
}
class comportement extends CyclicBehaviour {
public void action() {
System.out.println ("ready");
}
}


    public static void main(String[] args) {

    }

}

honestly i'm not good in java programming and if someone can help me i will be very pleasure.

2

There are 2 answers

0
Just_Alex On

Create an array of agents???? I'm not sure what that means but if you want to create multiple agents use a for loop and a counter to increment the name.

 for (agentcounter=1;agentcounter++;agentcounter<agentmax)
 {
      Agent = container.createNewAgent("Agent"+agentcounter, "jade.Agent1", null);
 }

this should create multiple agents of type jade.Agent1 eg for agentmax=10

Agent1 (Type jade.Agent1)

Agent2 (Type jade.Agent1)

Agent3 (Type jade.Agent1)

....

Agent10 (Type jade.Agent1)

Notice that in

Agent = container.createNewAgent("Agent"+agentcounter, "jade.AgentClass", null);

The first field is the name of the instance of the agent The second field is the agent class

0
shirowww On

You cannot call your agent 'Agent', because is the name of another class, the root class your agents are inheriting from.

    AgentController anotherName=null;
    anotherName = container.createNewAgent("Agent1", "jade.Agent1", null);
    anotherName.start();      

That should work fine.

Another comment; Agents don't need and don't have a 'main()' method, because they are created in another fashion, as you have just seen.