diff --git a/src/commandSystem/EventQueue.java b/src/commandSystem/BasicInstructionQueue.java similarity index 68% rename from src/commandSystem/EventQueue.java rename to src/commandSystem/BasicInstructionQueue.java index ff2a712..610fcb4 100644 --- a/src/commandSystem/EventQueue.java +++ b/src/commandSystem/BasicInstructionQueue.java @@ -4,8 +4,9 @@ import java.util.ArrayList; import java.util.Collections; import Events.Event; +import Events.EventType; -public class EventQueue { +public class BasicInstructionQueue implements InstructionQueue { private ArrayList upQueue = new ArrayList<>(); private ArrayList downQueue = new ArrayList<>(); private boolean emergencyState; @@ -13,7 +14,7 @@ public class EventQueue { private int currentFloor; private Direction direction; - public EventQueue() { + public BasicInstructionQueue() { upQueue = new ArrayList(); downQueue = new ArrayList(); @@ -23,14 +24,13 @@ public class EventQueue { } /** - * Adds a new floor to upQueue or downQueue then sorts the - * elements. + * Adds a new floor to the argument's queue then sorts the elements. * - * @param queue ArrayList - * @param floor int - the floor we add to queue - * @param reversed boolean - reverse queue order + * @param queue the list to be sorted. + * @param floor the index of the floor that will be added. + * @param reversed if set to true, the queue will be sorted by descending order. */ - public void appSort(ArrayList queue, int floor, boolean reversed) { + private void appSort(ArrayList queue, int floor, boolean reversed) { queue.add(floor); if (reversed) Collections.sort(queue, Collections.reverseOrder()); @@ -38,23 +38,35 @@ public class EventQueue { Collections.sort(queue); } - /** - * Compute a request into the {@link #EventQueue} depending on the elevator - * status. - * - * @param event - the request to compute - */ public void computeEvent(Event event) { + /* + * Emergency state prevents any event from affecting the queue except the cancel + * emergency stop event. + */ + if (emergencyState && event.getType() != EventType.CANCELEMERGENCYSTOP) + return; + switch (event.getType()) { + + /* + * The elevator will have to stop at this floor when it's headed in the same + * direction the call is directed to. + */ case CALLFROMFLOOR: - if (emergencyState) - return; switch (event.getRequestedDirection()) { + + /* + * Adds the instruction to the upQueue then sorts it. + */ case UP: if (!upQueue.contains(event.getIncomingCallFloor())) appSort(upQueue, event.getIncomingCallFloor(), false); break; + + /* + * Adds the instruction to the downQueue then sorts it. + */ case DOWN: if (!downQueue.contains(event.getIncomingCallFloor())) appSort(downQueue, event.getIncomingCallFloor(), true); @@ -63,29 +75,44 @@ public class EventQueue { break; } break; + + /* + * If the requested floor is under the elevator, adds the instruction to the + * downQueue then sorts it. if it's above, adds the instruction to the upQueue + * then sorts it. If it's the same floor, doesn't do anything + */ case CALLFROMELEVATOR: - if (emergencyState) - return; if (event.getRequestedFloor() > currentFloor && !upQueue.contains(event.getRequestedFloor())) appSort(upQueue, event.getRequestedFloor(), false); else if (event.getRequestedFloor() < currentFloor && !upQueue.contains(event.getRequestedFloor())) appSort(downQueue, event.getRequestedFloor(), true); break; + + /* + * Puts emergencyState to true. Blocks every event from affecting the queue and + * clears it. + */ case EMERGENCYSTOP: - if (emergencyState) - return; emergencyState = true; clearQueue(); break; + + /* + * Puts emergencyState to false. Unblocks events. + */ case CANCELEMERGENCYSTOP: emergencyState = false; break; + /* + * Refreshes the local values of current floor and current direction with the + * ones given by the event. These values will + */ case REACHEDFLOORSIGNAL: - if (emergencyState) - return; currentFloor = event.getCurrentFloor(); direction = event.getCurrentDirection(); + removeInstruction(currentFloor); break; + default: break; } @@ -98,7 +125,7 @@ public class EventQueue { * @param floor the reached floor * @param direction the direction of the elevator */ - public void removeInstruction(int floor, Direction direction) { + private void removeInstruction(int floor) { switch (direction) { case UP: if (upQueue.contains(floor)) @@ -158,7 +185,7 @@ public class EventQueue { return nextFloor; } - public void clearQueue() { + private void clearQueue() { downQueue.removeAll(downQueue); upQueue.removeAll(upQueue); } diff --git a/src/commandSystem/InstructionQueue.java b/src/commandSystem/InstructionQueue.java new file mode 100644 index 0000000..df9bd80 --- /dev/null +++ b/src/commandSystem/InstructionQueue.java @@ -0,0 +1,20 @@ +package commandSystem; + +import Events.Event; + +public interface InstructionQueue { + + /** + * Compute a request into the instruction queue. + * + * @param event - the request to compute + */ + public void computeEvent(Event event); + + /** + * Returns the next floor the elevator has to go to. + * + * @return the next floor if it exists, else -1 + */ + public int getNextInstruction(); +} diff --git a/src/commandSystem/Test.java b/src/commandSystem/Test.java index 49702d8..c0dd45e 100644 --- a/src/commandSystem/Test.java +++ b/src/commandSystem/Test.java @@ -2,39 +2,42 @@ package commandSystem; import Events.CallFromElevatorEvent; import Events.CallFromFloorEvent; +import Events.ReachedFloorEvent; public class Test { private static boolean running = false; - private static EventQueue queue; + private static BasicInstructionQueue queue; - private static Direction direction; + private static Direction currentDirection; private static int currentFloor; public static void iter() { int nextDestination = queue.getNextInstruction(); - //System.out.println("next dest = " + nextDestination); + if (!running) { - if (nextDestination > 0) { // return -1 if no next destination + // would be -1 if the queue doesn't have any next instruction + if (nextDestination != -1) { running = true; - direction = currentFloor < nextDestination ? Direction.UP : Direction.DOWN; + currentDirection = currentFloor < nextDestination ? Direction.UP : Direction.DOWN; } } else { + // if we just reached a new floor if (currentFloor == nextDestination) { - queue.removeInstruction(currentFloor, direction); + queue.computeEvent(new ReachedFloorEvent(currentFloor, currentDirection)); running = false; } else { - direction = currentFloor < nextDestination ? Direction.UP : Direction.DOWN; - currentFloor += direction == Direction.UP ? 1 : -1; + currentDirection = currentFloor < nextDestination ? Direction.UP : Direction.DOWN; + currentFloor += currentDirection == Direction.UP ? 1 : -1; } } System.out.println("elevator floor " + currentFloor + " direction " + - (direction == Direction.UP? "up" : "down") + " running: " + running); + (currentDirection == Direction.UP? "up" : "down") + " running: " + running); } public static void main(String[] args) { //TODO envoyer les notifs de changements d'étages - queue = new EventQueue(); + queue = new BasicInstructionQueue(); iter(); iter(); queue.computeEvent(new CallFromFloorEvent(2, Direction.DOWN)); diff --git a/src/simulation/Simulation.java b/src/simulation/Simulation.java index a84d68f..d761563 100644 --- a/src/simulation/Simulation.java +++ b/src/simulation/Simulation.java @@ -1,6 +1,6 @@ package simulation; -import commandSystem.EventQueue; +import commandSystem.BasicInstructionQueue; import Events.CallFromElevatorEvent; import Events.CallFromFloorEvent; import commandSystem.Direction; @@ -11,7 +11,7 @@ import simulation.Elevator; public class Simulation implements ElevatorListener { private boolean running = false; private int currentFloor = 0; - private EventQueue queue; + private BasicInstructionQueue queue; private Elevator elevator; public Simulation(Elevator elevator) {