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    public PreferenceCombination() {}
039    
040    public PreferenceCombination(PreferenceCombination c) {
041        iIsProhibited = c.iIsProhibited;
042        iIsRequired = c.iIsRequired;
043    }
044
045    /** Add preference a preference 
046     * @param prologPref prolog preference (R for required, P for prohibited, -2 strongly preferred, ... 2 strongly discouraged)
047     **/
048    public void addPreferenceProlog(String prologPref) {
049        addPreferenceInt(Constants.preference2preferenceLevel(prologPref));
050    }
051
052    /** Returns combined preference from the given preferences 
053     * @param intPref preference
054     **/
055    public void addPreferenceInt(int intPref) {
056        String prologPref = Constants.preferenceLevel2preference(intPref);
057        if (Constants.sPreferenceRequired.equals(prologPref))
058            iIsRequired = true;
059        if (Constants.sPreferenceProhibited.equals(prologPref))
060            iIsProhibited = true;
061    }
062
063    public boolean isRequired() {
064        return iIsRequired && !iIsProhibited;
065    }
066
067    public boolean isProhibited() {
068        return iIsProhibited;
069    }
070    
071    public abstract PreferenceCombination clonePreferenceCombination();
072
073    public abstract int getPreferenceInt();
074
075    public String getPreferenceProlog() {
076        if (iIsProhibited)
077            return Constants.sPreferenceProhibited;
078        if (iIsRequired)
079            return Constants.sPreferenceRequired;
080        return Constants.preferenceLevel2preference(getPreferenceInt());
081    }
082
083    public static PreferenceCombination getDefault() {
084        return new SumPreferenceCombination();
085    }
086}