Skip to content

Commit

Permalink
PLANNER-458 ScoreVerifier for each score impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Aug 21, 2016
1 parent 5422306 commit 7f2bfc8
Show file tree
Hide file tree
Showing 14 changed files with 856 additions and 62 deletions.
Expand Up @@ -42,17 +42,17 @@ public void requiredCpuPowerTotal() {
CloudBalance solution = new CloudBalance(0L,
Arrays.asList(c1, c2, c3),
Arrays.asList(p1, p2, p3));
scoreVerifier.assertHard("requiredCpuPowerTotal", 0, solution);
scoreVerifier.assertHardWeight("requiredCpuPowerTotal", 0, solution);
p1.setComputer(c1);
p2.setComputer(c1);
scoreVerifier.assertHard("requiredCpuPowerTotal", 0, solution);
scoreVerifier.assertHardWeight("requiredCpuPowerTotal", 0, solution);
p1.setComputer(c2);
p2.setComputer(c2);
scoreVerifier.assertHard("requiredCpuPowerTotal", -570, solution);
scoreVerifier.assertHardWeight("requiredCpuPowerTotal", -570, solution);
p3.setComputer(c3);
scoreVerifier.assertHard("requiredCpuPowerTotal", -570, solution);
scoreVerifier.assertHardWeight("requiredCpuPowerTotal", -570, solution);
p2.setComputer(c3);
scoreVerifier.assertHard("requiredCpuPowerTotal", -547, solution);
scoreVerifier.assertHardWeight("requiredCpuPowerTotal", -547, solution);
}

@Test
Expand All @@ -66,17 +66,17 @@ public void requiredMemoryTotal() {
CloudBalance solution = new CloudBalance(0L,
Arrays.asList(c1, c2, c3),
Arrays.asList(p1, p2, p3));
scoreVerifier.assertHard("requiredMemoryTotal", 0, solution);
scoreVerifier.assertHardWeight("requiredMemoryTotal", 0, solution);
p1.setComputer(c1);
p2.setComputer(c1);
scoreVerifier.assertHard("requiredMemoryTotal", 0, solution);
scoreVerifier.assertHardWeight("requiredMemoryTotal", 0, solution);
p1.setComputer(c2);
p2.setComputer(c2);
scoreVerifier.assertHard("requiredMemoryTotal", -570, solution);
scoreVerifier.assertHardWeight("requiredMemoryTotal", -570, solution);
p3.setComputer(c3);
scoreVerifier.assertHard("requiredMemoryTotal", -570, solution);
scoreVerifier.assertHardWeight("requiredMemoryTotal", -570, solution);
p2.setComputer(c3);
scoreVerifier.assertHard("requiredMemoryTotal", -547, solution);
scoreVerifier.assertHardWeight("requiredMemoryTotal", -547, solution);
}

@Test
Expand All @@ -90,17 +90,17 @@ public void requiredNetworkBandwidthTotal() {
CloudBalance solution = new CloudBalance(0L,
Arrays.asList(c1, c2, c3),
Arrays.asList(p1, p2, p3));
scoreVerifier.assertHard("requiredNetworkBandwidthTotal", 0, solution);
scoreVerifier.assertHardWeight("requiredNetworkBandwidthTotal", 0, solution);
p1.setComputer(c1);
p2.setComputer(c1);
scoreVerifier.assertHard("requiredNetworkBandwidthTotal", 0, solution);
scoreVerifier.assertHardWeight("requiredNetworkBandwidthTotal", 0, solution);
p1.setComputer(c2);
p2.setComputer(c2);
scoreVerifier.assertHard("requiredNetworkBandwidthTotal", -570, solution);
scoreVerifier.assertHardWeight("requiredNetworkBandwidthTotal", -570, solution);
p3.setComputer(c3);
scoreVerifier.assertHard("requiredNetworkBandwidthTotal", -570, solution);
scoreVerifier.assertHardWeight("requiredNetworkBandwidthTotal", -570, solution);
p2.setComputer(c3);
scoreVerifier.assertHard("requiredNetworkBandwidthTotal", -547, solution);
scoreVerifier.assertHardWeight("requiredNetworkBandwidthTotal", -547, solution);
}

