diff --git a/Cahier_des_charges.pdf b/Cahier_des_charges.pdf new file mode 100644 index 0000000..4d8da44 Binary files /dev/null and b/Cahier_des_charges.pdf differ diff --git a/diag_classes.uml b/diag_classes.uml index 46481ad..0e982da 100644 --- a/diag_classes.uml +++ b/diag_classes.uml @@ -54,7 +54,7 @@ CodeGeneration - + ArgoUML (using Netbeans XMI Writer version 1.0) 0.34(6) revised on $Date: 2010-01-11 22:20:14 +0100 (Mon, 11 Jan 2010) $ @@ -63,12 +63,1838 @@ + isAbstract = 'false'> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + InstructionQueue + + + + + + <<interface>> + + + + + + + + computeEvent(event : Event) : void + getNextInstruction() : int + + + + + + + + BasicInstructionQueue + + + + + + + + + + + + + currentFloor : Integer + currentDirection : Direction + + + + + + + + appSort() : void + removeInstruction() : void + clearQueues() : void + + + + + sourcePortFig="Fig1.0" + destPortFig="Fig0" + sourceFigNode="Fig1" + destFigNode="Fig0" + + + + + + + + + + + + + Event + + + + + + <<interface>> + + + + + + + + getType() + getRequestedDirection() + getIncomingCallFloor() + getRequestedFloor() + getCurrentFloor() + getCurrentDirection() + + + + + + + + CallFromElevatorEvent + + + + + + + + + + + + + requestedFloor : Integer + + + + + + + + + + + + + + + CancelEmergencyStopEvent + + + + + + + + + + + + + + + + + + + + + + + + + + + CallFromFloorEvent + + + + + + + + + + + + + incomingCallFloor : Integer + requestedDirection : Direction + + + + + + + + + + + + + + + EmergencyStopEvent + + + + + + + + + + + + + + + + + + + + + + + + + + + ReachedFloorEvent + + + + + + + + + + + + + currentFloor : Integer + currentDirection : Direction + + + + + + + + + + + + sourcePortFig="Fig8.0" + destPortFig="Fig3" + sourceFigNode="Fig8" + destFigNode="Fig3" + + + + + + + + + + sourcePortFig="Fig4.0" + destPortFig="Fig3" + sourceFigNode="Fig4" + destFigNode="Fig3" + + + + + + + + + + + sourcePortFig="Fig6.0" + destPortFig="Fig3" + sourceFigNode="Fig6" + destFigNode="Fig3" + + + + + + + + + + + sourcePortFig="Fig7.0" + destPortFig="Fig3" + sourceFigNode="Fig7" + destFigNode="Fig3" + + + + + + + + + + sourcePortFig="Fig5.0" + destPortFig="Fig3" + sourceFigNode="Fig5" + destFigNode="Fig3" + + + + + + + diff --git a/diag_classes.uml~ b/diag_classes.uml~ deleted file mode 100644 index e69de29..0000000 diff --git a/src/Events/CallFromElevatorEvent.java b/src/Events/CallFromElevatorEvent.java index 24835a9..19ea80a 100644 --- a/src/Events/CallFromElevatorEvent.java +++ b/src/Events/CallFromElevatorEvent.java @@ -4,10 +4,10 @@ import commandSystem.Direction; public class CallFromElevatorEvent implements Event { - private final int wantedFloor; + private final int requestedFloor; - public CallFromElevatorEvent(int wantedFloor) { - this.wantedFloor = wantedFloor; + public CallFromElevatorEvent(int requestedFloor) { + this.requestedFloor = requestedFloor; } @Override @@ -17,7 +17,7 @@ public class CallFromElevatorEvent implements Event { @Override public int getRequestedFloor() { - return wantedFloor; + return requestedFloor; } @Override diff --git a/src/commandSystem/BasicInstructionQueue.java b/src/commandSystem/BasicInstructionQueue.java index 610fcb4..5f1b4f6 100644 --- a/src/commandSystem/BasicInstructionQueue.java +++ b/src/commandSystem/BasicInstructionQueue.java @@ -12,7 +12,7 @@ public class BasicInstructionQueue implements InstructionQueue { private boolean emergencyState; private int currentFloor; - private Direction direction; + private Direction currentDirection; public BasicInstructionQueue() { @@ -20,22 +20,7 @@ public class BasicInstructionQueue implements InstructionQueue { downQueue = new ArrayList(); emergencyState = false; currentFloor = 0; - direction = Direction.NONE; - } - - /** - * Adds a new floor to the argument's queue then sorts the elements. - * - * @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. - */ - private void appSort(ArrayList queue, int floor, boolean reversed) { - queue.add(floor); - if (reversed) - Collections.sort(queue, Collections.reverseOrder()); - else - Collections.sort(queue); + currentDirection = Direction.UP; } public void computeEvent(Event event) { @@ -79,7 +64,7 @@ public class BasicInstructionQueue implements InstructionQueue { /* * 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 + * then sorts it. If it's the same floor, doesn't do anything. */ case CALLFROMELEVATOR: if (event.getRequestedFloor() > currentFloor && !upQueue.contains(event.getRequestedFloor())) @@ -94,7 +79,7 @@ public class BasicInstructionQueue implements InstructionQueue { */ case EMERGENCYSTOP: emergencyState = true; - clearQueue(); + clearQueues(); break; /* @@ -109,8 +94,9 @@ public class BasicInstructionQueue implements InstructionQueue { */ case REACHEDFLOORSIGNAL: currentFloor = event.getCurrentFloor(); - direction = event.getCurrentDirection(); - removeInstruction(currentFloor); + currentDirection = event.getCurrentDirection(); + if (currentFloor == getNextInstruction()) + removeInstruction(currentFloor); break; default: @@ -118,32 +104,6 @@ public class BasicInstructionQueue implements InstructionQueue { } } - /** - * Removes one instruction from the queue - has to be called when the elevator - * reaches targeted floor. - * - * @param floor the reached floor - * @param direction the direction of the elevator - */ - private void removeInstruction(int floor) { - switch (direction) { - case UP: - if (upQueue.contains(floor)) - upQueue.remove(upQueue.indexOf(floor)); - else // we go up to the top of descending queue - downQueue.remove(downQueue.indexOf(floor)); - break; - case DOWN: - if (downQueue.contains(floor)) - downQueue.remove(downQueue.indexOf(floor)); - else // we go down to the bottom of ascending queue - upQueue.remove(upQueue.indexOf(floor)); - break; - default: - break; - } - } - /** * Returns the next floor the elevator has to go to * @@ -151,7 +111,10 @@ public class BasicInstructionQueue implements InstructionQueue { */ public int getNextInstruction() { int nextFloor = -1; - switch (direction) { + + //System.out.println("" + upQueue + downQueue); + + switch (currentDirection) { // get the first element of upQueue that is ABOVE the elevator's current floor case UP: for (int i = 0; i < upQueue.size() && nextFloor < 0; i++) { @@ -185,7 +148,60 @@ public class BasicInstructionQueue implements InstructionQueue { return nextFloor; } - private void clearQueue() { + /** + * Adds a new floor to the argument's queue then sorts the elements. + * + * @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. + */ + private void appSort(ArrayList queue, int floor, boolean reversed) { + queue.add(floor); + if (reversed) + Collections.sort(queue, Collections.reverseOrder()); + else + Collections.sort(queue); + } + + /** + * Removes one instruction from the queue - has to be called when the elevator + * reaches targeted floor. + * + * @param floor the reached floor. + * @param currentDirection the direction of the elevator. + */ + private void removeInstruction(int floor) { + switch (currentDirection) { + + /* + * Elevator is headed upwards. + */ + case UP: + if (upQueue.contains(floor)) + upQueue.remove(upQueue.indexOf(floor)); + else + downQueue.remove(downQueue.indexOf(floor)); + break; + + /* + * Elevator is headed downwards. + */ + case DOWN: + if (downQueue.contains(floor)) + downQueue.remove(downQueue.indexOf(floor)); + else + upQueue.remove(upQueue.indexOf(floor)); + break; + + default: + break; + } + } + + /** + * Clears both queues. + */ + private void clearQueues() { downQueue.removeAll(downQueue); upQueue.removeAll(upQueue); } diff --git a/src/commandSystem/ElevatorListener.java b/src/commandSystem/ElevatorListener.java index 9af97ea..6b0f032 100644 --- a/src/commandSystem/ElevatorListener.java +++ b/src/commandSystem/ElevatorListener.java @@ -5,4 +5,6 @@ public interface ElevatorListener { public void elevatorCall(int floor); public void floorCall(int floor, Direction direction); public void floorChange(Direction direction); + public void emergency(); + public void cancelEmergency(); } diff --git a/src/commandSystem/InstructionQueue.java b/src/commandSystem/InstructionQueue.java index df9bd80..ae1ed84 100644 --- a/src/commandSystem/InstructionQueue.java +++ b/src/commandSystem/InstructionQueue.java @@ -17,4 +17,4 @@ public interface InstructionQueue { * @return the next floor if it exists, else -1 */ public int getNextInstruction(); -} +} \ No newline at end of file diff --git a/src/commandSystem/Test.java b/src/commandSystem/Test.java index c0dd45e..7d260af 100644 --- a/src/commandSystem/Test.java +++ b/src/commandSystem/Test.java @@ -7,21 +7,24 @@ import Events.ReachedFloorEvent; public class Test { private static boolean running = false; - private static BasicInstructionQueue queue; + private static InstructionQueue queue; - private static Direction currentDirection; - private static int currentFloor; + private static Direction currentDirection = Direction.UP; + private static int currentFloor = 0; public static void iter() { int nextDestination = queue.getNextInstruction(); if (!running) { // would be -1 if the queue doesn't have any next instruction + + //System.out.println("next dest : " + nextDestination); if (nextDestination != -1) { running = true; currentDirection = currentFloor < nextDestination ? Direction.UP : Direction.DOWN; } } else { + //System.out.println("not running"); // if we just reached a new floor if (currentFloor == nextDestination) { queue.computeEvent(new ReachedFloorEvent(currentFloor, currentDirection)); @@ -32,16 +35,14 @@ public class Test { } } System.out.println("elevator floor " + currentFloor + " direction " + - (currentDirection == Direction.UP? "up" : "down") + " running: " + running); + (currentDirection) + " running: " + running); } public static void main(String[] args) { - //TODO envoyer les notifs de changements d'étages queue = new BasicInstructionQueue(); iter(); iter(); queue.computeEvent(new CallFromFloorEvent(2, Direction.DOWN)); - iter(); System.out.println("someone calls from floor 2 to go down"); iter(); queue.computeEvent(new CallFromFloorEvent(1, Direction.UP)); diff --git a/src/gui/ElevatorApplication.java b/src/gui/ElevatorApplication.java index fbf325a..96a9593 100644 --- a/src/gui/ElevatorApplication.java +++ b/src/gui/ElevatorApplication.java @@ -26,7 +26,8 @@ public class ElevatorApplication implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - canvas.setElevatorY((canvas.getElevatorY() + 1) % canvas.getHeight()); + elevator.update(); + canvas.repaint(); } private void createAndShowGUI() { @@ -42,11 +43,13 @@ public class ElevatorApplication implements ActionListener { canvas.setAlignmentY(Component.TOP_ALIGNMENT); pane.add(canvas); + pane.add(Box.createRigidArea(new Dimension(40, 0))); + FloorPanels fp = new FloorPanels(5, simulation); fp.setAlignmentY(Component.TOP_ALIGNMENT); pane.add(fp); - pane.add(Box.createRigidArea(new Dimension(20, 0))); + pane.add(Box.createRigidArea(new Dimension(40, 0))); ElevatorPanel elevatorPanel = new ElevatorPanel(5, simulation); elevatorPanel.setAlignmentY(Component.TOP_ALIGNMENT); diff --git a/src/gui/ElevatorCanvas.java b/src/gui/ElevatorCanvas.java index 0a16215..861e138 100644 --- a/src/gui/ElevatorCanvas.java +++ b/src/gui/ElevatorCanvas.java @@ -11,7 +11,6 @@ class ElevatorCanvas extends JPanel { private final static int HEIGHT = 50; private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200); - private int y = 0; private Elevator elevator; public ElevatorCanvas(Elevator elevator) { @@ -46,15 +45,7 @@ class ElevatorCanvas extends JPanel { else { g.setColor(Color.BLACK); } - g.fillRect(0, y, WIDTH, HEIGHT); - } - - public void setElevatorY(int y) { - this.y = y; - repaint(); - } - - public int getElevatorY() { - return y; + g.fillRect(0, 200 - (int) Math.floor(elevator.getHeight() * 200) - HEIGHT, WIDTH, HEIGHT); + Toolkit.getDefaultToolkit().sync(); } } diff --git a/src/gui/ElevatorPanel.java b/src/gui/ElevatorPanel.java index 99b78af..2893cd9 100644 --- a/src/gui/ElevatorPanel.java +++ b/src/gui/ElevatorPanel.java @@ -1,6 +1,7 @@ package gui; import javax.swing.*; +import java.awt.*; import java.awt.event.*; import commandSystem.ElevatorListener; @@ -9,6 +10,7 @@ import commandSystem.ElevatorListener; @SuppressWarnings("serial") class ElevatorPanel extends JPanel { private JButton emergencyStop = new JButton("Emergency stop."); + private JButton cancelEmergencyStop = new JButton("Cancel emergency stop."); private JButton[] buttons; public ElevatorPanel(int nbFloors, ElevatorListener l) { @@ -25,11 +27,20 @@ class ElevatorPanel extends JPanel { } }); this.add(buttons[i]); + this.add(Box.createRigidArea(new Dimension(0, 5))); } + emergencyStop.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + l.emergency(); + } + }); add(emergencyStop); - } - - public void addEmergencyStopListener(ActionListener l) { - emergencyStop.addActionListener(l); + this.add(Box.createRigidArea(new Dimension(0, 5))); + cancelEmergencyStop.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + l.cancelEmergency(); + } + }); + add(cancelEmergencyStop); } } diff --git a/src/gui/FloorPanels.java b/src/gui/FloorPanels.java index 6f3e983..4378203 100644 --- a/src/gui/FloorPanels.java +++ b/src/gui/FloorPanels.java @@ -22,6 +22,7 @@ class FloorPanels extends JPanel { floors[i] = new JPanel(); floors[i].setLayout(new BoxLayout(floors[i], BoxLayout.LINE_AXIS)); floors[i].add(new JLabel("" + i)); + floors[i].add(Box.createRigidArea(new Dimension(5, 0))); if (i < nbFloors - 1) { final int j = i; JButton upButton = new JButton("↑"); @@ -31,6 +32,7 @@ class FloorPanels extends JPanel { } }); floors[i].add(upButton); + floors[i].add(Box.createRigidArea(new Dimension(5, 0))); } if (i > 0) { final int j = i; @@ -43,6 +45,7 @@ class FloorPanels extends JPanel { floors[i].add(downButton); } this.add(floors[i]); + this.add(Box.createRigidArea(new Dimension(0, 5))); } } } diff --git a/src/simulation/Elevator.java b/src/simulation/Elevator.java index a0438d5..649bcfe 100644 --- a/src/simulation/Elevator.java +++ b/src/simulation/Elevator.java @@ -23,6 +23,7 @@ public class Elevator { height = (double) currentFloor / nbFloors; floorHeight = 1. / nbFloors; step = floorHeight / PRECISION; + System.out.println("step: " + step + ", floorHeight: " + floorHeight); } public void setActionListener(ElevatorListener listener) { @@ -41,7 +42,7 @@ public class Elevator { direction = Direction.DOWN; } - public void stopElevator() { + public void stop() { direction = Direction.NONE; } @@ -73,7 +74,7 @@ public class Elevator { /* Passed a floor, invoke the listener. */ if (Math.floor(height / floorHeight) < prevFloor && listener != null) { - listener.floorChange(Direction.UP); + listener.floorChange(Direction.DOWN); } // /* Go halfway below. */ // if (!betweenFloors) currentFloor--; @@ -89,8 +90,8 @@ public class Elevator { } /* Passed a floor, invoke the listener. */ - if (Math.floor(height / floorHeight) < prevFloor && listener != null) { - listener.floorChange(Direction.DOWN); + if (Math.floor(height / floorHeight) > prevFloor && listener != null) { + listener.floorChange(Direction.UP); } // /* Go halfway above. */ // if (betweenFloors) currentFloor++; diff --git a/src/simulation/Simulation.java b/src/simulation/Simulation.java index d761563..79e0725 100644 --- a/src/simulation/Simulation.java +++ b/src/simulation/Simulation.java @@ -6,12 +6,13 @@ import Events.CallFromFloorEvent; import commandSystem.Direction; import commandSystem.ElevatorListener; import simulation.Elevator; +import Events.*; public class Simulation implements ElevatorListener { private boolean running = false; private int currentFloor = 0; - private BasicInstructionQueue queue; + private BasicInstructionQueue queue = new BasicInstructionQueue(); private Elevator elevator; public Simulation(Elevator elevator) { @@ -19,15 +20,20 @@ public class Simulation implements ElevatorListener { } public void elevatorCall(int floor) { - System.out.println("elevator call " + floor); + System.out.println("Call from elevator: " + floor); + queue.computeEvent(new CallFromElevatorEvent(floor)); + update(); } public void floorCall(int floor, Direction direction) { - System.out.println("floor call " + floor + " " + direction); + System.out.println("Call from floor: " + floor + " going " + direction); + queue.computeEvent(new CallFromFloorEvent(floor, direction)); + update(); } - public void floorChange(Direction d) { - switch (d) { + public void floorChange(Direction direction) { + System.out.println("Floor reached: " + direction); + switch (direction) { case UP: currentFloor++; break; @@ -35,30 +41,32 @@ public class Simulation implements ElevatorListener { currentFloor--; break; } - // iter(); + queue.computeEvent(new ReachedFloorEvent(currentFloor, direction)); + update(); } - // public void iter() { - // int nextDestination = queue.getNextInstruction(); - // int currentFloor = 0; - // //System.out.println("next dest = " + nextDestination); - // if (!running) { - // if (nextDestination > 0) { /* We have a destination to go to. */ - // running = true; - // elevator.goDown(); - // } - // } - // else { - // if (elevator.currentFloor == nextDestination) { - // queue.removeInstruction(elevator.currentFloor, elevator.direction); - // running = false; - // } - // else { - // elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN; - // elevator.currentFloor += elevator.direction == Direction.UP ? 1 : -1; - // } - // } - // System.out.println("elevator floor " + elevator.currentFloor + " direction " + - // (elevator.direction == Direction.UP? "up" : "down") + " running: " + running); - // } + public void emergency() { + System.out.println("Emergency declared."); + } + + public void cancelEmergency() { + System.out.println("Emergency cancelled."); + } + + private void update() { + final int next = queue.getNextInstruction(); + System.out.println("Next instruction: " + next); + if (next == -1 || next == currentFloor) { + System.out.println("Stopping."); + elevator.stop(); + } + else if (next > currentFloor) { + System.out.println("Going up."); + elevator.goUp(); + } + else if (next < currentFloor) { + System.out.println("Going down."); + elevator.goDown(); + } + } }