001    package net.sf.cpsolver.coursett.preference;
002    
003    import net.sf.cpsolver.coursett.Constants;
004    
005    /**
006     * Preference combination.
007     * <br><br>
008     * A preference can be:<ul>
009     * <li>R .. required
010     * <li>P .. prohibited
011     * <li>number .. soft preference (smaller value is better)
012     * </ul>
013     *
014     * @version
015     * CourseTT 1.1 (University Course Timetabling)<br>
016     * Copyright (C) 2006 Tomáš Müller<br>
017     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
018     * Lazenska 391, 76314 Zlin, Czech Republic<br>
019     * <br>
020     * This library is free software; you can redistribute it and/or
021     * modify it under the terms of the GNU Lesser General Public
022     * License as published by the Free Software Foundation; either
023     * version 2.1 of the License, or (at your option) any later version.
024     * <br><br>
025     * This library is distributed in the hope that it will be useful,
026     * but 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.
029     * <br><br>
030     * You should have received a copy of the GNU Lesser General Public
031     * License along with this library; if not, write to the Free Software
032     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
033     */
034    public 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        /** Returns combined preference from the given preferences */
043        public void addPreferenceInt(int intPref) {
044            String prologPref = Constants.preferenceLevel2preference(intPref);
045            if (Constants.sPreferenceRequired.equals(prologPref)) iIsRequired = true;
046            if (Constants.sPreferenceProhibited.equals(prologPref)) iIsProhibited = true;
047        }
048        public boolean isRequired() {
049            return iIsRequired && !iIsProhibited;
050        }
051        public boolean isProhibited() {
052            return iIsProhibited;
053        }
054        public abstract int getPreferenceInt();
055        public String getPreferenceProlog() {
056            if (iIsProhibited) return Constants.sPreferenceProhibited;
057            if (iIsRequired) return Constants.sPreferenceRequired;
058            return Constants.preferenceLevel2preference(getPreferenceInt());
059        }
060        
061        public static PreferenceCombination getDefault() {
062            return new SumPreferenceCombination();
063        }
064    }