@Test
Expand All @@ -114,12 +114,12 @@ public void computerCost() {
CloudBalance solution = new CloudBalance(0L,
Arrays.asList(c1, c2, c3),
Arrays.asList(p1, p2, p3));
scoreVerifier.assertSoft("computerCost", 0, solution);
scoreVerifier.assertSoftWeight("computerCost", 0, solution);
p1.setComputer(c1);
p2.setComputer(c1);
scoreVerifier.assertSoft("computerCost", -200, solution);
scoreVerifier.assertSoftWeight("computerCost", -200, solution);
p3.setComputer(c3);
scoreVerifier.assertSoft("computerCost", -204, solution);
scoreVerifier.assertSoftWeight("computerCost", -204, solution);
}

}
Expand Up @@ -55,13 +55,13 @@ public void skillRequirements() {
Arrays.asList(c1),
Arrays.asList(e1, e2, e3),
Arrays.asList(t1, t2, t3));
scoreVerifier.assertHard("Skill requirements", 0, 0, solution);
scoreVerifier.assertHardWeight("Skill requirements", 0, 0, solution);
setPreviousAndShadows(t1, e1);
scoreVerifier.assertHard("Skill requirements", 0, -1, solution);
scoreVerifier.assertHardWeight("Skill requirements", 0, -1, solution);
setPreviousAndShadows(t2, t1);
scoreVerifier.assertHard("Skill requirements", 0, -2, solution);
scoreVerifier.assertHardWeight("Skill requirements", 0, -2, solution);
setPreviousAndShadows(t3, e1);
scoreVerifier.assertHard("Skill requirements", 0, -2, solution);
scoreVerifier.assertHardWeight("Skill requirements", 0, -2, solution);
}

