001    package ttsolver.model;
002    
003    import ifs.model.Value;
004    import edu.purdue.smas.timetable.data.Preferences;
005    import edu.purdue.smas.timetable.data.pattern.EveningTimePattern;
006    import edu.purdue.smas.timetable.data.pattern.GenericTimePattern;
007    import edu.purdue.smas.timetable.data.pattern.SaturdayTimePattern;
008    
009    /**
010     * Placement (value).
011     * <br><br>
012     * It combines room and time location
013     *
014     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
015     * @version 1.0
016     */
017    
018    public class Placement extends Value {
019        private TimeLocation iTimeLocation;
020        private RoomLocation iRoomLocation;
021        private boolean iIsInitial = false;
022        
023        /** Constructor
024         * @param lecture lecture
025         * @param timeLocation time location
026         * @param roomLocation room location
027         */
028        public Placement(Lecture lecture, TimeLocation timeLocation, RoomLocation roomLocation) {
029            super(lecture);
030            iTimeLocation = timeLocation;
031            iRoomLocation = roomLocation;
032        }
033        
034        /** Time location */
035        public TimeLocation getTimeLocation() { return iTimeLocation; }
036        /** Room id */
037        public String getRoomId() { return iRoomLocation.getId(); }
038        /** Building id */
039        public String getBuildingId() { return iRoomLocation.getBuildingId(); }
040        /** Room location */
041        public RoomLocation getRoomLocation() { return iRoomLocation; }
042        /** Instructor id */
043        public String getInstructorId() { return ((Lecture)variable()).getInstructorId(); }
044        public String getName() { return getTimeLocation().getText()+" "+getRoomLocation().getName()+(((Lecture)variable()).getInstructorName()!=null?" "+((Lecture)variable()).getInstructorName():getInstructorId()==null?"":" "+getInstructorId()); }
045    
046        /** Is initial placement */
047        public boolean isInitial() { return iIsInitial; }
048        /** Is initial placement */
049        public void setInitial(boolean initial) { iIsInitial=initial; }
050        
051        public boolean equals(Object object) {
052            if (object==null || !(object instanceof Placement)) return false;
053            Placement placement = (Placement)object;
054            if (placement.getId()==getId()) return true; //quick check
055    //        if (((Lecture)placement.variable()).getClassId()!=((Lecture)variable()).getClassId()) return false;
056            Lecture lecture = (Lecture)placement.variable();
057            Lecture thisLecture = (Lecture)variable();
058            if (lecture!=null && thisLecture!=null && lecture.getClassId()!=thisLecture.getClassId()) return false;
059            return (placement.getTimeLocation()==getTimeLocation() && 
060                    placement.getRoomId().equals(getRoomId()) &&
061                        (placement.getInstructorId()==getInstructorId() || 
062                            (placement.getInstructorId()!=null && 
063                                placement.getInstructorId().equals(getInstructorId())
064                            )
065                        )
066                   );
067        }
068        
069        public String toString() {
070            return variable().getName()+" "+getName();
071            //return "Placement{lecture="+variable().getName()+", time="+getTimeLocation().getText()+", len="+getTimeLocation().getLength()+", instructor="+iInstructorId+", room="+iRoomLocation.getName()+", timePref="+iTimeLocation.getNormalizedPreference()+", roomPref="+iRoomLocation.getPreference()+"}";
072        }
073        
074        
075        public String getDescription() {
076            return iTimeLocation.getNrMeetings()+"x"+(25*iTimeLocation.getNrHalfHoursPerMeeting())+
077                   (iTimeLocation.getModel()==null?"":
078                   (iTimeLocation.getModel() instanceof GenericTimePattern  ? " gen.":"") +
079                   (iTimeLocation.getModel() instanceof SaturdayTimePattern ? " Sat.":"") +
080                   (iTimeLocation.getModel() instanceof EveningTimePattern  ? " evn.":"")) +
081                   (iTimeLocation.getPreference()!=0 || iTimeLocation.getNormalizedPreference()!=0.0?", "+Preferences.getPreference(String.valueOf(iTimeLocation.getPreference())).getName().toLowerCase()+" time ("+iTimeLocation.getNormalizedPreference()+")":"")+
082                   (iRoomLocation.getPreference()!=0?(iRoomLocation.getPreference()>=-2 && iRoomLocation.getPreference()<=2?", "+Preferences.getPreference(String.valueOf(iRoomLocation.getPreference())).getName().toLowerCase()+" room":", "+iRoomLocation.getPreference()+" room"):"");
083        }
084        
085        /** Distance between two placements */
086        public static double getDistance(Placement p1, Placement p2) {
087            if (p1.getRoomId().equals(p2.getRoomId())) return 0.0;
088            RoomLocation r1=p1.getRoomLocation();
089            RoomLocation r2=p2.getRoomLocation();
090            long x = r1.getPosX()-r2.getPosX();
091            long y = r1.getPosY()-r2.getPosY();
092            return Math.sqrt((x*x)+(y*y));
093        }
094        /** True if the two given placements are using at one same day*/
095        public static boolean shareDays(Placement p1, Placement p2) {
096            return ((p1.getTimeLocation().getDayCode() & p2.getTimeLocation().getDayCode())!=0);
097        }
098    }