/
DefaultSingleConstraintAssertion.java
293 lines (264 loc) · 13.2 KB
/
DefaultSingleConstraintAssertion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package org.optaplanner.test.impl.score.stream;
import static java.util.Objects.requireNonNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import org.optaplanner.constraint.streams.common.AbstractConstraint;
import org.optaplanner.constraint.streams.common.AbstractConstraintStreamScoreDirectorFactory;
import org.optaplanner.constraint.streams.common.ScoreImpactType;
import org.optaplanner.core.api.score.Score;
import org.optaplanner.core.api.score.constraint.ConstraintMatchTotal;
import org.optaplanner.core.api.score.constraint.Indictment;
import org.optaplanner.core.impl.score.DefaultScoreExplanation;
import org.optaplanner.core.impl.score.definition.ScoreDefinition;
import org.optaplanner.core.impl.util.Pair;
import org.optaplanner.test.api.score.stream.SingleConstraintAssertion;
public final class DefaultSingleConstraintAssertion<Solution_, Score_ extends Score<Score_>>
implements SingleConstraintAssertion {
private final AbstractConstraint<Solution_, ?, ?> constraint;
private final ScoreDefinition<Score_> scoreDefinition;
private final Score_ score;
private final Collection<ConstraintMatchTotal<Score_>> constraintMatchTotalCollection;
private final Collection<Indictment<Score_>> indictmentCollection;
DefaultSingleConstraintAssertion(AbstractConstraintStreamScoreDirectorFactory<Solution_, Score_> scoreDirectorFactory,
Score_ score, Map<String, ConstraintMatchTotal<Score_>> constraintMatchTotalMap,
Map<Object, Indictment<Score_>> indictmentMap) {
this.constraint = (AbstractConstraint<Solution_, ?, ?>) scoreDirectorFactory.getConstraints()[0];
this.scoreDefinition = scoreDirectorFactory.getScoreDefinition();
this.score = requireNonNull(score);
this.constraintMatchTotalCollection = new ArrayList<>(requireNonNull(constraintMatchTotalMap).values());
this.indictmentCollection = new ArrayList<>(requireNonNull(indictmentMap).values());
}
@Override
public void penalizesBy(int matchWeightTotal, String message) {
validateMatchWeighTotal(matchWeightTotal);
assertImpact(ScoreImpactType.PENALTY, matchWeightTotal, message);
}
@Override
public void penalizesBy(long matchWeightTotal, String message) {
validateMatchWeighTotal(matchWeightTotal);
assertImpact(ScoreImpactType.PENALTY, matchWeightTotal, message);
}
@Override
public void penalizesBy(BigDecimal matchWeightTotal, String message) {
validateMatchWeighTotal(matchWeightTotal);
assertImpact(ScoreImpactType.PENALTY, matchWeightTotal, message);
}
@Override
public void penalizes(long times, String message) {
assertMatchCount(ScoreImpactType.PENALTY, times, message);
}
@Override
public void penalizes(String message) {
assertMatch(ScoreImpactType.PENALTY, message);
}
@Override
public void rewardsWith(int matchWeightTotal, String message) {
validateMatchWeighTotal(matchWeightTotal);
assertImpact(ScoreImpactType.REWARD, matchWeightTotal, message);
}
@Override
public void rewardsWith(long matchWeightTotal, String message) {
validateMatchWeighTotal(matchWeightTotal);
assertImpact(ScoreImpactType.REWARD, matchWeightTotal, message);
}
@Override
public void rewardsWith(BigDecimal matchWeightTotal, String message) {
validateMatchWeighTotal(matchWeightTotal);
assertImpact(ScoreImpactType.REWARD, matchWeightTotal, message);
}
@Override
public void rewards(long times, String message) {
assertMatchCount(ScoreImpactType.REWARD, times, message);
}
@Override
public void rewards(String message) {
assertMatch(ScoreImpactType.REWARD, message);
}
private void validateMatchWeighTotal(Number matchWeightTotal) {
if (matchWeightTotal.doubleValue() < 0) {
throw new IllegalArgumentException("The matchWeightTotal (" + matchWeightTotal + ") must be positive.");
}
}
private void assertImpact(ScoreImpactType scoreImpactType, Number matchWeightTotal, String message) {
BiPredicate<Number, Number> equalityPredicate =
NumberEqualityUtil.getEqualityPredicate(scoreDefinition, matchWeightTotal);
Pair<Number, Number> deducedImpacts = deduceImpact();
Number impact = deducedImpacts.getKey();
ScoreImpactType actualScoreImpactType = constraint.getScoreImpactType();
if (actualScoreImpactType == ScoreImpactType.MIXED) {
// Impact means we need to check for expected impact type and actual impact match.
switch (scoreImpactType) {
case REWARD:
Number negatedImpact = deducedImpacts.getValue();
if (equalityPredicate.test(matchWeightTotal, negatedImpact)) {
return;
}
break;
case PENALTY:
if (equalityPredicate.test(matchWeightTotal, impact)) {
return;
}
break;
}
} else if (actualScoreImpactType == scoreImpactType && equalityPredicate.test(matchWeightTotal, impact)) {
// Reward and positive or penalty and negative means all is OK.
return;
}
String constraintId = constraint.getConstraintId();
String assertionMessage = buildAssertionErrorMessage(scoreImpactType, matchWeightTotal, actualScoreImpactType,
impact, constraintId, message);
throw new AssertionError(assertionMessage);
}
/**
* Returns sum total of constraint match impacts,
* deduced from constraint matches.
*
* @return never null; key is the deduced impact, the value its negation
*/
private Pair<Number, Number> deduceImpact() {
Score_ zeroScore = scoreDefinition.getZeroScore();
Number zero = zeroScore.toLevelNumbers()[0]; // Zero in the exact numeric type expected by the caller.
if (constraintMatchTotalCollection.isEmpty()) {
return Pair.of(zero, zero);
}
// We do not know the matchWeight, so we need to deduce it.
// Constraint matches give us a score, whose levels are in the form of (matchWeight * constraintWeight).
// Here, we strip the constraintWeight.
Score_ totalMatchWeightedScore = constraintMatchTotalCollection.stream()
.map(matchScore -> scoreDefinition.divideBySanitizedDivisor(matchScore.getScore(),
matchScore.getConstraintWeight()))
.reduce(zeroScore, Score::add);
// Each level of the resulting score now has to be the same number, the matchWeight.
// Except for where the number is zero.
Number deducedImpact = retrieveImpact(totalMatchWeightedScore, zero);
if (deducedImpact.equals(zero)) {
return Pair.of(zero, zero);
}
Number negatedDeducedImpact = retrieveImpact(totalMatchWeightedScore.negate(), zero);
return Pair.of(deducedImpact, negatedDeducedImpact);
}
private Number retrieveImpact(Score_ score, Number zero) {
Number[] levelNumbers = score.toLevelNumbers();
List<Number> impacts = Arrays.stream(levelNumbers)
.distinct()
.filter(matchWeight -> !Objects.equals(matchWeight, zero))
.collect(Collectors.toList());
switch (impacts.size()) {
case 0:
return zero;
case 1:
return impacts.get(0);
default:
throw new IllegalStateException("Impossible state: expecting at most one match weight (" +
impacts.size() + ") in matchWeightedScore level numbers (" + Arrays.toString(levelNumbers) + ").");
}
}
private void assertMatchCount(ScoreImpactType scoreImpactType, long expectedMatchCount, String message) {
long actualMatchCount = determineMatchCount(scoreImpactType);
if (actualMatchCount == expectedMatchCount) {
return;
}
String constraintId = constraint.getConstraintId();
String assertionMessage =
buildAssertionErrorMessage(scoreImpactType, expectedMatchCount, actualMatchCount, constraintId, message);
throw new AssertionError(assertionMessage);
}
private void assertMatch(ScoreImpactType scoreImpactType, String message) {
if (determineMatchCount(scoreImpactType) > 0) {
return;
}
String constraintId = constraint.getConstraintId();
String assertionMessage = buildAssertionErrorMessage(scoreImpactType, constraintId, message);
throw new AssertionError(assertionMessage);
}
private long determineMatchCount(ScoreImpactType scoreImpactType) {
if (constraintMatchTotalCollection.isEmpty()) {
return 0;
}
ScoreImpactType actualImpactType = constraint.getScoreImpactType();
if (actualImpactType != scoreImpactType && actualImpactType != ScoreImpactType.MIXED) {
return 0;
}
Score_ zeroScore = scoreDefinition.getZeroScore();
return constraintMatchTotalCollection.stream()
.mapToLong(constraintMatchTotal -> {
if (actualImpactType == ScoreImpactType.MIXED) {
boolean isImpactPositive = constraintMatchTotal.getScore().compareTo(zeroScore) > 0;
boolean isImpactNegative = constraintMatchTotal.getScore().compareTo(zeroScore) < 0;
if (isImpactPositive && scoreImpactType == ScoreImpactType.PENALTY) {
return constraintMatchTotal.getConstraintMatchSet().size();
} else if (isImpactNegative && scoreImpactType == ScoreImpactType.REWARD) {
return constraintMatchTotal.getConstraintMatchSet().size();
} else {
return 0;
}
} else {
return constraintMatchTotal.getConstraintMatchSet().size();
}
})
.sum();
}
private String buildAssertionErrorMessage(ScoreImpactType expectedImpactType, Number expectedImpact,
ScoreImpactType actualImpactType, Number actualImpact, String constraintId, String message) {
String expectation = message != null ? message : "Broken expectation.";
String preformattedMessage = "%s%n" +
"%18s: %s%n" +
"%18s: %s (%s)%n" +
"%18s: %s (%s)%n%n" +
" %s";
String expectedImpactLabel = "Expected " + getImpactTypeLabel(expectedImpactType);
String actualImpactLabel = "Actual " + getImpactTypeLabel(actualImpactType);
return String.format(preformattedMessage,
expectation,
"Constraint", constraintId,
expectedImpactLabel, expectedImpact, expectedImpact.getClass(),
actualImpactLabel, actualImpact, actualImpact.getClass(),
DefaultScoreExplanation.explainScore(score, constraintMatchTotalCollection, indictmentCollection));
}
private String buildAssertionErrorMessage(ScoreImpactType impactType, long expectedTimes, long actualTimes,
String constraintId, String message) {
String expectation = message != null ? message : "Broken expectation.";
String preformattedMessage = "%s%n" +
"%18s: %s%n" +
"%18s: %s time(s)%n" +
"%18s: %s time(s)%n%n" +
" %s";
String expectedImpactLabel = "Expected " + getImpactTypeLabel(impactType);
String actualImpactLabel = "Actual " + getImpactTypeLabel(impactType);
return String.format(preformattedMessage,
expectation,
"Constraint", constraintId,
expectedImpactLabel, expectedTimes,
actualImpactLabel, actualTimes,
DefaultScoreExplanation.explainScore(score, constraintMatchTotalCollection, indictmentCollection));
}
private String buildAssertionErrorMessage(ScoreImpactType impactType, String constraintId, String message) {
String expectation = message != null ? message : "Broken expectation.";
String preformattedMessage = "%s%n" +
"%18s: %s%n" +
"%18s but there was none.%n%n" +
" %s";
String expectedImpactLabel = "Expected " + getImpactTypeLabel(impactType);
return String.format(preformattedMessage,
expectation,
"Constraint", constraintId,
expectedImpactLabel,
DefaultScoreExplanation.explainScore(score, constraintMatchTotalCollection, indictmentCollection));
}
private String getImpactTypeLabel(ScoreImpactType scoreImpactType) {
if (scoreImpactType == ScoreImpactType.PENALTY) {
return "penalty";
} else if (scoreImpactType == ScoreImpactType.REWARD) {
return "reward";
} else { // Needs to work with null.
return "impact";
}
}
}