带有 toList 的 Optaplanner GroupBy 未按预期工作 [英] Optaplanner GroupBy with toList not working as expected

查看:45
本文介绍了带有 toList 的 Optaplanner GroupBy 未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为强制午休编写一个简单的约束.

I'd like to write a simple constraint for mandatory lunch breaks.

我使用 ConstraintCollector toList"在我的小组中像这样:

I use the ConstraintCollector "toList" in my groupBy like so:

public Constraint lunchBreak(ConstraintFactory constraintFactory) {
    return constraintFactory.from(TimetableEntry.class)
            .groupBy(TimetableEntry::getPlanningDay, TimetableEntry::getSchoolClass, toList())
            .filter((day, schoolClass, entries) -> {
                if (entries.size() == 7) {
                    List<TimetableEntry> freePeriods = entries.stream().filter(
                            timetableEntry -> timetableEntry.getSubject().getId() == 1L).collect(Collectors.toList()
                    );
                    if (freePeriods.size() > 0) {
                        return false;
                    }
                    return true;
                }
                // todo logic for 8 hours
                // todo logic for 9 hours
                // todo logic for 10 hours
                return false;
            })
            .penalize("A 7-hour-day must have a lunch break somewhere",
                    ConstraintConstants.MANDATORY_LUNCH_BREAK_CONFLICT);
}

当我测试约束时(注意条目是一个集合)

When I test the constraint (Note that entries is a Set)

        this.constraintVerifier.verifyThat(TimetableConstraintProvider::lunchBreak)
                .given(entries.toArray())
                .penalizesBy(1);

我得到一个java.lang.ClassCastException: class TimetableEntry cannot be cast to class java.util.List".我很困惑为什么这是因为 GroupBy 与 count() 一起工作得很好,但我需要分组列表.

I get a "java.lang.ClassCastException: class TimetableEntry cannot be cast to class java.util.List". I am confused as to why that is because the GroupBy works with a count() just fine but I need the grouped list.

当我改为测试

        this.constraintVerifier.verifyThat(TimetableConstraintProvider::lunchBreak)
                .given(entries)
                .penalizesBy(1);

groupBy + 过滤器没有触发.

The groupBy + the filter is not firing.

推荐答案

正如评论所述,测试应该是这样的

As a comment states, the Test should look like this

        constraintVerifier.verifyThat(TimetableConstraintProvider::lunchBreak)
            .given(entries.toArray())
            .penalizesBy(0);

但是,groupBy 看起来不错,我找不到导致错误的原因.以下不知何故不会导致错误,但我不明白为什么.注意第一组基本上什么都不做,但删除它会导致错误:

However, the groupBy looks fine, and i cannot find what is causing the error. The following is somehow not causing an error, but i don't understand why. Note the first group by which is doing basically nothing, but removing it causes an error:

 public Constraint lunchBreak(ConstraintFactory constraintFactory) {
    return constraintFactory.from(TimetableEntry.class)
            .groupBy(timetableEntry -> timetableEntry)
            .groupBy(TimetableEntry::getPlanningDay, TimetableEntry::getSchoolClass, toList())
            .filter((day, schoolClass, entries) -> {
                // some logic
                })
             .penalize("A school class must have a lunch break after 6 hours",
                    ConstraintConstants.MANDATORY_LUNCH_BREAK_CONFLICT);

缺少第一个 group by 时的错误堆栈跟踪:

The stack trace of the error when the first group by is missing:

java.lang.RuntimeException: java.lang.ClassCastException: class ase.tito.entity.TimetableEntry cannot be cast to class java.util.List (ase.tito.entity.TimetableEntry is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')

at org.drools.modelcompiler.constraints.ConstraintEvaluator.evaluate(ConstraintEvaluator.java:124)
at org.drools.modelcompiler.constraints.LambdaConstraint.isAllowedCachedLeft(LambdaConstraint.java:148)
at org.drools.core.common.SingleBetaConstraints.isAllowedCachedLeft(SingleBetaConstraints.java:134)
at org.drools.core.phreak.PhreakFromNode.propagate(PhreakFromNode.java:283)
at org.drools.core.phreak.PhreakFromNode.doLeftInserts(PhreakFromNode.java:121)
at org.drools.core.phreak.PhreakFromNode.doNode(PhreakFromNode.java:68)
at org.drools.core.phreak.RuleNetworkEvaluator.evalNode(RuleNetworkEvaluator.java:395)
at org.drools.core.phreak.RuleNetworkEvaluator.innerEval(RuleNetworkEvaluator.java:341)
at org.drools.core.phreak.RuleNetworkEvaluator.outerEval(RuleNetworkEvaluator.java:177)
at org.drools.core.phreak.RuleNetworkEvaluator.evaluateNetwork(RuleNetworkEvaluator.java:135)
at org.drools.core.phreak.RuleExecutor.reEvaluateNetwork(RuleExecutor.java:213)
at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:88)
at org.drools.core.concurrent.AbstractRuleEvaluator.internalEvaluateAndFire(AbstractRuleEvaluator.java:33)
at org.drools.core.concurrent.SequentialRuleEvaluator.evaluateAndFire(SequentialRuleEvaluator.java:43)
at org.drools.core.common.DefaultAgenda.fireLoop(DefaultAgenda.java:1111)
at org.drools.core.common.DefaultAgenda.internalFireAllRules(DefaultAgenda.java:1058)
at org.drools.core.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1050)
at org.drools.core.impl.StatefulKnowledgeSessionImpl.internalFireAllRules(StatefulKnowledgeSessionImpl.java:1343)
at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1334)
at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1318)
at org.optaplanner.core.impl.score.stream.drools.DroolsConstraintSession.calculateScore(DroolsConstraintSession.java:72)
at org.optaplanner.test.impl.score.stream.DefaultSingleConstraintVerification.given(DefaultSingleConstraintVerification.java:43)
at org.optaplanner.test.impl.score.stream.DefaultSingleConstraintVerification.given(DefaultSingleConstraintVerification.java:28)
at ase.tito.scheduler.LunchBreakTest.valid7hourDayOneLunchBreak(LunchBreakTest.java:90)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
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$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(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$6(TestMethodTestDescriptor.java:212)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)
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$5(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$7(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$8(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)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(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$8(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)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(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$8(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)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.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:248)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

这篇关于带有 toList 的 Optaplanner GroupBy 未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