JADE

CDTrader

package examples.content;

import jade.core.*;
import jade.core.behaviours.*;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import jade.util.leap.List;
import jade.util.leap.ArrayList;
import jade.util.leap.Iterator;

import jade.content.*;
import jade.content.abs.*;
import jade.content.onto.*;
import jade.content.onto.basic.*;
import jade.content.lang.*;
import jade.content.lang.sl.*;

import examples.content.musicShopOntology.*;
import examples.content.ecommerceOntology.*;

import java.util.Date;

/**
   This is an agent that plays at the same time the part of a seller of 
   CDs and a buyer of CDs.
   More in details the conversation between the "seller" and the "buyer"
   will go on as follows:
   - "Seller" informs "buyer" that he owns a CD ("Synchronicity").
   - "Buyer" asks for the price of that CD
   - "Seller" informs "buyer" about the price
   - "Buyer" requests "seller" to sell him the CD specifying his credit card
   - "Seller" performs the action (this step is not actually implemented as 
   it would imply interacting with a delivery system like UPS and an
   electronic payment system) and notifies "buyer"
*/
public class CDTrader extends Agent {
  private ContentManager manager  = (ContentManager) getContentManager();
  // This agent "speaks" the SL language
  private Codec      codec    = new SLCodec();
  // This agent "knows" the Music-Shop ontology
  private Ontology   ontology = MusicShopOntology.getInstance();

  protected void setup() {

    //regiter the codec and ontology that I am using
    manager.registerLanguage(codec);
    manager.registerOntology(ontology);
  
    // BUYER PART
    addBehaviour(new HandleInformBehaviour(this));
      
    // SELLER PART
    addBehaviour(new HandleQueryBehaviour(this));      
    addBehaviour(new HandleRequestBehaviour(this)); 
      
    CD myCd = new CD();
    myCd.setSerialID(123456);
    myCd.setTitle("Synchronicity");
    List tracks = new ArrayList();
    Track t = new Track();
    t.setName("Synchronicity");
    tracks.add(t);
    t = new Track();
    t.setName("Every breath you take");
    tracks.add(t);
    t = new Track();
    t.setName("King of pain");
    t.setDuration(new Integer(240));
    tracks.add(t);
      
    myCd.setTracks(tracks);
          
    addBehaviour(new InformOwnsBehaviour(this, myCd));      
  }
    
  protected void takeDown() {
    System.out.println(getName()+" exiting ...");
  }
    
  // SELLER informs BUYER that he owns a given Item
  class InformOwnsBehaviour extends OneShotBehaviour {
    private Item it;
    
    public InformOwnsBehaviour(Agent a, Item it) { 
      super(a); 
      this.it = it;
    }
    
    public void action() {
      try {
        System.out.println("\nSELLER: Inform BUYER that I own "+it);

        //This is an example of sending a message using the ontology support.
        
        // Prepare the message
        ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
        AID receiver = getAID(); // Send the message to myself
                    
        msg.setSender(getAID());
        msg.addReceiver(receiver);
        msg.setLanguage(codec.getName());
        msg.setOntology(ontology.getName());

        // Fill the content
        Owns owns = new Owns();
        owns.setOwner(getAID());
        owns.setItem(it);
          
        manager.fillContent(msg, owns);
        send(msg);
      } 
      catch(Exception e) { 
        e.printStackTrace(); 
      }

    }
  }
  
  ///some stuff here
     
}

José M. Vidal .

21 of 21