001package net.sf.cpsolver.coursett.preference;
002
003import net.sf.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.2 (University Course Timetabling)<br>
016 *          Copyright (C) 2006 - 2010 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    public void addPreferenceProlog(String prologPref) {
040        addPreferenceInt(Constants.preference2preferenceLevel(prologPref));
041    }
042
043    /** Returns combined preference from the given preferences */
044    public void addPreferenceInt(int intPref) {
045        String prologPref = Constants.preferenceLevel2preference(intPref);
046        if (Constants.sPreferenceRequired.equals(prologPref))
047            iIsRequired = true;
048        if (Constants.sPreferenceProhibited.equals(prologPref))
049            iIsProhibited = true;
050    }
051
052    public boolean isRequired() {
053        return iIsRequired && !iIsProhibited;
054    }
055
056    public boolean isProhibited() {
057        return iIsProhibited;
058    }
059
060    public abstract int getPreferenceInt();
061
062    public String getPreferenceProlog() {
063        if (iIsProhibited)
064            return Constants.sPreferenceProhibited;
065        if (iIsRequired)
066            return Constants.sPreferenceRequired;
067        return Constants.preferenceLevel2preference(getPreferenceInt());
068    }
069
070    public static PreferenceCombination getDefault() {
071        return new SumPreferenceCombination();
072    }
073}