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 * @author  Tomáš Müller
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 VariableWithContext<V extends Variable<V, T>, T extends Value<V, T>, C extends AssignmentContext> extends Variable<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    /** Constructor */
046    public VariableWithContext() {
047        super();
048    }
049
050    /**
051     * Constructor
052     * @param initialValue initial value (minimal-perturbation problem)
053     */
054    public VariableWithContext(T initialValue) {
055        super(initialValue);
056    }
057    
058    @Override
059    public void setModel(Model<V, T> model) {
060        super.setModel(model);
061        if (model != null)
062            iContextReference = model.createReference(this);
063    }
064    
065    /**
066     * Returns an assignment context associated with this extension. If there is no 
067     * assignment context associated with this extension yet, one is created using the
068     * {@link ConstraintWithContext#createAssignmentContext(Assignment)} method. From that time on,
069     * this context is kept with the assignment.
070     * @param assignment given assignment
071     * @return assignment context associated with this extension and the given assignment
072     */
073    @Override
074    public C getContext(Assignment<V, T> assignment) {
075        return AssignmentContextHelper.getContext(this, assignment);
076    }
077
078    @Override
079    public AssignmentContextReference<V, T, C> getAssignmentContextReference() { return iContextReference; }
080
081    @Override
082    public void setAssignmentContextReference(AssignmentContextReference<V, T, C> reference) { iContextReference = reference; }
083
084    @Override
085    public AssignmentContext[] getContext() { return iContext; }
086}