SHARE
TWEET

Untitled

a guest Jan 20th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package jadelab2;
  2.  
  3. import jade.core.Agent;
  4. import jade.core.AID;
  5. import jade.core.behaviours.*;
  6. import jade.lang.acl.ACLMessage;
  7. import jade.lang.acl.MessageTemplate;
  8. import jade.domain.DFService;
  9. import jade.domain.FIPAException;
  10. import jade.domain.FIPAAgentManagement.DFAgentDescription;
  11. import jade.domain.FIPAAgentManagement.ServiceDescription;
  12. import java.util.ArrayList;
  13. import java.util.Iterator;
  14. import jade.lang.acl.StringACLCodec;
  15. import java.io.StringReader;
  16.  
  17.  
  18.  
  19. import java.util.*;
  20.  
  21.  
  22.  
  23. public class MeetingAgent extends Agent {
  24.    
  25.   private int calendar[];
  26.  
  27.   private AID[] others;
  28.  
  29.     protected void setup() {
  30.       System.out.println("Hello! " + getAID().getLocalName() + " is ready for scheduling the meeting.");
  31.      
  32.        
  33.        
  34.  
  35.       addBehaviour(new TickerBehaviour(this, interval)
  36.       {
  37.           protected void onTick()
  38.           {
  39.               //search only if the purchase task was ordered
  40.               if (!targetBookTitle.equals(""))
  41.               {
  42.                   System.out.println(getAID().getLocalName() + ": I'm looking for " + targetBookTitle);
  43.                   //update a list of known sellers (DF)
  44.                   DFAgentDescription template = new DFAgentDescription();
  45.                   ServiceDescription sd = new ServiceDescription();
  46.                   sd.setType("book-selling");
  47.                   template.addServices(sd);
  48.                   try
  49.                   {
  50.                       DFAgentDescription[] result = DFService.search(myAgent, template);
  51.                       System.out.println(getAID().getLocalName() + ": the following sellers have been found");
  52.                       sellerAgents = new AID[result.length];
  53.                       for (int i = 0; i < result.length; ++i)
  54.                       {
  55.                           sellerAgents[i] = result[i].getName();
  56.                           System.out.println(sellerAgents[i].getLocalName());
  57.                       }
  58.                   }
  59.                   catch (FIPAException fe)
  60.                   {
  61.                       fe.printStackTrace();
  62.                   }
  63.  
  64.                   myAgent.addBehaviour(new RequestPerformer());
  65.               }
  66.           }
  67.       });
  68.   }
  69.  
  70.     //invoked from GUI, when purchase was ordered
  71.     public void lookForTitle(final String title)
  72.     {
  73.         addBehaviour(new OneShotBehaviour()
  74.         {
  75.             public void action()
  76.             {
  77.                 targetBookTitle = title;
  78.                 System.out.println(getAID().getLocalName() + ": purchase order for " + targetBookTitle + " accepted");
  79.             }
  80.         });
  81.     }
  82.  
  83.         protected void takeDown() {
  84.         myGui.dispose();
  85.         System.out.println("Buyer agent " + getAID().getLocalName() + " terminated.");
  86.     }
  87.  
  88.     private class RequestPerformer extends Behaviour {
  89.       private AID bestSeller;
  90.       private int bestPrice;
  91.       private int repliesCnt = 0;
  92.       private long startedWaiting = 0;
  93.       private MessageTemplate mt;
  94.       private int step = 0;
  95.       ArrayList<AID> offers = new ArrayList<AID>();
  96.    
  97.       public void action() {
  98.         switch (step) {
  99.         case 0:
  100.           //call for proposal (CFP) to found sellers
  101.           ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
  102.           for (int i = 0; i < sellerAgents.length; ++i) {
  103.             cfp.addReceiver(sellerAgents[i]);
  104.           }
  105.           cfp.setContent(targetBookTitle);
  106.           cfp.setConversationId("book-trade");
  107.           cfp.setReplyWith("cfp"+System.currentTimeMillis()); //unique value
  108.           myAgent.send(cfp);
  109.          
  110.           startedWaiting = System.nanoTime(); //start waiting for replies
  111.          
  112.           mt = MessageTemplate.and(MessageTemplate.MatchConversationId("book-trade"),
  113.                                    MessageTemplate.MatchInReplyTo(cfp.getReplyWith()));
  114.           step = 1;
  115.           break;
  116.         case 1:
  117.           //collect proposals
  118.          
  119.           if((System.nanoTime() - startedWaiting)/1000000 >= interval){
  120.               if(repliesCnt == 0){
  121.                     step = 4;
  122.                     System.out.println(getAID().getLocalName() + "No seller is answering me ... " );
  123.                    
  124.                 }
  125.                 else{
  126.                     step = 2;
  127.                 }
  128.           }
  129.          
  130.           ACLMessage reply = myAgent.receive(mt);
  131.           if (reply != null) {
  132.             if (reply.getPerformative() == ACLMessage.PROPOSE) {
  133.               //proposal received
  134.              
  135.               offers.add(reply.getSender());
  136.              
  137.               int price = Integer.parseInt(reply.getContent());
  138.               if (bestSeller == null || price < bestPrice) {
  139.                 //the best proposal as for now
  140.                 bestPrice = price;
  141.                 bestSeller = reply.getSender();
  142.               }
  143.             repliesCnt++;
  144.             startedWaiting = System.nanoTime(); //reset timeout
  145.             }
  146.            
  147.  
  148.             if (repliesCnt  >= sellerAgents.length) {
  149.               //all proposals have been received
  150.               step = 2;
  151.             }
  152.            
  153.           }
  154.           else {
  155.                
  156.                 block(interval);
  157.                
  158.            
  159.            
  160.           }
  161.           break;
  162.         case 2:
  163.           //best proposal consumption - purchase
  164.           if(budget<bestPrice){
  165.               System.out.println(getAID().getLocalName() + ": I can't afford : " +targetBookTitle );
  166.               //System.out.println(getAID().getLocalName() + ": waiting for the next purchase order.");
  167.               //targetBookTitle = "";
  168.              
  169.               Iterator iterator = offers.iterator();
  170.                 AID targetAid = null;
  171.                 while(iterator.hasNext()) {
  172.                     targetAid = (AID) iterator.next();
  173.                
  174.                    ACLMessage ref = new ACLMessage(ACLMessage.REFUSE);
  175.                         ref.addReceiver(targetAid);
  176.  
  177.                    
  178.                     myAgent.send(ref);
  179.                
  180.                 }
  181.               step = 4;
  182.               break;
  183.           }
  184.          
  185.          
  186.             Iterator iterator = offers.iterator();
  187.             AID targetAid = null;
  188.             while(iterator.hasNext()) {
  189.                 targetAid = (AID) iterator.next();
  190.                if(!targetAid.toString().equals(bestSeller.toString())){
  191.                    ACLMessage ref = new ACLMessage(ACLMessage.REFUSE);
  192.                         ref.addReceiver(targetAid);
  193.  
  194.                    
  195.                     myAgent.send(ref);
  196.                }
  197.             }
  198.          
  199.          
  200.          
  201.          
  202.    
  203.           ACLMessage order = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL);
  204.           order.addReceiver(bestSeller);
  205.           order.setConversationId("book-trade");
  206.           order.setReplyWith("order"+System.currentTimeMillis());
  207.           myAgent.send(order);
  208.           mt = MessageTemplate.and(MessageTemplate.MatchConversationId("book-trade"),
  209.                                    MessageTemplate.MatchInReplyTo(order.getReplyWith()));
  210.           step = 3;
  211.           break;
  212.         case 3:      
  213.           //seller confirms the transaction
  214.           reply = myAgent.receive(mt);
  215.           if (reply != null) {
  216.             if (reply.getPerformative() == ACLMessage.INFORM) {
  217.               //purchase succeeded
  218.               budget =budget - bestPrice;
  219.              
  220.               myGui.updateBudget();
  221.              
  222.               System.out.println(getAID().getLocalName() + ": " + targetBookTitle + " purchased for " + bestPrice + " from " + reply.getSender().getLocalName());
  223.           System.out.println(getAID().getLocalName() + ": waiting for the next purchase order.");
  224.           targetBookTitle = "";
  225.               //myAgent.doDelete();
  226.             }
  227.             else {
  228.               System.out.println(getAID().getLocalName() + ": purchase has failed. " + targetBookTitle + " was sold in the meantime.");
  229.             }
  230.             step = 4;   //this state ends the purchase process
  231.           }
  232.           else {
  233.             block();
  234.           }
  235.           break;
  236.         }        
  237.       }
  238.    
  239.       public boolean done() {
  240.         if (step == 2 && bestSeller == null) {
  241.             System.out.println(getAID().getLocalName() + ": " + targetBookTitle + " is not on sale.");
  242.         }
  243.         //process terminates here if purchase has failed (title not on sale) or book was successfully bought
  244.         return ((step == 2 && bestSeller == null) || step == 4);
  245.       }
  246.     }
  247.  
  248. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top