private static void setPreviousAndShadows(Task task, TaskOrEmployee previous) {
Expand Down
Expand Up @@ -66,38 +66,43 @@ public AbstractScoreVerifier(SolverFactory<Solution_> solverFactory,
* When null, {@code constraintName} for the {@code scoreLevel} must be unique.
* @param scoreLevel at least 0
* @param constraintName never null, the name of the constraint, which is usually the name of the score rule
* @param expectedWeight sometimes null, the total weight for all matches of that 1 constraint
* @param expectedWeight never null, the total weight for all matches of that 1 constraint
* @param solution never null
*/
protected void assertConstraintWeight(
protected void assertWeight(
String constraintPackage, String constraintName, int scoreLevel, Number expectedWeight,
Solution_ solution) {
ScoreDirector<Solution_> scoreDirector = scoreDirectorFactory.buildScoreDirector();
scoreDirector.setWorkingSolution(solution);
scoreDirector.calculateScore();
ConstraintMatchTotal matchTotal = findConstraintMatchTotal(constraintPackage, constraintName, scoreLevel, scoreDirector);
// A matchTotal is null if the score rule didn't fire now and never fired in a previous incremental calculation
// (including those that are undone).
// To avoid user pitfalls, the expectedWeight cannot be null and a matchTotal of null is treated as zero.
if (expectedWeight == null) {
throw new IllegalArgumentException("The expectedWeight (" + expectedWeight + ") cannot be null,"
+ " regardless of the matchTotal (" + matchTotal + ").");
}
if (matchTotal == null) {
if (expectedWeight != null) {
if (expectedWeight instanceof Byte) {
assertEquals(expectedWeight, (byte) 0);
} else if (expectedWeight instanceof Short) {
assertEquals(expectedWeight, (short) 0);
} else if (expectedWeight instanceof Integer) {
assertEquals(expectedWeight, 0);
} else if (expectedWeight instanceof Long) {
assertEquals(expectedWeight, 0L);
} else if (expectedWeight instanceof Float) {
assertEquals(expectedWeight, 0F);
} else if (expectedWeight instanceof Double) {
assertEquals(expectedWeight, 0D);
} else if (expectedWeight instanceof BigInteger) {
assertEquals(expectedWeight, BigInteger.ZERO);
} else if (expectedWeight instanceof BigDecimal) {
assertEquals(expectedWeight, BigDecimal.ZERO);
} else {
throw new IllegalStateException("Unsupported " + Number.class.getSimpleName()
+ " type (" + expectedWeight.getClass() + ") for expectedWeight (" + expectedWeight + ").");
}
if (expectedWeight instanceof Byte) {
assertEquals(expectedWeight, (byte) 0);
} else if (expectedWeight instanceof Short) {
assertEquals(expectedWeight, (short) 0);
} else if (expectedWeight instanceof Integer) {
assertEquals(expectedWeight, 0);
} else if (expectedWeight instanceof Long) {
assertEquals(expectedWeight, 0L);
} else if (expectedWeight instanceof Float) {
assertEquals(expectedWeight, 0F);
} else if (expectedWeight instanceof Double) {
assertEquals(expectedWeight, 0D);
} else if (expectedWeight instanceof BigInteger) {
assertEquals(expectedWeight, BigInteger.ZERO);
} else if (expectedWeight instanceof BigDecimal) {
assertEquals(expectedWeight, BigDecimal.ZERO);
} else {
throw new IllegalStateException("Unsupported " + Number.class.getSimpleName()
+ " type (" + expectedWeight.getClass() + ") for expectedWeight (" + expectedWeight + ").");
}
} else {
assertEquals(expectedWeight, matchTotal.getWeightTotalAsNumber());
Expand Down
Expand Up @@ -18,7 +18,6 @@

import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.score.buildin.bendable.BendableScore;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.impl.score.buildin.bendable.BendableScoreDefinition;
import org.optaplanner.test.impl.score.AbstractScoreVerifier;
Expand Down Expand Up @@ -49,8 +48,8 @@ public BendableScoreVerifier(SolverFactory<Solution_> solverFactory) {
* @param expectedWeight the total weight for all matches of that 1 constraint
* @param solution never null, the actual {@link PlanningSolution}
*/
public void assertHard(String constraintName, int hardLevel, int expectedWeight, Solution_ solution) {
assertHard(null, constraintName, hardLevel, expectedWeight, solution);
public void assertHardWeight(String constraintName, int hardLevel, int expectedWeight, Solution_ solution) {
assertHardWeight(null, constraintName, hardLevel, expectedWeight, solution);
}

/**
Expand All @@ -64,10 +63,10 @@ public void assertHard(String constraintName, int hardLevel, int expectedWeight,
* @param expectedWeight the total weight for all matches of that 1 constraint
* @param solution never null, the actual {@link PlanningSolution}
*/
public void assertHard(String constraintPackage, String constraintName,
public void assertHardWeight(String constraintPackage, String constraintName,
int hardLevel, int expectedWeight,
Solution_ solution) {
assertConstraintWeight(constraintPackage, constraintName,
assertWeight(constraintPackage, constraintName,
hardLevel, Integer.valueOf(expectedWeight), solution);
}

Expand All @@ -80,8 +79,8 @@ public void assertHard(String constraintPackage, String constraintName,
* @param expectedWeight the total weight for all matches of that 1 constraint
* @param solution never null, the actual {@link PlanningSolution}
*/
public void assertSoft(String constraintName, int softLevel, int expectedWeight, Solution_ solution) {
assertSoft(null, constraintName, softLevel, expectedWeight, solution);
public void assertSoftWeight(String constraintName, int softLevel, int expectedWeight, Solution_ solution) {
assertSoftWeight(null, constraintName, softLevel, expectedWeight, solution);
}

/**
Expand All @@ -95,9 +94,9 @@ public void assertSoft(String constraintName, int softLevel, int expectedWeight,
* @param expectedWeight the total weight for all matches of that 1 constraint
* @param solution never null, the actual {@link PlanningSolution}
*/
public void assertSoft(String constraintPackage, String constraintName, int softLevel, int expectedWeight,
public void assertSoftWeight(String constraintPackage, String constraintName, int softLevel, int expectedWeight,
Solution_ solution) {
assertConstraintWeight(constraintPackage, constraintName,
assertWeight(constraintPackage, constraintName,
hardLevelsSize + softLevel, Integer.valueOf(expectedWeight), solution);
}

Expand Down
@@ -0,0 +1,105 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.optaplanner.test.impl.score.buildin.bendablebigdecimal;

import java.math.BigDecimal;

import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.impl.score.buildin.bendablebigdecimal.BendableBigDecimalScoreDefinition;
import org.optaplanner.test.impl.score.AbstractScoreVerifier;

/**
* To assert the constraints (including score rules) of a {@link SolverFactory}
* that uses a {@link BendableBigDecimalScore}.
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
*/
public class BendableBigDecimalScoreVerifier<Solution_> extends AbstractScoreVerifier<Solution_> {

protected final int hardLevelsSize;

/**
* @param solverFactory never null, the {@link SolverFactory} of which you want to test the constraints.
*/
public BendableBigDecimalScoreVerifier(SolverFactory<Solution_> solverFactory) {
super(solverFactory, BendableBigDecimalScore.class);
hardLevelsSize = ((BendableBigDecimalScoreDefinition) scoreDirectorFactory.getScoreDefinition()).getHardLevelsSize();
}

/**
* Assert that the constraint (which is usually a score rule) of {@link PlanningSolution}
* has the expected weight for that score level
* @param constraintName never null, the name of the constraint, which is usually the name of the score rule
* @param hardLevel {@code 0 <= hardLevel <} {@code hardLevelSize}.
* The {@code scoreLevel} is {@code hardLevel} for hard levels and {@code softLevel + hardLevelSize} for soft levels.
* @param expectedWeight never null, the total weight for all matches of that 1 constraint
* @param solution never null, the actual {@link PlanningSolution}
*/
public void assertHardWeight(String constraintName, int hardLevel, BigDecimal expectedWeight, Solution_ solution) {
assertHardWeight(null, constraintName, hardLevel, expectedWeight, solution);
}

/**
* Assert that the constraint (which is usually a score rule) of {@link PlanningSolution}
* has the expected weight for that score level.
* @param constraintPackage sometimes null.
* When null, {@code constraintName} for the {@code scoreLevel} must be unique.
* @param constraintName never null, the name of the constraint, which is usually the name of the score rule
* @param hardLevel {@code 0 <= hardLevel <} {@code hardLevelSize}.
* The {@code scoreLevel} is {@code hardLevel} for hard levels and {@code softLevel + hardLevelSize} for soft levels.
* @param expectedWeight never null, the total weight for all matches of that 1 constraint
* @param solution never null, the actual {@link PlanningSolution}
*/
public void assertHardWeight(String constraintPackage, String constraintName,
int hardLevel, BigDecimal expectedWeight,
Solution_ solution) {
assertWeight(constraintPackage, constraintName,
hardLevel, expectedWeight, solution);
}

/**
* Assert that the constraint (which is usually a score rule) of {@link PlanningSolution}
* has the expected weight for that score level.
* @param constraintName never null, the name of the constraint, which is usually the name of the score rule
* @param softLevel {@code 0 <= softLevel <} {@code softLevelSize}.
* The {@code scoreLevel} is {@code hardLevel} for hard levels and {@code softLevel + hardLevelSize} for soft levels.
* @param expectedWeight never null, the total weight for all matches of that 1 constraint
* @param solution never null, the actual {@link PlanningSolution}
*/
public void assertSoftWeight(String constraintName, int softLevel, BigDecimal expectedWeight, Solution_ solution) {
assertSoftWeight(null, constraintName, softLevel, expectedWeight, solution);
}

/**
* Assert that the constraint (which is usually a score rule) of {@link PlanningSolution}
* has the expected weight for that score level.
* @param constraintPackage sometimes null.
* When null, {@code constraintName} for the {@code scoreLevel} must be unique.
* @param constraintName never null, the name of the constraint, which is usually the name of the score rule
* @param softLevel {@code 0 <= softLevel <} {@code softLevelSize}.
* The {@code scoreLevel} is {@code hardLevel} for hard levels and {@code softLevel + hardLevelSize} for soft levels.
* @param expectedWeight never null, the total weight for all matches of that 1 constraint
* @param solution never null, the actual {@link PlanningSolution}
*/
public void assertSoftWeight(String constraintPackage, String constraintName, int softLevel, BigDecimal expectedWeight,
Solution_ solution) {
assertWeight(constraintPackage, constraintName,
hardLevelsSize + softLevel, expectedWeight, solution);
}

}

0 comments on commit 7f2bfc8

Please sign in to comment.