|
| 1 | +package ai.timefold.solver.core.impl.domain.variable.declarative; |
| 2 | + |
| 3 | +import java.util.BitSet; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.PriorityQueue; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.function.Consumer; |
| 9 | +import java.util.function.Function; |
| 10 | + |
| 11 | +import ai.timefold.solver.core.impl.domain.variable.descriptor.VariableDescriptor; |
| 12 | +import ai.timefold.solver.core.impl.util.LinkedIdentityHashSet; |
| 13 | + |
| 14 | +final class AffectedEntitiesUpdater<Solution_> |
| 15 | + implements Consumer<BitSet> { |
| 16 | + |
| 17 | + // From WorkingReferenceGraph. |
| 18 | + private final BaseTopologicalOrderGraph graph; |
| 19 | + private final List<EntityVariablePair<Solution_>> instanceList; // Immutable. |
| 20 | + private final Function<Object, List<EntityVariablePair<Solution_>>> entityVariablePairFunction; |
| 21 | + private final ChangedVariableNotifier<Solution_> changedVariableNotifier; |
| 22 | + |
| 23 | + // Internal state; expensive to create, therefore we reuse. |
| 24 | + private final AffectedEntities<Solution_> affectedEntities; |
| 25 | + private final LoopedTracker loopedTracker; |
| 26 | + private final BitSet visited; |
| 27 | + private final PriorityQueue<BaseTopologicalOrderGraph.NodeTopologicalOrder> changeQueue; |
| 28 | + |
| 29 | + AffectedEntitiesUpdater(BaseTopologicalOrderGraph graph, List<EntityVariablePair<Solution_>> instanceList, |
| 30 | + Function<Object, List<EntityVariablePair<Solution_>>> entityVariablePairFunction, |
| 31 | + ChangedVariableNotifier<Solution_> changedVariableNotifier) { |
| 32 | + this.graph = graph; |
| 33 | + this.instanceList = instanceList; |
| 34 | + this.entityVariablePairFunction = entityVariablePairFunction; |
| 35 | + this.changedVariableNotifier = changedVariableNotifier; |
| 36 | + var instanceCount = instanceList.size(); |
| 37 | + this.affectedEntities = new AffectedEntities<>(this::updateLoopedStatusOfAffectedEntity); |
| 38 | + this.loopedTracker = new LoopedTracker(instanceCount); |
| 39 | + this.visited = new BitSet(instanceCount); |
| 40 | + this.changeQueue = new PriorityQueue<>(instanceCount); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void accept(BitSet changed) { |
| 45 | + initializeChangeQueue(changed); |
| 46 | + |
| 47 | + while (!changeQueue.isEmpty()) { |
| 48 | + var nextNode = changeQueue.poll().nodeId(); |
| 49 | + if (visited.get(nextNode)) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + visited.set(nextNode); |
| 53 | + var shadowVariable = instanceList.get(nextNode); |
| 54 | + var isChanged = updateShadowVariable(shadowVariable, graph.isLooped(loopedTracker, nextNode)); |
| 55 | + |
| 56 | + if (isChanged) { |
| 57 | + var iterator = graph.nodeForwardEdges(nextNode); |
| 58 | + while (iterator.hasNext()) { |
| 59 | + var nextNodeForwardEdge = iterator.nextInt(); |
| 60 | + if (!visited.get(nextNodeForwardEdge)) { |
| 61 | + changeQueue.add(graph.getTopologicalOrder(nextNodeForwardEdge)); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + affectedEntities.processAndClear(); |
| 68 | + // Prepare for the next time updateChanged() is called. |
| 69 | + // No need to clear changeQueue, as that already finishes empty. |
| 70 | + loopedTracker.clear(); |
| 71 | + visited.clear(); |
| 72 | + } |
| 73 | + |
| 74 | + private void initializeChangeQueue(BitSet changed) { |
| 75 | + // BitSet iteration: get the first set bit at or after 0, |
| 76 | + // then get the first set bit after that bit. |
| 77 | + // Iteration ends when nextSetBit returns -1. |
| 78 | + // This has the potential to overflow, since to do the |
| 79 | + // test, we necessarily need to do nextSetBit(i + 1), |
| 80 | + // and i + 1 can be negative if Integer.MAX_VALUE is set |
| 81 | + // in the BitSet. |
| 82 | + // This should never happen, since arrays in Java are limited |
| 83 | + // to slightly less than Integer.MAX_VALUE. |
| 84 | + for (var i = changed.nextSetBit(0); i >= 0; i = changed.nextSetBit(i + 1)) { |
| 85 | + changeQueue.add(graph.getTopologicalOrder(i)); |
| 86 | + if (i == Integer.MAX_VALUE) { |
| 87 | + break; // or (i+1) would overflow |
| 88 | + } |
| 89 | + } |
| 90 | + changed.clear(); |
| 91 | + } |
| 92 | + |
| 93 | + private void updateLoopedStatusOfAffectedEntity(Object affectedEntity) { |
| 94 | + ShadowVariableLoopedVariableDescriptor<Solution_> shadowVariableLoopedDescriptor = null; |
| 95 | + var isEntityLooped = false; |
| 96 | + for (var node : entityVariablePairFunction.apply(affectedEntity)) { |
| 97 | + // All variables come from the same entity, |
| 98 | + // therefore all have the same looped marker. |
| 99 | + shadowVariableLoopedDescriptor = node.variableReference().shadowVariableLoopedDescriptor(); |
| 100 | + if (graph.isLooped(loopedTracker, node.graphNodeId())) { |
| 101 | + isEntityLooped = true; |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + if (shadowVariableLoopedDescriptor == null) { |
| 106 | + // At this point, affectedEntity is guaranteed to have looped marker. |
| 107 | + // Otherwise AffectedEntities would not have sent it here. |
| 108 | + throw new IllegalStateException("Impossible state: loop marker descriptor does not exist."); |
| 109 | + } |
| 110 | + var oldValue = shadowVariableLoopedDescriptor.getValue(affectedEntity); |
| 111 | + if (!Objects.equals(oldValue, isEntityLooped)) { |
| 112 | + changeShadowVariableAndNotify(shadowVariableLoopedDescriptor, affectedEntity, isEntityLooped); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + private boolean updateShadowVariable(EntityVariablePair<Solution_> entityVariable, boolean isLooped) { |
| 118 | + var entity = entityVariable.entity(); |
| 119 | + var shadowVariableReference = entityVariable.variableReference(); |
| 120 | + var oldValue = shadowVariableReference.memberAccessor().executeGetter(entity); |
| 121 | + |
| 122 | + if (isLooped) { |
| 123 | + // null might be a valid value, and thus it could be the case |
| 124 | + // that is was not looped and null, then turned to looped and null, |
| 125 | + // which is still considered a change. |
| 126 | + affectedEntities.add(entityVariable); |
| 127 | + if (oldValue != null) { |
| 128 | + changeShadowVariableAndNotify(shadowVariableReference, entity, null); |
| 129 | + } |
| 130 | + return true; |
| 131 | + } else { |
| 132 | + var newValue = shadowVariableReference.calculator().apply(entity); |
| 133 | + if (!Objects.equals(oldValue, newValue)) { |
| 134 | + affectedEntities.add(entityVariable); |
| 135 | + changeShadowVariableAndNotify(shadowVariableReference, entity, newValue); |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + private void changeShadowVariableAndNotify(VariableUpdaterInfo<Solution_> shadowVariableReference, Object entity, |
| 143 | + Object newValue) { |
| 144 | + var variableDescriptor = shadowVariableReference.variableDescriptor(); |
| 145 | + changeShadowVariableAndNotify(variableDescriptor, entity, newValue); |
| 146 | + } |
| 147 | + |
| 148 | + private void changeShadowVariableAndNotify(VariableDescriptor<Solution_> variableDescriptor, Object entity, |
| 149 | + Object newValue) { |
| 150 | + changedVariableNotifier.beforeVariableChanged().accept(variableDescriptor, entity); |
| 151 | + variableDescriptor.setValue(entity, newValue); |
| 152 | + changedVariableNotifier.afterVariableChanged().accept(variableDescriptor, entity); |
| 153 | + } |
| 154 | + |
| 155 | + private static final class AffectedEntities<Solution_> { |
| 156 | + |
| 157 | + private final Consumer<Object> consumer; |
| 158 | + private final Set<Object> entitiesForLoopedVarUpdateSet; |
| 159 | + |
| 160 | + public AffectedEntities(Consumer<Object> consumer) { |
| 161 | + this.consumer = consumer; |
| 162 | + this.entitiesForLoopedVarUpdateSet = new LinkedIdentityHashSet<>(); |
| 163 | + } |
| 164 | + |
| 165 | + public void add(EntityVariablePair<Solution_> shadowVariable) { |
| 166 | + var shadowVariableLoopedDescriptor = shadowVariable.variableReference().shadowVariableLoopedDescriptor(); |
| 167 | + if (shadowVariableLoopedDescriptor == null) { |
| 168 | + return; |
| 169 | + } |
| 170 | + entitiesForLoopedVarUpdateSet.add(shadowVariable.entity()); |
| 171 | + } |
| 172 | + |
| 173 | + public void processAndClear() { |
| 174 | + for (var entity : entitiesForLoopedVarUpdateSet) { |
| 175 | + consumer.accept(entity); |
| 176 | + } |
| 177 | + entitiesForLoopedVarUpdateSet.clear(); |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments