Skip to content

Commit

Permalink
Assignment 4 schedule and aggregation of order #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr4gonbl4de committed Nov 19, 2018
1 parent 4f78ba1 commit cfe104f
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 101 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/team_pjt/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,21 @@ public static List<String> buildCMD(List<String> agents, JSONArray jaBakeries, J
for (String a : agents) {
if(isHost){
if(a.contains("Scheduler")){
appendAgentAndArguments(sb, bakery.toString().replaceAll(",", "###"), a);
appendAgentAndArguments(sb, bakery.toString().replaceAll(",", "###") + "," + joMeta.toString().replaceAll(",", "###"), a);
sb.append(";");
continue;
}
if(a.contains("OrderProcessing")) {
bakery = (JSONObject)bakery_iterator.next();
appendAgentAndArguments(sb, bakery.toString().replaceAll(",", "###"), a);
appendAgentAndArguments(sb, bakery.toString().replaceAll(",", "###") + "," + joMeta.toString().replaceAll(",", "###"), a);
sb.append(";");
continue;
}
}
else {
if(a.contains("Client")){
JSONObject client = (JSONObject)customer_iterator.next();
appendAgentAndArguments(sb, client.toString().replaceAll(",", "###"), a);
appendAgentAndArguments(sb, client.toString().replaceAll(",", "###") + "," + joMeta.toString().replaceAll(",", "###"), a);
sb.append(";");
continue;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/team_pjt/agents/BaseAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void action(){
currentDay = day;
currentHour = hour;
allowAction = true;
finished();
}
else {
block();
Expand Down
130 changes: 123 additions & 7 deletions src/main/java/org/team_pjt/agents/ClientDummy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.core.behaviours.WakerBehaviour;
Expand All @@ -18,25 +19,27 @@
import org.team_pjt.objects.Location;
import org.team_pjt.objects.Order;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

public class ClientDummy extends BaseAgent {
private AID[] aidSchedulerAgents;
private List<Order> orders;
private List<Order> ordersToSent;
private List<Order> ordersSent;
private List<Order> ordersReceived;
private String guid;
private String name;
private int type;
private Location location;
private int endDays;

protected void setup(){
super.setup();
Object[] oArguments = getArguments();
if(!readArgs(oArguments)){
System.out.println("No parameters given for ClientDummy " + getName());
}
register("customer", guid);
addBehaviour(new shutdown());
addBehaviour(new OrderTimeChecker());
}

private boolean readArgs(Object[] oArguments) {
Expand All @@ -49,16 +52,129 @@ private boolean readArgs(Object[] oArguments) {
this.type = joClient.getInt("type");
this.location = new Location(joClient.getJSONObject("location").getDouble("y"),joClient.getJSONObject("location").getDouble("x"));

orders = new LinkedList<>();
ordersToSent = new LinkedList<>();

Iterator<Object> order_iterator = joClient.getJSONArray("orders").iterator();
while(order_iterator.hasNext()) {
JSONObject joOrder = (JSONObject)order_iterator.next();
orders.add(new Order(joOrder.toString()));
ordersToSent.add(new Order(joOrder.toString()));
}

Collections.sort(ordersToSent, new Comparator<Order>() {
@Override
public int compare(Order o1, Order o2) {
if(o1.getOrderDay() < o2.getOrderDay()) {
return -1;
}
else if(o1.getOrderDay() == o2.getOrderDay() && o1.getOrderHour() < o2.getOrderHour()) {
return -1;
}
else if(o1.getOrderDay() == o2.getOrderDay() && o1.getOrderHour() == o2.getOrderHour()) {
return 0;
}
return 1;
}
});

JSONObject meta_data = new JSONObject(((String)oArguments[1]).replaceAll("###", ","));
this.endDays = meta_data.getInt("durationInDays");

return true;
}
return false;
}

private class OrderTimeChecker extends CyclicBehaviour {

@Override
public void action() {
if(getCurrentDay() >= endDays) {
System.out.println("system shutdown!");
addBehaviour(new shutdown());
}
System.out.println(getCurrentDay() + " - " + getCurrentHour());
if(ordersToSent.get(0).getOrderDay() == getCurrentDay() && ordersToSent.get(0).getOrderHour() == getCurrentHour()) {
myAgent.addBehaviour(new RequestPerformer(ordersToSent.get(0)));
ordersSent.add(ordersToSent.remove(0));
}
}
}

private class RequestPerformer extends OneShotBehaviour {
private AID[] orderProcessingAgents;
private Order order;
private Hashtable<String, Hashtable<String, Double>> proposedPrices;
private ACLMessage lastSendMessage;
private ACLMessage lastReceivedMessage;
// private int proposalStep = 0;

public RequestPerformer(Order order) {
super();
proposedPrices = new Hashtable<>();
this.order = order;
}

@Override
public void action() {
findOrderProcessingAgents();
sendCallForProposal();
receiveProposals();
}

private void sendCallForProposal() {
ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
cfp.setConversationId(order.getGuid());
cfp.setContent(order.toJSONString());
for(AID agent : orderProcessingAgents) {
cfp.addReceiver(agent);
}
myAgent.send(cfp);
lastSendMessage = cfp;
}

private void receiveProposals() {
int proposalCounter = 0;
MessageTemplate proposalTemplate = MessageTemplate.or(MessageTemplate.MatchPerformative(ACLMessage.PROPOSE),
MessageTemplate.MatchPerformative(ACLMessage.REFUSE));
ACLMessage proposal = myAgent.receive(proposalTemplate);
while(proposalCounter != orderProcessingAgents.length) {
if (proposal != null) {
proposalCounter++;
if(proposal.getPerformative() == ACLMessage.REFUSE) {
System.out.println("Bakery " + proposal.getSender() + " refused call for proposal with message:");
System.out.println(proposal.getContent());
}
else {
Hashtable<String, Double> available_products = new Hashtable<>();
JSONObject order = new JSONObject(proposal.getContent());
for(String product_name : order.getJSONObject("products").keySet()) {
available_products.put(product_name, order.getJSONObject("products").getDouble(product_name));
}
proposedPrices.put(proposal.getSender().getName(), available_products);
}
} else {
block();
}
}
}

private void findOrderProcessingAgents() {
DFAgentDescription template = new DFAgentDescription();
ServiceDescription sd = new ServiceDescription();
sd.setType("OrderProcessing");
template.addServices(sd);
try {
DFAgentDescription[] result = DFService.search(myAgent, template);
orderProcessingAgents = new AID[result.length];
for (int i = 0; i < result.length; ++i) {
orderProcessingAgents[i] = result[i].getName();
}
}
catch (FIPAException fe) {
System.out.println("Error searching OrderProcessingAgents");
fe.printStackTrace();
}
System.out.println(orderProcessingAgents.length + " OrderProcessingAgents found!");
}
}
}
Loading

0 comments on commit cfe104f

Please sign in to comment.