Reformatted code

This commit is contained in:
2023-01-14 16:34:06 +01:00
parent 7bbcfaed36
commit 6659f96257
5 changed files with 306 additions and 246 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
*
!build.xml
!jade
!src
!.gitignore

View File

@@ -10,14 +10,16 @@ import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
public class BookBuyerAgent extends Agent {
public class BookBuyerAgent extends Agent
{
private BookBuyerGui myGui;
private String targetBookTitle;
//list of found sellers
private AID[] sellerAgents;
protected void setup() {
protected void setup()
{
targetBookTitle = "";
System.out.println("Hello! " + getAID().getLocalName() + " is ready for the purchase order.");
myGui = new BookBuyerGui(this);
@@ -75,24 +77,29 @@ public class BookBuyerAgent extends Agent {
});
}
protected void takeDown() {
protected void takeDown()
{
myGui.dispose();
System.out.println("Buyer agent " + getAID().getLocalName() + " terminated.");
}
private class RequestPerformer extends Behaviour {
private class RequestPerformer extends Behaviour
{
private AID bestSeller;
private int bestPrice;
private int repliesCnt = 0;
private MessageTemplate mt;
private int step = 0;
public void action() {
switch (step) {
public void action()
{
switch (step)
{
case 0:
//call for proposal (CFP) to found sellers
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.setContent(targetBookTitle);
@@ -106,23 +113,28 @@ public class BookBuyerAgent extends Agent {
case 1:
//collect proposals
ACLMessage reply = myAgent.receive(mt);
if (reply != null) {
if (reply.getPerformative() == ACLMessage.PROPOSE) {
if (reply != null)
{
if (reply.getPerformative() == ACLMessage.PROPOSE)
{
//proposal received
int price = Integer.parseInt(reply.getContent());
if (bestSeller == null || price < bestPrice) {
if (bestSeller == null || price < bestPrice)
{
//the best proposal as for now
bestPrice = price;
bestSeller = reply.getSender();
}
}
repliesCnt++;
if (repliesCnt >= sellerAgents.length) {
if (repliesCnt >= sellerAgents.length)
{
//all proposals have been received
step = 2;
}
}
else {
else
{
block();
}
break;
@@ -161,8 +173,10 @@ public class BookBuyerAgent extends Agent {
}
}
public boolean done() {
if (step == 2 && bestSeller == null) {
public boolean done()
{
if (step == 2 && bestSeller == null)
{
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

View File

@@ -6,12 +6,14 @@ import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BookBuyerGui extends JFrame {
class BookBuyerGui extends JFrame
{
private BookBuyerAgent myAgent;
private JTextField titleField;
BookBuyerGui(BookBuyerAgent a) {
BookBuyerGui(BookBuyerAgent a)
{
super(a.getLocalName());
myAgent = a;
@@ -24,14 +26,18 @@ class BookBuyerGui extends JFrame {
getContentPane().add(p, BorderLayout.CENTER);
JButton addButton = new JButton("Search");
addButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ev) {
try {
addButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
try
{
String title = titleField.getText().trim();
myAgent.lookForTitle(title);
titleField.setText("");
}
catch (Exception e) {
catch (Exception e)
{
JOptionPane.showMessageDialog(BookBuyerGui.this, "Invalid values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
@@ -40,16 +46,21 @@ class BookBuyerGui extends JFrame {
p.add(addButton);
getContentPane().add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
myAgent.doDelete();
}
} );
}
);
setResizable(false);
}
public void display() {
public void display()
{
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = (int)screenSize.getWidth() / 2;

View File

@@ -11,11 +11,13 @@ import jade.domain.FIPAAgentManagement.ServiceDescription;
import java.util.*;
public class BookSellerAgent extends Agent {
public class BookSellerAgent extends Agent
{
private Hashtable catalogue;
private BookSellerGui myGui;
protected void setup() {
protected void setup()
{
catalogue = new Hashtable();
myGui = new BookSellerGui(this);
myGui.display();
@@ -27,10 +29,12 @@ public class BookSellerAgent extends Agent {
sd.setType("book-selling");
sd.setName("JADE-book-trading");
dfd.addServices(sd);
try {
try
{
DFService.register(this, dfd);
}
catch (FIPAException fe) {
catch (FIPAException fe)
{
fe.printStackTrace();
}
@@ -39,77 +43,96 @@ public class BookSellerAgent extends Agent {
addBehaviour(new PurchaseOrdersServer());
}
protected void takeDown() {
protected void takeDown()
{
//book selling service deregistration at DF
try {
try
{
DFService.deregister(this);
}
catch (FIPAException fe) {
catch (FIPAException fe)
{
fe.printStackTrace();
}
myGui.dispose();
System.out.println("Seller agent " + getAID().getName() + " terminated.");
}
//invoked from GUI, when a new book is added to the catalogue
public void updateCatalogue(final String title, final int price) {
addBehaviour(new OneShotBehaviour() {
public void action() {
public void updateCatalogue(final String title, final int price)
{
addBehaviour(new OneShotBehaviour()
{
public void action()
{
catalogue.put(title, new Integer(price));
System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price);
}
} );
}
private class OfferRequestsServer extends CyclicBehaviour {
public void action() {
private class OfferRequestsServer extends CyclicBehaviour
{
public void action()
{
//proposals only template
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP);
ACLMessage msg = myAgent.receive(mt);
if (msg != null) {
if (msg != null)
{
String title = msg.getContent();
ACLMessage reply = msg.createReply();
Integer price = (Integer) catalogue.get(title);
if (price != null) {
if (price != null)
{
//title found in the catalogue, respond with its price as a proposal
reply.setPerformative(ACLMessage.PROPOSE);
reply.setContent(String.valueOf(price.intValue()));
}
else {
else
{
//title not found in the catalogue
reply.setPerformative(ACLMessage.REFUSE);
reply.setContent("not-available");
}
myAgent.send(reply);
}
else {
else
{
block();
}
}
}
private class PurchaseOrdersServer extends CyclicBehaviour {
public void action() {
private class PurchaseOrdersServer extends CyclicBehaviour
{
public void action()
{
//purchase order as proposal acceptance only template
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
ACLMessage msg = myAgent.receive(mt);
if (msg != null) {
if (msg != null)
{
String title = msg.getContent();
ACLMessage reply = msg.createReply();
Integer price = (Integer) catalogue.remove(title);
if (price != null) {
if (price != null)
{
reply.setPerformative(ACLMessage.INFORM);
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)
reply.setPerformative(ACLMessage.FAILURE);
reply.setContent("not-available");
}
myAgent.send(reply);
}
else {
else
{
block();
}
}

View File

@@ -6,12 +6,14 @@ import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BookSellerGui extends JFrame {
class BookSellerGui extends JFrame
{
private BookSellerAgent myAgent;
private JTextField titleField, priceField;
BookSellerGui(BookSellerAgent a) {
BookSellerGui(BookSellerAgent a)
{
super(a.getLocalName());
myAgent = a;
@@ -27,9 +29,11 @@ class BookSellerGui extends JFrame {
getContentPane().add(p, BorderLayout.CENTER);
JButton addButton = new JButton("Add");
addButton.addActionListener( new ActionListener() {
addButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ev) {
try {
try
{
String title = titleField.getText().trim();
String price = priceField.getText().trim();
myAgent.updateCatalogue(title, Integer.parseInt(price));
@@ -45,8 +49,10 @@ class BookSellerGui extends JFrame {
p.add(addButton);
getContentPane().add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
myAgent.doDelete();
}
} );
@@ -54,7 +60,8 @@ class BookSellerGui extends JFrame {
setResizable(false);
}
public void display() {
public void display()
{
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = (int)screenSize.getWidth() / 2;