Ensayo de Agente Comprador jade
Enviado por Kate • 25 de Octubre de 2018 • 899 Palabras (4 Páginas) • 246 Visitas
...
case 0:
// Send the cfp to all sellers
ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
for (int i = 0; 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());
if (bestSeller == null || price
// This is the best offer at present
bestPrice = price;
bestSeller = reply.getSender();
}
}
repliesCnt++;
if (repliesCnt >= AgentesVendedores.length) {
// We received all replies
step = 2;
}
}
else {
block();
}
break;
case 2:
// Send the purchase order to the seller that provided the best offer
ACLMessage order = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL);
order.addReceiver(bestSeller);
order.setContent(TituloLibroComprar);
order.setConversationId("Comercio-libros");
order.setReplyWith("Orden"+System.currentTimeMillis());
myAgent.send(order);
// Prepare the template to get the purchase order reply
mt = MessageTemplate.and(MessageTemplate.MatchConversationId("Comercio-libros"),
MessageTemplate.MatchInReplyTo(order.getReplyWith()));
step = 3;
break;
case 3:
// Receive the purchase order reply
reply = myAgent.receive(mt);
if (reply != null) {
// Purchase order reply received
if (reply.getPerformative() == ACLMessage.INFORM) {
// Purchase successful. We can terminate
System.out.println(TituloLibroComprar+" comprado con éxito de agente "+reply.getSender().getName());
System.out.println("Precio = "+bestPrice);
myAgent.doDelete();
}
else {
System.out.println("falla: libro solicitado ya está vendido.");
}
step = 4;
}
else {
block();
}
break;
}
}
public boolean done() {
if (step == 2 && bestSeller == null) {
System.out.println("Falla: "+TituloLibroComprar+" no esta a la venta");
}
return ((step == 2 && bestSeller == null) || step == 4);
}
} // End of inner class RequestPerformer
}
Vendedor
package com.agentes;
import jade.core.Agent;
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;
import java.util.*;
public class AgenteVendedorLibros extends Agent {
...