    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 * An abstract implementation of an assignment context holding class.
    010 * This works much like {@link ConstraintWithContext} or {@link VariableWithContext}, however,
    011 * it is not tight to a particular class type. Instead {@link AbstractClassWithContext#getModel()}
    012 * needs to be implemented.
    013 *
    014 * @author  Tomáš Müller
    015 * @version IFS 1.3 (Iterative Forward Search)<br>
    016 *          Copyright (C) 2014 Tomáš Müller<br>
    017 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    018 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    019 * <br>
    020 *          This library is free software; you can redistribute it and/or modify
    021 *          it under the terms of the GNU Lesser General Public License as
    022 *          published by the Free Software Foundation; either version 3 of the
    023 *          License, or (at your option) any later version. <br>
    024 * <br>
    025 *          This library is distributed in the hope that it will be useful, but
    026 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    027 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    028 *          Lesser General Public License for more details. <br>
    029 * <br>
    030 *          You should have received a copy of the GNU Lesser General Public
    031 *          License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>.
    032 * @param <V> Variable
    033 * @param <T> Value
    034 * @param <C> Assignment Context
    035 **/
    036public abstract class AbstractClassWithContext<V extends Variable<V, T>, T extends Value<V, T>, C extends AssignmentContext> implements HasAssignmentContext<V, T, C>, CanHoldContext {
    037    private AssignmentContextReference<V, T, C> iContextReference = null;
    038    private AssignmentContext[] iContext = new AssignmentContext[CanHoldContext.sMaxSize];
    039    private C iSingleContextWhenNoModel = null;
    040
    041    /**
    042     * Returns an assignment context associated with this object. If there is no
    043     * assignment context associated with this object yet, one is created using the
    044     * {@link ConstraintWithContext#createAssignmentContext(Assignment)} method.
    045     * @param assignment given assignment
    046     * @return assignment context associated with this object and the given assignment
    047     */
    048    @Override
    049    public C getContext(Assignment<V, T> assignment) {
    050        if (getModel() == null) {
    051            if (iSingleContextWhenNoModel == null)
    052                iSingleContextWhenNoModel = createAssignmentContext(assignment);
    053            return iSingleContextWhenNoModel;
    054        }
    055        return AssignmentContextHelper.getContext(this, assignment);
    056    }
    057
    058    @Override
    059    public synchronized AssignmentContextReference<V, T, C> getAssignmentContextReference() {
    060        if (iContextReference == null)
    061            iContextReference = getModel().createReference(this);
    062        return iContextReference;
    063    }
    064
    065    @Override
    066    public void setAssignmentContextReference(AssignmentContextReference<V, T, C> reference) { iContextReference = reference; }
    067
    068    @Override
    069    public AssignmentContext[] getContext() { return iContext; }
    070
    071    /**
    072     * Get the model. This is used to create an assignment context if needed.
    073     * @return model
    074     */
    075    public abstract Model<V,T> getModel();
    076}