Send a message with information in GUI from an agent in Java after clicking a bottom

77 views Asked by At

I'm creating a GUI in Java (Netbeans) and I want to create a multi-agent system on it. The GUI will have some Text Area and a Button, so I want to click that button and give all the text information to an agent to process but I don“t know how communicate the GUI with the Agents or the AgentContainer.

GUI Class (main):

public class GUI extends javax.swing.JFrame {
    Contenedor c;

    public GUI() {
        initComponents();
        c = new Contenedor();
        c.contenedor();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtArea = new javax.swing.JTextArea();
        btnDesplegar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtArea.setColumns(20);
        txtArea.setRows(5);
        jScrollPane1.setViewportView(txtArea);

        btnDesplegar.setText("Desplegar");
        btnDesplegar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesplegarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,     javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(btnDesplegar)
                .addContainerGap(47, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(btnDesplegar)))
                .addContainerGap(171, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void btnDesplegarActionPerformed(java.awt.event.ActionEvent evt) {                                             
        System.out.println(txtArea.getText());
    
    }                                            

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);                
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnDesplegar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtArea;
    // End of variables declaration                   
}

AgentContainer:

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentContainer;
import jade.wrapper.StaleProxyException;
import java.util.logging.Level;

public class Contenedor {
    private AgentContainer contenedorAgentes;

    public void contenedor(){
        Runtime runtime = Runtime.instance();
        Profile profile = new ProfileImpl(null, 1097, null);
        contenedorAgentes = runtime.createMainContainer(profile);
        agregarAgentes();
    }
 
    private void agregarAgentes(){
        try {
            contenedorAgentes.createNewAgent("Ag1", Ag1.class.getName(), null).start();
        } 
        catch (StaleProxyException ex) {
            java.util.logging.Logger.getLogger(Contenedor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
}

Agent: import jade.core.Agent; import jade.core.behaviours.CyclicBehaviour;

public class Ag1 extends Agent{

    @Override
    public void setup(){
        addBehaviour(new Comportamiento());
    }

    @Override
    public void takeDown(){
    
    }

    class Comportamiento extends CyclicBehaviour{

        @Override
        public void action() {
            System.out.println("pan");
        }
       
    }
}
1

There are 1 answers

0
Hc. On

We lack some information. As agents are threads, to give them the data collected with the GUI will vary depending of your design choices.

  • If you have one GUI for each agent : The GUI should be launched by the agent. That way the agent will own a reference to the JFrame and thus be able to access the GUI variables.
  • If one GUI is used to create all the agents : You get the parameters from the GUI using actionPerformed(). Then you create the agents by giving the data collected through the GUI in parameter to the agents instantiation function.
  • If one GUI is used to update all agents at runtime : Then either your agents possess a reference to the GUI, the GUI possess a reference to the agents (considered as objects, not agents), the GUI is backed by an agent whose only role is to notify the agents through messages, or both agents and GUI share a common place to store data. It will depend of your constraints and objectives. In any case, I recommend using an asynchronous approach and an observer design pattern