Skip to content

Commit 638c2ab

Browse files
committed
sonar
1 parent a6b7de9 commit 638c2ab

11 files changed

Lines changed: 58 additions & 47 deletions

File tree

core/src/main/java/ai/timefold/solver/core/impl/bavet/AbstractBavetNodeNetwork.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected static AbstractNode[][] buildLayeredNodes(List<AbstractNode> nodeList)
6262
* @param layeredNodes nodes grouped first by their layer, then by their index within the layer;
6363
* propagation needs to happen in this order.
6464
*/
65-
public AbstractBavetNodeNetwork(Map<Class<?>, List<AbstractRootNode<?>>> declaredClassToNodeMap,
65+
protected AbstractBavetNodeNetwork(Map<Class<?>, List<AbstractRootNode<?>>> declaredClassToNodeMap,
6666
AbstractNode[][] layeredNodes, Function<AbstractNode, Propagator> propagatorFunction) {
6767
this.declaredClassToNodeMap = declaredClassToNodeMap;
6868
this.layeredNodes = layeredNodes;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @see PropagationQueue Description of the propagation mechanism.
1818
*/
1919
@NullMarked
20-
public sealed abstract class AbstractNode
20+
public abstract sealed class AbstractNode
2121
permits AbstractRootNode, AbstractSingleInputNode, AbstractTwoInputNode {
2222

2323
private long id;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final void afterAllFactsInsertedLeft(boolean upstreamCanProduceTuples) {
3434
leftInitialized = true;
3535
}
3636

37-
abstract protected boolean canProduceTuples();
37+
protected abstract boolean canProduceTuples();
3838

3939
@Override
4040
public final void afterAllFactsInsertedRight(boolean upstreamCanProduceTuples) {

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

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@
88

99
/**
1010
* When a Bavet session is created, some nodes can become inactive.
11-
* An inactive node cannot be triggered during a session as it cannot produce output.
12-
* Processing inserts, updates and retracts in inactive nodes is possibly expensive.
11+
* An inactive node cannot impact the score, as it cannot produce tuples.
12+
* Yet processing inserts, updates and retracts in inactive nodes still consumes CPU cycles.
1313
* This interface establishes a protocol through which nodes can signal at runtime
1414
* that they are inactive, so that the session can ignore them.
1515
* <p>
1616
* The pattern of interaction is as follows:
1717
* <ul>
1818
* <li>The session will first call {@link #afterAllFactsInserted(boolean)} on every {@link AbstractRootNode}.
19-
* The nodes will propagate this call downstream.
20-
* Each downstream node must propagate the call further downstream,
21-
* until a {@link Scorer} is reached. (In case of Constraint Streams.)</li>
22-
* <li>Then the session will call {@link #isActive()} on every single node,
23-
* and entirely remove deactivated nodes from propagation.
24-
* Each node makes its activity decision independently,
25-
* but may ask its downstream nodes.</li>
19+
* Each lifecycle must propagate the call further downstream, until a {@link Scorer} is reached.
20+
* (In case of Constraint Streams.)</li>
21+
* <li>Then the session will call {@link #isActive()} on every single lifecycle,
22+
* and entirely remove deactivated lifecycles from propagation.
23+
* Each lifecycle makes its activity decision independently,
24+
* but may ask its downstream lifecycles.</li>
2625
* <li>Once these two steps have been executed,
2726
* no method of this interface will ever be called again
2827
* for the duration of the session.</li>
2928
* </ul>
29+
*
30+
* @see TupleLifecycle
31+
* @see LeftTupleLifecycle
32+
* @see RightTupleLifecycle
3033
*/
3134
public interface ActivitySupport {
3235

@@ -39,26 +42,24 @@ public interface ActivitySupport {
3942
* <p>
4043
* It is the responsibility of the lifecycle to trigger initialization
4144
* of all of its downstream lifecycles, should there be any.
42-
* It must first decide for itself if it can produce tuples
43-
* based on what it learned from upstream,
45+
* It must first decide for itself if it can produce tuples based on what it learned from upstream,
4446
* and then propagate that information downstream so that they can make their own activation decisions.
4547
* <p>
4648
* When deciding whether a lifecycle can produce tuples, consider the following:
4749
* <ul>
4850
* <li>
4951
* Typically, when upstream cannot produce tuples, neither can downstream.
50-
* Exceptions exist; ifNotExists() produces tuples exactly when downstream doesn't.
52+
* (Unless downstream has the capability to fabricate tuples out of nowhere.)
5153
* </li>
5254
* <li>
53-
* Do not make decisions based on whether downstream produced any tuples by this point.
55+
* Do not make decisions based on whether upstream actually produced any tuples by this point.
5456
* If upstream produced no tuples so far, it doesn't mean it will never produce any.
55-
* Filters on variables which previously did not match
56-
* can easily create tuples during tuple updates.
57+
* Filters on variables which previously did not match can easily create tuples during tuple updates.
5758
* </li>
5859
* </ul>
5960
*
60-
* @param upstreamCanProduceTuples True if the upstream node(s) will produce any tuples.
61-
* If false, this lifecycle will never receive any tuples and can deactivate itself.
61+
* @param upstreamCanProduceTuples True if the upstream lifecycle(s) will produce any tuples.
62+
* If false, this lifecycle will never receive any tuples and can most likely deactivate itself.
6263
*/
6364
void afterAllFactsInserted(boolean upstreamCanProduceTuples);
6465

@@ -71,8 +72,9 @@ public interface ActivitySupport {
7172
* (Such as forEach(MyClass), when no MyClass instances were inserted.)</li>
7273
* <li>Its downstream tuples are inactive.
7374
* (A forEach() itself may be able to produce a tuple,
74-
* but a join downstream cannot, because its other side cannot produce tuples.
75-
* In this case, the left side must be deactivated as well.)
75+
* but a join() downstream cannot, because its other side cannot produce tuples.
76+
* In this case, the left side must be deactivated as well,
77+
* unless it also outputs to another active downstream lifecycle.)
7678
* </li>
7779
* </ul>
7880
*
@@ -86,17 +88,21 @@ public interface ActivitySupport {
8688
* </ul>
8789
*
8890
* This is a one-time decision;
89-
* for each lifecycle, this method will only be called once for the duration of {@link BavetConstraintSession}.
91+
* for each lifecycle, this method will only be called at the start of {@link BavetConstraintSession},
92+
* and never again.
93+
* It may be called multiple times by different upstream lifecycles,
94+
* and must always return the same value.
9095
*
9196
* <p>
9297
* Typically this decision will be {@code upstreamCanProduceTuples && downstreamIsActive},
93-
* but some nodes will have to use different logic.
98+
* but some specialized lifecycles may have to use different logic.
9499
*
95100
* @return true if this lifecycle can produce tuples
96101
*/
97102
default boolean isActive() {
98-
throw new IllegalStateException("Impossible state: node (%s) not yet initialized (afterAllFactsInserted not called)."
99-
.formatted(this));
103+
throw new IllegalStateException(
104+
"Impossible state: lifecycle (%s) not yet initialized (afterAllFactsInserted not called)."
105+
.formatted(this));
100106
}
101107

102108
}

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/LeftTupleLifecycleImpl.java renamed to core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/DefaultLeftTupleLifecycle.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import java.util.Objects;
44

5-
final class LeftTupleLifecycleImpl<Tuple_ extends Tuple>
5+
final class DefaultLeftTupleLifecycle<Tuple_ extends Tuple>
66
implements TupleLifecycle<Tuple_> {
77

88
private final LeftTupleLifecycle<Tuple_> leftTupleLifecycle;
99
private boolean isActive;
1010

11-
LeftTupleLifecycleImpl(LeftTupleLifecycle<Tuple_> leftTupleLifecycle) {
11+
DefaultLeftTupleLifecycle(LeftTupleLifecycle<Tuple_> leftTupleLifecycle) {
1212
Objects.requireNonNull(leftTupleLifecycle);
1313
this.leftTupleLifecycle = leftTupleLifecycle;
1414
}
@@ -50,7 +50,7 @@ public String toString() {
5050

5151
@Override
5252
public boolean equals(Object o) {
53-
return o instanceof LeftTupleLifecycleImpl<?> other
53+
return o instanceof DefaultLeftTupleLifecycle<?> other
5454
&& Objects.equals(this.leftTupleLifecycle, other.leftTupleLifecycle);
5555
}
5656

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/RightTupleLifecycleImpl.java renamed to core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/DefaultRightTupleLifecycle.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import java.util.Objects;
44

5-
final class RightTupleLifecycleImpl<Tuple_ extends Tuple>
5+
final class DefaultRightTupleLifecycle<Tuple_ extends Tuple>
66
implements TupleLifecycle<Tuple_> {
77

88
private final RightTupleLifecycle<Tuple_> rightTupleLifecycle;
99
private boolean isActive;
1010

11-
RightTupleLifecycleImpl(RightTupleLifecycle<Tuple_> rightTupleLifecycle) {
11+
DefaultRightTupleLifecycle(RightTupleLifecycle<Tuple_> rightTupleLifecycle) {
1212
Objects.requireNonNull(rightTupleLifecycle);
1313
this.rightTupleLifecycle = rightTupleLifecycle;
1414
}
@@ -50,7 +50,7 @@ public String toString() {
5050

5151
@Override
5252
public boolean equals(Object o) {
53-
return o instanceof RightTupleLifecycleImpl<?> other
53+
return o instanceof DefaultRightTupleLifecycle<?> other
5454
&& rightTupleLifecycle.equals(other.rightTupleLifecycle);
5555
}
5656

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import org.jspecify.annotations.Nullable;
77

88
@NullMarked
9-
public class RecordingTupleLifecycle<Tuple_ extends Tuple> implements TupleLifecycle<Tuple_>, AutoCloseable {
9+
public class RecordingTupleLifecycle<Tuple_ extends Tuple>
10+
implements TupleLifecycle<Tuple_>, AutoCloseable {
11+
1012
@Nullable
1113
TupleRecorder<Tuple_> tupleRecorder;
1214

@@ -22,19 +24,18 @@ public void close() {
2224

2325
@Override
2426
public void afterAllFactsInserted(boolean upstreamCanProduceTuples) {
27+
// Nothing to propagate to.
2528
}
2629

2730
@Override
2831
public boolean isActive() {
29-
return true;
32+
return true; // Always active.
3033
}
3134

3235
@Override
3336
public void insert(Tuple_ tuple) {
3437
if (tupleRecorder != null) {
35-
throw new IllegalStateException("""
36-
Impossible state: tuple %s was inserted during recording.
37-
""".formatted(tuple));
38+
throw new IllegalStateException("Impossible state: tuple %s was inserted during recording.".formatted(tuple));
3839
}
3940
}
4041

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public interface TupleLifecycle<Tuple_ extends Tuple>
2020
extends ActivitySupport {
2121

2222
static <Tuple_ extends Tuple> TupleLifecycle<Tuple_> ofLeft(LeftTupleLifecycle<Tuple_> leftTupleLifecycle) {
23-
return new LeftTupleLifecycleImpl<>(leftTupleLifecycle);
23+
return new DefaultLeftTupleLifecycle<>(leftTupleLifecycle);
2424
}
2525

2626
static <Tuple_ extends Tuple> TupleLifecycle<Tuple_> ofRight(RightTupleLifecycle<Tuple_> rightTupleLifecycle) {
27-
return new RightTupleLifecycleImpl<>(rightTupleLifecycle);
27+
return new DefaultRightTupleLifecycle<>(rightTupleLifecycle);
2828
}
2929

3030
@SuppressWarnings({ "rawtypes", "unchecked" })
@@ -80,11 +80,11 @@ static <Stream_ extends BavetStream, Tuple_ extends Tuple> TupleLifecycle<Tuple_
8080
if (delegate instanceof AbstractNode node) {
8181
streamKind = node.getStreamKind();
8282
qualifier = Qualifier.NODE;
83-
} else if (delegate instanceof LeftTupleLifecycleImpl<?> parent
83+
} else if (delegate instanceof DefaultLeftTupleLifecycle<?> parent
8484
&& parent.leftTupleLifecycle() instanceof AbstractNode node) {
8585
streamKind = node.getStreamKind();
8686
qualifier = Qualifier.LEFT_INPUT;
87-
} else if (delegate instanceof RightTupleLifecycleImpl<?> parent
87+
} else if (delegate instanceof DefaultRightTupleLifecycle<?> parent
8888
&& parent.rightTupleLifecycle() instanceof AbstractNode node) {
8989
streamKind = node.getStreamKind();
9090
qualifier = Qualifier.RIGHT_INPUT;

core/src/main/java/ai/timefold/solver/core/impl/neighborhood/stream/enumerating/DatasetSessionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private NeighborhoodsBavetNodeNetwork buildNodeNetwork(Set<AbstractEnumeratingSt
5757
}
5858
forEachUniNodeList.add(forEachUniNode);
5959
});
60-
return buildHelper.buildNodeNetwork(nodeList, declaredClassToNodeMap);
60+
return DataNodeBuildHelper.buildNodeNetwork(nodeList, declaredClassToNodeMap);
6161
}
6262

6363
}

core/src/main/java/ai/timefold/solver/core/impl/neighborhood/stream/enumerating/common/DataNodeBuildHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public SessionContext<Solution_> getSessionContext() {
4848
return Collections.unmodifiableList(datasetInstanceList);
4949
}
5050

51-
public NeighborhoodsBavetNodeNetwork buildNodeNetwork(List<AbstractNode> nodeList,
51+
public static NeighborhoodsBavetNodeNetwork buildNodeNetwork(List<AbstractNode> nodeList,
5252
Map<Class<?>, List<AbstractRootNode<?>>> declaredClassToNodeMap) {
5353
return NeighborhoodsBavetNodeNetwork.of(nodeList, declaredClassToNodeMap);
5454
}

0 commit comments

Comments
 (0)