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 Tomáš Müller<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 = new AssignmentContext[CanHoldContext.sMaxSize];
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    @Override
048    public C getContext(Assignment<V, T> assignment) {
049        if (getModel() == null) {
050            if (iSingleContextWhenNoModel == null)
051                iSingleContextWhenNoModel = createAssignmentContext(assignment);
052            return iSingleContextWhenNoModel;
053        }
054        return AssignmentContextHelper.getContext(this, assignment);
055    }
056    
057    @Override
058    public synchronized AssignmentContextReference<V, T, C> getAssignmentContextReference() {
059        if (iContextReference == null)
060            iContextReference = getModel().createReference(this);
061        return iContextReference;
062    }
063
064    @Override
065    public void setAssignmentContextReference(AssignmentContextReference<V, T, C> reference) { iContextReference = reference; }
066
067    @Override
068    public AssignmentContext[] getContext() { return iContext; }
069
070    /**
071     * Get the model. This is used to create an assignment context if needed.
072     * @return model
073     */
074    public abstract Model<V,T> getModel();
075}