org.cpsolver.ifs.assignment.context

## Class ModelWithContext<V extends Variable<V,T>,T extends Value<V,T>,C extends AssignmentConstraintContext<V,T>>

- java.lang.Object
- - org.cpsolver.ifs.model.Model<V,T>
  - - org.cpsolver.ifs.assignment.context.ModelWithContext<V,T,C>

- Type Parameters:
    `V` - Variable
    `T` - Value
    `C` - Assignment Context
  All Implemented Interfaces:
    CanHoldContext, HasAssignmentContext<V,T,C>
  Direct Known Subclasses:
    ExamModel, StudentSectioningModel

      public abstract class ModelWithContext<V extends Variable<V,T>,T extends Value<V,T>,C extends AssignmentConstraintContext<V,T>>
      extends Model<V,T>
      implements HasAssignmentContext<V,T,C>, CanHoldContext

  A model with an assignment context. In order to be able to hold multiple assignments in memory it is desired for all the assignment dependent data a constraint may need (to effectively enumerate problem objectives), to store these data in a separate class (implementing the `AssignmentConstraintContext` interface). This context is created by calling `HasAssignmentContext.createAssignmentContext(Assignment)` and accessed by `ConstraintWithContext.getContext(Assignment)`.
  Version:
    IFS 1.3 (Iterative Forward Search)
    Copyright (C) 2014 Tomáš Müller
    muller@unitime.org
    http://muller.unitime.org
    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License along with this library; if not see http://www.gnu.org/licenses.
  Author:
    Tomáš Müller
  See Also:
    `AssignmentContext`

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `protected static class` | `ModelWithContext.ContextUpdateType` Defines how the context of the model should be automatically updated (i.e., when `AssignmentConstraintContext.assigned(Assignment, Value)` and `AssignmentConstraintContext.unassigned(Assignment, Value)` are called).

  - ### Field Summary

    - ### Fields inherited from class org.cpsolver.ifs.model.Model

      `sDoubleFormat, sPercentageFormat, sTimeFormat`

    - ### Fields inherited from interface org.cpsolver.ifs.assignment.context.CanHoldContext

      `sMaxSize`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ModelWithContext()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `afterAssigned(Assignment<V,T> assignment, long iteration, T value)` Called after a value is assigned to its variable
    `void` | `afterUnassigned(Assignment<V,T> assignment, long iteration, T value)` Called after a value is unassigned from its variable
    `void` | `beforeAssigned(Assignment<V,T> assignment, long iteration, T value)` Called before a value is assigned to its variable
    `void` | `beforeUnassigned(Assignment<V,T> assignment, long iteration, T value)` Called before a value is unassigned from its variable
    `AssignmentContextReference<V,T,C>` | `getAssignmentContextReference()` Returns an assignment context reference
    `AssignmentContext[]` | `getContext()` An array of `CanHoldContext.sMaxSize` assignment contexts
    `C` | `getContext(Assignment<V,T> assignment)` Returns an assignment context associated with this model.
    `ModelWithContext.ContextUpdateType` | `getContextUpdateType()`
    `void` | `setAssignmentContextReference(AssignmentContextReference<V,T,C> reference)` Store an assignment context reference that was given for the class by the `Model.createReference(HasAssignmentContext)`.
    `void` | `setContextUpdateType(ModelWithContext.ContextUpdateType iContextUpdateType)`

    - ### Methods inherited from class org.cpsolver.ifs.model.Model

      `addConstraint, addCriterion, addGlobalConstraint, addModelListener, addVariable, afterAssigned, afterUnassigned, assignedVariables, assignedVariables, beforeAssigned, beforeUnassigned, bestUnassignedVariables, bestUnassignedVariables, clearAssignmentContexts, clearBest, conflictConstraints, conflictValues, conflictValues, constraints, countConstraints, countGlobalConstraints, countVariables, createAssignmentContexts, createInheritedAssignment, createReference, getBestPerturbations, getBestUnassignedVariables, getBestValue, getCriteria, getCriterion, getDefaultAssignment, getEmptyAssignment, getExtendedInfo, getExtendedInfo, getInfo, getInfo, getInfo, getInfo, getInfoProviders, getModelListeners, getPerc, getPercRev, getTotalValue, getTotalValue, getTotalValue, getTotalValue, globalConstraints, inConflict, inConflict, init, invalidateVariablesWithInitialValueCache, modelListenerOfType, nrAssignedVariables, nrAssignedVariables, nrUnassignedVariables, nrUnassignedVariables, perturbVariables, perturbVariables, perturbVariables, perturbVariables, perturbVariables, removeConstraint, removeCriterion, removeCriterion, removeGlobalConstraint, removeModelListener, removeReference, removeVariable, restoreBest, restoreBest, restoreBest, saveBest, saveBest, setBestValue, setDefaultAssignment, toString, toString, unassignedHardConstraints, unassignedVariables, unassignedVariables, variables, variablesWithInitialValue, weaken`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.cpsolver.ifs.assignment.context.HasAssignmentContext

      `createAssignmentContext`

