41 lines
2.1 KiB
Markdown

# Logic
## Details:
***Stylised text in <span style="color:green">green</span> represents the actual lift metaphor***
***<span style="color:red">Red</span> represents significant changes to the program***
## Initial State:
* Lifts all at the bottom of the building
* People all outside the building
* not waiting on an arrow
* not in a lift
## Loop:
### *These are performed simulataneously, with a thread for each instance*
---
* ### Lift Thread - *this is a black-box as far as the metaphor goes*
1. While there are people to unload at this floor, unload them
2. Call `get_into_lift(Lift, Direction)`, to load people from the apporopriate waiting queue (up | down) from the current floor
3. Redraw
3. Change position
* `get_into_lift()`
1. Fetch the correct semaphor for our queue
2. Get the number of waiting people on that queue
3. While there are people waiting:
1. If the lift is empty, change our direction to the supplied direction
* <span style="color:red">A semaphore is used before the conditional to help ensure the correct value of `in progress` transfers is correct</span>
2. While the lift is not full, and we have people waiting:
* <span style="color:red">Keep track of the number of `in progress` waiting people</span>
1. Add a person to the lift.
2. Redraw the waiting queue
3. Remove the person from the waiting queue
4. Signal person to actually get into the lift
---
* ### Person Thread - *this is modelled around the actual metaphor*
1. Choose a floor to enter
2. Decide if the floor is above or below the current position
3. Add ourselves to the appropriate move (up | down) group <span style="color:green">Press the `call elevator` button</span>
4. <span style="color:green">Wait for the lift to arrive</span> *on the appropriate spot (up | down); no metaphor for this concept*
5. Enter the lift that is waiting
6. Move to the waiting group for the target floor <span style="color:green">Press the target floor button</span>
7. Wait until the lift reaches our destination floor and leave when it is our turn to exit.