    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 * @author  Tomáš Müller
    023 * @version IFS 1.3 (Iterative Forward Search)<br>
    024 *          Copyright (C) 2014 Tomáš Müller<br>
    025 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    026 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    027 * <br>
    028 *          This library is free software; you can redistribute it and/or modify
    029 *          it under the terms of the GNU Lesser General Public License as
    030 *          published by the Free Software Foundation; either version 3 of the
    031 *          License, or (at your option) any later version. <br>
    032 * <br>
    033 *          This library is distributed in the hope that it will be useful, but
    034 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    035 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    036 *          Lesser General Public License for more details. <br>
    037 * <br>
    038 *          You should have received a copy of the GNU Lesser General Public
    039 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
    040 * @param <V> Variable
    041 * @param <T> Value
    042 * @param <C> Assignment Context
    043 **/
    044public 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 {
    045    private AssignmentContextReference<V, T, C> iContextReference = null;
    046    private AssignmentContext[] iContext = new AssignmentContext[CanHoldContext.sMaxSize];
    047
    048    public ExtensionWithContext(Solver<V, T> solver, DataProperties properties) {
    049        super(solver, properties);
    050    }
    051
    052    @Override
    053    public boolean init(Solver<V, T> solver) {
    054        iContextReference = solver.currentSolution().getModel().createReference(this);
    055        return super.init(solver);
    056    }
    057
    058    /**
    059     * Returns an assignment context associated with this extension. If there is no
    060     * assignment context associated with this extension yet, one is created using the
    061     * {@link ConstraintWithContext#createAssignmentContext(Assignment)} method. From that time on,
    062     * this context is kept with the assignment.
    063     * @param assignment given assignment
    064     * @return assignment context associated with this extension 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 unregister(Model<V, T> model) {
    082        model.removeReference(this);
    083        super.unregister(model);
    084    }
    085
    086}