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; package Requests;
public class CallFromElevatorRequest extends Request { import commandSystem.Direction;
public class CallFromElevatorRequest implements Request {
private final int wantedFloor;
public CallFromElevatorRequest(int wantedFloor) { public CallFromElevatorRequest(int wantedFloor) {
super(RequestType.CALLFROMELEVATOR);
this.wantedFloor = wantedFloor; 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; import commandSystem.Direction;
public class CallFromFloorRequest extends Request { public class CallFromFloorRequest implements Request {
public CallFromFloorRequest(int sourceFloor, Direction direction) { private final int incomingCallFloor;
super(RequestType.CALLFROMFLOOR); private final Direction direction;
this.sourceFloor = sourceFloor;
public CallFromFloorRequest(int incomingCallFloor, Direction direction) {
this.incomingCallFloor = incomingCallFloor;
this.direction = direction; 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; package Requests;
public class CancelEmergencyStopRequest extends Request{ import commandSystem.Direction;
public class CancelEmergencyStopRequest implements Request{
public CancelEmergencyStopRequest() { 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; package Requests;
public class EmergencyStopRequest extends Request { import commandSystem.Direction;
public class EmergencyStopRequest implements Request {
public EmergencyStopRequest() { 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; package Requests;
public class ReachedFloorSignal extends Request{ import commandSystem.Direction;
public ReachedFloorSignal(int reachedFLoor) {
super(RequestType.REACHEDFLOORSIGNAL); public class ReachedFloorSignal implements Request{
this.reachedFloor = reachedFLoor;
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; import commandSystem.Direction;
public abstract class Request { public interface 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;
}
/** /**
* *
* @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() { public Direction getRequestedDirection();
return direction;
}
/** /**
* *
* @return the wished floor, unconcerned extending classes return -1. * @return the wished floor, unconcerned extending classes return -1.
*/ */
public int getWantedFloor() { public int getRequestedFloor();
return wantedFloor;
}
/** /**
* *
* @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() { public int getIncomingCallFloor();
return sourceFloor;
}
/** /**
* *
* @return the RequestType of the request. * @return the RequestType of the request.
*/ */
public RequestType getType() { public RequestType getType();
return type;
}
/** /**
* *
* @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() { public int getCurrentFloor();
return reachedFloor;
} /**
*
* @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 java.util.Collections;
import Requests.Request; import Requests.Request;
import simulation.Elevator;
// emergencyStop -> clear the queue // emergencyStop -> clear the queue
@ -11,14 +12,17 @@ public class EventQueue {
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;
private Elevator elevator;
private int currentFloor;
private Direction direction;
public EventQueue() {
public EventQueue(Elevator elevator) {
this.elevator = elevator;
upQueue = new ArrayList<Integer>(); upQueue = new ArrayList<Integer>();
downQueue = new ArrayList<Integer>(); downQueue = new ArrayList<Integer>();
emergencyState = false; emergencyState = false;
currentFloor = 0;
direction = Direction.NONE;
} }
/** /**
@ -48,7 +52,7 @@ public class EventQueue {
case CALLFROMFLOOR: case CALLFROMFLOOR:
if (emergencyState) if (emergencyState)
return; return;
switch (request.getDirection()) { switch (request.getRequestedDirection()) {
case UP: case UP:
if (!upQueue.contains(request.getIncomingCallFloor())) if (!upQueue.contains(request.getIncomingCallFloor()))
appSort(upQueue, request.getIncomingCallFloor(), false); appSort(upQueue, request.getIncomingCallFloor(), false);
@ -64,11 +68,11 @@ public class EventQueue {
case CALLFROMELEVATOR: case CALLFROMELEVATOR:
if (emergencyState) if (emergencyState)
return; return;
if (request.getWantedFloor() > elevator.getCurrentFloor() && !upQueue.contains(request.getWantedFloor())) if (request.getRequestedFloor() > currentFloor && !upQueue.contains(request.getRequestedFloor()))
appSort(upQueue, request.getWantedFloor(), false); appSort(upQueue, request.getRequestedFloor(), false);
else if (request.getWantedFloor() < elevator.getCurrentFloor() else if (request.getRequestedFloor() < currentFloor
&& !upQueue.contains(request.getWantedFloor())) && !upQueue.contains(request.getRequestedFloor()))
appSort(downQueue, request.getWantedFloor(), true); appSort(downQueue, request.getRequestedFloor(), true);
break; break;
case EMERGENCYSTOP: case EMERGENCYSTOP:
if (emergencyState) if (emergencyState)
@ -79,6 +83,11 @@ public class EventQueue {
case CANCELEMERGENCYSTOP: case CANCELEMERGENCYSTOP:
emergencyState = false; emergencyState = false;
break; break;
case REACHEDFLOORSIGNAL:
if (emergencyState)
return;
currentFloor = request.getCurrentFloor();
direction = request.getCurrentDirection();
default: default:
break; break;
} }
@ -118,11 +127,11 @@ public class EventQueue {
public int getNextInstruction() { public int getNextInstruction() {
//System.out.println("" + upQueue + '\n' + downQueue); //System.out.println("" + upQueue + '\n' + downQueue);
int nextFloor = -1; int nextFloor = -1;
switch (elevator.getCurrentDirection()) { switch (direction) {
// get first element of upQueue that is ABOVE the elevator's current floor // get first element of upQueue that is ABOVE the elevator's current floor
case UP: case UP:
for (int i = 0; i < upQueue.size() && nextFloor < 0; i++) { 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); nextFloor = upQueue.get(i);
} }
} }
@ -136,7 +145,7 @@ public class EventQueue {
// get first element of downQueue that is UNDER the elevator's current floor // get first element of downQueue that is UNDER the elevator's current floor
case DOWN: case DOWN:
for (int i = 0; i < downQueue.size() && nextFloor < 0; i++) { 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); nextFloor = downQueue.get(i);
} }
} }

View File

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