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