maj requests et eventqueue (pas terminé)

This commit is contained in:
MathieuPietri 2019-10-15 12:15:59 +02:00
parent a9708d8d14
commit 0b3dc7381c
10 changed files with 232 additions and 110 deletions

View File

@ -1,9 +1,42 @@
package Requests;
public class CallFromElevatorRequest extends Request {
import commandSystem.Direction;
public class CallFromElevatorRequest implements Request {
private final int wantedFloor;
public CallFromElevatorRequest(int wantedFloor) {
super(RequestType.CALLFROMELEVATOR);
this.wantedFloor = wantedFloor;
}
@Override
public Direction getRequestedDirection() {
return Direction.NONE;
}
@Override
public int getRequestedFloor() {
return wantedFloor;
}
@Override
public int getIncomingCallFloor() {
return -1;
}
@Override
public RequestType getType() {
return RequestType.CALLFROMELEVATOR;
}
@Override
public int getCurrentFloor() {
return -1;
}
@Override
public Direction getCurrentDirection() {
return Direction.NONE;
}
}

View File

@ -2,13 +2,45 @@ package Requests;
import commandSystem.Direction;
public class CallFromFloorRequest extends Request {
public class CallFromFloorRequest implements Request {
public CallFromFloorRequest(int sourceFloor, Direction direction) {
super(RequestType.CALLFROMFLOOR);
this.sourceFloor = sourceFloor;
private final int incomingCallFloor;
private final Direction direction;
public CallFromFloorRequest(int incomingCallFloor, Direction direction) {
this.incomingCallFloor = incomingCallFloor;
this.direction = direction;
}
@Override
public Direction getRequestedDirection() {
return direction;
}
@Override
public int getRequestedFloor() {
return -1;
}
@Override
public int getIncomingCallFloor() {
return incomingCallFloor;
}
@Override
public RequestType getType() {
return RequestType.CALLFROMFLOOR;
}
@Override
public int getCurrentFloor() {
return -1;
}
@Override
public Direction getCurrentDirection() {
return Direction.NONE;
}
}

View File

@ -1,9 +1,40 @@
package Requests;
public class CancelEmergencyStopRequest extends Request{
import commandSystem.Direction;
public class CancelEmergencyStopRequest implements Request{
public CancelEmergencyStopRequest() {
super(RequestType.CANCELEMERGENCYSTOP);
}
@Override
public Direction getRequestedDirection() {
return Direction.NONE;
}
@Override
public int getRequestedFloor() {
return -1;
}
@Override
public int getIncomingCallFloor() {
return -1;
}
@Override
public RequestType getType() {
return RequestType.CANCELEMERGENCYSTOP;
}
@Override
public int getCurrentFloor() {
return -1;
}
@Override
public Direction getCurrentDirection() {
return Direction.NONE;
}
}

View File

@ -1,8 +1,39 @@
package Requests;
public class EmergencyStopRequest extends Request {
import commandSystem.Direction;
public class EmergencyStopRequest implements Request {
public EmergencyStopRequest() {
super(RequestType.EMERGENCYSTOP);
}
@Override
public Direction getRequestedDirection() {
return Direction.NONE;
}
@Override
public int getRequestedFloor() {
return -1;
}
@Override
public int getIncomingCallFloor() {
return -1;
}
@Override
public RequestType getType() {
return RequestType.EMERGENCYSTOP;
}
@Override
public int getCurrentFloor() {
return -1;
}
@Override
public Direction getCurrentDirection() {
return Direction.NONE;
}
}

View File

@ -1,9 +1,45 @@
package Requests;
public class ReachedFloorSignal extends Request{
public ReachedFloorSignal(int reachedFLoor) {
super(RequestType.REACHEDFLOORSIGNAL);
this.reachedFloor = reachedFLoor;
import commandSystem.Direction;
public class ReachedFloorSignal implements Request{
private final int currentFloor;
private final Direction currentDirection;
public ReachedFloorSignal(int currentFloor, Direction currentDirection) {
this.currentFloor = currentFloor;
this.currentDirection = currentDirection;
}
@Override
public Direction getRequestedDirection() {
return Direction.NONE;
}
@Override
public int getRequestedFloor() {
return -1;
}
@Override
public int getIncomingCallFloor() {
return -1;
}
@Override
public RequestType getType() {
return RequestType.REACHEDFLOORSIGNAL;
}
@Override
public int getCurrentFloor() {
return currentFloor;
}
@Override
public Direction getCurrentDirection() {
return currentDirection;
}

View File

@ -2,59 +2,45 @@ package Requests;
import commandSystem.Direction;
public abstract class Request {
protected RequestType type;
protected int wantedFloor;
protected int sourceFloor;
protected Direction direction;
protected int reachedFloor;
public Request(RequestType type) {
this.type = type;
this.reachedFloor = -1;
this.wantedFloor = -1;
this.sourceFloor = -1;
this.direction = Direction.NONE;
}
public interface Request {
/**
*
* @return the direction of the request, unconcerned extending classes return NONE.
* @return the direction of the request, unconcerned extending classes return
* NONE.
*/
public Direction getDirection() {
return direction;
}
public Direction getRequestedDirection();
/**
*
* @return the wished floor, unconcerned extending classes return -1.
*/
public int getWantedFloor() {
return wantedFloor;
}
public int getRequestedFloor();
/**
*
* @return the floor it was called from, unconcerned extending classes return -1.
* @return the floor it was called from, unconcerned extending classes return
* -1.
*/
public int getIncomingCallFloor() {
return sourceFloor;
}
public int getIncomingCallFloor();
/**
*
* @return the RequestType of the request.
*/
public RequestType getType() {
return type;
}
public RequestType getType();
/**
*
* @return the floor the elevator just reached, unconcerned extending classes return -1.
* @return the floor the elevator just reached, unconcerned extending classes
* return -1.
*/
public int getReachedFloor() {
return reachedFloor;
}
public int getCurrentFloor();
/**
*
* @return the direction of the elevator, when a reachedFloorSignal is sent.
* Unconcerned extending classes return NONE
*/
public Direction getCurrentDirection();
}

View File

@ -1,9 +0,0 @@
package commandSystem;
public interface Elevator {
public void sendFloorReachedNotification();
public int getCurrentFloor();
public Direction getCurrentDirection();
}

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Collections;
import Requests.Request;
import simulation.Elevator;
// emergencyStop -> clear the queue
@ -11,14 +12,17 @@ public class EventQueue {
private ArrayList<Integer> upQueue = new ArrayList<>();
private ArrayList<Integer> downQueue = new ArrayList<>();
private boolean emergencyState;
private Elevator elevator;
private int currentFloor;
private Direction direction;
public EventQueue() {
public EventQueue(Elevator elevator) {
this.elevator = elevator;
upQueue = new ArrayList<Integer>();
downQueue = new ArrayList<Integer>();
emergencyState = false;
currentFloor = 0;
direction = Direction.NONE;
}
/**
@ -48,7 +52,7 @@ public class EventQueue {
case CALLFROMFLOOR:
if (emergencyState)
return;
switch (request.getDirection()) {
switch (request.getRequestedDirection()) {
case UP:
if (!upQueue.contains(request.getIncomingCallFloor()))
appSort(upQueue, request.getIncomingCallFloor(), false);
@ -64,11 +68,11 @@ public class EventQueue {
case CALLFROMELEVATOR:
if (emergencyState)
return;
if (request.getWantedFloor() > elevator.getCurrentFloor() && !upQueue.contains(request.getWantedFloor()))
appSort(upQueue, request.getWantedFloor(), false);
else if (request.getWantedFloor() < elevator.getCurrentFloor()
&& !upQueue.contains(request.getWantedFloor()))
appSort(downQueue, request.getWantedFloor(), true);
if (request.getRequestedFloor() > currentFloor && !upQueue.contains(request.getRequestedFloor()))
appSort(upQueue, request.getRequestedFloor(), false);
else if (request.getRequestedFloor() < currentFloor
&& !upQueue.contains(request.getRequestedFloor()))
appSort(downQueue, request.getRequestedFloor(), true);
break;
case EMERGENCYSTOP:
if (emergencyState)
@ -79,6 +83,11 @@ public class EventQueue {
case CANCELEMERGENCYSTOP:
emergencyState = false;
break;
case REACHEDFLOORSIGNAL:
if (emergencyState)
return;
currentFloor = request.getCurrentFloor();
direction = request.getCurrentDirection();
default:
break;
}
@ -118,11 +127,11 @@ public class EventQueue {
public int getNextInstruction() {
//System.out.println("" + upQueue + '\n' + downQueue);
int nextFloor = -1;
switch (elevator.getCurrentDirection()) {
switch (direction) {
// get first element of upQueue that is ABOVE the elevator's current floor
case UP:
for (int i = 0; i < upQueue.size() && nextFloor < 0; i++) {
if (upQueue.get(i) >= elevator.getCurrentFloor()) {
if (upQueue.get(i) >= currentFloor) {
nextFloor = upQueue.get(i);
}
}
@ -136,7 +145,7 @@ public class EventQueue {
// get first element of downQueue that is UNDER the elevator's current floor
case DOWN:
for (int i = 0; i < downQueue.size() && nextFloor < 0; i++) {
if (downQueue.get(i) <= elevator.getCurrentFloor()) {
if (downQueue.get(i) <= currentFloor) {
nextFloor = downQueue.get(i);
}
}

View File

@ -2,13 +2,15 @@ package commandSystem;
import Requests.CallFromElevatorRequest;
import Requests.CallFromFloorRequest;
import simulation.Elevator;
public class Test {
private static boolean running = false;
private static EventQueue queue;
private static TestElevator elevator = new TestElevator();
private static Direction direction;
private static int currentFloor;
public static void iter() {
int nextDestination = queue.getNextInstruction();
@ -16,23 +18,24 @@ public class Test {
if (!running) {
if (nextDestination > 0) { // return -1 if no next destination
running = true;
elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
direction = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
}
} else {
if (elevator.currentFloor == nextDestination) {
queue.removeInstruction(elevator.currentFloor, elevator.direction);
if (currentFloor == nextDestination) {
queue.removeInstruction(currentFloor, direction);
running = false;
} else {
elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
elevator.currentFloor += elevator.direction == Direction.UP ? 1 : -1;
direction = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
currentFloor += direction == Direction.UP ? 1 : -1;
}
}
System.out.println("elevator floor " + elevator.currentFloor + " direction " +
(elevator.direction == Direction.UP? "up" : "down") + " running: " + running);
System.out.println("elevator floor " + currentFloor + " direction " +
(direction == Direction.UP? "up" : "down") + " running: " + running);
}
public static void main(String[] args) {
queue = new EventQueue(elevator);
//TODO envoyer les notifs de changements d'étages
queue = new EventQueue();
iter();
iter();
queue.computeRequest(new CallFromFloorRequest(2, Direction.DOWN));

View File

@ -1,30 +0,0 @@
package commandSystem;
public class TestElevator implements Elevator{
public int currentFloor;
public Direction direction;
public TestElevator() {
this.direction = Direction.UP;
this.currentFloor = 0;
}
@Override
public void sendFloorReachedNotification() {
// TODO Auto-generated method stub
}
@Override
public int getCurrentFloor() {
return currentFloor;
}
@Override
public Direction getCurrentDirection() {
return direction;
}
}