Skip to content

Commit

Permalink
Assignment 4 schedule and aggregation of order #12
Browse files Browse the repository at this point in the history
most messages working
added definition of messages
  • Loading branch information
Dr4gonbl4de committed Nov 19, 2018
1 parent a44e627 commit 8eaf6da
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 111 deletions.
54 changes: 54 additions & 0 deletions docs/messages/order-processing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# OrderProcessingAgent
## Messages received:
* Receive Order from client:
* Type: CFP
* Sender: Client
* Receiver: All OrderProcessingAgents
* Content: JSONObject as String -> Order
* Receive accepted proposal:
* Type: ACCEPT_PROPOSAL
* Sender: Client
* Receiver: OrderProcessingAgent
* Content: JSONObject as String -> Full Order but only containing products available at specified bakery
* Receive Reject proposal:
* Type: REJECT_PROPOSAL
* Sender: Client
* Receiver: not chosen OrderProcessingAgent
* Content: String -> "rejected"
* Confirm Schedule:
* Type: CONFIRM
* Sender: SchedulerAgent
* Receiver: OrderProcessingAgent
* Content: String -> Scheduling possible
* Disconfirm Schedule:
* Type: DISCONFIRM
* Sender: SchedulerAgent
* Receiver: OrderProcessingAgent
* Content: String -> Scheduling impossible

## Messages sent:
* Proposal to Client:
* Type: PROPOSAL
* Sender: OrderProcessingAgent
* Receiver: Client
* Content: JSONObject as String -> List of available products with prices (amount of product times sales_price for product)
* Refusal to Client:
* Type: REFUSE
* Sender: OrderProcessingAgent
* Receiver: Client
* Content: String -> reason for refusal (no needed product available or not enough time to produce order)
* Confirmation of order:
* Type: CONFIRM
* Sender: OrderProcessingAgent
* Receiver: client
* Content: String -> "order confirmed"
* Check Scheduler:
* Type: REQUEST
* Sender: OrderProcessingAgent
* Receiver: SchedulerAgent
* Content: JSONObject -> order with only available products
* Propagate accepted Orders:
* Type: PROPAGATE
* Sender: OrderProcessingAgent
* Receiver: allAgents
* Content: JSONArray -> sorted List of all received Orders -> according bakeryid
24 changes: 24 additions & 0 deletions docs/messages/scheduler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SchedulerAgent
## Messages received:
* Disconfirm Schedule:
* Type: DISCONFIRM
* Sender: SchedulerAgent
* Receiver: OrderProcessingAgent
* Content: String -> Scheduling impossible
* Propagate accepted Orders:
* Type: PROPAGATE
* Sender: OrderProcessingAgent
* Receiver: allAgents
* Content: JSONObject -> "sorterOrder": JSONArray containing sorted List of all received and scheduled Orders -> "bakeryId": according bakeryid
* Check Scheduler:
* Type: REQUEST
* Sender: OrderProcessingAgent
* Receiver: SchedulerAgent
* Content: JSONObject -> order with only available products

## Messages sent:
* Confirm Schedule:
* Type: CONFIRM
* Sender: SchedulerAgent
* Receiver: OrderProcessingAgent
* Content: String -> Scheduling possible
6 changes: 3 additions & 3 deletions src/main/java/org/team_pjt/agents/ClientDummy.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected void setup(){
System.out.println("No parameters given for ClientDummy " + getName());
}
register("customer", guid);
ordersSent = new LinkedList<>();
addBehaviour(new OrderTimeChecker());
}

Expand Down Expand Up @@ -92,7 +93,6 @@ public void action() {
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));
Expand Down Expand Up @@ -128,8 +128,8 @@ private void sendCallForProposal() {
for(AID agent : orderProcessingAgents) {
cfp.addReceiver(agent);
}
myAgent.send(cfp);
lastSendMessage = cfp;
sendMessage(cfp);
System.out.println("day: " + getCurrentDay() + " hour: " + getCurrentHour() + " cfp send " + order.getGuid());
}

