001package org.cpsolver.ifs.assignment.context; 002 003import org.cpsolver.ifs.assignment.Assignment; 004import org.cpsolver.ifs.model.BinaryConstraint; 005import org.cpsolver.ifs.model.Model; 006import org.cpsolver.ifs.model.Value; 007import org.cpsolver.ifs.model.Variable; 008 009/** 010 * A binary constraint with an assignment context. This is a variant of the {@link ConstraintWithContext} that extends the 011 * {@link BinaryConstraint} class. 012 * 013 * 014 * @see AssignmentContext 015 * 016 * @author Tomáš Müller 017 * @version IFS 1.3 (Iterative Forward Search)<br> 018 * Copyright (C) 2014 Tomáš Müller<br> 019 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 020 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 021 * <br> 022 * This library is free software; you can redistribute it and/or modify 023 * it under the terms of the GNU Lesser General Public License as 024 * published by the Free Software Foundation; either version 3 of the 025 * License, or (at your option) any later version. <br> 026 * <br> 027 * This library is distributed in the hope that it will be useful, but 028 * WITHOUT ANY WARRANTY; without even the implied warranty of 029 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 030 * Lesser General Public License for more details. <br> 031 * <br> 032 * You should have received a copy of the GNU Lesser General Public 033 * License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>. 034 * @param <V> Variable 035 * @param <T> Value 036 * @param <C> Assignment Context 037 **/ 038public abstract class BinaryConstraintWithContext<V extends Variable<V, T>, T extends Value<V, T>, C extends AssignmentConstraintContext<V, T>> extends BinaryConstraint<V, T> implements HasAssignmentContext<V, T, C>, CanHoldContext { 039 private AssignmentContextReference<V, T, C> iContextReference = null; 040 private AssignmentContext[] iContext = new AssignmentContext[CanHoldContext.sMaxSize]; 041 042 public BinaryConstraintWithContext() { 043 super(); 044 } 045 046 @Override 047 public void setModel(Model<V, T> model) { 048 super.setModel(model); 049 if (model != null) 050 iContextReference = model.createReference(this); 051 } 052 053 /** 054 * Returns an assignment context associated with this constraint. If there is no 055 * assignment context associated with this constraint yet, one is created using the 056 * {@link ConstraintWithContext#createAssignmentContext(Assignment)} method. From that time on, 057 * this context is kept with the assignment and automatically updated by calling the 058 * {@link AssignmentConstraintContext#assigned(Assignment, Value)} and {@link AssignmentConstraintContext#unassigned(Assignment, Value)} 059 * whenever a variable of this constraint is changed. 060 * @param assignment given assignment 061 * @return assignment context associated with this constraint and the given assignment 062 */ 063 @Override 064 public C getContext(Assignment<V, T> assignment) { 065 return AssignmentContextHelper.getContext(this, assignment); 066 } 067 068 @Override 069 public AssignmentContextReference<V, T, C> getAssignmentContextReference() { return iContextReference; } 070 071 @Override 072 public void setAssignmentContextReference(AssignmentContextReference<V, T, C> reference) { iContextReference = reference; } 073 074 @Override 075 public AssignmentContext[] getContext() { return iContext; } 076 077 @Override 078 public void assigned(Assignment<V, T> assignment, long iteration, T value) { 079 super.assigned(assignment, iteration, value); 080 getContext(assignment).assigned(assignment, value); 081 } 082 083 @Override 084 public void unassigned(Assignment<V, T> assignment, long iteration, T value) { 085 super.unassigned(assignment, iteration, value); 086 getContext(assignment).unassigned(assignment, value); 087 } 088}