|
1 | 1 | package ai.timefold.solver.core.impl.score.director; |
2 | 2 |
|
3 | | -import java.util.HashSet; |
4 | 3 | import java.util.IdentityHashMap; |
5 | 4 | import java.util.Map; |
6 | 5 | import java.util.Objects; |
7 | 6 |
|
8 | 7 | import ai.timefold.solver.core.api.domain.valuerange.ValueRange; |
| 8 | +import ai.timefold.solver.core.api.solver.change.ProblemChange; |
9 | 9 | import ai.timefold.solver.core.impl.domain.valuerange.descriptor.ValueRangeDescriptor; |
10 | 10 | import ai.timefold.solver.core.impl.domain.variable.descriptor.BasicVariableDescriptor; |
11 | 11 | import ai.timefold.solver.core.impl.domain.variable.descriptor.ListVariableDescriptor; |
12 | | -import ai.timefold.solver.core.impl.phase.event.PhaseLifecycleListener; |
13 | | -import ai.timefold.solver.core.impl.phase.scope.AbstractPhaseScope; |
14 | | -import ai.timefold.solver.core.impl.phase.scope.AbstractStepScope; |
15 | | -import ai.timefold.solver.core.impl.solver.scope.SolverScope; |
16 | 12 | import ai.timefold.solver.core.preview.api.domain.metamodel.GenuineVariableMetaModel; |
17 | 13 |
|
18 | 14 | import org.jspecify.annotations.NullMarked; |
|
28 | 24 | * It is not built in advance as it would be too expensive, and some code paths do not use it at all. |
29 | 25 | * |
30 | 26 | * <p> |
| 27 | + * Outside a {@link ProblemChange}, value ranges are not allowed to change. |
31 | 28 | * Call {@link #resetWorkingSolution(Object)} every time the working solution changes through a problem fact, |
32 | 29 | * so that all caches can be invalidated. |
33 | | - * |
34 | | - * <p> |
35 | | - * Call {@link #markEntityDependentValueRangesAsInvalid(Object)} every time an entity is changed, |
36 | | - * so that the cached value range can be invalidated. |
37 | | - * The actual invalidation happens at {@link #stepEnded(AbstractStepScope)}, called by the solver. |
38 | | - * Otherwise every undone move would be invalidating the value ranges. |
39 | | - * Value ranges should only be invalidated when a move has been selected at the end of a step. |
40 | 30 | */ |
41 | 31 | @NullMarked |
42 | | -public final class ValueRangeState<Solution_> |
43 | | - implements PhaseLifecycleListener<Solution_> { |
| 32 | +public final class ValueRangeState<Solution_> { |
44 | 33 |
|
45 | | - private final HashSet<Object> entitiesWithInvalidValueRangesSet = new HashSet<>(); |
46 | 34 | private final Map<ValueRangeDescriptor<Solution_>, ValueRange<Object>> fromSolutionValueRangeMap = new IdentityHashMap<>(); |
47 | 35 | private final Map<Object, Map<ValueRangeDescriptor<Solution_>, ValueRange<Object>>> fromEntityValueRangeMap = |
48 | 36 | new IdentityHashMap<>(); |
@@ -93,40 +81,4 @@ public boolean isInRange(ValueRangeDescriptor<Solution_> valueRangeDescriptor, @ |
93 | 81 | } |
94 | 82 | } |
95 | 83 |
|
96 | | - void markEntityDependentValueRangesAsInvalid(Object entity) { |
97 | | - entitiesWithInvalidValueRangesSet.add(entity); |
98 | | - } |
99 | | - |
100 | | - @Override |
101 | | - public void solvingStarted(SolverScope<Solution_> solverScope) { |
102 | | - // No need to do anything here. |
103 | | - } |
104 | | - |
105 | | - @Override |
106 | | - public void phaseStarted(AbstractPhaseScope<Solution_> phaseScope) { |
107 | | - // No need to do anything here. |
108 | | - } |
109 | | - |
110 | | - @Override |
111 | | - public void stepStarted(AbstractStepScope<Solution_> stepScope) { |
112 | | - // No need to do anything here. |
113 | | - } |
114 | | - |
115 | | - @Override |
116 | | - public void stepEnded(AbstractStepScope<Solution_> stepScope) { |
117 | | - entitiesWithInvalidValueRangesSet.forEach(fromEntityValueRangeMap::remove); |
118 | | - entitiesWithInvalidValueRangesSet.clear(); |
119 | | - } |
120 | | - |
121 | | - @Override |
122 | | - public void phaseEnded(AbstractPhaseScope<Solution_> phaseScope) { |
123 | | - // No need to do anything here. |
124 | | - } |
125 | | - |
126 | | - @Override |
127 | | - public void solvingEnded(SolverScope<Solution_> solverScope) { |
128 | | - this.workingSolution = null; |
129 | | - fromSolutionValueRangeMap.clear(); |
130 | | - fromEntityValueRangeMap.clear(); |
131 | | - } |
132 | 84 | } |
0 commit comments