Class Simulation

java.lang.Object
  |
  +--Simulation
Direct Known Subclasses:
Simulation1, Simulation2

public abstract class Simulation
extends java.lang.Object

A class to run the lunch simulation problem. This is an abstract class, because we are going to run two different seating schemes: each of these schemes will be implemented by the classes Simulation1 and Simulation2

See Also:
Clock, Event, Party, Simulation1, Simulation2, SimulationStats

Field Summary
static int MAX_PARTY_SIZE
          The maximum possible size of a party
static int MIN_PARTY_SIZE
          The minimum possible size of a party
static int[] PARTY_SIZES
          An array of all of the various party sizes.
protected  Clock schedule
          The schedule to which all events will be added and extracted
protected  SimulationStats stats
          Keeps track of all of the statistics for this Simulation
static int[] TABLE_COUNTS
          An array containing the the number of tables for the index's size.
protected  int[] tablesTaken
          An array of size MAX_PARTY_SIZE + 1 which keeps track of how many tables have been taken for the index's size.
protected static EventTimer timer
          The object used to get the times for all of the events
 
Constructor Summary
protected Simulation(long seed1, long seed2)
          Creates a new lunch Simulation: initializing tablesTaken, and adding all arrival events to the schedule.
 
Method Summary
protected  void addArrivals()
          Adds all of the arrivals to the schedule in a uniform fashion.
protected abstract  void addToQueue(Party p)
          If a party is not able to find a table to be seated at, they will be inserted into some queue.
protected  double arrivalTime()
          Returns a arrival time.
protected  double eatingTime()
          Returns an eating time.
protected abstract  int findTable(Party p)
          A method to choose the table the party will sit at.
protected abstract  void freeTable(int tableSize)
          Frees a table of the specified size.
protected  boolean isFree(int tableSize)
          Checks is there is a table of the specified size available for immediate seating.
protected  void processArrival(ArrivalEvent e)
          A method to process an arrival event.
protected  void processDeparture(DepartureEvent e)
          A method to process a departure event.
protected  void processEvent(Event e)
          This method forwards an event to either processArrival or processDeparture, depending upon the type of the event.
 SimulationStats runSimulation()
          Processes events until there are no more events to process, returning a summary of the simulation statistics
protected  void seatAtTable(Party p, int tableNum)
          Seats a party at a certain table size.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

MAX_PARTY_SIZE

public static final int MAX_PARTY_SIZE
The maximum possible size of a party

MIN_PARTY_SIZE

public static final int MIN_PARTY_SIZE
The minimum possible size of a party

PARTY_SIZES

public static final int[] PARTY_SIZES
An array of all of the various party sizes. The length of the array is MAX_PARTY_SIZE + 1

TABLE_COUNTS

public static final int[] TABLE_COUNTS
An array containing the the number of tables for the index's size. The number of elements in the array is MAX_PARTY_SIZE + 1

schedule

protected Clock schedule
The schedule to which all events will be added and extracted

timer

protected static EventTimer timer
The object used to get the times for all of the events

tablesTaken

protected int[] tablesTaken
An array of size MAX_PARTY_SIZE + 1 which keeps track of how many tables have been taken for the index's size. Note that element at an index i has a value between 0 and TABLE_COUNTS[i], inclusive.

stats

protected SimulationStats stats
Keeps track of all of the statistics for this Simulation
Constructor Detail

Simulation

protected Simulation(long seed1,
                     long seed2)
Creates a new lunch Simulation: initializing tablesTaken, and adding all arrival events to the schedule. Seeds the EventTimer with the specified seeds
Parameters:
seed1 - a value used to seed the EventTimer
seed2 - a value used to seed the EventTimer
Method Detail

addArrivals

protected void addArrivals()
Adds all of the arrivals to the schedule in a uniform fashion.

runSimulation

public SimulationStats runSimulation()
Processes events until there are no more events to process, returning a summary of the simulation statistics
Returns:
the statistics for this Simulation
See Also:
SimulationStats

processEvent

protected void processEvent(Event e)
This method forwards an event to either processArrival or processDeparture, depending upon the type of the event.
Parameters:
e - the event to be processed (forwarded)

processArrival

protected void processArrival(ArrivalEvent e)
A method to process an arrival event. The method extracts the party from the event, and then tries to find a table for that party. If a table is found, the party is seated at that table; otherwise the party will have to wait in some sort of queue.
Parameters:
e - the arrival event to be processed

findTable

protected abstract int findTable(Party p)
A method to choose the table the party will sit at. This is an abstract method, as different schemes may seat parties in a different fashion
Parameters:
p - the party for which a table is to be found

addToQueue

protected abstract void addToQueue(Party p)
If a party is not able to find a table to be seated at, they will be inserted into some queue. This is an abstract method, as different schemes may have use different queues for waiting parties.
Parameters:
p - the party that has to wait for a table to open up.

seatAtTable

protected void seatAtTable(Party p,
                           int tableNum)
Seats a party at a certain table size. The appropriate tablesTaken element is incremented. Then, a new departure event is added to the schedule signifying when this party has finished eating.

processDeparture

protected void processDeparture(DepartureEvent e)
A method to process a departure event. This method extracts the size of the table the party was seated at, then frees a table of that size.
Parameters:
e - the departure event to be processed

freeTable

protected abstract void freeTable(int tableSize)
Frees a table of the specified size. This is an abstract method, as different schemes will do different things with a recently freed table.
Parameters:
tableSize - the size of the table to be freed

isFree

protected boolean isFree(int tableSize)
Checks is there is a table of the specified size available for immediate seating. A table is "free" is the number of tables taken of the specified size is less than the number of tables of that size.
Parameters:
tableSize - the size of the table to be queried
Returns:
true iff there is a table of size tableSize free

eatingTime

protected double eatingTime()
Returns an eating time. Uses the timer to find the next eating time

arrivalTime

protected double arrivalTime()
Returns a arrival time. Uses the timer to find the next arrival time