CFP and replies done, still buggywq
Esse commit está contido em:
@@ -1,10 +1,13 @@
|
|||||||
package demo.DutchAuctionDemo;
|
package demo.DutchAuctionDemo;
|
||||||
|
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
import jade.core.Profile;
|
import jade.core.Profile;
|
||||||
import jade.core.ProfileImpl;
|
import jade.core.ProfileImpl;
|
||||||
import jade.core.Runtime;
|
import jade.core.Runtime;
|
||||||
import jade.util.ExtendedProperties;
|
import jade.util.ExtendedProperties;
|
||||||
import jade.util.leap.Properties;
|
import jade.util.leap.Properties;
|
||||||
|
import jade.wrapper.AgentController;
|
||||||
import jade.wrapper.ContainerController;
|
import jade.wrapper.ContainerController;
|
||||||
import jade.wrapper.StaleProxyException;
|
import jade.wrapper.StaleProxyException;
|
||||||
import examples.protocols.DutchAuctionInitiatorAgent;
|
import examples.protocols.DutchAuctionInitiatorAgent;
|
||||||
@@ -12,21 +15,29 @@ import examples.protocols.DutchAuctionResponderAgent;
|
|||||||
|
|
||||||
public class DutchAuctionDemo
|
public class DutchAuctionDemo
|
||||||
{
|
{
|
||||||
public static void main(String[] args)
|
public static void main(String[] args) throws InterruptedException
|
||||||
{
|
{
|
||||||
Runtime jade = Runtime.instance();
|
Runtime jade = Runtime.instance();
|
||||||
Properties mainProps = new ExtendedProperties();
|
Properties mainProps = new ExtendedProperties();
|
||||||
mainProps.setProperty(Profile.MAIN, "true");
|
mainProps.setProperty(Profile.MAIN, "true");
|
||||||
mainProps.setProperty(Profile.GUI, "true");
|
mainProps.setProperty(Profile.GUI, "true");
|
||||||
|
mainProps.setProperty(Profile.MAIN_HOST, "127.0.0.1");
|
||||||
|
|
||||||
ContainerController mainContainer = jade.createMainContainer(new ProfileImpl(mainProps));
|
jade.createMainContainer(new ProfileImpl(mainProps));
|
||||||
|
ContainerController container = jade.createAgentContainer(new ProfileImpl());
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
String[] bidderNames = new String[] {"Albert", "Benny", "Celia", "Dan", "Eustace", "Fenny"};
|
String[] bidderNames = new String[] {"Albert", "Benny", "Celia", "Dan", "Eustace", "Fenny"};
|
||||||
mainContainer.createNewAgent("Auctioneer", DutchAuctionInitiatorAgent.class.getCanonicalName(), bidderNames);
|
Vector<AgentController> ctrls = new Vector<AgentController>();
|
||||||
|
|
||||||
for(String bidderName : bidderNames)
|
for(String bidderName : bidderNames)
|
||||||
mainContainer.createNewAgent(bidderName, DutchAuctionResponderAgent.class.getCanonicalName(), new Object[] {});
|
ctrls.add(container.createNewAgent(bidderName, DutchAuctionResponderAgent.class.getCanonicalName(), new Object[] {}));
|
||||||
|
|
||||||
|
AgentController auct = container.createNewAgent("Auctioneer", DutchAuctionInitiatorAgent.class.getCanonicalName(), bidderNames);
|
||||||
|
Thread.sleep(10000);
|
||||||
|
auct.start();
|
||||||
|
for(AgentController ctrl : ctrls)
|
||||||
|
ctrl.start();
|
||||||
|
|
||||||
} catch(StaleProxyException e)
|
} catch(StaleProxyException e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -33,22 +33,30 @@ public class DutchAuctionInitiatorAgent extends Agent {
|
|||||||
args = getArguments();
|
args = getArguments();
|
||||||
if (args != null && args.length > 0) {
|
if (args != null && args.length > 0) {
|
||||||
numBidders = args.length;
|
numBidders = args.length;
|
||||||
System.out.println("There are: " + numBidders + " responders.");
|
System.out.println("["+getLocalName()+"] There are: " + numBidders + " responders.");
|
||||||
|
|
||||||
ACLMessage cfp = getCfp(args);
|
ACLMessage cfp = getCfp(args);
|
||||||
|
|
||||||
addBehaviour(new DutchAuctionInitiator(this, cfp) {
|
addBehaviour(new DutchAuctionInitiator(this, cfp) {
|
||||||
|
|
||||||
|
|
||||||
protected void handleBid(ACLMessage bid, ACLMessage accept) {
|
protected void handleBid(ACLMessage bid, ACLMessage accept) {
|
||||||
accept = bid.createReply();
|
accept = bid.createReply();
|
||||||
accept.setPerformative(ACLMessage.ACCEPT_PROPOSAL);
|
accept.setPerformative(ACLMessage.ACCEPT_PROPOSAL);
|
||||||
System.out.println("Sold to: " + bid.getSender().getName());
|
System.out.println("Sold to: " + bid.getSender().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void handleTimeout(Vector responses, Vector acceptances) {
|
||||||
|
if (endAuction()){
|
||||||
|
System.out.println("end");
|
||||||
|
} else {
|
||||||
|
acceptances.add(prepareCFP());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean endAuction() {
|
protected boolean endAuction() {
|
||||||
return crtPrice-- < minPrice;
|
return crtPrice < minPrice;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class DutchAuctionResponderAgent extends Agent
|
|||||||
@Override
|
@Override
|
||||||
protected boolean handleCfp(ACLMessage cfp)
|
protected boolean handleCfp(ACLMessage cfp)
|
||||||
{
|
{
|
||||||
System.out.println("Agent " + getLocalName() + ": CFP received from " + cfp.getSender().getName() + ". Action is " + cfp.getContent());
|
log("CFP received from " + cfp.getSender().getName() + ". Action is " + cfp.getContent());
|
||||||
|
|
||||||
return shouldPropose(cfp);
|
return shouldPropose(cfp);
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,8 @@ public class DutchAuctionResponderAgent extends Agent
|
|||||||
*/
|
*/
|
||||||
protected boolean shouldPropose(ACLMessage cfp)
|
protected boolean shouldPropose(ACLMessage cfp)
|
||||||
{
|
{
|
||||||
return (Math.random() > 0.1);
|
// return false;
|
||||||
|
return (Math.random() > 0.9);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -114,6 +114,15 @@ public abstract class DutchAuctionInitiator extends Initiator {
|
|||||||
return prepareCfps(initiation);
|
return prepareCfps(initiation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void sendInitiations(Vector initiations) {
|
||||||
|
// By default the initiations parameter points to the Vector of the CFPs.
|
||||||
|
// However at step 2 we need to deal with the acceptances
|
||||||
|
/* if (step == 1) {
|
||||||
|
initiations = (Vector) getDataStore().get(ALL_ACCEPTANCES_KEY);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
super.sendInitiations(initiations);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Check whether a reply is in-sequence and update the appropriate Session
|
Check whether a reply is in-sequence and update the appropriate Session
|
||||||
@@ -174,8 +183,9 @@ public abstract class DutchAuctionInitiator extends Initiator {
|
|||||||
* by this class.
|
* by this class.
|
||||||
**/
|
**/
|
||||||
protected Vector prepareCfps(ACLMessage cfp) {
|
protected Vector prepareCfps(ACLMessage cfp) {
|
||||||
|
System.out.println(cfp.toString());
|
||||||
Vector v = new Vector(1);
|
Vector v = new Vector(1);
|
||||||
v.addElement(cfp);
|
v.addElement(cfp);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,13 +32,13 @@ public class DutchAuctionResponder extends SSResponder {
|
|||||||
|
|
||||||
/* receive INFORM_START_OF_AUCTION */
|
/* receive INFORM_START_OF_AUCTION */
|
||||||
MessageTemplate mt = MessageTemplate.and(MessageTemplate.MatchProtocol(FIPANames.InteractionProtocol.FIPA_DUTCH_AUCTION),MessageTemplate.MatchPerformative(ACLMessage.CFP));
|
MessageTemplate mt = MessageTemplate.and(MessageTemplate.MatchProtocol(FIPANames.InteractionProtocol.FIPA_DUTCH_AUCTION),MessageTemplate.MatchPerformative(ACLMessage.CFP));
|
||||||
b = new MsgReceiver(a, mt, -1, getDataStore(), SOA_KEY);
|
// b = new MsgReceiver(a, mt, -1, getDataStore(), SOA_KEY);
|
||||||
registerFirstState(b, RECEIVE_INFORM_START_OF_AUCTION);
|
// registerFirstState(b, RECEIVE_INFORM_START_OF_AUCTION);
|
||||||
registerDefaultTransition(RECEIVE_INFORM_START_OF_AUCTION, RECEIVE_CFP);
|
// registerDefaultTransition(RECEIVE_INFORM_START_OF_AUCTION, RECEIVE_CFP);
|
||||||
|
|
||||||
/* receive CFP */
|
/* receive CFP */
|
||||||
b = new MsgReceiver(a, mt, -1, getDataStore(), CFP_KEY);
|
b = new MsgReceiver(a, mt, -1, getDataStore(), CFP_KEY);
|
||||||
registerState(b, RECEIVE_CFP);
|
registerFirstState(b, RECEIVE_CFP);
|
||||||
registerDefaultTransition(RECEIVE_CFP, HANDLE_CFP);
|
registerDefaultTransition(RECEIVE_CFP, HANDLE_CFP);
|
||||||
|
|
||||||
/* handle CFP */
|
/* handle CFP */
|
||||||
@@ -49,7 +49,7 @@ public class DutchAuctionResponder extends SSResponder {
|
|||||||
registerDefaultTransition(HANDLE_CFP, RECEIVE_CFP);
|
registerDefaultTransition(HANDLE_CFP, RECEIVE_CFP);
|
||||||
|
|
||||||
/* handle SEND_REPLY */
|
/* handle SEND_REPLY */
|
||||||
deregisterDefaultTransition(SEND_REPLY);
|
// deregisterDefaultTransition(SEND_REPLY);
|
||||||
registerTransition(SEND_REPLY, RECEIVE_NEXT, ACLMessage.PROPOSE);
|
registerTransition(SEND_REPLY, RECEIVE_NEXT, ACLMessage.PROPOSE);
|
||||||
|
|
||||||
/* handle RECEIVE_NEXT & CHECK_IN_SEQ */
|
/* handle RECEIVE_NEXT & CHECK_IN_SEQ */
|
||||||
@@ -91,8 +91,13 @@ public class DutchAuctionResponder extends SSResponder {
|
|||||||
public void action() {
|
public void action() {
|
||||||
DutchAuctionResponder parent = (DutchAuctionResponder) getParent();
|
DutchAuctionResponder parent = (DutchAuctionResponder) getParent();
|
||||||
if (parent.handleCfp((ACLMessage) getDataStore().get(parent.CFP_KEY))) {
|
if (parent.handleCfp((ACLMessage) getDataStore().get(parent.CFP_KEY))) {
|
||||||
|
System.out.println(myAgent.getLocalName() + ": " + true);
|
||||||
|
ACLMessage cfp = (ACLMessage) getDataStore().get(parent.CFP_KEY);
|
||||||
|
ACLMessage reply = cfp.createReply();
|
||||||
|
getDataStore().put(parent.REPLY_KEY, reply);
|
||||||
ret = CFP_ACCEPTED;
|
ret = CFP_ACCEPTED;
|
||||||
} else {
|
} else {
|
||||||
|
System.out.println(myAgent.getLocalName() + ": " + false);
|
||||||
ret = CFP_DENIED;
|
ret = CFP_DENIED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Referência em uma Nova Issue
Bloquear um usuário