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