- - ### Constructor Detail

    - #### ModelWithContext

          public ModelWithContext()

  - ### Method Detail

    - #### getContext

          public C getContext(Assignment<V,T> assignment)

      Returns an assignment context associated with this model. If there is no assignment context associated with this model yet, one is created using the `HasAssignmentContext.createAssignmentContext(Assignment)` method. From that time on, this context is kept with the assignment and automatically updated by calling the `AssignmentConstraintContext.assigned(Assignment, Value)` and `AssignmentConstraintContext.unassigned(Assignment, Value)` whenever a variable is changed.
      Specified by:
        `getContext` in interface `HasAssignmentContext<V extends Variable<V,T>,T extends Value<V,T>,C extends AssignmentConstraintContext<V,T>>`
      Parameters:
        `assignment` - given assignment
      Returns:
        assignment context associated with this model and the given assignment

    - #### getAssignmentContextReference

          public AssignmentContextReference<V,T,C> getAssignmentContextReference()

      Description copied from interface: `HasAssignmentContext`
      Returns an assignment context reference
      Specified by:
        `getAssignmentContextReference` in interface `HasAssignmentContext<V extends Variable<V,T>,T extends Value<V,T>,C extends AssignmentConstraintContext<V,T>>`
      Returns:
        reference provided by the model by calling `Model.createReference(HasAssignmentContext)` during initialization

    - #### setAssignmentContextReference

          public void setAssignmentContextReference(AssignmentContextReference<V,T,C> reference)

      Description copied from interface: `HasAssignmentContext`
      Store an assignment context reference that was given for the class by the `Model.createReference(HasAssignmentContext)`.
      Specified by:
        `setAssignmentContextReference` in interface `HasAssignmentContext<V extends Variable<V,T>,T extends Value<V,T>,C extends AssignmentConstraintContext<V,T>>`
      Parameters:
        `reference` - reference provided by the model by calling `Model.createReference(HasAssignmentContext)` during initialization

    - #### getContext

          public AssignmentContext[] getContext()

      Description copied from interface: `CanHoldContext`
      An array of `CanHoldContext.sMaxSize` assignment contexts
      Specified by:
        `getContext` in interface `CanHoldContext`
      Returns:
        assignment contexts

    - #### beforeUnassigned

          public void beforeUnassigned(Assignment<V,T> assignment,
                                       long iteration,
                                       T value)

      Description copied from class: `Model`
      Called before a value is unassigned from its variable
      Overrides:
        `beforeUnassigned` in class `Model<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `assignment` - current assignment
        `iteration` - current iteration
        `value` - a value to be unassigned

    - #### afterUnassigned

          public void afterUnassigned(Assignment<V,T> assignment,
                                      long iteration,
                                      T value)

      Description copied from class: `Model`
      Called after a value is unassigned from its variable
      Overrides:
        `afterUnassigned` in class `Model<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `assignment` - current assignment
        `iteration` - current iteration
        `value` - a value that was unassigned

    - #### afterAssigned

          public void afterAssigned(Assignment<V,T> assignment,
                                    long iteration,
                                    T value)

      Description copied from class: `Model`
      Called after a value is assigned to its variable
      Overrides:
        `afterAssigned` in class `Model<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `assignment` - current assignment
        `iteration` - current iteration
        `value` - a value that was assigned

    - #### beforeAssigned

          public void beforeAssigned(Assignment<V,T> assignment,
                                     long iteration,
                                     T value)

      Description copied from class: `Model`
      Called before a value is assigned to its variable
      Overrides:
        `beforeAssigned` in class `Model<V extends Variable<V,T>,T extends Value<V,T>>`
      Parameters:
        `assignment` - current assignment
        `iteration` - current iteration
        `value` - a value to be assigned

    - #### getContextUpdateType

          public ModelWithContext.ContextUpdateType getContextUpdateType()

    - #### setContextUpdateType

          public void setContextUpdateType(ModelWithContext.ContextUpdateType iContextUpdateType)