Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding penalty methods for long and BigDecimal values without match weighter #656

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ default void expandToQuad() {

void penalizeUnweighted();

void penalizeUnweightedLong();

void penalizeUnweightedBigDecimal();

void penalize();

void penalizeLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,42 @@ public void penalizeUnweighted() {
assertDefaultJustifications(scoreDirector, solution.getEntityList());
}

@Override
@TestTemplate
public void penalizeUnweightedLong() {
TestdataSimpleLongScoreSolution solution = TestdataSimpleLongScoreSolution.generateSolution();

InnerScoreDirector<TestdataSimpleLongScoreSolution, SimpleLongScore> scoreDirector = buildScoreDirector(
TestdataSimpleLongScoreSolution.buildSolutionDescriptor(),
factory -> new Constraint[] {
factory.forEachUniquePair(TestdataEntity.class)
.penalizeLong(SimpleLongScore.ONE)
.asConstraint(TEST_CONSTRAINT_NAME)
});

scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
assertThat(scoreDirector.calculateScore()).isEqualTo(SimpleLongScore.of(-21));
}

@Override
@TestTemplate
public void penalizeUnweightedBigDecimal() {
TestdataSimpleBigDecimalScoreSolution solution = TestdataSimpleBigDecimalScoreSolution.generateSolution();

InnerScoreDirector<TestdataSimpleBigDecimalScoreSolution, SimpleBigDecimalScore> scoreDirector =
buildScoreDirector(TestdataSimpleBigDecimalScoreSolution.buildSolutionDescriptor(),
factory -> new Constraint[] {
factory.forEachUniquePair(TestdataEntity.class)
.penalizeBigDecimal(SimpleBigDecimalScore.ONE)
.asConstraint(TEST_CONSTRAINT_NAME)
});

scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
assertThat(scoreDirector.calculateScore()).isEqualTo(SimpleBigDecimalScore.of(BigDecimal.valueOf(-21)));
}

private <Score_ extends Score<Score_>, Solution_, Entity_> void assertDefaultJustifications(
InnerScoreDirector<Solution_, Score_> scoreDirector, List<Entity_> entityList) {
if (!implSupport.isConstreamMatchEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,48 @@ public void penalizeUnweighted() {
assertDefaultJustifications(scoreDirector, solution.getEntityList(), solution.getValueList());
}

@Override
@TestTemplate
public void penalizeUnweightedLong() {
TestdataSimpleLongScoreSolution solution = TestdataSimpleLongScoreSolution.generateSolution();

InnerScoreDirector<TestdataSimpleLongScoreSolution, SimpleLongScore> scoreDirector = buildScoreDirector(
TestdataSimpleLongScoreSolution.buildSolutionDescriptor(),
factory -> new Constraint[] {
factory.forEachUniquePair(TestdataEntity.class, equal(TestdataEntity::getValue))
.join(TestdataValue.class, equal((entity, entity2) -> entity.getValue(), identity()))
.join(TestdataValue.class, equal((entity, entity2, value) -> value, identity()))
.penalizeLong(SimpleLongScore.ONE)
.asConstraint(TEST_CONSTRAINT_NAME)
});

scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
assertThat(scoreDirector.calculateScore()).isEqualTo(SimpleLongScore.of(-2));
assertDefaultJustifications(scoreDirector, solution.getEntityList(), solution.getValueList());
}

@Override
@TestTemplate
public void penalizeUnweightedBigDecimal() {
TestdataSimpleBigDecimalScoreSolution solution = TestdataSimpleBigDecimalScoreSolution.generateSolution();

InnerScoreDirector<TestdataSimpleBigDecimalScoreSolution, SimpleBigDecimalScore> scoreDirector =
buildScoreDirector(TestdataSimpleBigDecimalScoreSolution.buildSolutionDescriptor(),
factory -> new Constraint[] {
factory.forEachUniquePair(TestdataEntity.class, equal(TestdataEntity::getValue))
.join(TestdataValue.class, equal((entity, entity2) -> entity.getValue(), identity()))
.join(TestdataValue.class, equal((entity, entity2, value) -> value, identity()))
.penalizeBigDecimal(SimpleBigDecimalScore.ONE)
.asConstraint(TEST_CONSTRAINT_NAME)
});

scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
assertThat(scoreDirector.calculateScore()).isEqualTo(SimpleBigDecimalScore.of(BigDecimal.valueOf(-2)));
assertDefaultJustifications(scoreDirector, solution.getEntityList(), solution.getValueList());
}

private <Score_ extends Score<Score_>, Solution_, Entity_, Value_> void assertDefaultJustifications(
InnerScoreDirector<Solution_, Score_> scoreDirector, List<Entity_> entityList, List<Value_> valueList) {
if (!implSupport.isConstreamMatchEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,46 @@ public void penalizeUnweighted() {
assertDefaultJustifications(scoreDirector, solution.getEntityList(), solution.getValueList());
}

@Override
@TestTemplate
public void penalizeUnweightedLong() {
TestdataSimpleLongScoreSolution solution = TestdataSimpleLongScoreSolution.generateSolution();

InnerScoreDirector<TestdataSimpleLongScoreSolution, SimpleLongScore> scoreDirector = buildScoreDirector(
TestdataSimpleLongScoreSolution.buildSolutionDescriptor(),
factory -> new Constraint[] {
factory.forEachUniquePair(TestdataEntity.class, equal(TestdataEntity::getValue))
.join(TestdataValue.class, equal((entity, entity2) -> entity.getValue(), identity()))
.penalizeLong(SimpleLongScore.ONE)
.asConstraint(TEST_CONSTRAINT_NAME)
});

scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
assertThat(scoreDirector.calculateScore()).isEqualTo(SimpleLongScore.of(-2));
assertDefaultJustifications(scoreDirector, solution.getEntityList(), solution.getValueList());
}

@Override
@TestTemplate
public void penalizeUnweightedBigDecimal() {
TestdataSimpleBigDecimalScoreSolution solution = TestdataSimpleBigDecimalScoreSolution.generateSolution();

InnerScoreDirector<TestdataSimpleBigDecimalScoreSolution, SimpleBigDecimalScore> scoreDirector =
buildScoreDirector(TestdataSimpleBigDecimalScoreSolution.buildSolutionDescriptor(),
factory -> new Constraint[] {
factory.forEachUniquePair(TestdataEntity.class, equal(TestdataEntity::getValue))
.join(TestdataValue.class, equal((entity, entity2) -> entity.getValue(), identity()))
.penalizeBigDecimal(SimpleBigDecimalScore.ONE)
.asConstraint(TEST_CONSTRAINT_NAME)
});

scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
assertThat(scoreDirector.calculateScore()).isEqualTo(SimpleBigDecimalScore.of(BigDecimal.valueOf(-2)));
assertDefaultJustifications(scoreDirector, solution.getEntityList(), solution.getValueList());
}

private <Score_ extends Score<Score_>, Solution_, Entity_, Value_> void assertDefaultJustifications(
InnerScoreDirector<Solution_, Score_> scoreDirector, List<Entity_> entityList, List<Value_> valueList) {
if (!implSupport.isConstreamMatchEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,42 @@ public void penalizeUnweighted() {
assertDefaultJustifications(scoreDirector, solution.getEntityList());
}

@Override
@TestTemplate
public void penalizeUnweightedLong() {
TestdataSimpleLongScoreSolution solution = TestdataSimpleLongScoreSolution.generateSolution();

InnerScoreDirector<TestdataSimpleLongScoreSolution, SimpleLongScore> scoreDirector = buildScoreDirector(
TestdataSimpleLongScoreSolution.buildSolutionDescriptor(),
factory -> new Constraint[] {
factory.forEach(TestdataEntity.class)
.penalizeLong(SimpleLongScore.ONE)
.asConstraint(TEST_CONSTRAINT_NAME)
});

scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
assertThat(scoreDirector.calculateScore()).isEqualTo(SimpleLongScore.of(-7));
assertDefaultJustifications(scoreDirector, solution.getEntityList());
}

@Override
@TestTemplate
public void penalizeUnweightedBigDecimal() {
TestdataSimpleBigDecimalScoreSolution solution = TestdataSimpleBigDecimalScoreSolution.generateSolution();

InnerScoreDirector<TestdataSimpleBigDecimalScoreSolution, SimpleBigDecimalScore> scoreDirector =
buildScoreDirector(TestdataSimpleBigDecimalScoreSolution.buildSolutionDescriptor(),
factory -> new Constraint[] { factory.forEach(TestdataEntity.class)
.penalizeBigDecimal(SimpleBigDecimalScore.ONE)
.asConstraint(TEST_CONSTRAINT_NAME) });

scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
assertThat(scoreDirector.calculateScore()).isEqualTo(SimpleBigDecimalScore.of(BigDecimal.valueOf(-7)));
assertDefaultJustifications(scoreDirector, solution.getEntityList());
}

private <Score_ extends Score<Score_>, Solution_, Entity_> void assertDefaultJustifications(
InnerScoreDirector<Solution_, Score_> scoreDirector, List<Entity_> entityList) {
if (!implSupport.isConstreamMatchEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,24 @@ default <Score_ extends Score<Score_>> BiConstraintBuilder<A, B, Score_> penaliz
return penalize(constraintWeight, ConstantLambdaUtils.biConstantOne());
}

/**
* As defined by {@link #penalizeLong(Score, ToLongBiFunction)}, where the match weight is one (1).
*
* @return never null
*/
default <Score_ extends Score<Score_>> BiConstraintBuilder<A, B, Score_> penalizeLong(Score_ constraintWeight) {
return penalizeLong(constraintWeight, ConstantLambdaUtils.biConstantOneLong());
}

/**
* As defined by {@link #penalizeBigDecimal(Score, BiFunction)}, where the match weight is one (1).
*
* @return never null
*/
default <Score_ extends Score<Score_>> BiConstraintBuilder<A, B, Score_> penalizeBigDecimal(Score_ constraintWeight) {
return penalizeBigDecimal(constraintWeight, ConstantLambdaUtils.biConstantOneBigDecimal());
}

/**
* Applies a negative {@link Score} impact,
* subtracting the constraintWeight multiplied by the match weight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,25 @@ default <Score_ extends Score<Score_>> QuadConstraintBuilder<A, B, C, D, Score_>
return penalize(constraintWeight, ConstantLambdaUtils.quadConstantOne());
}

/**
* As defined by {@link #penalizeLong(Score, ToLongQuadFunction)}, where the match weight is one (1).
*
* @return never null
*/
default <Score_ extends Score<Score_>> QuadConstraintBuilder<A, B, C, D, Score_> penalizeLong(Score_ constraintWeight) {
return penalizeLong(constraintWeight, ConstantLambdaUtils.quadConstantOneLong());
}

/**
* As defined by {@link #penalizeBigDecimal(Score, QuadFunction)}, where the match weight is one (1).
*
* @return never null
*/
default <Score_ extends Score<Score_>> QuadConstraintBuilder<A, B, C, D, Score_>
penalizeBigDecimal(Score_ constraintWeight) {
return penalizeBigDecimal(constraintWeight, ConstantLambdaUtils.quadConstantOneBigDecimal());
}

/**
* Applies a negative {@link Score} impact,
* subtracting the constraintWeight multiplied by the match weight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,24 @@ default <Score_ extends Score<Score_>> TriConstraintBuilder<A, B, C, Score_> pen
return penalize(constraintWeight, ConstantLambdaUtils.triConstantOne());
}

/**
* As defined by {@link #penalizeLong(Score, ToLongTriFunction)}, where the match weight is one (1).
*
* @return never null
*/
default <Score_ extends Score<Score_>> TriConstraintBuilder<A, B, C, Score_> penalizeLong(Score_ constraintWeight) {
return penalizeLong(constraintWeight, ConstantLambdaUtils.triConstantOneLong());
}

/**
* As defined by {@link #penalizeBigDecimal(Score, TriFunction)}, where the match weight is one (1).
*
* @return never null
*/
default <Score_ extends Score<Score_>> TriConstraintBuilder<A, B, C, Score_> penalizeBigDecimal(Score_ constraintWeight) {
return penalizeBigDecimal(constraintWeight, ConstantLambdaUtils.triConstantOneBigDecimal());
}

/**
* Applies a negative {@link Score} impact,
* subtracting the constraintWeight multiplied by the match weight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,24 @@ default <Score_ extends Score<Score_>> UniConstraintBuilder<A, Score_> penalize(
return penalize(constraintWeight, ConstantLambdaUtils.uniConstantOne());
}

/**
* As defined by {@link #penalizeLong(Score, ToLongFunction)}, where the match weight is one (1).
*
* @return never null
*/
default <Score_ extends Score<Score_>> UniConstraintBuilder<A, Score_> penalizeLong(Score_ constraintWeight) {
return penalizeLong(constraintWeight, ConstantLambdaUtils.uniConstantOneLong());
}

/**
* As defined by {@link #penalizeBigDecimal(Score, Function)}, where the match weight is one (1).
*
* @return never null
*/
default <Score_ extends Score<Score_>> UniConstraintBuilder<A, Score_> penalizeBigDecimal(Score_ constraintWeight) {
return penalizeBigDecimal(constraintWeight, ConstantLambdaUtils.uniConstantOneBigDecimal());
}

/**
* Applies a negative {@link Score} impact,
* subtracting the constraintWeight multiplied by the match weight,
Expand Down