refactoring eventQueue (devenue BasicInstructionQueue), création de l'interface InstructionQueue (pour garantir la modularité)
This commit is contained in:
parent
a2fdc0934e
commit
d75c088637
@ -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<Integer> upQueue = new ArrayList<>();
|
||||
private ArrayList<Integer> 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<Integer>();
|
||||
downQueue = new ArrayList<Integer>();
|
||||
@ -23,14 +24,13 @@ public class EventQueue {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new floor to <b>upQueue</b> or <b>downQueue</b> then sorts the
|
||||
* elements.
|
||||
* Adds a new floor to the argument's <b>queue</b> then sorts the elements.
|
||||
*
|
||||
* @param queue ArrayList<Int>
|
||||
* @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<Integer> queue, int floor, boolean reversed) {
|
||||
private void appSort(ArrayList<Integer> 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);
|
||||
}
|
20
src/commandSystem/InstructionQueue.java
Normal file
20
src/commandSystem/InstructionQueue.java
Normal file
@ -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();
|
||||
}
|
@ -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));
|
||||
|
@ -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) {
|
||||
|
Reference in New Issue
Block a user