Reformatted code
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
*
|
||||||
|
!build.xml
|
||||||
|
!jade
|
||||||
|
!src
|
||||||
|
!.gitignore
|
||||||
@@ -10,14 +10,16 @@ import jade.domain.FIPAException;
|
|||||||
import jade.domain.FIPAAgentManagement.DFAgentDescription;
|
import jade.domain.FIPAAgentManagement.DFAgentDescription;
|
||||||
import jade.domain.FIPAAgentManagement.ServiceDescription;
|
import jade.domain.FIPAAgentManagement.ServiceDescription;
|
||||||
|
|
||||||
public class BookBuyerAgent extends Agent {
|
public class BookBuyerAgent extends Agent
|
||||||
|
{
|
||||||
private BookBuyerGui myGui;
|
private BookBuyerGui myGui;
|
||||||
private String targetBookTitle;
|
private String targetBookTitle;
|
||||||
|
|
||||||
//list of found sellers
|
//list of found sellers
|
||||||
private AID[] sellerAgents;
|
private AID[] sellerAgents;
|
||||||
|
|
||||||
protected void setup() {
|
protected void setup()
|
||||||
|
{
|
||||||
targetBookTitle = "";
|
targetBookTitle = "";
|
||||||
System.out.println("Hello! " + getAID().getLocalName() + " is ready for the purchase order.");
|
System.out.println("Hello! " + getAID().getLocalName() + " is ready for the purchase order.");
|
||||||
myGui = new BookBuyerGui(this);
|
myGui = new BookBuyerGui(this);
|
||||||
@@ -75,24 +77,29 @@ public class BookBuyerAgent extends Agent {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void takeDown() {
|
protected void takeDown()
|
||||||
|
{
|
||||||
myGui.dispose();
|
myGui.dispose();
|
||||||
System.out.println("Buyer agent " + getAID().getLocalName() + " terminated.");
|
System.out.println("Buyer agent " + getAID().getLocalName() + " terminated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private class RequestPerformer extends Behaviour {
|
private class RequestPerformer extends Behaviour
|
||||||
|
{
|
||||||
private AID bestSeller;
|
private AID bestSeller;
|
||||||
private int bestPrice;
|
private int bestPrice;
|
||||||
private int repliesCnt = 0;
|
private int repliesCnt = 0;
|
||||||
private MessageTemplate mt;
|
private MessageTemplate mt;
|
||||||
private int step = 0;
|
private int step = 0;
|
||||||
|
|
||||||
public void action() {
|
public void action()
|
||||||
switch (step) {
|
{
|
||||||
|
switch (step)
|
||||||
|
{
|
||||||
case 0:
|
case 0:
|
||||||
//call for proposal (CFP) to found sellers
|
//call for proposal (CFP) to found sellers
|
||||||
ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
|
ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
|
||||||
for (int i = 0; i < sellerAgents.length; ++i) {
|
for (int i = 0; i < sellerAgents.length; ++i)
|
||||||
|
{
|
||||||
cfp.addReceiver(sellerAgents[i]);
|
cfp.addReceiver(sellerAgents[i]);
|
||||||
}
|
}
|
||||||
cfp.setContent(targetBookTitle);
|
cfp.setContent(targetBookTitle);
|
||||||
@@ -106,23 +113,28 @@ public class BookBuyerAgent extends Agent {
|
|||||||
case 1:
|
case 1:
|
||||||
//collect proposals
|
//collect proposals
|
||||||
ACLMessage reply = myAgent.receive(mt);
|
ACLMessage reply = myAgent.receive(mt);
|
||||||
if (reply != null) {
|
if (reply != null)
|
||||||
if (reply.getPerformative() == ACLMessage.PROPOSE) {
|
{
|
||||||
|
if (reply.getPerformative() == ACLMessage.PROPOSE)
|
||||||
|
{
|
||||||
//proposal received
|
//proposal received
|
||||||
int price = Integer.parseInt(reply.getContent());
|
int price = Integer.parseInt(reply.getContent());
|
||||||
if (bestSeller == null || price < bestPrice) {
|
if (bestSeller == null || price < bestPrice)
|
||||||
|
{
|
||||||
//the best proposal as for now
|
//the best proposal as for now
|
||||||
bestPrice = price;
|
bestPrice = price;
|
||||||
bestSeller = reply.getSender();
|
bestSeller = reply.getSender();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
repliesCnt++;
|
repliesCnt++;
|
||||||
if (repliesCnt >= sellerAgents.length) {
|
if (repliesCnt >= sellerAgents.length)
|
||||||
|
{
|
||||||
//all proposals have been received
|
//all proposals have been received
|
||||||
step = 2;
|
step = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
block();
|
block();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -161,8 +173,10 @@ public class BookBuyerAgent extends Agent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean done() {
|
public boolean done()
|
||||||
if (step == 2 && bestSeller == null) {
|
{
|
||||||
|
if (step == 2 && bestSeller == null)
|
||||||
|
{
|
||||||
System.out.println(getAID().getLocalName() + ": " + targetBookTitle + " is not on sale.");
|
System.out.println(getAID().getLocalName() + ": " + targetBookTitle + " is not on sale.");
|
||||||
}
|
}
|
||||||
//process terminates here if purchase has failed (title not on sale) or book was successfully bought
|
//process terminates here if purchase has failed (title not on sale) or book was successfully bought
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ import java.awt.*;
|
|||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
class BookBuyerGui extends JFrame {
|
class BookBuyerGui extends JFrame
|
||||||
|
{
|
||||||
private BookBuyerAgent myAgent;
|
private BookBuyerAgent myAgent;
|
||||||
|
|
||||||
private JTextField titleField;
|
private JTextField titleField;
|
||||||
|
|
||||||
BookBuyerGui(BookBuyerAgent a) {
|
BookBuyerGui(BookBuyerAgent a)
|
||||||
|
{
|
||||||
super(a.getLocalName());
|
super(a.getLocalName());
|
||||||
|
|
||||||
myAgent = a;
|
myAgent = a;
|
||||||
@@ -24,14 +26,18 @@ class BookBuyerGui extends JFrame {
|
|||||||
getContentPane().add(p, BorderLayout.CENTER);
|
getContentPane().add(p, BorderLayout.CENTER);
|
||||||
|
|
||||||
JButton addButton = new JButton("Search");
|
JButton addButton = new JButton("Search");
|
||||||
addButton.addActionListener( new ActionListener() {
|
addButton.addActionListener( new ActionListener()
|
||||||
public void actionPerformed(ActionEvent ev) {
|
{
|
||||||
try {
|
public void actionPerformed(ActionEvent ev)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
String title = titleField.getText().trim();
|
String title = titleField.getText().trim();
|
||||||
myAgent.lookForTitle(title);
|
myAgent.lookForTitle(title);
|
||||||
titleField.setText("");
|
titleField.setText("");
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e)
|
||||||
|
{
|
||||||
JOptionPane.showMessageDialog(BookBuyerGui.this, "Invalid values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
JOptionPane.showMessageDialog(BookBuyerGui.this, "Invalid values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -40,16 +46,21 @@ class BookBuyerGui extends JFrame {
|
|||||||
p.add(addButton);
|
p.add(addButton);
|
||||||
getContentPane().add(p, BorderLayout.SOUTH);
|
getContentPane().add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
addWindowListener(new WindowAdapter() {
|
addWindowListener(
|
||||||
public void windowClosing(WindowEvent e) {
|
new WindowAdapter()
|
||||||
|
{
|
||||||
|
public void windowClosing(WindowEvent e)
|
||||||
|
{
|
||||||
myAgent.doDelete();
|
myAgent.doDelete();
|
||||||
}
|
}
|
||||||
} );
|
}
|
||||||
|
);
|
||||||
|
|
||||||
setResizable(false);
|
setResizable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void display() {
|
public void display()
|
||||||
|
{
|
||||||
pack();
|
pack();
|
||||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
int centerX = (int)screenSize.getWidth() / 2;
|
int centerX = (int)screenSize.getWidth() / 2;
|
||||||
|
|||||||
@@ -11,11 +11,13 @@ import jade.domain.FIPAAgentManagement.ServiceDescription;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class BookSellerAgent extends Agent {
|
public class BookSellerAgent extends Agent
|
||||||
|
{
|
||||||
private Hashtable catalogue;
|
private Hashtable catalogue;
|
||||||
private BookSellerGui myGui;
|
private BookSellerGui myGui;
|
||||||
|
|
||||||
protected void setup() {
|
protected void setup()
|
||||||
|
{
|
||||||
catalogue = new Hashtable();
|
catalogue = new Hashtable();
|
||||||
myGui = new BookSellerGui(this);
|
myGui = new BookSellerGui(this);
|
||||||
myGui.display();
|
myGui.display();
|
||||||
@@ -27,10 +29,12 @@ public class BookSellerAgent extends Agent {
|
|||||||
sd.setType("book-selling");
|
sd.setType("book-selling");
|
||||||
sd.setName("JADE-book-trading");
|
sd.setName("JADE-book-trading");
|
||||||
dfd.addServices(sd);
|
dfd.addServices(sd);
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
DFService.register(this, dfd);
|
DFService.register(this, dfd);
|
||||||
}
|
}
|
||||||
catch (FIPAException fe) {
|
catch (FIPAException fe)
|
||||||
|
{
|
||||||
fe.printStackTrace();
|
fe.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,77 +43,96 @@ public class BookSellerAgent extends Agent {
|
|||||||
addBehaviour(new PurchaseOrdersServer());
|
addBehaviour(new PurchaseOrdersServer());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void takeDown() {
|
protected void takeDown()
|
||||||
|
{
|
||||||
//book selling service deregistration at DF
|
//book selling service deregistration at DF
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
DFService.deregister(this);
|
DFService.deregister(this);
|
||||||
}
|
}
|
||||||
catch (FIPAException fe) {
|
catch (FIPAException fe)
|
||||||
|
{
|
||||||
fe.printStackTrace();
|
fe.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
myGui.dispose();
|
myGui.dispose();
|
||||||
System.out.println("Seller agent " + getAID().getName() + " terminated.");
|
System.out.println("Seller agent " + getAID().getName() + " terminated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
//invoked from GUI, when a new book is added to the catalogue
|
//invoked from GUI, when a new book is added to the catalogue
|
||||||
public void updateCatalogue(final String title, final int price) {
|
public void updateCatalogue(final String title, final int price)
|
||||||
addBehaviour(new OneShotBehaviour() {
|
{
|
||||||
public void action() {
|
addBehaviour(new OneShotBehaviour()
|
||||||
|
{
|
||||||
|
public void action()
|
||||||
|
{
|
||||||
catalogue.put(title, new Integer(price));
|
catalogue.put(title, new Integer(price));
|
||||||
System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price);
|
System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price);
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
private class OfferRequestsServer extends CyclicBehaviour {
|
private class OfferRequestsServer extends CyclicBehaviour
|
||||||
public void action() {
|
{
|
||||||
|
public void action()
|
||||||
|
{
|
||||||
//proposals only template
|
//proposals only template
|
||||||
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP);
|
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP);
|
||||||
ACLMessage msg = myAgent.receive(mt);
|
ACLMessage msg = myAgent.receive(mt);
|
||||||
if (msg != null) {
|
if (msg != null)
|
||||||
|
{
|
||||||
String title = msg.getContent();
|
String title = msg.getContent();
|
||||||
ACLMessage reply = msg.createReply();
|
ACLMessage reply = msg.createReply();
|
||||||
Integer price = (Integer) catalogue.get(title);
|
Integer price = (Integer) catalogue.get(title);
|
||||||
if (price != null) {
|
if (price != null)
|
||||||
|
{
|
||||||
//title found in the catalogue, respond with its price as a proposal
|
//title found in the catalogue, respond with its price as a proposal
|
||||||
reply.setPerformative(ACLMessage.PROPOSE);
|
reply.setPerformative(ACLMessage.PROPOSE);
|
||||||
reply.setContent(String.valueOf(price.intValue()));
|
reply.setContent(String.valueOf(price.intValue()));
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
//title not found in the catalogue
|
//title not found in the catalogue
|
||||||
reply.setPerformative(ACLMessage.REFUSE);
|
reply.setPerformative(ACLMessage.REFUSE);
|
||||||
reply.setContent("not-available");
|
reply.setContent("not-available");
|
||||||
}
|
}
|
||||||
myAgent.send(reply);
|
myAgent.send(reply);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
block();
|
block();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private class PurchaseOrdersServer extends CyclicBehaviour {
|
private class PurchaseOrdersServer extends CyclicBehaviour
|
||||||
public void action() {
|
{
|
||||||
|
public void action()
|
||||||
|
{
|
||||||
//purchase order as proposal acceptance only template
|
//purchase order as proposal acceptance only template
|
||||||
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
|
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
|
||||||
ACLMessage msg = myAgent.receive(mt);
|
ACLMessage msg = myAgent.receive(mt);
|
||||||
if (msg != null) {
|
if (msg != null)
|
||||||
|
{
|
||||||
String title = msg.getContent();
|
String title = msg.getContent();
|
||||||
ACLMessage reply = msg.createReply();
|
ACLMessage reply = msg.createReply();
|
||||||
Integer price = (Integer) catalogue.remove(title);
|
Integer price = (Integer) catalogue.remove(title);
|
||||||
if (price != null) {
|
if (price != null)
|
||||||
|
{
|
||||||
reply.setPerformative(ACLMessage.INFORM);
|
reply.setPerformative(ACLMessage.INFORM);
|
||||||
System.out.println(getAID().getLocalName() + ": " + title + " sold to " + msg.getSender().getLocalName());
|
System.out.println(getAID().getLocalName() + ": " + title + " sold to " + msg.getSender().getLocalName());
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
//title not found in the catalogue, sold to another agent in the meantime (after proposal submission)
|
//title not found in the catalogue, sold to another agent in the meantime (after proposal submission)
|
||||||
reply.setPerformative(ACLMessage.FAILURE);
|
reply.setPerformative(ACLMessage.FAILURE);
|
||||||
reply.setContent("not-available");
|
reply.setContent("not-available");
|
||||||
}
|
}
|
||||||
myAgent.send(reply);
|
myAgent.send(reply);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
block();
|
block();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ import java.awt.*;
|
|||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
class BookSellerGui extends JFrame {
|
class BookSellerGui extends JFrame
|
||||||
|
{
|
||||||
private BookSellerAgent myAgent;
|
private BookSellerAgent myAgent;
|
||||||
|
|
||||||
private JTextField titleField, priceField;
|
private JTextField titleField, priceField;
|
||||||
|
|
||||||
BookSellerGui(BookSellerAgent a) {
|
BookSellerGui(BookSellerAgent a)
|
||||||
|
{
|
||||||
super(a.getLocalName());
|
super(a.getLocalName());
|
||||||
|
|
||||||
myAgent = a;
|
myAgent = a;
|
||||||
@@ -27,9 +29,11 @@ class BookSellerGui extends JFrame {
|
|||||||
getContentPane().add(p, BorderLayout.CENTER);
|
getContentPane().add(p, BorderLayout.CENTER);
|
||||||
|
|
||||||
JButton addButton = new JButton("Add");
|
JButton addButton = new JButton("Add");
|
||||||
addButton.addActionListener( new ActionListener() {
|
addButton.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
public void actionPerformed(ActionEvent ev) {
|
public void actionPerformed(ActionEvent ev) {
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
String title = titleField.getText().trim();
|
String title = titleField.getText().trim();
|
||||||
String price = priceField.getText().trim();
|
String price = priceField.getText().trim();
|
||||||
myAgent.updateCatalogue(title, Integer.parseInt(price));
|
myAgent.updateCatalogue(title, Integer.parseInt(price));
|
||||||
@@ -45,8 +49,10 @@ class BookSellerGui extends JFrame {
|
|||||||
p.add(addButton);
|
p.add(addButton);
|
||||||
getContentPane().add(p, BorderLayout.SOUTH);
|
getContentPane().add(p, BorderLayout.SOUTH);
|
||||||
|
|
||||||
addWindowListener(new WindowAdapter() {
|
addWindowListener(new WindowAdapter()
|
||||||
public void windowClosing(WindowEvent e) {
|
{
|
||||||
|
public void windowClosing(WindowEvent e)
|
||||||
|
{
|
||||||
myAgent.doDelete();
|
myAgent.doDelete();
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
@@ -54,7 +60,8 @@ class BookSellerGui extends JFrame {
|
|||||||
setResizable(false);
|
setResizable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void display() {
|
public void display()
|
||||||
|
{
|
||||||
pack();
|
pack();
|
||||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
int centerX = (int)screenSize.getWidth() / 2;
|
int centerX = (int)screenSize.getWidth() / 2;
|
||||||
|
|||||||
Reference in New Issue
Block a user