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