package at.ac.tuwien.sbc.valesriegler.group;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import javax.swing.SwingUtilities;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import at.ac.tuwien.sbc.valesriegler.common.Util;
import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest;
import at.ac.tuwien.sbc.valesriegler.types.Order;
import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.swing.*;
+import java.util.*;
/**
* The Main class of the Group component.
}
groupAgent = new GroupAgent();
- if (!Util.runSimulation) {
+ if (! Util.runSimulation) {
groupGui = new GroupGUI();
SwingUtilities.invokeLater(groupGui);
}
package at.ac.tuwien.sbc.valesriegler.group.gui;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import at.ac.tuwien.sbc.valesriegler.common.TableModel;
import at.ac.tuwien.sbc.valesriegler.common.Util;
import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup;
import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-public class DeliveryOverviewModel extends TableModel<DeliveryGroup> {
- private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
-
- private static final String STATE = "State";
- private static final String PIZZAS = "Pizzas";
- private static final String ADDRESS = "Address";
- private static final String ID = "ID";
- private static final String PIZZERIA = "Pizzeria";
- private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA};
-
- @Override
- protected String[] getColumns() {
- return COLUMNS;
- }
-
- @Override
- public void addItems(List<DeliveryGroup> newItems) {
- log.info("addItems()");
- super.addItems(newItems);
- for (DeliveryGroup g : newItems) {
- if (Util.useJMS)
- g.orderSomeFood();
- }
- }
-
- @Override
- public Object getValueAt(int rowIndex, int columnIndex) {
- List<DeliveryGroup> values = new ArrayList<>(items.values());
- DeliveryGroup group = values.get(rowIndex);
- String wantedColumn = COLUMNS[columnIndex];
- switch (wantedColumn) {
- case ID :
- int groupId = group.getId();
- return groupId;
- case ADDRESS :
- return group.getDeliveryGroupData().getAddress();
- case PIZZAS :
- if (group.getDeliveryGroupData().getOrder() == null)
- return "none";
- return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas());
- case STATE :
- return group.getDeliveryGroupData().getDeliveryStatus();
- case PIZZERIA :
- return group.getDeliveryGroupData().getPizzeriaId();
- default :
- throw new RuntimeException(UNHANDLEDCOLUMN);
- }
- }
-
- public void setOrdersTaken(List<DeliveryGroupData> groups) {
- for (DeliveryGroupData group : groups) {
- changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED);
- }
-
- fireTableDataChanged();
- }
-
- private void changeStateOfGroup(int groupId, DeliveryStatus state) {
- items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state);
- }
-
- public void setOrdersDelivered(List<DeliveryGroupData> groups) {
- for (DeliveryGroupData group : groups) {
- changeStateOfGroup(group.getId(), group.getDeliveryStatus());
- }
-
- fireTableDataChanged();
- }
-
- public void addDeliveries(List<DeliveryGroupData> groups) {
- synchronized (items) {
- for (DeliveryGroupData group : groups) {
- final DeliveryGroup deliveryGroup = items.get(group.getId());
- if (deliveryGroup == null) {
- log.error("Delivery group not found!!!");
- }
- deliveryGroup.setDeliveryGroupData(group);
- }
- }
- fireTableDataChanged();
- }
-
- public void createStatistics() {
- int size;
- int finished;
- synchronized (items) {
- final Collection<DeliveryGroup> 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);
- }
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
- public boolean hasFinished() {
- int size;
- int finished;
- synchronized (items) {
- final Collection<DeliveryGroup> 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++;
- }
- }
- }
- return size == finished;
- }
+public class DeliveryOverviewModel extends TableModel<DeliveryGroup> {
+ private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
+
+ private static final String STATE = "State";
+ private static final String PIZZAS = "Pizzas";
+ private static final String ADDRESS = "Address";
+ private static final String ID = "ID";
+ private static final String PIZZERIA = "Pizzeria";
+ private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA};
+
+
+ @Override
+ protected String[] getColumns() {
+ return COLUMNS;
+ }
+
+ @Override
+ public Object getValueAt(int rowIndex, int columnIndex) {
+ List<DeliveryGroup> values = new ArrayList<>(items.values());
+ DeliveryGroup group = values.get(rowIndex);
+ String wantedColumn = COLUMNS[columnIndex];
+ switch (wantedColumn) {
+ case ID:
+ int groupId = group.getId();
+ return groupId;
+ case ADDRESS:
+ return group.getDeliveryGroupData().getAddress();
+ case PIZZAS:
+ if (group.getDeliveryGroupData().getOrder() == null)
+ return "none";
+ return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas());
+ case STATE:
+ return group.getDeliveryGroupData().getDeliveryStatus();
+ case PIZZERIA:
+ return group.getDeliveryGroupData().getPizzeriaId();
+ default:
+ throw new RuntimeException(UNHANDLEDCOLUMN);
+ }
+ }
+
+ public void addDeliveries(List<DeliveryGroupData> groups) {
+ synchronized (items) {
+ for (DeliveryGroupData group : groups) {
+ final DeliveryGroup deliveryGroup = items.get(group.getId());
+ if(deliveryGroup==null) {
+ log.error("Delivery group not found: {}", group.getId());
+ }
+ else deliveryGroup.setDeliveryGroupData(group);
+ }
+ }
+ fireTableDataChanged();
+ }
+
+ public void createStatistics() {
+ int size;
+ int finished;
+ synchronized (items) {
+ final Collection<DeliveryGroup> 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);
+ }
+
+ public boolean hasFinished() {
+ int size;
+ int finished;
+ synchronized (items) {
+ final Collection<DeliveryGroup> 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++;
+ }
+ }
+ }
+ final boolean result = size == finished;
+ return result;
+ }
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
List<Group> values = new ArrayList<>(items.values());
- Group group = values.get(rowIndex);
+ // TODO make sure this is necessary...
+// Collections.sort(values, new Comparator<Group>() {
+// @Override
+// public int compare(Group o1, Group o2) {
+// final int o1Id = o1.getId();
+// final int o2Id = o2.getId();
+// if(o1Id<o2Id) return -1;
+// else if(o1Id>o2Id) return 1;
+// else return 0;
+// }
+// });
+ Group group = values.get(rowIndex);
String wantedColumn = COLUMNS[columnIndex];
switch (wantedColumn) {
case ID:
package at.ac.tuwien.sbc.valesriegler.pizzeria;
-import java.util.List;
-
-import javax.swing.SwingUtilities;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import at.ac.tuwien.sbc.valesriegler.common.Util;
import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
import at.ac.tuwien.sbc.valesriegler.pizzeria.actions.TableNew;
import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame;
-import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryDetailsModel;
-import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryOrdersModel;
-import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel;
-import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel;
-import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel;
-import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewModel;
-import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel;
+import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.*;
import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.JMSPizzeriaConnector;
import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.PizzeriaJMSNACMsgListener;
import at.ac.tuwien.sbc.valesriegler.types.Table;
import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.swing.*;
+import java.util.List;
/**
* The Main class of the Pizzeria compoment.
case "XVSM" :
pizzeriaAgent.createModels();
pizzeriaAgent.initXVSM(args[1]);
- if (!Util.runSimulation) {
+ if (! Util.runSimulation) {
pizzeriaAgent.initGUI();
}
Util.useJMS = false;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
public class DeliveryOrdersModel extends AbstractOrdersModel<DeliveryGroupData> {
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
List<DeliveryGroupData> values = new ArrayList<>(items.values());
+ // TODO: make sure that is necessary
+ Collections.sort(values, new Comparator<DeliveryGroupData>() {
+ @Override
+ public int compare(DeliveryGroupData o1, DeliveryGroupData o2) {
+ final int o1Id = o1.getOrder().getId();
+ final int o2Id = o2.getOrder().getId();
+ if(o1Id==0) {
+ if(o2Id>0) return 1;
+ }
+ if(o2Id==0) {
+ if(o1Id>0) return 1;
+ }
+ if (o1Id < o2Id) return -1;
+ else if (o1Id > o2Id) return 1;
+ else return 0;
+ }
+ });
DeliveryGroupData group = values.get(rowIndex);
Order order = group.getOrder();
String wantedColumn = COLUMNS[columnIndex];
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
public class GroupsOverviewModel extends TableModel<GroupData> {
public Object getValueAt(int rowIndex, int columnIndex) {
List<GroupData> values = new ArrayList<>(items.values());
+ // TODO: make sure that is necessary
+ Collections.sort(values, new Comparator<GroupData>() {
+ @Override
+ public int compare(GroupData o1, GroupData o2) {
+ final int o1Id = o1.getOrder().getId();
+ final int o2Id = o2.getOrder().getId();
+ if (o1Id == 0) {
+ if (o2Id > 0) return 1;
+ }
+ if (o2Id == 0) {
+ if (o1Id > 0) return 1;
+ }
+ if (o1Id < o2Id) return -1;
+ else if (o1Id > o2Id) return 1;
+ else return 0;
+ }
+ });
GroupData group = values.get(rowIndex);
String wantedColumn = COLUMNS[columnIndex];
switch (wantedColumn) {
package at.ac.tuwien.sbc.valesriegler.types;
+import at.ac.tuwien.sbc.valesriegler.common.HasId;
+import at.ac.tuwien.sbc.valesriegler.common.Util;
+import org.mozartspaces.capi3.Queryable;
+
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
-import org.mozartspaces.capi3.Queryable;
-
-import at.ac.tuwien.sbc.valesriegler.common.HasId;
-import at.ac.tuwien.sbc.valesriegler.common.Util;
-
/**
* Object denotes a Group's interest in pizzas and the state of the pizzas
* themselves.
@Override
public int getId() {
- return id;
+ return Util.getIntSafe(id);
}
public List<PizzaOrder> getOrderedPizzas() {
import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction;
import org.mozartspaces.core.ContainerReference;
import org.mozartspaces.core.MzsConstants;
+import org.mozartspaces.core.MzsCoreException;
import org.mozartspaces.core.TransactionReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Override
public void onEntriesWritten(List<? extends Serializable> entries) throws Exception {
- final List<DeliveryGroupData> deliveries = castEntries(entries);
+ handleDeliveries(entries);
- for (DeliveryGroupData delivery : deliveries) {
- DeliveryGroupData template = new DeliveryGroupData(delivery.getId());
- template.setDeliveryStatus(DeliveryStatus.ORDERED);
+ }
+ }).createSpaceListenerImpl();
+ }
- final TransactionReference tx = capi.createTransaction(
- 7000,
- URI.create(String.format(Util.SERVER_ADDR, port)));
- final String error = "Another driver wants to deliver this order!";
+ private void handleDeliveries(List<? extends Serializable> entries) throws MzsCoreException {
+ final List<DeliveryGroupData> deliveries = castEntries(entries);
- try {
- // Get lock for delivering this delivery order
- 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);
+ for (DeliveryGroupData delivery : deliveries) {
+ DeliveryGroupData template = new DeliveryGroupData(delivery.getId());
+ template.setDeliveryStatus(DeliveryStatus.ORDERED);
- final List<DeliveryGroupData> groups = Arrays.asList(group);
-// sendItemsToContainer(groups,
-// pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null);
+ final TransactionReference tx = capi.createTransaction(
+ 7000,
+ URI.create(String.format(Util.SERVER_ADDR, port)));
+ final String error = "Another driver wants to deliver this order!";
- if (!Util.runSimulation) {
- Thread.sleep(3000);
- }
+ try {
+ // Get lock for delivering this delivery order
+ 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 String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT);
- ContainerReference cref = null;
- boolean success = false;
- try {
- cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null);
- log.debug("Looked up container {} successfully!", address);
- success = true;
+ final List<DeliveryGroupData> groups = Arrays.asList(group);
+// sendItemsToContainer(groups,
+// pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null);
- } catch (Exception ex) {
- log.info("Could not find container {}", address);
- }
- if (success) {
- group.setDeliveryStatus(DeliveryStatus.DELIVERED);
- if (!Util.runSimulation) {
- sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null);
- }
- } else {
- group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED);
- }
- sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx);
- capi.commitTransaction(tx);
+ if (!Util.runSimulation) {
+ Thread.sleep(3000);
+ }
+ final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT);
+ ContainerReference cref = null;
+ boolean success = false;
+ try {
+ cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null);
+ log.debug("Looked up container {} successfully!", address);
+ success = true;
- } catch (Exception e) {
- log.info("DRiverXVSM exception");
- log.info(e.getMessage());
- e.printStackTrace();
+ } catch (Exception ex) {
+ log.info("Could not find container {}", address);
+ }
+ if (success) {
+ group.setDeliveryStatus(DeliveryStatus.DELIVERED);
+ if (!Util.runSimulation) {
+ sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null);
}
+ } else {
+ group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED);
}
+ sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx);
+ capi.commitTransaction(tx);
+
+ } catch (Exception e) {
+ log.info("DRiverXVSM exception");
+ log.info(e.getMessage());
+ e.printStackTrace();
}
- }).createSpaceListenerImpl();
+ }
}
public void listenForPreparedDeliveryPizzas() {
tx);
capi.commitTransaction(tx);
+ handleDeliveries(groupsWithCompleteOrder);
} else {
log.info("Not yet all pizzas prepared! Order with id "
+ orderId + " has " + numberOfPizzas
boolean finished = GroupAgent.getInstance().getDeliveryModel().hasFinished();
if (finished) {
final long now = new Date().getTime();
- final long difference = startTime.get() - now;
+ final long difference = now - startTime.get();
log.info("Finished after {} seconds", difference / 1000);
}
}
List<PizzaType> pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA);
List<PizzaType> pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA);
- final String pizzeria1 = "9875";
- final String pizzeria2 = "9874";
+ final String pizzeria1 = "9874";
+ final String pizzeria2 = "9875";
List<DeliveryGroup> groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4);
List<DeliveryGroup> groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3);
List<DeliveryGroup> groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2);
final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
- deliveryModel.addItems(groups1);
+// deliveryModel.addItems(groups1);
deliveryModel.addItems(groups2);
deliveryModel.addItems(groups3);
GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
- GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
+// GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
log.info("ATTENTION: Now let's wait 60 seconds!");