    001package org.cpsolver.instructor.model;
    002
    003import org.cpsolver.ifs.assignment.Assignment;
    004import org.cpsolver.ifs.criteria.Criterion;
    005import org.cpsolver.ifs.model.Value;
    006
    007/**
    008 * Teaching assignment. An assignment of an instructor to a teaching request (a set of sections of a course).
    009 * A teaching assignment also contains the value of attribute, instructor, course, and time preferences.
    010 *
    011 * @version IFS 1.3 (Instructor Sectioning)<br>
    012 *          Copyright (C) 2016 Tomáš Müller<br>
    013 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    014 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    015 * <br>
    016 *          This library is free software; you can redistribute it and/or modify
    017 *          it under the terms of the GNU Lesser General Public License as
    018 *          published by the Free Software Foundation; either version 3 of the
    019 *          License, or (at your option) any later version. <br>
    020 * <br>
    021 *          This library is distributed in the hope that it will be useful, but
    022 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    023 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    024 *          Lesser General Public License for more details. <br>
    025 * <br>
    026 *          You should have received a copy of the GNU Lesser General Public
    027 *          License along with this library; if not see
    028 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    029 */
    030public class TeachingAssignment extends Value<TeachingRequest.Variable, TeachingAssignment> {
    031    private Instructor iInstructor;
    032    private int iHashCode;
    033    private int iAttributePreference, iInstructorPreference, iCoursePreference, iTimePreference;
    034
    035    /**
    036     * Constructor
    037     * @param variable teaching request variable
    038     * @param instructor instructor (it is expected that {@link Instructor#canTeach(TeachingRequest)} is true and that {@link TeachingRequest#getAttributePreference(Instructor)} is not prohibited)
    039     * @param attributePreference attribute preference (value of {@link TeachingRequest#getAttributePreference(Instructor)})
    040     */
    041    public TeachingAssignment(TeachingRequest.Variable variable, Instructor instructor, int attributePreference) {
    042        super(variable);
    043        iInstructor = instructor;
    044        iHashCode = variable.hashCode() ^ instructor.hashCode();
    045        iTimePreference = instructor.getTimePreference(variable.getRequest()).getPreferenceInt();
    046        iCoursePreference = instructor.getCoursePreference(variable.getCourse()).getPreference();
    047        iInstructorPreference = variable.getRequest().getInstructorPreference(instructor).getPreference();
    048        iAttributePreference = attributePreference;
    049    }
    050
    051    /**
    052     * Constructor
    053     * @param variable teaching request variable
    054     * @param instructor instructor (it is expected that {@link Instructor#canTeach(TeachingRequest)} is true and that {@link TeachingRequest#getAttributePreference(Instructor)} is not prohibited)
    055     */
    056    public TeachingAssignment(TeachingRequest.Variable variable, Instructor instructor) {
    057        this(variable, instructor, variable.getRequest().getAttributePreference(instructor).getPreferenceInt());
    058    }
    059
    060    /**
    061     * Assigned instructor
    062     * @return assigned instructor
    063     */
    064    public Instructor getInstructor() {
    065        return iInstructor;
    066    }
    067
    068    @Override
    069    public double toDouble(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) {
    070        double ret = 0.0;
    071        for (Criterion<TeachingRequest.Variable, TeachingAssignment> criterion : variable().getModel().getCriteria())
    072            ret += criterion.getWeightedValue(assignment, this, null);
    073        return ret;
    074    }
    075
    076    @Override
    077    public boolean equals(Object o) {
    078        if (o == null || !(o instanceof TeachingAssignment))
    079            return false;
    080        TeachingAssignment a = (TeachingAssignment) o;
    081        return variable().equals(a.variable()) && getInstructor().equals(a.getInstructor());
    082    }
    083
    084    /**
    085     * Attribute preference
    086     * @return {@link TeachingRequest#getAttributePreference(Instructor)}
    087     */
    088    public int getAttributePreference() {
    089        return iAttributePreference;
    090    }
    091
    092    /**
    093     * Time preference
    094     * @return {@link Instructor#getTimePreference(TeachingRequest)}
    095     */
    096    public int getTimePreference() {
    097        return iTimePreference;
    098    }
    099
    100    /**
    101     * Instructor preference
    102     * @return {@link TeachingRequest#getInstructorPreference(Instructor) }
    103     */
    104    public int getInstructorPreference() {
    105        return iInstructorPreference;
    106    }
    107
    108    /**
    109     * Course preference
    110     * @return {@link Instructor#getCoursePreference(Course)} for {@link TeachingRequest#getCourse()}
    111     */
    112    public int getCoursePreference() {
    113        return iCoursePreference;
    114    }
    115
    116    @Override
    117    public int hashCode() {
    118        return iHashCode;
    119    }
    120
    121    @Override
    122    public String toString() {
    123        return variable().getName() + ": " + getInstructor().getName();
    124    }
    125
    126    @Override
    127    public String getName() {
    128        return (getInstructor().hasName() ? getInstructor().getName() + (getInstructor().hasExternalId() ? " (" + getInstructor().getExternalId() + ")" : "") : getInstructor().getName());
    129    }
    130}