Skip to content

Commit 1de9105

Browse files
committed
chore: add matchCount method
1 parent cd04760 commit 1de9105

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

core/src/main/java/ai/timefold/solver/core/api/score/analysis/ConstraintAnalysis.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static ai.timefold.solver.core.api.score.analysis.ScoreAnalysis.DEFAULT_SUMMARY_CONSTRAINT_MATCH_LIMIT;
44
import static java.util.Comparator.comparing;
55

6-
import java.util.Collections;
76
import java.util.Comparator;
87
import java.util.HashSet;
98
import java.util.List;
@@ -61,6 +60,16 @@ static <Score_ extends Score<Score_>> ConstraintAnalysis<Score_> of(ConstraintRe
6160
Objects.requireNonNull(score);
6261
}
6362

63+
public int matchCount() {
64+
if (matches == null) {
65+
throw new IllegalArgumentException("""
66+
The constraint matches must be non-null.
67+
Maybe use ScoreAnalysisFetchPolicy.FETCH_ALL to request the score analysis
68+
""");
69+
}
70+
return matches.size();
71+
}
72+
6473
ConstraintAnalysis<Score_> negate() {
6574
if (matches == null) {
6675
return ConstraintAnalysis.of(constraintRef, weight.negate(), score.negate());
@@ -176,7 +185,10 @@ Explanation of score (%s):
176185

177186
var constraintMatches = matches();
178187
if (constraintMatches == null) {
179-
constraintMatches = Collections.emptyList();
188+
throw new IllegalArgumentException("""
189+
The constraint matches must be non-null.
190+
Maybe use ScoreAnalysisFetchPolicy.FETCH_ALL to request the score analysis
191+
""");
180192
}
181193
summary.append("""
182194
%s: constraint (%s) has %s matches:

core/src/main/java/ai/timefold/solver/core/api/score/analysis/ScoreAnalysis.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ Explanation of score (%s):
173173
.forEach(constraint -> {
174174
var matches = constraint.matches();
175175
if (matches == null) {
176-
matches = Collections.emptyList();
176+
throw new IllegalArgumentException("""
177+
The constraint matches must be non-null.
178+
Maybe use ScoreAnalysisFetchPolicy.FETCH_ALL to request the score analysis
179+
""");
177180
}
178181
summary.append("""
179182
%s: constraint (%s) has %s matches:

core/src/test/java/ai/timefold/solver/core/api/score/analysis/ScoreAnalysisTest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static ai.timefold.solver.core.impl.score.director.InnerScoreDirector.getConstraintAnalysis;
44
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
5+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
56
import static org.assertj.core.api.SoftAssertions.assertSoftly;
67

78
import java.util.Arrays;
@@ -31,9 +32,9 @@ void empty() {
3132
var summary = scoreAnalysis.summarize();
3233
assertThat(summary)
3334
.isEqualTo("""
34-
Explanation of score (0):
35-
Constraint matches:
36-
""");
35+
Explanation of score (0):
36+
Constraint matches:
37+
""");
3738
}
3839

3940
@Test
@@ -62,7 +63,7 @@ void summarize() {
6263
var constraintAnalysisMap = Map.of(
6364
constraintMatchTotal.getConstraintRef(), getConstraintAnalysis(constraintMatchTotal, true),
6465
constraintMatchTotal2.getConstraintRef(), getConstraintAnalysis(constraintMatchTotal2, true),
65-
emptyConstraintMatchTotal1.getConstraintRef(), getConstraintAnalysis(emptyConstraintMatchTotal1, false));
66+
emptyConstraintMatchTotal1.getConstraintRef(), getConstraintAnalysis(emptyConstraintMatchTotal1, true));
6667
var scoreAnalysis = new ScoreAnalysis<>(SimpleScore.of(67), constraintAnalysisMap);
6768

6869
// Single constraint analysis
@@ -80,6 +81,7 @@ Explanation of score (27):
8081

8182
// Complete score analysis
8283
var summary = scoreAnalysis.summarize();
84+
assertThat(scoreAnalysis.getConstraintAnalysis(constraintPackage, constraintName1).matchCount()).isEqualTo(5);
8385
assertThat(summary)
8486
.isEqualTo("""
8587
Explanation of score (67):
@@ -98,6 +100,25 @@ Explanation of score (67):
98100
""");
99101
}
100102

103+
@Test
104+
void failFastSummarize() {
105+
var constraintPackage = "constraintPackage";
106+
var constraintName1 = "constraint1";
107+
var constraintId1 = ConstraintRef.of(constraintPackage, constraintName1);
108+
109+
var constraintMatchTotal = new DefaultConstraintMatchTotal<>(constraintId1, SimpleScore.of(1));
110+
addConstraintMatch(constraintMatchTotal, SimpleScore.of(2), "A", "B", "C");
111+
var constraintAnalysisMap = Map.of(
112+
constraintMatchTotal.getConstraintRef(), getConstraintAnalysis(constraintMatchTotal, false));
113+
var scoreAnalysis = new ScoreAnalysis<>(SimpleScore.of(3), constraintAnalysisMap);
114+
115+
assertThatThrownBy(scoreAnalysis::summarize)
116+
.hasMessageContaining("The constraint matches must be non-null");
117+
118+
assertThatThrownBy(() -> constraintAnalysisMap.values().stream().findFirst().get().matchCount())
119+
.hasMessageContaining("The constraint matches must be non-null");
120+
}
121+
101122
@Test
102123
void compareWithConstraintMatchesWithoutMatchAnalysis() {
103124
var constraintPackage = "constraintPackage";

0 commit comments

Comments
 (0)