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