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