package cmsc433.p2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * Validates a simulation */ public class Validate { private static class InvalidSimulationException extends Exception { public InvalidSimulationException() { } }; // Helper method for validating the simulation private static void check(boolean check, String message) throws InvalidSimulationException { if (!check) { System.err.println("SIMULATION INVALID : "+message); throw new Validate.InvalidSimulationException(); } } private static void printList(List l) { for(int i=0;i events) { try { check(events.get(0).event == SimulationEvent.EventType.SimulationStarting, "Simulation didn't start with initiation event"); check(events.get(events.size()-1).event == SimulationEvent.EventType.SimulationEnded, "Simulation didn't end with termination event"); /* Write validation code for the following : Should not have more eaters than specified Should not have more cooks than specified The restaurant capacity should not be exceeded The capacity of each machine should not be exceeded Eater should not receive order until cook completes it Eater should not leave restaurant until order is received Eater should not place more than one order Cook should not work on order before it is placed */ Food burger = new Food("burger",500); Food fries = new Food("fries",250); Food coke = new Food("coke",100); int[] params = events.get(0).simParams; int numEaters = params[0]; int numCooks = params[1]; int numTables = params[2]; int capacity = params[3]; List defaultOrder = new ArrayList(); defaultOrder.add(burger); defaultOrder.add(fries); defaultOrder.add(coke); Eater e; String en; List o; Integer on; Cook c; String cn; Food f; String fn; Machine m; int mid; int tableCount = 0; ArrayList eaterList = new ArrayList(); ArrayList eaterStarted = new ArrayList(); ArrayList eaterEntered = new ArrayList(); ArrayList eaterPlaced = new ArrayList(); ArrayList eaterReceived = new ArrayList(); ArrayList eaterLeft = new ArrayList(); ArrayList orderList = new ArrayList(); ArrayList orderWaiting = new ArrayList(); ArrayList orderFinished = new ArrayList(); ArrayList cookList = new ArrayList(); ArrayList cookReady = new ArrayList(); HashMap cookWithOrder = new HashMap(); HashMap> cookFoodWaiting = new HashMap>(); HashMap> cookFoodFinished = new HashMap>(); int[] foodToCook = new int[3]; int[] foodToTake = new int[3]; int[] machineReady = new int[3]; int[] machineCooking = new int[3]; for(int i=1;i()); cookFoodFinished.put(cn, new ArrayList()); for(Food fi : o) cookFoodWaiting.get(cn).add(fi.name); break; case CookStartedFood: c = ei.cook; check(c!=null,"Null Cook"); cn = c.toString(); on = ei.orderNumber; check(cookWithOrder.get(cn).equals(on),cn+" cooking food for wrong order"); f = ei.food; fn = f.name; check(cookFoodWaiting.get(cn).contains(fn),cn+" cooking wrong food"); cookFoodWaiting.get(cn).remove(fn); foodToCook[foodToInt(f)]++; break; case CookFinishedFood: c = ei.cook; check(c!=null,"Null Cook"); cn = c.toString(); on = ei.orderNumber; check(cookWithOrder.get(cn).equals(on),cn+" getting food for wrong order"); f = ei.food; fn = f.name; check(foodToTake[foodToInt(f)]>0,cn+" getting food before finished"); foodToTake[foodToInt(f)]--; cookFoodFinished.get(cn).add(fn); break; case CookCompletedOrder: c = ei.cook; check(c!=null,"Null Cook"); cn = c.toString(); on = ei.orderNumber; check(cookWithOrder.get(cn).equals(on),cn+" getting wrong order"); check(cookFoodWaiting.get(cn).size()==0,cn+" didn't cook all foods"); for(Food fd : defaultOrder) check(cookFoodFinished.get(cn).contains(fd.name),cn+" didn't cook enough foods"); orderFinished.add(on); cookReady.add(cn); break; case CookEnding: c = ei.cook; check(c!=null,"Null Cook"); cn = c.toString(); check(cookReady.contains(cn),cn+" leaving while cooking"); cookReady.remove(cn); break; /* Machine events */ case MachineStarting: m = ei.machine; check(m!=null,"Null Machine"); mid = machineToInt(m); check(machineReady[mid]==0,m.name+" invalid start"); machineReady[mid]=1; break; case MachineStartingFood: m = ei.machine; check(m!=null,"Null Machine"); mid = machineToInt(m); check(machineReady[mid]==1,m.name+" cooking food before starting"); check(foodToCook[mid]>0,m.name+" no food to cook"); foodToCook[mid]--; machineCooking[mid]++; check(machineCooking[mid]<=capacity,m.name+" exceeded capacity"); break; case MachineDoneFood: m = ei.machine; check(m!=null,"Null Machine"); mid = machineToInt(m); check(machineReady[mid]==1,m.name+" cooking food before starting"); check(machineCooking[mid]>0,m.name+" no food to finish cooking"); machineCooking[mid]--; foodToTake[mid]++; break; case MachineEnding: m = ei.machine; check(m!=null,"Null Machine"); mid = machineToInt(m); check(machineReady[mid]==1,m.name+" ending before starting"); machineReady[mid]=-1; break; default: } } check(numEaters==eaterList.size(),"Incorrect number of eaters"); check(numCooks==cookList.size(),"Incorrect number of cooks"); check(eaterStarted.size()+eaterEntered.size()+eaterPlaced.size()+eaterReceived.size()==0,"Incompleted customers"); check(cookReady.size()==0,"Cooks not sent home"); check(machineReady[0]+machineReady[1]+machineReady[2]==-3,"Machines not ended"); return true; } catch (InvalidSimulationException e) { return false; } } }