From f6ca60fecd990d12bffa10b20b54feeafb1622b5 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Sun, 9 Jun 2013 18:42:19 +0200 Subject: [PATCH] [XVSM] Some performance improvements for the simulation --- .../group/gui/DeliveryOverviewModel.java | 19 ++++ .../sbc/valesriegler/xvsm/CookXVSM.java | 5 +- .../sbc/valesriegler/xvsm/DriverXVSM.java | 87 +++++++++++++++---- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 6 +- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 78 +---------------- .../sbc/valesriegler/xvsm/driver/Driver.java | 1 + .../sbc/valesriegler/xvsm/waiter/Waiter.java | 7 +- 7 files changed, 102 insertions(+), 101 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java index 9dbab65..f793948 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java @@ -10,6 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Collection; import java.util.List; public class DeliveryOverviewModel extends TableModel { @@ -84,4 +85,22 @@ public class DeliveryOverviewModel extends TableModel { } fireTableDataChanged(); } + + public void createStatistics() { + int size; + int finished; + synchronized (items) { + final Collection values = items.values(); + size = values.size(); + finished = 0; + for (DeliveryGroup group : values) { + final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); + if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { + finished++; + } + } + } + log.info("{} deliveries were ordered", size); + log.info("{} were finished", finished); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java index 9a70174..5f7154c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java @@ -3,7 +3,6 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.*; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; -import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListener; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.core.TransactionReference; @@ -90,7 +89,7 @@ public class CookXVSM extends AbstractXVSMConnector { } public void listenForDeliveryPizzas() { - SpaceListener pizzasListener = getDefaultBuilder().setLookaround(true).setCref(prepareDeliveryPizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setLookaround(true).setCref(prepareDeliveryPizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -98,7 +97,7 @@ public class CookXVSM extends AbstractXVSMConnector { List pizzas = castEntries(entries); - if (inNotification.get() || Util.runSimulation) Collections.shuffle(pizzas); + if (inNotification.get()) Collections.shuffle(pizzas); for (PizzaOrder pizzaOrder : pizzas) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java index 6d8a8c0..343b278 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java @@ -5,8 +5,9 @@ import at.ac.tuwien.sbc.valesriegler.common.Bill; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; +import at.ac.tuwien.sbc.valesriegler.types.Order; +import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; -import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListener; import org.mozartspaces.core.ContainerReference; import org.mozartspaces.core.MzsConstants; import org.mozartspaces.core.TransactionReference; @@ -28,17 +29,16 @@ public class DriverXVSM extends AbstractXVSMConnector { this.driverId = id; - this.preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); - this.deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); - this.deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER); + preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); + deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); + deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER); pizzeriaDeliveryContainer = useContainer(Util.PIZZERIA_DELIVERY); - deliveryDone = useContainer(Util.DELIVERY_DONE); - deliveryInProgress = useContainer(Util.DELIVERY_PROGRESS); + } public void listenForPreparedDeliveryOrders() { - SpaceListener orderListener = getDefaultBuilder().setCref(deliverDeliveryOrderContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(deliverDeliveryOrderContainer).setLookaround(true).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -55,18 +55,16 @@ public class DriverXVSM extends AbstractXVSMConnector { try { // Get lock for delivering this delivery order - takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error); + final DeliveryGroupData deliveryGroup = takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error); final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.INFINITE, "Cannot get the delivery order!"); group.setDriverId(driverId); group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS); final List groups = Arrays.asList(group); - sendItemsToContainer(groups, - deliveryInProgress, MzsConstants.RequestTimeout.ZERO, null); - sendItemsToContainer(groups, - pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); +// sendItemsToContainer(groups, +// pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); - if (! Util.runSimulation) { + if (!Util.runSimulation) { Thread.sleep(3000); } @@ -83,11 +81,12 @@ public class DriverXVSM extends AbstractXVSMConnector { } if (success) { group.setDeliveryStatus(DeliveryStatus.DELIVERED); - sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null); + if (!Util.runSimulation) { + sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null); + } } else { group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); } - sendItemsToContainer(groups, deliveryDone, MzsConstants.RequestTimeout.DEFAULT, tx); sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); @@ -102,4 +101,62 @@ public class DriverXVSM extends AbstractXVSMConnector { } }).createSpaceListenerImpl(); } + + public void listenForPreparedDeliveryPizzas() { + getDefaultBuilder().setCref(preparedDeliveryPizzasContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + + @Override + public void onEntriesWritten(List entries) + throws Exception { + + List pizzas = castEntries(entries); + + for (Pizza pizza : pizzas) { + int orderId = pizza.getOrderId(); + Order order = new Order(); + order.setId(orderId); + + TransactionReference tx = getDefaultTransaction(); + + try { + DeliveryGroupData entity = new DeliveryGroupData(); + entity.setDeliveryStatus(null); + entity.setOrder(order); + + final DeliveryGroupData groupData = takeMatchingEntity(entity, + deliveryOrderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT, + "Another driver just checks if the delivery order is complete"); + int numberOfPizzas = groupData.getOrder().getNumberOfPizzas(); + + Pizza pizzaTemplate = new Pizza(); + pizzaTemplate.setOrderId(orderId); + + List pizzasOfOrder = takeMatchingEntities( + pizzaTemplate, preparedDeliveryPizzasContainer, tx, + MzsConstants.RequestTimeout.DEFAULT, + "Cannot take the pizzas from the preparedDeliveryPizzasContainer"); + + if (pizzasOfOrder.size() == numberOfPizzas) { + final List groupsWithCompleteOrder = Arrays.asList(groupData); + sendItemsToContainer(groupsWithCompleteOrder, + deliverDeliveryOrderContainer, MzsConstants.RequestTimeout.DEFAULT, + tx); + + capi.commitTransaction(tx); + } else { + log.info("Not yet all pizzas prepared! Order with id " + + orderId + " has " + numberOfPizzas + + " pizzas, but only " + pizzasOfOrder.size() + + " pizzas are ready by now!"); + capi.rollbackTransaction(tx); + } + } catch (NullPointerException e) { + + } catch (Exception e) { + capi.rollbackTransaction(tx); + } + } + } + }).createSpaceListenerImpl(); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java index de1fc00..a227e06 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java @@ -118,8 +118,8 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { List pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA); List pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA); - final String pizzeria1 = "9875"; - final String pizzeria2 = "9874"; + final String pizzeria1 = "9874"; + final String pizzeria2 = "9875"; List groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4); List groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3); List groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2); @@ -144,7 +144,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { log.info("ATTENTION: It's all over now!"); notificationMgr.shutdown(); -// System.exit(1); + deliveryModel.createStatistics(); } }).start(); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java index 916052e..87c88d7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java @@ -46,11 +46,13 @@ public class WaiterXVSM extends AbstractXVSMConnector { } public void listenForPhoneOrders() { - SpaceListener phoneListener = getDefaultBuilder().setCref(phoneCallsContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(phoneCallsContainer).setLookaround(true).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List phoneOrders = castEntries(entries); +// if(inNotification.get()) Collections.shuffle(phoneOrders); + for (DeliveryGroupData phoneOrder : phoneOrders) { final int id = phoneOrder.getId(); final DeliveryGroupData template = new DeliveryGroupData(id); @@ -342,80 +344,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } - public void listenForPreparedDeliveryPizzas() { - /** - * A waiter gets informed when a new pizza is complete. He takes the - * orderId of the pizza and looks up the corresponding order from which - * he gets the number of necessary pizzas of the order. He then tries to - * fetch all pizzas with the corresponding orderId and compares the - * number of those pizzas with the number of necessary pizzas. If all - * pizzas of an order are complete he then delivers them! - */ - getDefaultBuilder().setCref(preparedDeliveryPizzasContainer).setLookaround(true).setSpaceAction(new SpaceAction() { - - @Override - public void onEntriesWritten(List entries) - throws Exception { - - List pizzas = castEntries(entries); - - for (Pizza pizza : pizzas) { - int orderId = pizza.getOrderId(); - Order order = new Order(); - order.setId(orderId); - - TransactionReference tx = getDefaultTransaction(); - - try { - DeliveryGroupData entity = new DeliveryGroupData(); - entity.setDeliveryStatus(null); - entity.setOrder(order); - - takeMatchingEntity(entity, - deliveryOrderTakenContainer, tx, RequestTimeout.DEFAULT, - "Another driver just checks if the delivery order is complete"); - DeliveryGroupData groupData = takeMatchingEntity(entity, - pizzeriaDeliveryContainer, tx, RequestTimeout.INFINITE, - "Waiter cannot take the delivery order from Space!"); - int groupId = groupData.getId(); - int numberOfPizzas = groupData.getOrder().getNumberOfPizzas(); - - Pizza pizzaTemplate = new Pizza(); - pizzaTemplate.setOrderId(orderId); - - List pizzasOfOrder = takeMatchingEntities( - pizzaTemplate, preparedDeliveryPizzasContainer, tx, - RequestTimeout.DEFAULT, - "Cannot take the pizzas from the preparedDeliveryPizzasContainer"); - - if (pizzasOfOrder.size() == numberOfPizzas) { - final List groupsWithCompleteOrder = Arrays.asList(groupData); - sendItemsToContainer(groupsWithCompleteOrder, - deliverDeliveryOrderContainer, RequestTimeout.DEFAULT, - tx); - sendItemsToContainer(groupsWithCompleteOrder, - pizzeriaDeliveryContainer, RequestTimeout.DEFAULT, - tx); - - capi.commitTransaction(tx); - } else { - log.info("Not yet all pizzas prepared! Order with id " - + orderId + " has " + numberOfPizzas - + " pizzas, but only " + pizzasOfOrder.size() - + " pizzas are ready by now!"); - capi.rollbackTransaction(tx); - } - } catch (NullPointerException e) { - - } catch (Exception e) { - capi.rollbackTransaction(tx); - } - } - } - }).createSpaceListenerImpl(); - } - - private void assignGroupToTable(GroupData lockedGroup, Table lockedFreeTable, TransactionReference tx) throws MzsCoreException { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java index 37de548..0eedfe5 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java @@ -25,6 +25,7 @@ public class Driver { private void start() { xvsm = new DriverXVSM(id, port); xvsm.listenForPreparedDeliveryOrders(); + xvsm.listenForPreparedDeliveryPizzas(); } public Driver(int id, int port) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java index d75b750..bce0b1b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java @@ -1,13 +1,12 @@ package at.ac.tuwien.sbc.valesriegler.xvsm.waiter; -import java.io.Serializable; - import at.ac.tuwien.sbc.valesriegler.common.Tuple; import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.xvsm.WaiterXVSM; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.xvsm.WaiterXVSM; +import java.io.Serializable; /** * This is a waiter using XVSM @@ -46,8 +45,6 @@ public class Waiter implements Serializable { xvsm.listenForFreeTable(); xvsm.listenForPhoneOrders(); - - xvsm.listenForPreparedDeliveryPizzas(); } -- 2.43.0