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