This is the 19th day of my participation in the Genwen Challenge

Antecedents prompt

So far we have studied three examples of OptaPlanner in the last few days, and I believe you have a better understanding of OptaPlanner. You’ll notice that in all of these examples we used Drools to write constraints and scores. In these examples, you will have a question, we add or modify constraints, how to test whether the constraint is effective, how does the match? So today we are going to learn about constraint testing based on Drools.

content

To use a unit testing tool for Drool-based constraints, start by adding optaplanner-test.jar to take advantage of the JUnit integration, and use the ScoreVerifier class to test score rules in DRL (or an incremental score calculator for constraint matching). For example, let’s look at the CloudBalance case and test these score rules:

global HardSoftScoreHolder scoreHolder;

rule "requiredCpuPowerTotal"when ... then scoreHolder.addHardConstraintMatch(...) ; end ... rule"computerCost"when ... then scoreHolder.addSoftConstraintMatch(...) ; endCopy the code

implementation

For each scoring rule, create a separate @test that tests only the effect of that scoring rule on the score.

CloudBalancingScoreConstraintTest.java

public class CloudBalancingScoreConstraintTest {

    private HardSoftScoreVerifier<CloudBalance> scoreVerifier = new HardSoftScoreVerifier<>(
            SolverFactory.createFromXmlResource(CloudBalancingApp.SOLVER_CONFIG));

    @Test
    public void requiredCpuPowerTotal(a) {... }@Test
    public void requiredMemoryTotal(a) {...@Test
    public void requiredNetworkBandwidthTotal(a) {... }@Test
    public void computerCost(a) {
        CloudComputer c1 = new CloudComputer(1L.1.1.1.200);
        CloudComputer c2 = new CloudComputer(2L.1.1.1.30);
        CloudComputer c3 = new CloudComputer(3L.1.1.1.4);
        CloudProcess p1 = new CloudProcess(1L.5.5.5);
        CloudProcess p2 = new CloudProcess(2L.5.5.5);
        CloudProcess p3 = new CloudProcess(3L.5.5.5);
        CloudBalance solution = new CloudBalance(0L,
                Arrays.asList(c1, c2, c3),
                Arrays.asList(p1, p2, p3));
        scoreVerifier.assertSoftWeight("computerCost".0, solution);
        p1.setComputer(c1);
        p2.setComputer(c1);
        scoreVerifier.assertSoftWeight("computerCost", -200, solution);
        p3.setComputer(c3);
        scoreVerifier.assertSoftWeight("computerCost", -204, solution); }}Copy the code

As you can see, if we are testing a rule, it is best to add a separate method to test it.

The test method

Let’s look at this constraint test:

 @Test
    public void computerCost(a) {
        CloudComputer c1 = new CloudComputer(1L.1.1.1.200);
        CloudComputer c2 = new CloudComputer(2L.1.1.1.30);
        CloudComputer c3 = new CloudComputer(3L.1.1.1.4);
        CloudProcess p1 = new CloudProcess(1L.5.5.5);
        CloudProcess p2 = new CloudProcess(2L.5.5.5);
        CloudProcess p3 = new CloudProcess(3L.5.5.5);
        CloudBalance solution = new CloudBalance(0L,
                Arrays.asList(c1, c2, c3),
                Arrays.asList(p1, p2, p3));
        scoreVerifier.assertSoftWeight("computerCost".0, solution);
        p1.setComputer(c1);
        p2.setComputer(c1);
        scoreVerifier.assertSoftWeight("computerCost", -200, solution);
        p3.setComputer(c3);
        scoreVerifier.assertSoftWeight("computerCost", -204, solution);
    }
Copy the code

Test steps

First, we need to create a new Solution object. In the CloudBalance case, the Solution class is CloudBalance-java. The data is then initialized.

CloudProcess is a PlanningEntity in which the computer property is PlanningVariable. After modifying this variable, we can test whether the result meets our expectations through the assertSoftWeight assertion method.

Step 1: Initialize Progress that does not assign a Computer, which costs 0;

Step 2: P1 and P2 threads allocate machine C1 in Computer, which costs -200 yuan;

Step 3: p3 thread allocates machine C3 in Computer, its cost is -204 yuan;

Let’s execute to see the result:

com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 org.optaplanner.examples.cloudbalancing.solver.CloudBalancingScoreConstraintTest,computerCost Process finished with exit  code0
Copy the code

At this point we modify a line:

. scoreVerifier.assertSoftWeight("computerCost".0, solution);
p1.setComputer(c1);
p2.setComputer(c1);
scoreVerifier.assertSoftWeight("computerCost".0, solution); / / 200 - > 0.Copy the code

Now let’s look at the results:

org.opentest4j.AssertionFailedError: 
Expected :0
Actual   :-200<Click to see difference> org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTe stExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
	at org.junit.platform.launcher.core.DefaultLauncher$$Lambda$132/695682681.accept(Unknown Source)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Copy the code

Expected results were not in line with Actual results.

conclusion

From this example, we learned how OptaPlanner tests Drools’ constraint rules, which is very important for us because you don’t expect to test in a production environment, or need a complete set of solution data locally to start testing.

conclusion

In the next chapter we’ll look at a test example of ConstraintStream.

Creation is not easy, unauthorized reprint is prohibited. If my article is helpful to you, please like/favorites/follow it to encourage and support 💕💕💕💕 college