From 85b7e2f37cfb2482dbc6eae8ad8d872e20057b55 Mon Sep 17 00:00:00 2001 From: janl1992 Date: Fri, 30 Nov 2018 16:12:39 +0100 Subject: [PATCH] Parse bug in Start class fixed. First draft of Assignment 5 #17 --- src/main/java/org/team_pjt/Start.java | 12 +- .../org/team_pjt/agents/DoughManager.java | 688 ++++++++++++++++++ .../mas_maas/agents/DoughManager.java | 628 ---------------- .../objects/DoughPreparationTable.java | 8 + .../org/team_pjt/objects/KneadingMachine.java | 8 + 5 files changed, 715 insertions(+), 629 deletions(-) create mode 100644 src/main/java/org/team_pjt/agents/DoughManager.java delete mode 100644 src/main/java/org/team_pjt/doughprep/mas_maas/agents/DoughManager.java create mode 100644 src/main/java/org/team_pjt/objects/DoughPreparationTable.java create mode 100644 src/main/java/org/team_pjt/objects/KneadingMachine.java diff --git a/src/main/java/org/team_pjt/Start.java b/src/main/java/org/team_pjt/Start.java index 6eb9029..3ec5c3d 100644 --- a/src/main/java/org/team_pjt/Start.java +++ b/src/main/java/org/team_pjt/Start.java @@ -25,6 +25,7 @@ public class Start { private static final String sSchPrefix2 = ":org.team_pjt.agents.SchedulerAgent"; private static final String sCPrefix = ":org.team_pjt.agents.ClientDummy"; private static final String sTPrefix = "timekeeper:org.team_pjt.agents.TimeKeeper"; + private static final String sDougManagerPrefix =":org.team_pjt.agents.DoughManager"; private static List agents = new Vector<>(); public static void main(String[] args) { @@ -58,7 +59,10 @@ public static void main(String[] args) { String bakery_idNum = id.split("-")[1]; agents.add(id + sOPPrefix); agents.add("scheduler-" + bakery_idNum + sSchPrefix2); - + agents.add("doughmanager-" + bakery_idNum + sDougManagerPrefix); +// JSONArray jsaDoughPrepTables = bakery.getJSONArray("doughPrepTables"); +// Iterator jsaIterator = jsaDoughPrepTables.iterator(); +// while(jsaIterator.hasNext()) } } @@ -108,6 +112,12 @@ public static List buildCMD(List agents, JSONArray jaBakeries, J sb.append(";"); continue; } + if(a.contains("doughmanager")){ +// bakery = (JSONObject)bakery_iterator.next(); + appendAgentAndArguments(sb, bakery.toString().replaceAll(",", "###"), a); + sb.append(";"); + continue; + } } else { if(a.contains("Client")){ diff --git a/src/main/java/org/team_pjt/agents/DoughManager.java b/src/main/java/org/team_pjt/agents/DoughManager.java new file mode 100644 index 0000000..b7f5ef6 --- /dev/null +++ b/src/main/java/org/team_pjt/agents/DoughManager.java @@ -0,0 +1,688 @@ +package org.team_pjt.agents; + +import com.google.gson.Gson; +import jade.core.AID; +import jade.core.behaviours.Behaviour; +import jade.core.behaviours.CyclicBehaviour; +import jade.domain.DFService; +import jade.domain.FIPAAgentManagement.DFAgentDescription; +import jade.domain.FIPAAgentManagement.ServiceDescription; +import jade.domain.FIPAException; +import jade.lang.acl.ACLMessage; +import jade.lang.acl.MessageTemplate; +import org.json.JSONArray; +import org.json.JSONObject; +import org.team_pjt.doughprep.mas_maas.JSONConverter; +import org.team_pjt.doughprep.mas_maas.agents.BaseAgent; +import org.team_pjt.doughprep.mas_maas.messages.*; +import org.team_pjt.doughprep.mas_maas.objects.*; +import org.team_pjt.objects.DoughPreparationTable; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Scanner; +import java.util.Vector; + + +public class DoughManager extends BaseAgent { + private Order order; + private String sIdBakery; + private Vector vKneadingMachine; + private Vector vDoughPrepTable; + private JSONArray jsaProducts; + private HashMap> hmRecipeProducts; + protected void setup() { + super.setup(); + Object[] oArguments = getArguments(); + if (!readArgs(oArguments)) { + System.out.println("No parameters given for DoughManager" + getName()); + } + System.out.println(getAID().getLocalName() + " is ready."); + this.register("Dough-manager", "JADE-bakery"); + addBehaviour(new receiveOrder()); + } + + private boolean readArgs(Object [] o) { + if(o!= null){ + vKneadingMachine = new Vector<>(); + JSONObject jsoBakery = new JSONObject(o[0].toString().replace("###",",")); + sIdBakery = jsoBakery.getString("guid"); + JSONObject jsoEquipment = (JSONObject) jsoBakery.get("equipment"); + readDoughPreparationTable(jsoEquipment); + readKneadingMachines(jsoEquipment); + readRecipes(jsoBakery); + return true; + } else { + return false; + } + + } + + private void readKneadingMachines(JSONObject jsoEquipment) { + Iterator iKneadingMachines = jsoEquipment.getJSONArray("kneadingMachines").iterator(); + while(iKneadingMachines.hasNext()){ + JSONObject jsoKneadingMachine = (JSONObject) iKneadingMachines.next(); + vKneadingMachine.add(new org.team_pjt.objects.KneadingMachine(jsoKneadingMachine.getString("guid"))); + } + } + + private void readDoughPreparationTable(JSONObject jsoEquipment) { + vDoughPrepTable = new Vector<>(); + Iterator iDoughPrepTables = jsoEquipment.getJSONArray("doughPrepTables").iterator(); + while(iDoughPrepTables.hasNext()){ + JSONObject jsoDoughPrepTablenext = (JSONObject) iDoughPrepTables.next(); + vDoughPrepTable.add(new DoughPreparationTable(jsoDoughPrepTablenext.getString("guid"))); + } + } + + private void readRecipes(JSONObject jsoBakery) { + jsaProducts = jsoBakery.getJSONArray("products"); + Iterator iJsaProducts = jsaProducts.iterator(); + hmRecipeProducts = new HashMap<>(); + while(iJsaProducts.hasNext()){ + JSONObject jsoProductInformation = (JSONObject) iJsaProducts.next(); + String sProduct = jsoProductInformation.getString("guid"); + JSONObject jsoRecipe = (JSONObject) jsoProductInformation.get("recipe"); + Iterator iSteps = jsoRecipe.getJSONArray("steps").iterator(); + HashMap hmRecipe = new HashMap<>(); + while(iSteps.hasNext()){ + JSONObject jsoRecipeInfo = (JSONObject) iSteps.next(); + if (jsoRecipeInfo.getString("action").equals("kneading") || jsoRecipeInfo.getString("action").equals("item preparation") || jsoRecipeInfo.getString("action").equals("resting")) { + hmRecipe.put(jsoRecipeInfo.getString("action"), jsoRecipeInfo.getInt("duration")); + } + } + hmRecipeProducts.put(sProduct, hmRecipe); + } + } + + private class receiveOrder extends CyclicBehaviour { + @Override + public void action() { +// @ToDo finished() aufrufen und isAllowedAction() aufrufen + MessageTemplate acceptedProposalMT = MessageTemplate.MatchPerformative(ACLMessage.PROPAGATE); + ACLMessage aclReceive = receive(acceptedProposalMT); + if(aclReceive != null ){ + String sContent = aclReceive.getContent(); + Vector bakedGoods = new Vector(); + JSONArray jsaArray = null; + JSONObject jsoObject = null; + jsaArray = new JSONArray(sContent); + Iterator iArray = jsaArray.iterator(); + while (iArray.hasNext()) { + jsoObject = (JSONObject) iArray.next(); +// System.out.println("The following order was received: " + jsoObject.toString()); + JSONObject jsoDeliveryDate = jsoObject.getJSONObject("deliveryDate"); + JSONObject jsoOrderDate = jsoObject.getJSONObject("orderDate"); + JSONObject jsoProducts = jsoObject.getJSONObject("products"); + Iterator iKeys = jsoProducts.keys(); + while(iKeys.hasNext()){ + String sNext = iKeys.next(); + jsoProducts.get(sNext); + bakedGoods.add(new BakedGood(sNext, (Integer) jsoProducts.get(sNext))); + } + order = new Order(jsoObject.getString("customerId"), jsoObject.getString("guid"), jsoOrderDate.getInt("day"), jsoOrderDate.getInt("hour"), jsoDeliveryDate.getInt("day"), jsoDeliveryDate.getInt("hour"), bakedGoods); + calculateKneadingAndPreparationTime(bakedGoods); + } + } else { + block(); + } + } + + private void calculateKneadingAndPreparationTime(Vector bakedGoods) { + Iterator iBakedGoods = bakedGoods.iterator(); + while(iBakedGoods.hasNext()){ + BakedGood bgNext = iBakedGoods.next(); + hmRecipeProducts.get(bgNext.getName()); + } + } + } + +// protected void takeDown() { +// System.out.println(getAID().getLocalName() + ": Terminating."); +// this.deRegister(); +// } +// +// public void queueOrder(Order order) { +// // Add productStatus to the needsKneading WorkQueue +// +// for(BakedGood bakedGood : order.getBakedGoods()) { +// +// String guid = order.getGuid(); +// String status = NEEDS_KNEADING; +// int amount = bakedGood.getAmount(); +// Product product = bakery.findProduct(bakedGood.getName()); +// ProductStatus productStatus = new ProductStatus(guid, status, amount, product); +// +// needsKneading.addProduct(productStatus); +// +// } +// } +// +// public KneadingRequest createKneadingRequestMessage() { +// // Checks the needsKneading workqueue and creates a KneadingRequestMessage +// +// Vector products = needsKneading.getProductBatch(); +// KneadingRequest kneadingRequest = null; +// +// if (products != null) { +// +// Vector guids = new Vector(); +// +// for (ProductStatus productStatus : products) { +// guids.add(productStatus.getGuid()); +// +// } +// String productType = products.get(0).getProduct().getGuid(); +// float kneadingTime = products.get(0).getProduct().getRecipe().getActionTime(Step.KNEADING_TIME); +// +// kneadingRequest = new KneadingRequest(guids, productType, kneadingTime); +// } +// +// return kneadingRequest; +// } +// +// public void queuePreparation(String productType, Vector guids ) { +// // Add productStatus to the needsPreparation WorkQueue +// +// for (String guid : guids) { +// +// int amount = -1; +// String status = NEEDS_PREPARATION; +// Product product = bakery.findProduct(productType); +// Order order = orders.get(guid); +// +// for(BakedGood bakedGood : order.getBakedGoods()) { +// if (bakedGood.getName().equals(productType)) { +// amount = bakedGood.getAmount(); +// } +// +// } +// ProductStatus productStatus = new ProductStatus(guid, status, amount, product); +// needsPreparation.addProduct(productStatus); +// } +// } +// +// public PreparationRequest createPreparationRequestMessage() { +// // Checks the needsPreparaion WorkQueue and creates a preparationRequestMessage +// Vector products = needsPreparation.getProductBatch(); +// +// PreparationRequest preparationRequest = null; +// +// if (products != null) { +// +// Vector guids = new Vector(); +// Vector productQuantities = new Vector(); +// Vector steps = new Vector(); +// +// +// +// for (ProductStatus productStatus : products) { +// guids.add(productStatus.getGuid()); +// productQuantities.add(productStatus.getAmount()); +// } +// +// String productType = products.get(0).getProduct().getGuid(); +// steps = products.get(0).getProduct().getRecipe().getPreparationSteps(); +// +// preparationRequest = new PreparationRequest(guids, productType, productQuantities, steps); +// } +// +// return preparationRequest; +// +// } +// +// public void queueProofing(String productType, Vector guids ) { +// // Add productStatus to the needsProofing WorkQueue +// +// for (String guid : guids) { +// +// int amount = -1; +// String status = NEEDS_PROOFING; +// Product product = bakery.findProduct(productType); +// Order order = orders.get(guid); +// +// for(BakedGood bakedGood : order.getBakedGoods()) { +// if (bakedGood.getName().equals(productType)) { +// amount = bakedGood.getAmount(); +// } +// +// } +// ProductStatus productStatus = new ProductStatus(guid, status, amount, product); +// needsProofing.addProduct(productStatus); +// } +// } +// +// public ProofingRequest createProofingRequestMessage() { +// // Checks the needsProofing WorkQueue and creates a proofingRequestMessage +// Vector products = needsProofing.getProductBatch(); +// +// ProofingRequest proofingRequest = null; +// +// if (products != null) { +// +// Vector guids = new Vector(); +// Vector productQuantities = new Vector(); +// +// for (ProductStatus productStatus : products) { +// guids.add(productStatus.getGuid()); +// productQuantities.add(productStatus.getAmount()); +// } +// +// String productType = products.get(0).getProduct().getGuid(); +// +// float proofingTime = products.get(0).getProduct().getRecipe().getActionTime(Step.PROOFING_TIME); +// +// proofingRequest = new ProofingRequest(productType, guids, proofingTime, productQuantities); +// } +// +// return proofingRequest; +// +// } +// +// public void getbakery(){ +// +// String jsonDir = "src/main/resources/config/dough_stage_communication/"; +// try { +// System.out.println("Working Directory = " + System.getProperty("user.dir")); +// String bakeryFile = new Scanner(new File(jsonDir + "bakery.json")).useDelimiter("\\Z").next(); +// Vector bakeries = JSONConverter.parseBakeries(bakeryFile); +// for (Bakery bakery : bakeries) +// { +// this.bakery = bakery; +// } +// } catch (FileNotFoundException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } +// +// public void getOrderProcessingAIDs() { +// DFAgentDescription template = new DFAgentDescription(); +// ServiceDescription sd = new ServiceDescription(); +// +// sd.setType("Order-processing"); +// template.addServices(sd); +// try { +// DFAgentDescription [] result = DFService.search(this, template); +// System.out.println("Found the following Order-processing agents:"); +// orderProcessingAgents = new AID [result.length]; +// +// for (int i = 0; i < result.length; ++i) { +// orderProcessingAgents[i] = result[i].getName(); +// System.out.println(orderProcessingAgents[i].getName()); +// } +// +// } +// catch (FIPAException fe) { +// fe.printStackTrace(); +// } +// } +// +// public void getProoferAIDs() { +// DFAgentDescription template = new DFAgentDescription(); +// ServiceDescription sd = new ServiceDescription(); +// +// sd.setType("Proofer"); +// template.addServices(sd); +// try { +// DFAgentDescription [] result = DFService.search(this, template); +// System.out.println("Found the following Proofer agents:"); +// prooferAgents = new AID [result.length]; +// +// for (int i = 0; i < result.length; ++i) { +// prooferAgents[i] = result[i].getName(); +// System.out.println(prooferAgents[i].getName()); +// } +// +// } +// catch (FIPAException fe) { +// fe.printStackTrace(); +// } +// } +// +// public void getPreparationTableAIDS() { +// DFAgentDescription template = new DFAgentDescription(); +// ServiceDescription sd = new ServiceDescription(); +// +// sd.setType("Preparation-table"); +// template.addServices(sd); +// try { +// DFAgentDescription [] result = DFService.search(this, template); +// System.out.println("Found the following Preparation-table agents:"); +// preparationTableAgents = new AID [result.length]; +// +// for (int i = 0; i < result.length; ++i) { +// preparationTableAgents[i] = result[i].getName(); +// System.out.println(preparationTableAgents[i].getName()); +// } +// +// } +// catch (FIPAException fe) { +// fe.printStackTrace(); +// } +// } +// +// public void getKneadingMachineAIDs() { +// DFAgentDescription template = new DFAgentDescription(); +// ServiceDescription sd = new ServiceDescription(); +// +// sd.setType("Kneading-machine"); +// template.addServices(sd); +// try { +// DFAgentDescription [] result = DFService.search(this, template); +// System.out.println("Found the following Kneading-machine agents:"); +// kneadingMachineAgents = new AID [result.length]; +// +// for (int i = 0; i < result.length; ++i) { +// kneadingMachineAgents[i] = result[i].getName(); +// System.out.println(kneadingMachineAgents[i].getName()); +// } +// +// } +// catch (FIPAException fe) { +// fe.printStackTrace(); +// } +// } +// +// /* This is the behavior used for receiving orders */ +// private class ReceiveOrders extends CyclicBehaviour { +// public void action() { +// // baseAgent.finished(); //call it if there are no generic behaviours +// MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM); +// ACLMessage msg = myAgent.receive(mt); +// if (msg != null) { +// ACLMessage reply = msg.createReply(); +// reply.setPerformative(ACLMessage.CONFIRM); +// reply.setContent("Order was received"); +// baseAgent.sendMessage(reply); +// // TODO convert String to order object +// // Add the order to the HashMap of orders. Trigger the doughPreparation (send Kneading request, etc) +// +// } +// else { +// block(); +// } +// } +// } +// +// /* This is the behavior used for receiving kneading notification messages */ +// private class ReceiveKneadingNotification extends CyclicBehaviour { +// public void action() { +// +// MessageTemplate mt = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), +// MessageTemplate.MatchConversationId("kneading-notification")); +// +// ACLMessage msg = baseAgent.receive(mt); +// +// if (msg != null) { +// +// System.out.println("-------> " + getAID().getLocalName()+" Received Kneading Notification from " + msg.getSender()); +// +// String kneadingNotificationString = msg.getContent(); +// +// System.out.println("-----> Kneading notification " + kneadingNotificationString); +// +// ACLMessage reply = msg.createReply(); +// +// reply.setPerformative(ACLMessage.CONFIRM); +// +// reply.setContent("Kneading Notification was received"); +// +// baseAgent.sendMessage(reply); +// +// // Convert kneadingNotificationString to kneadingNotification object +// KneadingNotification kneadingNotification = JSONConverter.parseKneadingNotification(kneadingNotificationString); +// +// String productType = kneadingNotification.getProductType(); +// +// Vector guids = kneadingNotification.getGuids(); +// +// System.out.println("-----> product type " + productType); +// +// System.out.println("-----> guid " + guids); +// +// // Add guids with this productType to the queuePreparation +// queuePreparation(productType, guids); +// +// // Create preparationRequestMessage with the information in the queuePreparation +// PreparationRequest preparationRequestMessage = createPreparationRequestMessage(); +// +// // Convert preparationRequestMessage to String +// Gson gson = new Gson(); +// +// String preparationRequestString = gson.toJson(preparationRequestMessage); +// +// // Send preparationRequestMessage +// addBehaviour(new RequestPreparation(preparationRequestString, preparationTableAgents)); +// +// } +// else { +// block(); +// } +// } +// } +// +// /* This is the behaviour used for receiving preparation notification */ +// private class ReceivePreparationNotification extends CyclicBehaviour { +// public void action() { +// // baseAgent.finished(); //call it if there are no generic behaviours +// +// MessageTemplate mt = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), +// MessageTemplate.MatchConversationId("preparation-notification")); +// +// ACLMessage msg = baseAgent.receive(mt); +// +// if (msg != null) { +// +// System.out.println("-------> " + getAID().getLocalName()+" Received Preparation Notification from " + msg.getSender()); +// +// String preparationNotificationString = msg.getContent(); +// +// ACLMessage reply = msg.createReply(); +// +// reply.setPerformative(ACLMessage.CONFIRM); +// +// reply.setContent("Preparation Notification was received"); +// +// baseAgent.sendMessage(reply); +// +// // Convert preparationNotificationString to preparationNotification object +// +// PreparationNotification preparationNotification = JSONConverter.parsePreparationNotification(preparationNotificationString); +// +// String productType = preparationNotification.getProductType(); +// Vector guids = preparationNotification.getGuids(); +// +// // Add guids with this productType to the queueProofing +// queueProofing(productType, guids); +// +// // Create proofingRequestMessage with the information in the queueProofing +// ProofingRequest proofingRequestMessage = createProofingRequestMessage(); +// +// // Convert proofingRequestMessage to String +// Gson gson = new Gson(); +// String proofingRequestString = gson.toJson(proofingRequestMessage); +// +// // Send preparationRequestMessage +// addBehaviour(new RequestProofing(proofingRequestString, prooferAgents)); +// +// +// } +// else { +// block(); +// } +// } +// +// +// } +// +// //This is the behaviour used for sensing a KneadingRequest +// private class RequestKneading extends Behaviour{ +// private String kneadingRequest; +// private AID [] kneadingMachineAgents; +// private MessageTemplate mt; +// // private ACLMessage msg; +// private int step = 0; +// +// public RequestKneading(String kneadingRequest, AID [] kneadingMachineAgents){ +// this.kneadingRequest = kneadingRequest; +// this.kneadingMachineAgents = kneadingMachineAgents; +// } +// public void action(){ +// //blocking action +// if (!baseAgent.getAllowAction()) { +// return; +// } +// switch(step){ +// case 0: +// +// ACLMessage msg = new ACLMessage(ACLMessage.INFORM); +// msg.setContent(kneadingRequest); +// msg.setConversationId("kneading-request"); +// +// // Send kneadingRequest msg to all kneadingMachineAgents +// for (int i=0; i orders = new HashMap(); - private static final String NEEDS_KNEADING = "needsKneading"; - private static final String NEEDS_PREPARATION = "needsPreparation"; - private static final String NEEDS_PROOFING = "needsProofing"; - private Order order; - - protected void setup() { - super.setup(); - System.out.println(getAID().getLocalName() + " is ready."); - this.register("Dough-manager", "JADE-bakery"); - addBehaviour(new receiveOrder()); - } - - private class receiveOrder extends CyclicBehaviour { - @Override - public void action() { -// @ToDo finished() aufrufen und isAllowedAction() aufrufen - MessageTemplate acceptedProposalMT = MessageTemplate.MatchPerformative(ACLMessage.PROPAGATE); - ACLMessage aclReceive = receive(acceptedProposalMT); - JSONArray jsaArray = null; - JSONObject jsoObject = null; - Vector bakedGoods = new Vector(); - if(aclReceive != null ){ - String sContent = aclReceive.getContent(); - jsaArray = new JSONArray(sContent); - Iterator iArray = jsaArray.iterator(); - while (iArray.hasNext()) { - jsoObject = (JSONObject) iArray.next(); - System.out.println("The following order was received: " + jsoObject.toString()); - JSONObject jsoDeliveryDate = jsoObject.getJSONObject("deliveryDate"); - JSONObject jsoOrderDate = jsoObject.getJSONObject("orderDate"); - JSONObject jsoProducts = jsoObject.getJSONObject("products"); - Iterator iKeys = jsoProducts.keys(); - while(iKeys.hasNext()){ - String sNext = iKeys.next(); - jsoProducts.get(sNext); - bakedGoods.add(new BakedGood(sNext, (Integer) jsoProducts.get(sNext))); - } - order = new Order(jsoObject.getString("customerId"), jsoObject.getString("guid"), jsoOrderDate.getInt("day"), jsoOrderDate.getInt("hour"), jsoDeliveryDate.getInt("day"), jsoDeliveryDate.getInt("hour"), bakedGoods); - } - } else { - block(); - } - } - } - - protected void takeDown() { - System.out.println(getAID().getLocalName() + ": Terminating."); - this.deRegister(); - } - - public void queueOrder(Order order) { - // Add productStatus to the needsKneading WorkQueue - - for(BakedGood bakedGood : order.getBakedGoods()) { - - String guid = order.getGuid(); - String status = NEEDS_KNEADING; - int amount = bakedGood.getAmount(); - Product product = bakery.findProduct(bakedGood.getName()); - ProductStatus productStatus = new ProductStatus(guid, status, amount, product); - - needsKneading.addProduct(productStatus); - - } - } - - public KneadingRequest createKneadingRequestMessage() { - // Checks the needsKneading workqueue and creates a KneadingRequestMessage - - Vector products = needsKneading.getProductBatch(); - KneadingRequest kneadingRequest = null; - - if (products != null) { - - Vector guids = new Vector(); - - for (ProductStatus productStatus : products) { - guids.add(productStatus.getGuid()); - - } - String productType = products.get(0).getProduct().getGuid(); - float kneadingTime = products.get(0).getProduct().getRecipe().getActionTime(Step.KNEADING_TIME); - - kneadingRequest = new KneadingRequest(guids, productType, kneadingTime); - } - - return kneadingRequest; - } - - public void queuePreparation(String productType, Vector guids ) { - // Add productStatus to the needsPreparation WorkQueue - - for (String guid : guids) { - - int amount = -1; - String status = NEEDS_PREPARATION; - Product product = bakery.findProduct(productType); - Order order = orders.get(guid); - - for(BakedGood bakedGood : order.getBakedGoods()) { - if (bakedGood.getName().equals(productType)) { - amount = bakedGood.getAmount(); - } - - } - ProductStatus productStatus = new ProductStatus(guid, status, amount, product); - needsPreparation.addProduct(productStatus); - } - } - - public PreparationRequest createPreparationRequestMessage() { - // Checks the needsPreparaion WorkQueue and creates a preparationRequestMessage - Vector products = needsPreparation.getProductBatch(); - - PreparationRequest preparationRequest = null; - - if (products != null) { - - Vector guids = new Vector(); - Vector productQuantities = new Vector(); - Vector steps = new Vector(); - - - - for (ProductStatus productStatus : products) { - guids.add(productStatus.getGuid()); - productQuantities.add(productStatus.getAmount()); - } - - String productType = products.get(0).getProduct().getGuid(); - steps = products.get(0).getProduct().getRecipe().getPreparationSteps(); - - preparationRequest = new PreparationRequest(guids, productType, productQuantities, steps); - } - - return preparationRequest; - - } - - public void queueProofing(String productType, Vector guids ) { - // Add productStatus to the needsProofing WorkQueue - - for (String guid : guids) { - - int amount = -1; - String status = NEEDS_PROOFING; - Product product = bakery.findProduct(productType); - Order order = orders.get(guid); - - for(BakedGood bakedGood : order.getBakedGoods()) { - if (bakedGood.getName().equals(productType)) { - amount = bakedGood.getAmount(); - } - - } - ProductStatus productStatus = new ProductStatus(guid, status, amount, product); - needsProofing.addProduct(productStatus); - } - } - - public ProofingRequest createProofingRequestMessage() { - // Checks the needsProofing WorkQueue and creates a proofingRequestMessage - Vector products = needsProofing.getProductBatch(); - - ProofingRequest proofingRequest = null; - - if (products != null) { - - Vector guids = new Vector(); - Vector productQuantities = new Vector(); - - for (ProductStatus productStatus : products) { - guids.add(productStatus.getGuid()); - productQuantities.add(productStatus.getAmount()); - } - - String productType = products.get(0).getProduct().getGuid(); - - float proofingTime = products.get(0).getProduct().getRecipe().getActionTime(Step.PROOFING_TIME); - - proofingRequest = new ProofingRequest(productType, guids, proofingTime, productQuantities); - } - - return proofingRequest; - - } - - public void getbakery(){ - - String jsonDir = "src/main/resources/config/dough_stage_communication/"; - try { - System.out.println("Working Directory = " + System.getProperty("user.dir")); - String bakeryFile = new Scanner(new File(jsonDir + "bakery.json")).useDelimiter("\\Z").next(); - Vector bakeries = JSONConverter.parseBakeries(bakeryFile); - for (Bakery bakery : bakeries) - { - this.bakery = bakery; - } - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public void getOrderProcessingAIDs() { - DFAgentDescription template = new DFAgentDescription(); - ServiceDescription sd = new ServiceDescription(); - - sd.setType("Order-processing"); - template.addServices(sd); - try { - DFAgentDescription [] result = DFService.search(this, template); - System.out.println("Found the following Order-processing agents:"); - orderProcessingAgents = new AID [result.length]; - - for (int i = 0; i < result.length; ++i) { - orderProcessingAgents[i] = result[i].getName(); - System.out.println(orderProcessingAgents[i].getName()); - } - - } - catch (FIPAException fe) { - fe.printStackTrace(); - } - } - - public void getProoferAIDs() { - DFAgentDescription template = new DFAgentDescription(); - ServiceDescription sd = new ServiceDescription(); - - sd.setType("Proofer"); - template.addServices(sd); - try { - DFAgentDescription [] result = DFService.search(this, template); - System.out.println("Found the following Proofer agents:"); - prooferAgents = new AID [result.length]; - - for (int i = 0; i < result.length; ++i) { - prooferAgents[i] = result[i].getName(); - System.out.println(prooferAgents[i].getName()); - } - - } - catch (FIPAException fe) { - fe.printStackTrace(); - } - } - - public void getPreparationTableAIDS() { - DFAgentDescription template = new DFAgentDescription(); - ServiceDescription sd = new ServiceDescription(); - - sd.setType("Preparation-table"); - template.addServices(sd); - try { - DFAgentDescription [] result = DFService.search(this, template); - System.out.println("Found the following Preparation-table agents:"); - preparationTableAgents = new AID [result.length]; - - for (int i = 0; i < result.length; ++i) { - preparationTableAgents[i] = result[i].getName(); - System.out.println(preparationTableAgents[i].getName()); - } - - } - catch (FIPAException fe) { - fe.printStackTrace(); - } - } - - public void getKneadingMachineAIDs() { - DFAgentDescription template = new DFAgentDescription(); - ServiceDescription sd = new ServiceDescription(); - - sd.setType("Kneading-machine"); - template.addServices(sd); - try { - DFAgentDescription [] result = DFService.search(this, template); - System.out.println("Found the following Kneading-machine agents:"); - kneadingMachineAgents = new AID [result.length]; - - for (int i = 0; i < result.length; ++i) { - kneadingMachineAgents[i] = result[i].getName(); - System.out.println(kneadingMachineAgents[i].getName()); - } - - } - catch (FIPAException fe) { - fe.printStackTrace(); - } - } - - /* This is the behavior used for receiving orders */ - private class ReceiveOrders extends CyclicBehaviour { - public void action() { - // baseAgent.finished(); //call it if there are no generic behaviours - MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM); - ACLMessage msg = myAgent.receive(mt); - if (msg != null) { - ACLMessage reply = msg.createReply(); - reply.setPerformative(ACLMessage.CONFIRM); - reply.setContent("Order was received"); - baseAgent.sendMessage(reply); - // TODO convert String to order object - // Add the order to the HashMap of orders. Trigger the doughPreparation (send Kneading request, etc) - - } - else { - block(); - } - } - } - - /* This is the behavior used for receiving kneading notification messages */ - private class ReceiveKneadingNotification extends CyclicBehaviour { - public void action() { - - MessageTemplate mt = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), - MessageTemplate.MatchConversationId("kneading-notification")); - - ACLMessage msg = baseAgent.receive(mt); - - if (msg != null) { - - System.out.println("-------> " + getAID().getLocalName()+" Received Kneading Notification from " + msg.getSender()); - - String kneadingNotificationString = msg.getContent(); - - System.out.println("-----> Kneading notification " + kneadingNotificationString); - - ACLMessage reply = msg.createReply(); - - reply.setPerformative(ACLMessage.CONFIRM); - - reply.setContent("Kneading Notification was received"); - - baseAgent.sendMessage(reply); - - // Convert kneadingNotificationString to kneadingNotification object - KneadingNotification kneadingNotification = JSONConverter.parseKneadingNotification(kneadingNotificationString); - - String productType = kneadingNotification.getProductType(); - - Vector guids = kneadingNotification.getGuids(); - - System.out.println("-----> product type " + productType); - - System.out.println("-----> guid " + guids); - - // Add guids with this productType to the queuePreparation - queuePreparation(productType, guids); - - // Create preparationRequestMessage with the information in the queuePreparation - PreparationRequest preparationRequestMessage = createPreparationRequestMessage(); - - // Convert preparationRequestMessage to String - Gson gson = new Gson(); - - String preparationRequestString = gson.toJson(preparationRequestMessage); - - // Send preparationRequestMessage - addBehaviour(new RequestPreparation(preparationRequestString, preparationTableAgents)); - - } - else { - block(); - } - } - } - - /* This is the behaviour used for receiving preparation notification */ - private class ReceivePreparationNotification extends CyclicBehaviour { - public void action() { - // baseAgent.finished(); //call it if there are no generic behaviours - - MessageTemplate mt = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), - MessageTemplate.MatchConversationId("preparation-notification")); - - ACLMessage msg = baseAgent.receive(mt); - - if (msg != null) { - - System.out.println("-------> " + getAID().getLocalName()+" Received Preparation Notification from " + msg.getSender()); - - String preparationNotificationString = msg.getContent(); - - ACLMessage reply = msg.createReply(); - - reply.setPerformative(ACLMessage.CONFIRM); - - reply.setContent("Preparation Notification was received"); - - baseAgent.sendMessage(reply); - - // Convert preparationNotificationString to preparationNotification object - - PreparationNotification preparationNotification = JSONConverter.parsePreparationNotification(preparationNotificationString); - - String productType = preparationNotification.getProductType(); - Vector guids = preparationNotification.getGuids(); - - // Add guids with this productType to the queueProofing - queueProofing(productType, guids); - - // Create proofingRequestMessage with the information in the queueProofing - ProofingRequest proofingRequestMessage = createProofingRequestMessage(); - - // Convert proofingRequestMessage to String - Gson gson = new Gson(); - String proofingRequestString = gson.toJson(proofingRequestMessage); - - // Send preparationRequestMessage - addBehaviour(new RequestProofing(proofingRequestString, prooferAgents)); - - - } - else { - block(); - } - } - - - } - - //This is the behaviour used for sensing a KneadingRequest - private class RequestKneading extends Behaviour{ - private String kneadingRequest; - private AID [] kneadingMachineAgents; - private MessageTemplate mt; - // private ACLMessage msg; - private int step = 0; - - public RequestKneading(String kneadingRequest, AID [] kneadingMachineAgents){ - this.kneadingRequest = kneadingRequest; - this.kneadingMachineAgents = kneadingMachineAgents; - } - public void action(){ - //blocking action - if (!baseAgent.getAllowAction()) { - return; - } - switch(step){ - case 0: - - ACLMessage msg = new ACLMessage(ACLMessage.INFORM); - msg.setContent(kneadingRequest); - msg.setConversationId("kneading-request"); - - // Send kneadingRequest msg to all kneadingMachineAgents - for (int i=0; i