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