Ensayo de Agente Comprador jade
Enviado por Marjo Acevedo • 1 de Junio de 2017 • Práctica o problema • 8.991 Palabras (36 Páginas) • 283 Visitas
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) {
...