Skip to content

Commit 57b24b1

Browse files
chore: move indictment code for IfExists to IndictmentSource
1 parent b67e780 commit 57b24b1

6 files changed

Lines changed: 19 additions & 24 deletions

File tree

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/AbstractIfExistsNode.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ai.timefold.solver.core.impl.bavet.common;
22

3-
import java.util.Objects;
4-
53
import ai.timefold.solver.core.impl.bavet.common.tuple.InTupleStorePositionTracker;
64
import ai.timefold.solver.core.impl.bavet.common.tuple.Tuple;
75
import ai.timefold.solver.core.impl.bavet.common.tuple.TupleLifecycle;
@@ -126,15 +124,13 @@ protected void incrementCounterRightUpdatingIndictment(ExistsCounter<LeftTuple_>
126124
doRetractCounter(counter);
127125
}
128126
} // Else do not even propagate an update
129-
counter.getTuple().getIndictmentSupportForNodeId(getId())
130-
.add(Objects.requireNonNull(rightTuple.getA()));
127+
IndictmentSource.addSupport(getId(), counter.leftTuple, rightTuple);
131128
counter.countRight++;
132129
}
133130

134131
protected void decrementCounterRight(ExistsCounter<LeftTuple_> counter) {
135132
counter.countRight--;
136-
counter.getTuple().getIndictmentSupportForNodeId(getId())
137-
.remove(Objects.requireNonNull(rightTuple.getA()));
133+
IndictmentSource.removeSupport(getId(), counter.leftTuple, rightTuple);
138134
if (counter.countRight == 0) {
139135
if (shouldExist) {
140136
doRetractCounter(counter);

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/Tuple.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public sealed interface Tuple permits BiTuple, QuadTuple, TriTuple, UniTuple {
2929

3030
void setIndictmentSource(IndictmentSource indictmentSource);
3131

32-
Set<Object> getIndictmentSupportForNodeId(long nodeId);
32+
Set<IndictmentSource> getIndictmentSupportForNodeId(long nodeId);
3333

3434
TupleState getState();
3535

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/UniversalTuple.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class UniversalTuple<A, B, C, D>
2828
private @Nullable C c;
2929
private @Nullable D d;
3030
private IndictmentSource indictmentSource = IndictmentSource.DISABLED;
31-
private @Nullable Map<Long, Set<Object>> nodeIdToIndictmentSupport;
31+
private @Nullable Map<Long, Set<IndictmentSource>> nodeIdToIndictmentSupport;
3232
private TupleState state = TupleState.DEAD; // It's the node's job to mark a new tuple as CREATING.
3333

3434
UniversalTuple(int storeSize, int cardinality) {
@@ -123,7 +123,7 @@ public void setIndictmentSource(IndictmentSource indictmentSource) {
123123
}
124124

125125
@Override
126-
public Set<Object> getIndictmentSupportForNodeId(long nodeId) {
126+
public Set<IndictmentSource> getIndictmentSupportForNodeId(long nodeId) {
127127
if (nodeIdToIndictmentSupport == null) {
128128
nodeIdToIndictmentSupport = new LinkedHashMap<>();
129129
}

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/indictment/IndictmentSource.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ static IndictmentSource removeFromAggregate(Tuple elementTuple, Tuple groupTuple
5050
}
5151
}
5252

53-
static IndictmentSource sourceWithSupport(Tuple carry, Tuple support) {
53+
static void addSupport(long nodeId, Tuple carry, Tuple support) {
5454
if (carry.getIndictmentSource() == DISABLED) {
55-
return DISABLED;
55+
return;
5656
}
57-
if (carry.getIndictmentSource() instanceof IndictmentSourceWithSupport indictmentSourceWithSupport) {
58-
indictmentSourceWithSupport.support.add(support.getIndictmentSource());
59-
return indictmentSourceWithSupport;
60-
} else {
61-
throw new IllegalStateException("Carry tuple (%s) does not have a %s %s; its source is (%s) instead."
62-
.formatted(carry, IndictmentSourceWithSupport.class.getSimpleName(), IndictmentSource.class.getSimpleName(),
63-
support.getIndictmentSource()));
57+
carry.getIndictmentSupportForNodeId(nodeId).add(support.getIndictmentSource());
58+
}
59+
60+
static void removeSupport(long nodeId, Tuple carry, Tuple support) {
61+
if (carry.getIndictmentSource() == DISABLED) {
62+
return;
6463
}
64+
carry.getIndictmentSupportForNodeId(nodeId).remove(support.getIndictmentSource());
6565
}
6666

6767
record DisabledIndictmentSource() implements IndictmentSource {

core/src/main/java/ai/timefold/solver/core/impl/score/stream/common/inliner/ConstraintMatchSupplier.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
public interface ConstraintMatchSupplier<Score_ extends Score<Score_>>
4343
extends BiFunction<Constraint, Score_, ConstraintMatch<Score_>> {
4444

45-
@SuppressWarnings("unchecked")
4645
static List<Object> collectIndictments(Constraint constraint, Tuple tuple) {
4746
if (tuple.getIndictmentSource() == IndictmentSource.DISABLED) {
4847
return Collections.emptyList();
@@ -51,7 +50,9 @@ static List<Object> collectIndictments(Constraint constraint, Tuple tuple) {
5150
tuple.getIndictmentSource().visitSources(out::add);
5251
var abstractConstraint = (AbstractConstraint<?, ?, ?>) constraint;
5352
for (var involvedNodeId : Objects.requireNonNull(abstractConstraint.getInvolvedNodeIds())) {
54-
out.addAll(tuple.getIndictmentSupportForNodeId(involvedNodeId));
53+
for (var indictmentSource : tuple.getIndictmentSupportForNodeId(involvedNodeId)) {
54+
indictmentSource.visitSources(out::add);
55+
}
5556
}
5657
return new ArrayList<>(out);
5758
}

core/src/test/java/ai/timefold/solver/core/impl/score/stream/common/uni/AbstractUniConstraintStreamTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,10 @@ public void ifExists_0Joiner0Filter() {
481481
assertScore(scoreDirector,
482482
assertMatch(solution.getFirstValueGroup()).withIndictedObjects(
483483
solution.getFirstValueGroup(),
484-
solution.getFirstEntityGroup(),
485-
entityGroup),
484+
solution.getFirstEntityGroup()),
486485
assertMatch(valueGroup).withIndictedObjects(
487486
valueGroup,
488-
solution.getFirstEntityGroup(),
489-
entityGroup));
487+
solution.getFirstEntityGroup()));
490488
}
491489

492490
@Override

0 commit comments

Comments
 (0)