001    package net.sf.cpsolver.ifs.model;
002    
003    /**
004     * Binary constraint.
005     * <br><br>
006     * Extension of {@link Constraint} that links exactly two variables.
007     *
008     * @see Variable
009     * @see Constraint
010     * @see Model
011     *
012     * @version
013     * IFS 1.1 (Iterative Forward Search)<br>
014     * Copyright (C) 2006 Tomáš Müller<br>
015     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
016     * Lazenska 391, 76314 Zlin, Czech Republic<br>
017     * <br>
018     * This library is free software; you can redistribute it and/or
019     * modify it under the terms of the GNU Lesser General Public
020     * License as published by the Free Software Foundation; either
021     * version 2.1 of the License, or (at your option) any later version.
022     * <br><br>
023     * This library is distributed in the hope that it will be useful,
024     * but WITHOUT ANY WARRANTY; without even the implied warranty of
025     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026     * Lesser General Public License for more details.
027     * <br><br>
028     * You should have received a copy of the GNU Lesser General Public
029     * License along with this library; if not, write to the Free Software
030     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
031     */
032    
033    public abstract class BinaryConstraint extends Constraint {
034        private Variable iFirst = null, iSecond = null;
035        public BinaryConstraint() {
036            super();
037        }
038        
039        public void addVariable(Variable var) {
040            if (iFirst == null)
041                iFirst = (Variable)var;
042            else
043                iSecond = (Variable)var;
044            super.addVariable(var);
045        }
046        
047        /** First variable */
048        public Variable first() {
049            return iFirst;
050        }
051        
052        /** Second variable */
053        public Variable second() {
054            return iSecond;
055        }
056        
057        /** True, id the given variable is the first one */
058        public boolean isFirst(Variable variable) {
059            return variable.equals(first());
060        }
061        
062        /** Returns the variable out of the constraints variables which is different from the given variable.*/
063        public Variable another(Variable variable) {
064            return (
065                    first() != null && variable.equals(first()) ? second() : first());
066        }
067    }