Class VariableListenerSupport<Solution_>

java.lang.Object
ai.timefold.solver.core.impl.domain.variable.listener.support.VariableListenerSupport<Solution_>
Type Parameters:
Solution_ - the solution type, the class with the PlanningSolution annotation
All Implemented Interfaces:
SupplyManager

@NullMarked public final class VariableListenerSupport<Solution_> extends Object implements SupplyManager
This class is not thread-safe.
  • Method Details

    • create

      public static <Solution_> VariableListenerSupport<Solution_> create(InnerScoreDirector<Solution_,?> scoreDirector)
    • getSupportedShadowVariableTypes

      public List<ShadowVariableType> getSupportedShadowVariableTypes()
    • linkVariableListeners

      public void linkVariableListeners()
    • demand

      public <Supply_ extends Supply> Supply_ demand(Demand<Supply_> demand)
      Description copied from interface: SupplyManager
      Returns the Supply for a Demand, preferably an existing one. If the Supply doesn't exist yet (as part of the domain model or externalized), it creates and attaches it. If two Demand instances are equal, they will result in the same Supply instance. Each supply instance keeps a counter of how many times it was requested, which can be decremented by SupplyManager.cancel(Demand).
      Specified by:
      demand in interface SupplyManager
      Type Parameters:
      Supply_ - Subclass of Supply
      Parameters:
      demand - never null
      Returns:
      never null
    • cancel

      public <Supply_ extends Supply> boolean cancel(Demand<Supply_> demand)
      Description copied from interface: SupplyManager
      Cancel an active SupplyManager.demand(Demand). Once the number of active demands reaches zero, the Supply in question is removed.

      This operation is optional. Supplies with active demands will live for as long as the SupplyManager lives, and get garbage-collected together with it.

      Specified by:
      cancel in interface SupplyManager
      Parameters:
      demand - never null
      Returns:
      true if the counter was decremented, false if there is no such supply
    • getActiveCount

      public <Supply_ extends Supply> long getActiveCount(Demand<Supply_> demand)
      Specified by:
      getActiveCount in interface SupplyManager
      Returns:
      0 when there is no active Supply for the given Demand, more when there is one.
    • resetWorkingSolution

      public void resetWorkingSolution()
    • close

      public void close()
    • beforeEntityAdded

      public void beforeEntityAdded(EntityDescriptor<Solution_> entityDescriptor, Object entity)
    • beforeEntityRemoved

      public void beforeEntityRemoved(EntityDescriptor<Solution_> entityDescriptor, Object entity)
    • beforeVariableChanged

      public void beforeVariableChanged(VariableDescriptor<Solution_> variableDescriptor, Object entity)
    • afterVariableChanged

      public void afterVariableChanged(VariableDescriptor<Solution_> variableDescriptor, Object entity)
    • afterElementUnassigned

      public void afterElementUnassigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element)
    • beforeListVariableChanged

      public void beforeListVariableChanged(ListVariableDescriptor<Solution_> variableDescriptor, Object entity, int fromIndex, int toIndex)
    • afterListVariableChanged

      public void afterListVariableChanged(ListVariableDescriptor<Solution_> variableDescriptor, Object entity, int fromIndex, int toIndex)
    • getScoreDirector

      public InnerScoreDirector<Solution_,?> getScoreDirector()
    • triggerVariableListenersInNotificationQueues

      public void triggerVariableListenersInNotificationQueues()
    • createShadowVariablesViolationMessage

      public @Nullable String createShadowVariablesViolationMessage()
      Returns:
      null if there are no violations
    • forceTriggerAllVariableListeners

      public void forceTriggerAllVariableListeners(Solution_ workingSolution)
      Triggers all variable listeners even though the notification queue is empty.

      To ensure each listener is triggered, an artificial notification is created for each genuine variable without doing any change on the working solution. If everything works correctly, triggering listeners at this point must not change any shadow variables either.

      Parameters:
      workingSolution - working solution
    • clearAllVariableListenerEvents

      public void clearAllVariableListenerEvents()
      Clear all variable listeners without triggering any logic. The goal is to clear all queues and avoid executing custom listener logic.
    • assertNotificationQueuesAreEmpty

      public void assertNotificationQueuesAreEmpty()