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 java.util.Collections;
|
||||||
|
|
||||||
import Events.Event;
|
import Events.Event;
|
||||||
|
import Events.EventType;
|
||||||
|
|
||||||
public class EventQueue {
|
public class BasicInstructionQueue implements InstructionQueue {
|
||||||
private ArrayList<Integer> upQueue = new ArrayList<>();
|
private ArrayList<Integer> upQueue = new ArrayList<>();
|
||||||
private ArrayList<Integer> downQueue = new ArrayList<>();
|
private ArrayList<Integer> downQueue = new ArrayList<>();
|
||||||
private boolean emergencyState;
|
private boolean emergencyState;
|
||||||
@ -13,7 +14,7 @@ public class EventQueue {
|
|||||||
private int currentFloor;
|
private int currentFloor;
|
||||||
private Direction direction;
|
private Direction direction;
|
||||||
|
|
||||||
public EventQueue() {
|
public BasicInstructionQueue() {
|
||||||
|
|
||||||
upQueue = new ArrayList<Integer>();
|
upQueue = new ArrayList<Integer>();
|
||||||
downQueue = 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
|
* Adds a new floor to the argument's <b>queue</b> then sorts the elements.
|
||||||
* elements.
|
|
||||||
*
|
*
|
||||||
* @param queue ArrayList<Int>
|
* @param queue the list to be sorted.
|
||||||
* @param floor int - the floor we add to queue
|
* @param floor the index of the floor that will be added.
|
||||||
* @param reversed boolean - reverse queue order
|
* @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);
|
queue.add(floor);
|
||||||
if (reversed)
|
if (reversed)
|
||||||
Collections.sort(queue, Collections.reverseOrder());
|
Collections.sort(queue, Collections.reverseOrder());
|
||||||
@ -38,23 +38,35 @@ public class EventQueue {
|
|||||||
Collections.sort(queue);
|
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) {
|
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()) {
|
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:
|
case CALLFROMFLOOR:
|
||||||
if (emergencyState)
|
|
||||||
return;
|
|
||||||
switch (event.getRequestedDirection()) {
|
switch (event.getRequestedDirection()) {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Adds the instruction to the upQueue then sorts it.
|
||||||
|
*/
|
||||||
case UP:
|
case UP:
|
||||||
if (!upQueue.contains(event.getIncomingCallFloor()))
|
if (!upQueue.contains(event.getIncomingCallFloor()))
|
||||||
appSort(upQueue, event.getIncomingCallFloor(), false);
|
appSort(upQueue, event.getIncomingCallFloor(), false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Adds the instruction to the downQueue then sorts it.
|
||||||
|
*/
|
||||||
case DOWN:
|
case DOWN:
|
||||||
if (!downQueue.contains(event.getIncomingCallFloor()))
|
if (!downQueue.contains(event.getIncomingCallFloor()))
|
||||||
appSort(downQueue, event.getIncomingCallFloor(), true);
|
appSort(downQueue, event.getIncomingCallFloor(), true);
|
||||||
@ -63,29 +75,44 @@ public class EventQueue {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
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:
|
case CALLFROMELEVATOR:
|
||||||
if (emergencyState)
|
|
||||||
return;
|
|
||||||
if (event.getRequestedFloor() > currentFloor && !upQueue.contains(event.getRequestedFloor()))
|
if (event.getRequestedFloor() > currentFloor && !upQueue.contains(event.getRequestedFloor()))
|
||||||
appSort(upQueue, event.getRequestedFloor(), false);
|
appSort(upQueue, event.getRequestedFloor(), false);
|
||||||
else if (event.getRequestedFloor() < currentFloor && !upQueue.contains(event.getRequestedFloor()))
|
else if (event.getRequestedFloor() < currentFloor && !upQueue.contains(event.getRequestedFloor()))
|
||||||
appSort(downQueue, event.getRequestedFloor(), true);
|
appSort(downQueue, event.getRequestedFloor(), true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Puts emergencyState to true. Blocks every event from affecting the queue and
|
||||||
|
* clears it.
|
||||||
|
*/
|
||||||
case EMERGENCYSTOP:
|
case EMERGENCYSTOP:
|
||||||
if (emergencyState)
|
|
||||||
return;
|
|
||||||
emergencyState = true;
|
emergencyState = true;
|
||||||
clearQueue();
|
clearQueue();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Puts emergencyState to false. Unblocks events.
|
||||||
|
*/
|
||||||
case CANCELEMERGENCYSTOP:
|
case CANCELEMERGENCYSTOP:
|
||||||
emergencyState = false;
|
emergencyState = false;
|
||||||
break;
|
break;
|
||||||
|
/*
|
||||||
|
* Refreshes the local values of current floor and current direction with the
|
||||||
|
* ones given by the event. These values will
|
||||||
|
*/
|
||||||
case REACHEDFLOORSIGNAL:
|
case REACHEDFLOORSIGNAL:
|
||||||
if (emergencyState)
|
|
||||||
return;
|
|
||||||
currentFloor = event.getCurrentFloor();
|
currentFloor = event.getCurrentFloor();
|
||||||
direction = event.getCurrentDirection();
|
direction = event.getCurrentDirection();
|
||||||
|
removeInstruction(currentFloor);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -98,7 +125,7 @@ public class EventQueue {
|
|||||||
* @param floor the reached floor
|
* @param floor the reached floor
|
||||||
* @param direction the direction of the elevator
|
* @param direction the direction of the elevator
|
||||||
*/
|
*/
|
||||||
public void removeInstruction(int floor, Direction direction) {
|
private void removeInstruction(int floor) {
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case UP:
|
case UP:
|
||||||
if (upQueue.contains(floor))
|
if (upQueue.contains(floor))
|
||||||
@ -158,7 +185,7 @@ public class EventQueue {
|
|||||||
return nextFloor;
|
return nextFloor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearQueue() {
|
private void clearQueue() {
|
||||||
downQueue.removeAll(downQueue);
|
downQueue.removeAll(downQueue);
|
||||||
upQueue.removeAll(upQueue);
|
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.CallFromElevatorEvent;
|
||||||
import Events.CallFromFloorEvent;
|
import Events.CallFromFloorEvent;
|
||||||
|
import Events.ReachedFloorEvent;
|
||||||
|
|
||||||
public class Test {
|
public class Test {
|
||||||
|
|
||||||
private static boolean running = false;
|
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;
|
private static int currentFloor;
|
||||||
|
|
||||||
public static void iter() {
|
public static void iter() {
|
||||||
int nextDestination = queue.getNextInstruction();
|
int nextDestination = queue.getNextInstruction();
|
||||||
//System.out.println("next dest = " + nextDestination);
|
|
||||||
if (!running) {
|
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;
|
running = true;
|
||||||
direction = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
|
currentDirection = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// if we just reached a new floor
|
||||||
if (currentFloor == nextDestination) {
|
if (currentFloor == nextDestination) {
|
||||||
queue.removeInstruction(currentFloor, direction);
|
queue.computeEvent(new ReachedFloorEvent(currentFloor, currentDirection));
|
||||||
running = false;
|
running = false;
|
||||||
} else {
|
} else {
|
||||||
direction = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
|
currentDirection = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
|
||||||
currentFloor += direction == Direction.UP ? 1 : -1;
|
currentFloor += currentDirection == Direction.UP ? 1 : -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("elevator floor " + currentFloor + " direction " +
|
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) {
|
public static void main(String[] args) {
|
||||||
//TODO envoyer les notifs de changements d'étages
|
//TODO envoyer les notifs de changements d'étages
|
||||||
queue = new EventQueue();
|
queue = new BasicInstructionQueue();
|
||||||
iter();
|
iter();
|
||||||
iter();
|
iter();
|
||||||
queue.computeEvent(new CallFromFloorEvent(2, Direction.DOWN));
|
queue.computeEvent(new CallFromFloorEvent(2, Direction.DOWN));
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package simulation;
|
package simulation;
|
||||||
|
|
||||||
import commandSystem.EventQueue;
|
import commandSystem.BasicInstructionQueue;
|
||||||
import Events.CallFromElevatorEvent;
|
import Events.CallFromElevatorEvent;
|
||||||
import Events.CallFromFloorEvent;
|
import Events.CallFromFloorEvent;
|
||||||
import commandSystem.Direction;
|
import commandSystem.Direction;
|
||||||
@ -11,7 +11,7 @@ import simulation.Elevator;
|
|||||||
public class Simulation implements ElevatorListener {
|
public class Simulation implements ElevatorListener {
|
||||||
private boolean running = false;
|
private boolean running = false;
|
||||||
private int currentFloor = 0;
|
private int currentFloor = 0;
|
||||||
private EventQueue queue;
|
private BasicInstructionQueue queue;
|
||||||
private Elevator elevator;
|
private Elevator elevator;
|
||||||
|
|
||||||
public Simulation(Elevator elevator) {
|
public Simulation(Elevator elevator) {
|
||||||
|
Reference in New Issue
Block a user