scheduler.generate
Class Schedule

java.lang.Object
  extended by java.util.Observable
      extended by scheduler.generate.Schedule

public class Schedule
extends java.util.Observable

Contains the methods for generating a schedule. Most of the "magic" behind this class is hidden under the mask of the Generate_ui class. Thus, much of what is contained here is "under the hood". Furthermore, this class does not contain any of its own: all its information will come from the external, all-powerful database(s).

Author:
Eric Liebowitz, Leland Garofalo, and Aaron Rivera

Field Summary
protected  java.util.LinkedList<Course> cList
          Records how many courses are in the schedule (not number of sections)
private  java.util.Hashtable<Instructor,WeekAvail> iBookings
          Used to keep track of when instructors are booked
protected  java.util.LinkedList<Instructor> iList
          Records how many instructors are in the schedule
private  java.util.Hashtable<Location,WeekAvail> lBookings
          Used to keep track of when rooms are booked
protected  java.util.LinkedList<Location> lList
          Records how many locations are in the schedule
 java.util.Vector<ScheduleItem> s
          The generated schedule
 java.util.Vector<ScheduleItem> TBAs
          List of courses that had TBA locations.
 java.util.Hashtable<Instructor,Treatment> treatment
          What courses, times, and WTU's an instructor had been assigned
 
Constructor Summary
Schedule()
          Creates an empty schedule.
Schedule(java.util.Collection<ScheduleItem> s)
          Creates a schedule from a given vector of ScheduleItem's.
 
Method Summary
 boolean add(ScheduleItem si)
          Overrides "add", so as to put courses into the instance variable "cList" when appropriate.
private static void book(WeekAvail a, Week days, Time start, Time end)
          Books a span of time over a set of days in a hash (indexed by numerical days) of availabilities.
private static boolean check(WeekAvail a, Week days, Time start, Time end)
          Checks whether a given hash of availability has a given span of free time.
private  ScheduleItem createSI(Instructor i, Course c, ScheduledLocation l)
          Creates a ScheduleItem.
private  Time endForLec(Time s, int numOfDays)
           
private  Time endForLecLab(Time s, int numOfDays)
           
private  Course findCourse(java.util.LinkedList<Course> cdb, java.util.LinkedList<Instructor> idb, Instructor i)
          Finds the most suitable course for a given instructor to teach.
private  void findCourseCheckPre(java.util.LinkedList cdb, java.util.LinkedList idb, Instructor i)
          Checks the preconditions for "findIdealCourse(...)" and throws appropriate exceptions.
private  ScheduledLocation findLoc(java.util.LinkedList<Location> ldb, DaysAndTime dat, Course c)
          Finds a location for a given course for a given list of available days/times.
private  java.util.Vector<ScheduledLocation> findLocation(java.util.LinkedList<Location> ldb, java.util.Vector<java.util.Vector<DaysAndTime>> times, Course c)
          Finds the location for a course and, if it has one, its labs.
private  java.util.Vector<java.util.Vector<DaysAndTime>> findTime(java.util.LinkedList<Course> cdb, Instructor i, Course c)
          Finds a suitable time to apply to section
private  void findTimeCheckPre(java.util.LinkedList<Course> cdb, Course c)
          Checks preconditions regarding courses for the "findTime" method.
 void generate(java.util.LinkedList<Course> cdb, java.util.LinkedList<Instructor> idb, java.util.LinkedList<Location> ldb)
          Generates a schedule.
 java.util.LinkedList<Course> getCourseList()
          Returns the list of courses.
 java.util.LinkedList<Instructor> getInstructorList()
          Returns the list of instructors.
 java.util.LinkedList<Location> getLocationList()
          Returns the list of locations.
private  java.util.Vector<java.util.Vector<DaysAndTime>> getTimeForLec(java.util.ArrayList<TimePreference> tPrefs, Instructor i)
          Schedules days and times for 4-unit courses with no lab.
private  java.util.Vector<java.util.Vector<DaysAndTime>> getTimeForLecWithLab(java.util.ArrayList<TimePreference> tPrefs, Instructor i)
          Schedules days and times for 4-unit courses which have a lab.
private  void reset()
          Resets all data particular to the schedule.
private  void updateRecords(ScheduleItem si)
          Updates the c, i, and l lists for a given schedule item, along with the instructor's "treatment".
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

treatment

public java.util.Hashtable<Instructor,Treatment> treatment
What courses, times, and WTU's an instructor had been assigned


s

public java.util.Vector<ScheduleItem> s
The generated schedule


TBAs

public java.util.Vector<ScheduleItem> TBAs
List of courses that had TBA locations.


lBookings

private java.util.Hashtable<Location,WeekAvail> lBookings
Used to keep track of when rooms are booked


iBookings

private java.util.Hashtable<Instructor,WeekAvail> iBookings
Used to keep track of when instructors are booked


cList

protected java.util.LinkedList<Course> cList
Records how many courses are in the schedule (not number of sections)


iList

