001    package net.sf.cpsolver.studentsct.model;
002    
003    import java.util.HashSet;
004    import java.util.Iterator;
005    import java.util.Set;
006    import java.util.Vector;
007    
008    import net.sf.cpsolver.coursett.model.TimeLocation;
009    
010    /**
011     * Representation of a request of a student for free time.
012     * This class directly implements {@link Assignment} API, with the appropriate free time. 
013     * <br><br>
014     * 
015     * @version
016     * StudentSct 1.1 (Student Sectioning)<br>
017     * Copyright (C) 2007 Tomáš Müller<br>
018     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
019     * Lazenska 391, 76314 Zlin, Czech Republic<br>
020     * <br>
021     * This library is free software; you can redistribute it and/or
022     * modify it under the terms of the GNU Lesser General Public
023     * License as published by the Free Software Foundation; either
024     * version 2.1 of the License, or (at your option) any later version.
025     * <br><br>
026     * This library is distributed in the hope that it will be useful,
027     * but WITHOUT ANY WARRANTY; without even the implied warranty of
028     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029     * Lesser General Public License for more details.
030     * <br><br>
031     * You should have received a copy of the GNU Lesser General Public
032     * License along with this library; if not, write to the Free Software
033     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
034     */
035    public class FreeTimeRequest extends Request implements Assignment {
036        private TimeLocation iTime = null;
037        private HashSet iEnrollments = new HashSet();
038        
039        /** Constructor
040         * @param id request unique id
041         * @param priority request priority
042         * @param alternative true if the request is alternative (alternative request can be assigned instead of a non-alternative course requests, if it is left unassigned)
043         * @param student appropriate student
044         * @param time appropriate time location that is requested to be free
045         */
046        public FreeTimeRequest(long id, int priority, boolean alternative, Student student, TimeLocation time) {
047            super(id, priority, alternative, student);
048            iTime = time;
049        }
050        
051        /** Return requested time to be free */
052        public TimeLocation getTime() {
053            return iTime;
054        }
055    
056        /** Assignment API: free time request has no rooms */
057        public int getNrRooms() {
058            return 0;
059        }
060        
061        /** Assignment API: free time request has no rooms */
062        public Vector getRooms() {
063            return new Vector(0);
064        }
065        
066        /** True, if this assignment is overlapping in time and space with the given assignment. */
067        public boolean isOverlapping(Assignment assignment) {
068            if (getTime()==null || assignment.getTime()==null) return false;
069            if (assignment instanceof FreeTimeRequest) return false;
070            return getTime().hasIntersection(assignment.getTime());
071        }
072        
073        /** True, if this assignment is overlapping in time and space with the given set of assignments. */
074        public boolean isOverlapping(Set assignments) {
075            if (getTime()==null) return false;
076            for (Iterator i=assignments.iterator();i.hasNext();) {
077                Assignment assignment = (Assignment)i.next();
078                if (assignment.getTime()==null) continue;
079                if (assignment instanceof FreeTimeRequest) return false;
080                if (getTime().hasIntersection(assignment.getTime())) return true;
081            }
082            return false;
083        }
084        
085        /** Create enrollment of this request */
086        public Enrollment createEnrollment() {
087            HashSet assignments = new HashSet();
088            assignments.add(this);
089            return new Enrollment(this, 1.0, null, assignments);
090        }
091        
092        /** Create all possible enrollments of this request -- there is only one possible enrollment: {@link FreeTimeRequest#createEnrollment()}*/
093        public Vector computeEnrollments() {
094            Vector enrollments = new Vector(1);
095            enrollments.add(createEnrollment());
096            return enrollments;
097        }
098        
099        /** Enrollment with this assignmnet was assigned to a {@link Request}. */
100        public void assigned(Enrollment enrollment) {
101            iEnrollments.add(enrollment);
102        }
103        
104        /** Enrollment with this assignmnet was unassigned from a {@link Request}. */
105        public void unassigned(Enrollment enrollment) {
106            iEnrollments.remove(enrollment);
107        }
108        
109        /** Return the list of assigned enrollments that contains this assignment.*/
110        public Set getEnrollments() {
111            return iEnrollments;
112        }
113        
114        /** Request name: A for alternative, 1 + priority, Free Time, long name of requested time */
115        public String getName() {
116            return (isAlternative()?"A":"")+(1+getPriority()+(isAlternative()?-getStudent().nrRequests():0))+". Free Time "+getTime().getLongName();
117        }
118    
119        public String toString() {
120            return getName();
121        }
122        
123        /** Estimated bound for this request */
124        public double getBound() {
125            return - Math.pow(Enrollment.sPriorityWeight,getPriority()) * (isAlternative()?Enrollment.sAlterativeWeight:1.0);
126        }
127    }