| package cloudTradingNorConVsColCon.buyer; |
|
import jade.core.; |
| import jade.core.behaviours.; |
| import jade.lang.acl.; |
| import jade.domain.; |
| import jade.domain.FIPAAgentManagement.; |
| import java.util.Vector; |
| import java.util.Date; |
| import java.util.; |
| import java.io.; |
| public class CloudBuyerAgent extends Agent |
|
| protected void setup() |
| System.out.println("Buyer-agent "+getAID().getName()+" is ready."); |
| Object args = getArguments(); |
| if (args != null && args.length > 0) |
| for (int i = 0; i < args.length; ++i) |
| AID seller = new AID((String) args[i], AID.ISLOCALNAME); |
| sellerAgents.addElement(seller); |
| addBehaviour(new TickerBehaviour(this, 60000) |
|
| protected void onTick() |
|
| DFAgentDescription template = new DFAgentDescription(); |
| ServiceDescription sd = new ServiceDescription(); |
| sd.setType("Cloud-selling"); |
| template.addServices(sd); |
| DFAgentDescription result = DFService.search(myAgent, template); |
| sellerAgents.clear(); |
| for (int i = 0; i < result.length; ++i) |
| sellerAgents.addElement(result[i].getName()); |
| ); |
| protected void takeDown() |
| if (myGui != null) |
| myGui.dispose(); |
| System.out.println("Buyer-agent "+getAID().getName()+"terminated."); |
| public void purchase(String title, float maxPrice, Date deadline) |
| addBehaviour(new PurchaseManager(this, title, maxPrice, deadline)); |
| private class PurchaseManager extends TickerBehaviour |
|
| private PurchaseManager(Agent a, String t, float mp, Date d) |
| super(a, 60000); // tick every minute |
| title = t; |
| maxPrice = mp; |
| deadline = d.getTime(); |
| initTime = System.currentTimeMillis(); |
| deltaT = deadline - initTime; |
| public void onTick() |
| long currentTime = System.currentTimeMillis(); |
| if (currentTime > deadline) |
| myGui.notifyUser("Deadline expired for buying "+title); |
| stop(); |
| else |
| long elapsedTime = currentTime - initTime; |
| long negotiationTime = (deadline - initTime)/60000; |
| initialPrice=15; |
| if(negRound==0) |
| myGui.notifyUser("Current Time: "+initTime); |
| myGui.notifyUser("Deadline Time: "+deadline); |
| myGui.notifyUser("Possible Negotiation Round: "+negotiationTime); |
| float acceptablePrice = (float)Math.round(initialPrice); |
| myAgent.addBehaviour(new CloudNegotiator(title, acceptablePrice, this)); |
| cPrice=acceptablePrice; |
| else |
| concessionPrice=concessionPrice+1; |
| float acceptablePrice = (float)Math.round(initialPrice+concessionPrice); |
| myAgent.addBehaviour(new CloudNegotiator(title, acceptablePrice, this)); |
| cPrice=acceptablePrice; |
| negRound+=1; |
| myGui.notifyUser("Negotiation Round: "+negRound); |
| myGui.notifyUser("Deadline Time: "+deadline); |
| private class CloudNegotiator extends Behaviour |
|
| public CloudNegotiator(String t, float p, PurchaseManager m) |
| super(null); |
| title = t; |
| maxPrice = p; |
| manager = m; |
| public void action() |
| switch (step) |
| case 0: |
| myGui.notifyUser("Current Time: "+System.currentTimeMillis()); |
| ACLMessage cfp = new ACLMessage(ACLMessage.CFP); |
| cfp.addReceiver((AID)sellerAgents.elementAt(i)); |
| cfp.setContent(String.valueOf(cPrice)); |
| cfp.setConversationId("cloud-trade"); |
| cfp.setReplyWith("cfp"+System.currentTimeMillis()); // Unique value |
| myAgent.send(cfp); |
| myGui.notifyUser("Sent Call for Proposal at price"+cPrice); |
| mt = MessageTemplate.and( |
| MessageTemplate.MatchConversationId("cloud-trade"), |
| MessageTemplate.MatchInReplyTo(cfp.getReplyWith())); |
| case 1: |
| ACLMessage reply = myAgent.receive(mt); |
| BufferedWriter bw = new BufferedWriter(new FileWriter("C:/jade/src/project/output/ITBA5.txt", true)); |
| bw.write(String.valueOf(reply.getSender().getLocalName())); |
| if (reply != null) |
| if (reply.getPerformative() == ACLMessage.PROPOSE) |
| pPrice=Float.valueOf(reply.getContent()); |
| myGui.notifyUser("Received Proposal at "+pPrice+" when maximum acceptable price was "+cPrice); |
| bw.write(String.valueOf("PROPOSE")); |
| bw.write(String.valueOf(pPrice)); |
| else if (pPrice == cPrice) |
| ACLMessage orderAccept = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL); |
| orderAccept.addReceiver(bestSeller); |
| orderAccept.setContent(String.valueOf(pPrice)); |
| orderAccept.setConversationId("cloud-trade"); |
| orderAccept.setReplyWith("order"+System.currentTimeMillis()); |
| myAgent.send(orderAccept); |
| myGui.notifyUser("send Accept Proposal"); |
| bw.write(String.valueOf("ACCEPT_PROPOSAL")); |
| bw.write∖t); |
| bw.write(String.valueOf(pPrice)); |
| step = 4; |
| else |
| bw.write(String.valueOf("ACCEPT_PROPOSAL")); // |
| else block(); |
| case 2: |
| BufferedWriter bw = new BufferedWriter(new FileWriter("C:/jade/src/project/output/ITBA5.txt", true)); |
| if (bestSeller != null && bestPrice <= maxPrice) |
| ACLMessage order = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL); |
| order.addReceiver(bestSeller); |
| order.setContent(String.valueOf(pPrice)); |
| order.setConversationId("cloud-trade"); |
| order.setReplyWith("order"+System.currentTimeMillis()); |
| myAgent.send(order); |
| myGui.notifyUser("send Accept Proposal"); |
| mt = MessageTemplate.and( |
| MessageTemplate.MatchConversationId("cloud-trade"), |
| MessageTemplate.MatchInReplyTo(order.getReplyWith())); |
| case 3: |
| BufferedWriter bw = new BufferedWriter(new FileWriter("C:/jade/src/project/output/ITBA5.txt", true)); |
| reply = myAgent.receive(mt); |
| if (reply != null) |
| Purchase order reply received |
| if (reply.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) |
| bw.write(String.valueOf("ACCEPT_PROPOSAL")); |
| bw.write(String.valueOf(pPrice)); |
| ACLMessage commit = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL); |
| commit.addReceiver(bestSeller); |
| commit.setContent(String.valueOf(pPrice)); |
| commit.setConversationId("cloud-trade"); |
| commit.setReplyWith("order"+System.currentTimeMillis()); |
| myAgent.send(commit); |
| myGui.notifyUser("SLA Commit Proposal"); |
| myGui.notifyUser("Cloud service successfully purchased."); |
|