protected java.util.LinkedList<Instructor> iList
Records how many instructors are in the schedule


lList

protected java.util.LinkedList<Location> lList
Records how many locations are in the schedule

Constructor Detail

Schedule

public Schedule()
Creates an empty schedule.


Schedule

public Schedule(java.util.Collection<ScheduleItem> s)
Creates a schedule from a given vector of ScheduleItem's.

Parameters:
s - Vector of ScheduleItem's to create a schedule from.
Method Detail

getCourseList

public java.util.LinkedList<Course> getCourseList()
Returns the list of courses.
 Post:
 
    //Returned list must be equal to "cList"
    return.equals(this.cList)
 

Returns:
the list of courses

getInstructorList

public java.util.LinkedList<Instructor> getInstructorList()
Returns the list of instructors.
 Post:
 
    //Returned list must be equal to "iList"
    return.equals(this.iList)
 

Returns:
the list of instructors

getLocationList

public java.util.LinkedList<Location> getLocationList()
Returns the list of locations.
 Post:
 
    //Returned list must be equal to "lList"
    return.equals(this.lList)
 

Returns:
the list of locations

generate

public void generate(java.util.LinkedList<Course> cdb,
                     java.util.LinkedList<Instructor> idb,
                     java.util.LinkedList<Location> ldb)
Generates a schedule.

Parameters:
cdb - The list of courses to schedule
idb - The list of instructors with which to teach the courses
ldb - The list of location in which to teach the courses

reset

private void reset()
Resets all data particular to the schedule.


findCourse

private Course findCourse(java.util.LinkedList<Course> cdb,
                          java.util.LinkedList<Instructor> idb,
                          Instructor i)
                   throws NullCourseDatabaseException,
                          EmptyCourseDatabaseException,
                          NullInstructorException,
                          InstructorNotInDatabaseException,
                          InstructorWTUMaxedException,
                          InstructorCanTeachNothingException
Finds the most suitable course for a given instructor to teach.

Parameters:
cdb - Database of courses
idb - Database of instructors
i - The instructor to select for
Returns:
The most ideal course to give this instructor. If this instructor cannot be assigned any more classes (i.e. maxed wtu's; no classes) available), null.
 // **Pre and Post conditions** //
 
 Pre:

    //The course database cannot be null
    db.coursedb != null

       &&

    //The course database must have at least one entry
    db.courseDB.getSize() > 0


       &&

    //Instructor cannot be null
    instructor != null

       &&

    //Instructor must be in the instructor database
    db.instructorDB.isValidInstructor(instructor)

       &&

    //Instructor "i" has a Treatment entry in the treatment hash
    Generate.treatment.get(i) != null

 Post:

    //The class returned must be one the instructor is "qualified" to teach
    instructor.getPref(return) > 0

       &&

    //The returned class must be in the course database
    cdb.contains(return)

       &&

    //The returned class must not push the instructor over his max WTU 
    //count
    treatment.get(i).wtu + return.getWTU() <=
        instructor.getMaxWTU()
 
Throws:
NullCourseDatabaseException
EmptyCourseDatabaseException
NullInstructorException
InstructorNotInDatabaseException
InstructorWTUMaxedException
InstructorCanTeachNothingException

findCourseCheckPre

private void findCourseCheckPre(java.util.LinkedList cdb,
                                java.util.LinkedList idb,
                                Instructor i)
                         throws NullCourseDatabaseException,
                                EmptyCourseDatabaseException,
                                NullInstructorException,
                                InstructorNotInDatabaseException,
                                InstructorWTUMaxedException,
                                InstructorCanTeachNothingException
Checks the preconditions for "findIdealCourse(...)" and throws appropriate exceptions. NOTE: If there is no entry for a given instructor in the Treatment hash, an empty entry will be immediately created, so as to avoid those nasty NullPointerException's.

Parameters:
cdb - The course database
idb - The instructor database
i - The instructor
Throws:
NullCourseDatabaseException
EmptyCourseDatabaseException
NullInstructorException
InstructorNotInDatabaseException
InstructorWTUMaxedException
InstructorCanTeachNothingException

findTime

private java.util.Vector<java.util.Vector<DaysAndTime>> findTime(java.util.LinkedList<Course> cdb,
                                                                 Instructor i,
                                                                 Course c)
                                                          throws NullCourseException,
                                                                 CourseNotInDatabaseException
Finds a suitable time to apply to section

Parameters:
i - The instructor to use in search
c - The course that is being used to search for a time
Returns:
Suitable time to teach specific course
 // ** Pre and Post conditions ** //
 
 Pre:

 // Instructor value can not be null
 i != nil

 &&

 // Course value can not be null
 c != nil

 &&

  //Instructor must be in the instructor database
  db.instructorDB.isValidInstructor(i)

 &&

 // Course must be in the course database
 db.coursedb.isValidCourse(c)

 Post:

 //The returned time must not conflict with instructors teaching times
 db.instructorDB.isValidTime(instructor,time)

 &&
 
 //The returned time must not violate any schedule attributes
 scheduler.isValidTime(time)

 
Throws:
NullCourseException
CourseNotInDatabaseException

findTimeCheckPre

private void findTimeCheckPre(java.util.LinkedList<Course> cdb,
                              Course c)
                       throws NullCourseException,
                              CourseNotInDatabaseException
Checks preconditions regarding courses for the "findTime" method.

Parameters:
cdb - The course database
c - The course being scheduled
Throws:
NullCourseException
CourseNotInDatabaseException

getTimeForLecWithLab

private java.util.Vector<java.util.Vector<DaysAndTime>> getTimeForLecWithLab(java.util.ArrayList<TimePreference> tPrefs,
                                                                             Instructor i)
Schedules days and times for 4-unit courses which have a lab. Each course has 3 hours of lecture and 3 hours of lab per week, for a total of 6 hours. Will collect as many acceptable permutations of 3-day and 2-day schedules for a course.

Parameters:
tPrefs - The list of the instructor's time preference.
i - The instructor to schedule.
Returns:
A vector of vectors of DaysAndTime objects. Each entry in the first vector will yield a vector of length two: the first entry is for the lecture; the second entry is for the lab.

getTimeForLec

private java.util.Vector<java.util.Vector<DaysAndTime>> getTimeForLec(java.util.ArrayList<TimePreference> tPrefs,
                                                                      Instructor i)
Schedules days and times for 4-unit courses with no lab. Each course has 4 of lecture per week. Will collect as many acceptable permutations of 4-day and 2-day schedules for a course.

Parameters:
tPrefs - The list of the instructor's time preference.
i - The instructor to schedule.
Returns:
A Vector of Vectors of DaysAndTime objects. Each entry in the first vector will yield a vector of length one, containing the DaysAndTime object for the course. Though it may not seem that a Vector of Vectors isn't needed here (it isn't), courses with labs do. So, to keep things consistent, this method returns the same thing "getTimeForLecWithLab" returns.

