?? bookselleragent.java
字號(hào):
package bookTrading.seller;
import jade.core.Agent;
import jade.core.behaviours.*;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.SSContractNetResponder;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.*;
import jade.content.ContentElementList;
import jade.content.ContentManager;
import jade.content.onto.*;
import jade.content.onto.basic.Action;
import jade.content.lang.*;
import jade.content.lang.sl.SLCodec;
import jade.content.lang.Codec.CodecException;
import bookTrading.ontology.*;
import java.util.*;
public class BookSellerAgent extends Agent {
// The catalogue of books available for sale
private Map catalogue = new HashMap();
// The GUI to interact with the user
private BookSellerGui myGui;
private Codec codec = new SLCodec();
private Ontology ontology = BookTradingOntology.getInstance();
/**
* Agent initializations
*/
protected void setup(){
//Creat and show the GUI
myGui = new BookSellerGuiImpl(this);
myGui.show();
getContentManager().registerLanguage(codec);
getContentManager().registerOntology(ontology);
// Add the behaviout serving calls for price from buyer agents
addBehaviour(new CallForOfferServer(this));
// Add the behaviour serving purchase requests from buyer agents
// addBehaviour(new PurchaseOrderServer());
// Register the book-selling service in the yellow pages
DFAgentDescription dfd = new DFAgentDescription();
dfd.setName(getAID());
ServiceDescription sd = new ServiceDescription();
sd.setType("Book-selling");
sd.setName(getLocalName()+"-Book-selling");
dfd.addServices(sd);
try {
DFService.register(this, dfd);
}
catch (FIPAException fe) {
fe.printStackTrace();
}
// Printout a welcome message
System.out.println("Seller-agent " + getAID().getName()
+ " is ready.");
}
/**
* Agent clean-up
*/
protected void takeDown(){
// Deregister from the yellow pages
try {
DFService.deregister(this);
}
catch (FIPAException fe) {
fe.printStackTrace();
}
//Dispose the GUI if it is there
if(myGui != null){
myGui.dispose();
}
//Printout a dismissal message
System.out.println("Seller-agent " + getAID().getName()
+ "terminating.");
}
/**
* This method is called by the GUI when the user inserts a new
* book for sale
* @param title The title of the book for sale
* @param initialPrice The initial price
* @param minPrice The minimum price
* @param deadline The deadline by which to sell the book
*/
public void putForSale(String title, float initPrice,
float minPrice, Date deadline){
addBehaviour(new PriceManager(this,
title, initPrice, minPrice, deadline));
}
private class PriceManager extends TickerBehaviour{
private String title;
private float minPrice, currentPrice, deltaP;
private long initTime, deadline, deltaT;
private PriceManager(Agent a, String t, float ip, float mp, Date d){
super(a, 60000); // tick every minute
title = t;
minPrice = mp;
currentPrice = ip;
deltaP = ip - mp;
deadline = d.getTime();
initTime = System.currentTimeMillis();
deltaT = deadline - initTime;
}
public void onStart(){
// Insert the book in the catalogue of books available for sale
catalogue.put(title, this);
super.onStart();
}
public void onTick(){
long currentTime = System.currentTimeMillis();
if(currentTime > deadline){
// Deadline expired
myGui.notifyUser("Cannot sell book" + title);
catalogue.remove(title);
stop();
} else {
// Compute the current price
long elapsedTime = currentTime - initTime;
currentPrice = minPrice + deltaP * (1 - (float)((double)elapsedTime / (double)deltaT));
}
}
public float getCurrentPrice(){
return currentPrice;
}
}
/**
Inner class CallForOfferServer.
This is the behaviour used by Book-seller agents to serve
incoming call for offer from buyer agents.
If the indicated book is in the local catalogue, the seller agent
replies with a PROPOSE message specifying the price. Otherwise
a REFUSE message is sent back.
*/
private class CallForOfferServer extends CyclicBehaviour{
private MessageTemplate mt = MessageTemplate.and(
MessageTemplate.MatchProtocol(FIPANames.InteractionProtocol.FIPA_CONTRACT_NET),
MessageTemplate.MatchPerformative(ACLMessage.CFP));
public CallForOfferServer(Agent a) {
super(a);
}
public void action(){
ACLMessage cfp = myAgent.receive(mt);
if(cfp != null) {
// Message received.Process it
myAgent.addBehaviour(new SSContractNetResponder(myAgent, cfp){
protected ACLMessage handleCfp(ACLMessage cfp) {
ACLMessage reply = cfp.createReply();
try {
ContentManager cm = myAgent.getContentManager();
Action act = (Action)cm.extractContent(cfp);
Sell sellAction = (Sell)act.getAction();
Book book = sellAction.getItem();
PriceManager pm = (PriceManager)catalogue.get(book.getTitle());
if(pm != null) {
// The requested book is available for sale. Reply with the price
reply.setPerformative(ACLMessage.PROPOSE);
ContentElementList cel = new ContentElementList();
cel.add(act);
Costs costs = new Costs();
costs.setItem(book);
costs.setPrice(pm.getCurrentPrice());
cel.add(costs);
cm.fillContent(reply, cel);
}
else{
// The requested book is NOT available for sale .
reply.setPerformative(ACLMessage.REFUSE);
}
}
catch (OntologyException oe) {
oe.printStackTrace();
reply.setPerformative(ACLMessage.NOT_UNDERSTOOD);
}
catch (CodecException ce) {
ce.printStackTrace();
reply.setPerformative(ACLMessage.NOT_UNDERSTOOD);
}
return reply;
}
protected ACLMessage handleAcceptProposal(
ACLMessage cfp, ACLMessage propose, ACLMessage accept) {
ACLMessage reply = accept.createReply();
try {
ContentManager cm = myAgent.getContentManager();
ContentElementList cel =
(ContentElementList)cm.extractContent(accept);
Costs costs = (Costs)cel.get(1);
Book book = costs.getItem();
float price = costs.getPrice();
PriceManager pm = (PriceManager)catalogue.get(book.getTitle());
if(pm != null && pm.getCurrentPrice() <= price){
// The requested book is available for sale. Reply with the price
catalogue.remove(book.getTitle());
pm.stop();
myGui.notifyUser("Book " + book.getTitle()
+ " has been sold to "
+ accept.getSender() + ".");
reply.setPerformative(ACLMessage.INFORM);
reply.setContent(accept.getContent());
}
else{
// The requested book is NOT available for sale .
reply.setPerformative(ACLMessage.FAILURE);
}
}
catch (OntologyException oe) {
oe.printStackTrace();
reply.setPerformative(ACLMessage.FAILURE);
myGui.notifyUser("accept proposal from "
+ accept.getSender()
+ " can't be understood.");
}
catch (CodecException ce) {
ce.printStackTrace();
reply.setPerformative(ACLMessage.FAILURE);
myGui.notifyUser("accept proposal from "
+ accept.getSender()
+ " can't be understood.");
}
return reply;
}
});
}
else {
block();
}
}
}//End of inner class CallForOfferServer
/**
Inner class PurchaseOrderServer.
This is the behaviour used by Book-seller agents to serve
incoming purchase order from buyer agents.
If the indicated book is in the local catalogue, the seller agent
replies with a INFORM message specifying the price. Otherwise
a REFUSE message is sent back.
*/
/*private class PurchaseOrderServer extends CyclicBehaviour{
private MessageTemplate mt = MessageTemplate.and(
MessageTemplate.MatchProtocol("fipa-contract-net"),
MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL));
public void action(){
ACLMessage msg = myAgent.receive(mt);
if(msg != null){
// Message received.Process it
ACLMessage reply = msg.createReply();
try {
ContentManager cm = myAgent.getContentManager();
ContentElementList cel =
(ContentElementList)cm.extractContent(msg);
Costs costs = (Costs)cel.get(1);
Book book = costs.getItem();
float price = costs.getPrice();
PriceManager pm = (PriceManager)catalogue.get(book.getTitle());
if(pm != null && pm.getCurrentPrice() <= price){
// The requested book is available for sale. Reply with the price
reply.setPerformative(ACLMessage.INFORM);
reply.setContent(msg.getContent());
}
else{
// The requested book is NOT available for sale .
reply.setPerformative(ACLMessage.FAILURE);
}
}
catch (OntologyException oe) {
oe.printStackTrace();
reply.setPerformative(ACLMessage.FAILURE);
myGui.notifyUser("accept proposal from "
+ msg.getSender()
+ " can't be understood.");
}
catch (CodecException ce) {
ce.printStackTrace();
reply.setPerformative(ACLMessage.FAILURE);
myGui.notifyUser("accept proposal from "
+ msg.getSender()
+ " can't be understood.");
}
myAgent.send(reply);
}
else {
block();
}
}
}//End of inner class PurchaseOrderServer
*/}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -