ClubEnsayos.com - Ensayos de Calidad, Tareas y Monografias
Buscar

Ensayo de Agente Comprador jade

Marjo AcevedoPráctica o problema1 de Junio de 2017

8.991 Palabras (36 Páginas)322 Visitas

Página 1 de 36

Comprador

package com.agentes;

import jade.core.Agent;

import jade.core.AID;

import jade.core.behaviours.*;

import jade.lang.acl.ACLMessage;

import jade.lang.acl.MessageTemplate;

import jade.domain.DFService;

import jade.domain.FIPAException;

import jade.domain.FIPAAgentManagement.DFAgentDescription;

import jade.domain.FIPAAgentManagement.ServiceDescription;

public class AgenteCompradorLibros extends Agent {

        // The title of the book to buy

        private String TituloLibroComprar;

        // The list of known seller agents

        private AID[] AgentesVendedores;

        // Put agent initializations here

        protected void setup() {

                // Printout a welcome message

                System.out.println("Hola! Agente-Comprador "+

                        getAID().getName()+" es Leido.");

                // Get the title of the book to buy as a start-up argument

                Object[] args = getArguments();

                if (args != null && args.length > 0) {

                        TituloLibroComprar = (String) args[0];

                        System.out.println("Libro Objetivo: "+

                                TituloLibroComprar);

                        // Add a TickerBehaviour that schedules a request to seller agents every minute

                        addBehaviour(new TickerBehaviour(this, 60000) {

                                protected void onTick() {

                                        System.out.println("Trato de Comprar "+

                                                TituloLibroComprar);

                                        // Update the list of seller agents

                                        DFAgentDescription template =

                                                new DFAgentDescription();

                                        ServiceDescription sd =

                                                new ServiceDescription();

                                        sd.setType("venta-libros");

                                        template.addServices(sd);

                                        try {

                                                DFAgentDescription[] result = DFService.search(myAgent, template);

                                                System.out.println("Se encuentran los siguientes agentes de vendedor:");

                                                AgentesVendedores = new AID[result.length];

                                                for (int i = 0; i < result.length; ++i) {

                                                        AgentesVendedores[i] = result[i].getName();

                                                        System.out.println(AgentesVendedores[i].getName());

                                                }

                                        }

                                        catch (FIPAException fe) {

                                                fe.printStackTrace();

                                        }

                                        // Perform the request

                                        myAgent.addBehaviour(new SolicitudCompra());

                                }

                        } );

                }

                else {

                        // Make the agent terminate

                        System.out.println("No se encuentra el libro Especificado");

                        doDelete();

                }

        }

        // Put agent clean-up operations here

        protected void takeDown() {

                // Printout a dismissal message

                System.out.println("Agente-Comprador "+getAID().getName()+

                        " Terminado.");

        }

        /**

           Inner class RequestPerformer.

           This is the behaviour used by Book-buyer agents to request seller

           agents the target book.

         */

        private class SolicitudCompra extends Behaviour {

                private AID bestSeller; // The agent who provides the best offer

                private int bestPrice;  // The best offered price

                private int repliesCnt = 0; // The counter of replies from seller agents

                private MessageTemplate mt; // The template to receive replies

                private int step = 0;

                public void action() {

                        switch (step) {

                        case 0:

                                // Send the cfp to all sellers

                                ACLMessage cfp = new ACLMessage(ACLMessage.CFP);

                                for (int i = 0; i < AgentesVendedores.length; ++i) {

                                        cfp.addReceiver(AgentesVendedores[i]);

                                }

                                cfp.setContent(TituloLibroComprar);

                                cfp.setConversationId("Comercio-libros");

                                cfp.setReplyWith("cfp"+System.currentTimeMillis()); // Unique value

                                myAgent.send(cfp);

                                // Prepare the template to get proposals

                                mt = MessageTemplate.and(MessageTemplate.MatchConversationId("Comercio-libros"),

                                                MessageTemplate.MatchInReplyTo(cfp.getReplyWith()));

                                step = 1;

                                break;

                        case 1:

                                // Receive all proposals/refusals from seller agents

                                ACLMessage reply = myAgent.receive(mt);

                                if (reply != null) {

                                        // Reply received

                                        if (reply.getPerformative() == ACLMessage.PROPOSE) {

                                                // This is an offer

                                                int price = Integer.parseInt(reply.getContent());

...

Descargar como (para miembros actualizados) txt (19 Kb) pdf (65 Kb) docx (16 Kb)
Leer 35 páginas más »
Disponible sólo en Clubensayos.com