|
| 1 | +package ai.timefold.solver.core.impl.domain.variable.listener.support.violation; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import ai.timefold.solver.core.api.domain.variable.ListVariableListener; |
| 7 | +import ai.timefold.solver.core.api.score.director.ScoreDirector; |
| 8 | +import ai.timefold.solver.core.impl.domain.variable.descriptor.ListVariableDescriptor; |
| 9 | +import ai.timefold.solver.core.impl.domain.variable.descriptor.VariableDescriptor; |
| 10 | +import ai.timefold.solver.core.impl.domain.variable.listener.SourcedVariableListener; |
| 11 | +import ai.timefold.solver.core.impl.domain.variable.supply.Demand; |
| 12 | +import ai.timefold.solver.core.impl.domain.variable.supply.Supply; |
| 13 | +import ai.timefold.solver.core.impl.domain.variable.supply.SupplyManager; |
| 14 | +import ai.timefold.solver.core.impl.score.director.InnerScoreDirector; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tracks variable listener events for a given {@link ai.timefold.solver.core.api.domain.variable.PlanningListVariable}. |
| 18 | + */ |
| 19 | +public class ListVariableTracker<Solution_> |
| 20 | + implements SourcedVariableListener<Solution_>, ListVariableListener<Solution_, Object, Object>, Supply { |
| 21 | + private final ListVariableDescriptor<Solution_> variableDescriptor; |
| 22 | + private final List<Object> beforeVariableChangedEntityList; |
| 23 | + private final List<Object> afterVariableChangedEntityList; |
| 24 | + |
| 25 | + public ListVariableTracker(ListVariableDescriptor<Solution_> variableDescriptor) { |
| 26 | + this.variableDescriptor = variableDescriptor; |
| 27 | + beforeVariableChangedEntityList = new ArrayList<>(); |
| 28 | + afterVariableChangedEntityList = new ArrayList<>(); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public VariableDescriptor<Solution_> getSourceVariableDescriptor() { |
| 33 | + return variableDescriptor; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void resetWorkingSolution(ScoreDirector<Solution_> scoreDirector) { |
| 38 | + beforeVariableChangedEntityList.clear(); |
| 39 | + afterVariableChangedEntityList.clear(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void beforeEntityAdded(ScoreDirector<Solution_> scoreDirector, Object entity) { |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void afterEntityAdded(ScoreDirector<Solution_> scoreDirector, Object entity) { |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void beforeEntityRemoved(ScoreDirector<Solution_> scoreDirector, Object entity) { |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void afterEntityRemoved(ScoreDirector<Solution_> scoreDirector, Object entity) { |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void afterListVariableElementUnassigned(ScoreDirector<Solution_> scoreDirector, Object element) { |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void beforeListVariableChanged(ScoreDirector<Solution_> scoreDirector, Object entity, int fromIndex, int toIndex) { |
| 69 | + beforeVariableChangedEntityList.add(entity); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void afterListVariableChanged(ScoreDirector<Solution_> scoreDirector, Object entity, int fromIndex, int toIndex) { |
| 74 | + afterVariableChangedEntityList.add(entity); |
| 75 | + } |
| 76 | + |
| 77 | + public List<String> getEntitiesMissingBeforeAfterEvents( |
| 78 | + List<VariableId<Solution_>> changedVariables) { |
| 79 | + List<String> out = new ArrayList<>(); |
| 80 | + for (var changedVariable : changedVariables) { |
| 81 | + if (!variableDescriptor.equals(changedVariable.variableDescriptor())) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + Object entity = changedVariable.entity(); |
| 85 | + if (!beforeVariableChangedEntityList.contains(entity)) { |
| 86 | + out.add("Entity (" + entity |
| 87 | + + ") is missing a beforeListVariableChanged call for list variable (" |
| 88 | + + variableDescriptor.getVariableName() + ")."); |
| 89 | + } |
| 90 | + if (!afterVariableChangedEntityList.contains(entity)) { |
| 91 | + out.add("Entity (" + entity |
| 92 | + + ") is missing a afterListVariableChanged call for list variable (" |
| 93 | + + variableDescriptor.getVariableName() + ")."); |
| 94 | + } |
| 95 | + } |
| 96 | + beforeVariableChangedEntityList.clear(); |
| 97 | + afterVariableChangedEntityList.clear(); |
| 98 | + return out; |
| 99 | + } |
| 100 | + |
| 101 | + public TrackerDemand demand() { |
| 102 | + return new TrackerDemand(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * In order for the {@link ListVariableTracker} to be registered as a variable listener, |
| 107 | + * it needs to be passed to the {@link InnerScoreDirector#getSupplyManager()}, which requires a {@link Demand}. |
| 108 | + * <p> |
| 109 | + * Unlike most other {@link Demand}s, there will only be one instance of |
| 110 | + * {@link ListVariableTracker} in the {@link InnerScoreDirector} for each list variable. |
| 111 | + */ |
| 112 | + public class TrackerDemand implements Demand<ListVariableTracker<Solution_>> { |
| 113 | + @Override |
| 114 | + public ListVariableTracker<Solution_> createExternalizedSupply(SupplyManager supplyManager) { |
| 115 | + return ListVariableTracker.this; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments