001package org.cpsolver.coursett.preference;
002
003import org.cpsolver.coursett.Constants;
004
005/**
006 * Preference combination. <br>
007 * <br>
008 * A preference can be:
009 * <ul>
010 * <li>R .. required
011 * <li>P .. prohibited
012 * <li>number .. soft preference (smaller value is better)
013 * </ul>
014 * 
015 * @version CourseTT 1.3 (University Course Timetabling)<br>
016 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
017 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
018 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
019 * <br>
020 *          This library is free software; you can redistribute it and/or modify
021 *          it under the terms of the GNU Lesser General Public License as
022 *          published by the Free Software Foundation; either version 3 of the
023 *          License, or (at your option) any later version. <br>
024 * <br>
025 *          This library is distributed in the hope that it will be useful, but
026 *          WITHOUT ANY WARRANTY; without even the implied warranty of
027 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
028 *          Lesser General Public License for more details. <br>
029 * <br>
030 *          You should have received a copy of the GNU Lesser General Public
031 *          License along with this library; if not see
032 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
033 */
034public abstract class PreferenceCombination {
035    boolean iIsRequired = false;
036    boolean iIsProhibited = false;
037
038    /** Add preference a preference 
039     * @param prologPref prolog preference (R for required, P for prohibited, -2 strongly preferred, ... 2 strongly discouraged)
040     **/
041    public void addPreferenceProlog(String prologPref) {
042        addPreferenceInt(Constants.preference2preferenceLevel(prologPref));
043    }
044
045    /** Returns combined preference from the given preferences 
046     * @param intPref preference
047     **/
048    public void addPreferenceInt(int intPref) {
049        String prologPref = Constants.preferenceLevel2preference(intPref);
050        if (Constants.sPreferenceRequired.equals(prologPref))
051            iIsRequired = true;
052        if (Constants.sPreferenceProhibited.equals(prologPref))
053            iIsProhibited = true;
054    }
055
056    public boolean isRequired() {
057        return iIsRequired && !iIsProhibited;
058    }
059
060    public boolean isProhibited() {
061        return iIsProhibited;
062    }
063
064    public abstract int getPreferenceInt();
065
066    public String getPreferenceProlog() {
067        if (iIsProhibited)
068            return Constants.sPreferenceProhibited;
069        if (iIsRequired)
070            return Constants.sPreferenceRequired;
071        return Constants.preferenceLevel2preference(getPreferenceInt());
072    }
073
074    public static PreferenceCombination getDefault() {
075        return new SumPreferenceCombination();
076    }
077}