如何正确初始化 ConstraintVerifier 以在 Kotlin 中测试 Optaplanner ConstraintStreams

How to initialize correctly the ConstraintVerifier for testing Optaplanner ConstraintStreams in Kotlin

如何在不使用 Drools 和 Quarkus 的情况下在 Kotlin 中初始化 ConstraintVerifier? 我已经为 Optaplanner 8.6 添加了 optaplanner-test JAR 和 Maven 依赖项。0.Final 并尝试了以下方式:

var constraintVerifier : ConstraintVerifier<GroupAssignmentConstraintProvider, GroupAssignmentSolution> =
    ConstraintVerifier.build(GroupAssignmentConstraintProvider(),
        GroupAssignmentSolution::class.java,
        GroupAssignment::class.java) 

(ConstraintProvider:GroupAssignmentConstraintProvider,解决方案:GroupAssignmentSolution,PlanningEntity:GroupAssignment) 解决方案 class 得到一个 mutableSetOf Students、Lecturers 和一些配置(组的最小/最大数量 participants/students、最佳组大小和加权关键字)用于求解。

我的测试结果

    @Test
    fun lessThanMinGroupSize() {
    val students = Student().generateStudents(2)
    val configuration = Configuration(3, 9, 5, specialKeywords)

    val lecturer = mutableSetOf(
    Lecturer("4664807", mutableListOf(KeyWord("kotlin"), KeyWord("logistics"), KeyWord("java"), KeyWord("bigdata"), KeyWord("c")), mutableListOf("BIN", "BWI", "BEC"),
        mutableListOf(LocalDate.parse("2021-08-05"), LocalDate.parse("2021-08-08")),
        false, true))

    constraintVerifier.verifyThat(GroupAssignmentConstraintProvider::minGroupSize)
        .given(students, lecturer, configuration)
        .penalizes()
}

使用 minGroupSize 约束:

    fun minGroupSize(constraintFactory: ConstraintFactory): Constraint {
    return constraintFactory
        .from(GroupAssignment::class.java)
        .groupBy(GroupAssignment::group, count())
        .filter { group, number ->
            number < group!!.minsize
        }
        .penalize("group min Conflict", ofHard(medium))
}

告诉我: (GroupAssignmentTest.kt:65指的是Test中的.penalizes())

java.lang.AssertionError: Broken expectation.

Constraint: groupAssignment.solver/group min Conflict

Explanation of score (0hard/0soft):

Constraint match totals:

Indictments:

Expected penalty but there was none.

at org.optaplanner.test.impl.score.stream.DefaultSingleConstraintAssertion.assertMatch(DefaultSingleConstraintAssertion.java:185) at org.optaplanner.test.impl.score.stream.DefaultSingleConstraintAssertion.penalizes(DefaultSingleConstraintAssertion.java:82) at org.optaplanner.test.api.score.stream.SingleConstraintAssertion.penalizes(SingleConstraintAssertion.java:121) at GroupAssignmentTest.lessThanMinGroupSize(GroupAssignmentTest.kt:65) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod[=20=](ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke[=20=](ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod(TestMethodTestDescriptor.java:205) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:201) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:135) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)

除此之外,约束起作用了。

我想,我没有以正确的方式初始化或使用 ConstraintVerifier。有没有人可以帮我解决这个问题? 谢谢!

您的测试有几个问题。首先:

        .given(students, lecturer, configuration)

given() 调用需要单独的实例。您正在为其提供实例集合,特别是一组可变的学生。您的约束不会执行 from(Set.class) 或类似的任何操作,因此它不会看到这些实例。您将不得不枚举您的学生,或者根本不给他们,因为您的约束既不 from() 也不 join() 对学生 class.

此外,您的约束 from(GroupAssignment.class),但我在您的 given() 参数中没有看到 GroupAssignment。再一次,约束流没有任何东西可以开始,因此断言消息是正确的——没有什么可以惩罚的。