OptaPlanner Nurse Rostering累积和Null Pointer Exception

时间:2017-03-08 08:02:50

标签: java nullpointerexception drools optaplanner drools-planner

我尝试修改OptaPlanner护士名册示例。 到目前为止,我想在drl文件中编写一条规则,当员工每天工作超过13小时时,该规则会作出反应。 我将类shiftType扩展为每个Shift的小时数。

初看起来,规则似乎有效,但是在一段时间后没有可行的解决方案时,我得到一个NullPointerException。

以下是一些相关代码:

rule "dailyRestPeriod"
when


ShiftAssignment($employee : employee, $shiftType : shiftType, $shiftDate : shiftDate, $employee!=null)
$dailyTotalHours : Number( intValue > 13) from accumulate(
                 $assignmentEmployee:  ShiftAssignment(employee == $employee, shiftDate == $shiftDate),
            sum($assignmentEmployee.getShiftType().getShiftDurationHours())
        )
then

System.out.println("Employee " +$employee +" dailyTotal " +$dailyTotalHours);
 scoreHolder.addHardConstraintMatch(kcontext, - 1);
end

部分异常

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Solving failed.
    at org.optaplanner.examples.common.swingui.SolverAndPersistenceFrame$SolveWorker.done(SolverAndPersistenceFrame.java:319)
    at javax.swing.SwingWorker$5.run(Unknown Source)
    at javax.swing.SwingWorker$DoSubmitAccumulativeRunnable.run(Unknown Source)
    at sun.swing.AccumulativeRunnable.run(Unknown Source)
    at javax.swing.SwingWorker$DoSubmitAccumulativeRunnable.actionPerformed(Unknown Source)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at org.drools.core.rule.SingleAccumulate.reverse(SingleAccumulate.java:124)
    at org.drools.core.phreak.PhreakAccumulateNode.removeMatch(PhreakAccumulateNode.java:806)
    at org.drools.core.phreak.PhreakAccumulateNode.removePreviousMatchesForRightTuple(PhreakAccumulateNode.java:865)
    at org.drools.core.phreak.PhreakAccumulateNode.doRightUpdates(PhreakAccumulateNode.java:444)
    at org.drools.core.phreak.PhreakAccumulateNode.doNode(PhreakAccumulateNode.java:87)
    at org.drools.core.phreak.RuleNetworkEvaluator.switchOnDoBetaNode(RuleNetworkEvaluator.java:534)
    at org.drools.core.phreak.RuleNetworkEvaluator.evalBetaNode(RuleNetworkEvaluator.java:505)
    at org.drools.core.phreak.RuleNetworkEvaluator.evalNode(RuleNetworkEvaluator.java:341)
    at org.drools.core.phreak.RuleNetworkEvaluator.innerEval(RuleNetworkEvaluator.java:301)
    at org.drools.core.phreak.RuleNetworkEvaluator.outerEval(RuleNetworkEvaluator.java:136)
    at org.drools.core.phreak.RuleNetworkEvaluator.evaluateNetwork(RuleNetworkEvaluator.java:94)
    at org.drools.core.phreak.RuleExecutor.reEvaluateNetwork(RuleExecutor.java:194)
    at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:73)
    at org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:970)
    at org.drools.core.common.DefaultAgenda.fireLoop(DefaultAgenda.java:1312)
    at org.drools.core.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1251)

1 个答案:

答案 0 :(得分:0)

我不明白为什么,但当我改变规则时,当我打破规则时,它可以正常工作。 也许它与规划变量和规划实体有关,但我不确定。 以下是现在的代码:

rule "dailyRestPeriod"
when
$employee : Employee()
$shift : Shift( $shiftType : shiftType ,  $shiftDate :shiftDate )
$dailyTotalHours : Number( intValue >= 13) from accumulate(
                 $assignmentEmployee:  ShiftAssignment(employee == $employee, shiftDate == $shiftDate),
            sum($assignmentEmployee.getShiftType().getShiftDurationHours())
        )
then
scoreHolder.addHardConstraintMatch(kcontext, - 1);
end