private void receiveProposals() {
Expand Down
116 changes: 35 additions & 81 deletions src/main/java/org/team_pjt/agents/OrderProcessing.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected void setup(){
this.register("OrderProcessing", this.sBakeryId);
findScheduler();
addBehaviour(new OfferRequestServer());
addBehaviour(new distributeOrder());
System.out.println("OrderProcessing " + getName() + " ready");

}
Expand Down Expand Up @@ -90,12 +91,25 @@ private boolean checkForAvailableProducts(List<String> neededProducts) {
return bFeasibleOrder;
}

private void addOrderToQueue() {

}

private void distributeOrder() {
private class distributeOrder extends CyclicBehaviour {

@Override
public void action() {
MessageTemplate acceptedProposalMT = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
ACLMessage accepted_proposal = receive(acceptedProposalMT);
if(accepted_proposal != null) {
findAllAgents();
ACLMessage propagate_accepted_order = new ACLMessage(ACLMessage.PROPAGATE);
propagate_accepted_order.setContent(accepted_proposal.getContent());
for(AID agent : allAgents) {
propagate_accepted_order.addReceiver(agent);
}
sendMessage(propagate_accepted_order);
}
else {
block();
}
}
}

private boolean readArgs(Object[] oArgs){
Expand Down Expand Up @@ -133,6 +147,7 @@ private void sendNotFeasibleMessage(ACLMessage msg, String content) {
clientReply.setPerformative(ACLMessage.REFUSE);
clientReply.setContent(content);
myAgent.send(clientReply);
System.out.println("not feasible message sent");
}

@Override
Expand All @@ -144,33 +159,41 @@ public void action() {
MessageTemplate cfpMT = MessageTemplate.MatchPerformative(ACLMessage.CFP);
ACLMessage cfpMsg = myAgent.receive(cfpMT);
if(cfpMsg != null) {
System.out.println("cfp received");
Order order = new Order(cfpMsg.getContent());
List<String> order_av_products = new LinkedList<>(order.getProducts().keySet());
bFeasibleOrder = checkForAvailableProducts(order_av_products);
System.out.println("checked available products");

if(!bFeasibleOrder) {
sendNotFeasibleMessage(cfpMsg, "No needed Product available!");
System.out.println("no product available");
return;
}

ACLMessage schedulerRequest = new ACLMessage(ACLMessage.REQUEST);
Hashtable<String, Integer> order_products = order.getProducts();

for(String product_name : order_products.keySet()) {
Iterator<String> product_iterator = order_products.keySet().iterator();
while(product_iterator.hasNext()) {
String product_name = product_iterator.next();
if(!order_av_products.contains(product_name)) {
order_products.remove(product_name);
product_iterator.remove();
}
}

order.setProducts(order_products);
schedulerRequest.setConversationId(order.getGuid());
schedulerRequest.setContent(order.toJSONString());
myAgent.send(schedulerRequest);
schedulerRequest.addReceiver(aidScheduler);
sendMessage(schedulerRequest);

System.out.println("asked scheduler for feasibility");

MessageTemplate schedulerReply = MessageTemplate.and(MessageTemplate.MatchConversationId(order.getGuid()),
MessageTemplate.MatchSender(aidScheduler));
ACLMessage schedulerMessage = myAgent.receive(schedulerReply);
if(schedulerMessage != null) {
System.out.println("schedule reply received!");
if(schedulerMessage.getPerformative() == ACLMessage.CONFIRM) {
ACLMessage proposeMsg = cfpMsg.createReply();
proposeMsg.setPerformative(ACLMessage.PROPOSE);
Expand All @@ -185,23 +208,8 @@ public void action() {
proposeObject.put("products", products);
proposeMsg.setContent(proposeObject.toString());
proposeMsg.setConversationId(order.getGuid());
myAgent.send(proposeMsg);

MessageTemplate proposalReplyMT = MessageTemplate.and(MessageTemplate.MatchConversationId(order.getGuid()),
MessageTemplate.MatchSender(cfpMsg.getSender()));
ACLMessage proposalReply = myAgent.receive(proposalReplyMT);
if(proposalReply != null) {
if(proposalReply.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) {

// send to all Agents, send to schedule to addToQueue
}
else if (proposalReply.getPerformative() == ACLMessage.REJECT_PROPOSAL) {
return;
}
}
else {
block();
}
sendMessage(proposeMsg);
System.out.println("proposed available products");
}
else if(schedulerMessage.getPerformative() == ACLMessage.DISCONFIRM) {
bFeasibleOrder = false;
Expand All @@ -220,58 +228,4 @@ else if(schedulerMessage.getPerformative() == ACLMessage.DISCONFIRM) {
}
}
}
}

/*
Needed Messages
* Receive Order from client:
* Type: CFP
* Sender: Client
* Receiver: All OrderProcessingAgents
* Content: JSONObject as String -> Order
* Proposal to Client:
* Type: PROPOSAL
* Sender: OrderProcessingAgent
* Receiver: client who send order
* Content: JSONObject as String -> List of available products with prices (amount of product times sales_price for product)
* Refusal to Client:
* Type: REFUSE
* Sender: OrderProcessingAgent
* Receiver: client who send order
* Content: String -> reason for refusal (no needed product available or not enough time to produce order)
* Receive accepted proposal:
* Type: ACCEPT_PROPOSAL
* Sender: Client
* Receiver: chosen OrderProcessingAgent
* Content: JSONObject as String -> List of Products which should be produced by chosen bakery
* Receive Reject proposal:
* Type: REJECT_PROPOSAL
* Sender: Client
* Receiver: not chosen OrderProcessingAgent
* Content: String -> "rejected"
* Confirmation of order:
* Type: CONFIRM
* Sender: OrderProcessingAgent
* Receiver: client who send order
* Content: String -> "order confirmed"
* Check Scheduler:
* Type: REQUEST
* Sender: OrderProcessingAgent
* Receiver: SchedulerAgent
* Content: JSONObject -> order with only available products
* Confirm Schedule:
* Type: CONFIRM
* Sender: SchedulerAgent
* Receiver: OrderProcessingAgent
* Content: String -> Scheduling possible
* Disconfirm Schedule:
* Type: DISCONFIRM
* Sender: SchedulerAgent
* Receiver: OrderProcessingAgent
* Content: String -> Scheduling impossible
* Propagate accepted Orders:
* Type: PROPAGATE
* Sender: SchedulerAgent
* Receiver: allAgents
* Content: JSONArray -> sorted List of all received Orders
*/
}
Loading

0 comments on commit 8eaf6da

Please sign in to comment.