endForLecLab

private Time endForLecLab(Time s,
                          int numOfDays)

endForLec

private Time endForLec(Time s,
                       int numOfDays)

findLocation

private java.util.Vector<ScheduledLocation> findLocation(java.util.LinkedList<Location> ldb,
                                                         java.util.Vector<java.util.Vector<DaysAndTime>> times,
                                                         Course c)
Finds the location for a course and, if it has one, its labs.

Parameters:
ldb - List of locations to try
time - List of day/time combinations to try
c - The course to find a location for (contains its lab, if any).
Returns:
Location for a course and, if it has one, its lab.

findLoc

private ScheduledLocation findLoc(java.util.LinkedList<Location> ldb,
                                  DaysAndTime dat,
                                  Course c)
Finds a location for a given course for a given list of available days/times.

Parameters:
ldb - List of locations to try
times - Day/time combination to try
c - The course to put in the returned location.
Returns:
A ScheduledLocation object "c" can be taught in, or "TBA" if none was found.

createSI

private ScheduleItem createSI(Instructor i,
                              Course c,
                              ScheduledLocation l)
Creates a ScheduleItem.

Parameters:
i - The instructor
c - The course
l - The location and day/time pair
Returns:
A ScheduleItem containing all the given parameters

add

public boolean add(ScheduleItem si)
Overrides "add", so as to put courses into the instance variable "cList" when appropriate.

Parameters:
si - ScheduleItem to add
 Pre:
    
    //Can't add a null ScheduleItem. Note, the exception for this
    //should already be caught in the "super"'s "add"
    si != null

       &&

    //"si" cannot double book a room
    for (Integer d: lBookings.get(si.l).keySet())
    {
       if (days[d])
       {
          lBookings.get(si.l).get(d).isFree(si.start, si.end))
       }
    }

       &&

    //"si" cannot put an instructor in two places at the same time
    for (Integer d: iBookings.get(si.i).keySet())
    {
       if (days[d])
       {
          iBookings.get(si.l).get(d).isFree(si.start, si.end))
       }
    }

 Post:
 
    //Only thing different about old schedule and new schedule is "si"
    for (ScheduleItem si': s'))
    { 
       if (!s.contains(si')
       {
          si == si';
       }
    }
 

check

private static boolean check(WeekAvail a,
                             Week days,
                             Time start,
                             Time end)
Checks whether a given hash of availability has a given span of free time.

Parameters:
a - Hash of availability
days - Days to consider for span of availability
start - Beginning of the given span of time
end - End of the given span of time
Returns:
true if the time span from "start" to "end" is free for all "days"

book

private static void book(WeekAvail a,
                         Week days,
                         Time start,
                         Time end)
Books a span of time over a set of days in a hash (indexed by numerical days) of availabilities.

Parameters:
a - Hash of availabilities
days - Set of days
start - Beginning of span of time
end - End of span of time

updateRecords

private void updateRecords(ScheduleItem si)
Updates the c, i, and l lists for a given schedule item, along with the instructor's "treatment".

Parameters:
si - The ScheduleItem containing all the information needed