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 * @author  Tomáš Müller
016 * @version CourseTT 1.3 (University Course Timetabling)<br>
017 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
018 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
019 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
020 * <br>
021 *          This library is free software; you can redistribute it and/or modify
022 *          it under the terms of the GNU Lesser General Public License as
023 *          published by the Free Software Foundation; either version 3 of the
024 *          License, or (at your option) any later version. <br>
025 * <br>
026 *          This library is distributed in the hope that it will be useful, but
027 *          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. <br>
030 * <br>
031 *          You should have received a copy of the GNU Lesser General Public
032 *          License along with this library; if not see
033 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
034 */
035public abstract class PreferenceCombination {
036    boolean iIsRequired = false;
037    boolean iIsProhibited = false;
038    
039    public PreferenceCombination() {}
040    
041    public PreferenceCombination(PreferenceCombination c) {
042        iIsProhibited = c.iIsProhibited;
043        iIsRequired = c.iIsRequired;
044    }
045
046    /** Add preference a preference 
047     * @param prologPref prolog preference (R for required, P for prohibited, -2 strongly preferred, ... 2 strongly discouraged)
048     **/
049    public void addPreferenceProlog(String prologPref) {
050        addPreferenceInt(Constants.preference2preferenceLevel(prologPref));
051    }
052
053    /** Returns combined preference from the given preferences 
054     * @param intPref preference
055     **/
056    public void addPreferenceInt(int intPref) {
057        String prologPref = Constants.preferenceLevel2preference(intPref);
058        if (Constants.sPreferenceRequired.equals(prologPref))
059            iIsRequired = true;
060        if (Constants.sPreferenceProhibited.equals(prologPref))
061            iIsProhibited = true;
062    }
063
064    public boolean isRequired() {
065        return iIsRequired && !iIsProhibited;
066    }
067
068    public boolean isProhibited() {
069        return iIsProhibited;
070    }
071    
072    public abstract PreferenceCombination clonePreferenceCombination();
073
074    public abstract int getPreferenceInt();
075
076    public String getPreferenceProlog() {
077        if (iIsProhibited)
078            return Constants.sPreferenceProhibited;
079        if (iIsRequired)
080            return Constants.sPreferenceRequired;
081        return Constants.preferenceLevel2preference(getPreferenceInt());
082    }
083
084    public static PreferenceCombination getDefault() {
085        return new SumPreferenceCombination();
086    }
087}