OptaPlanner Nurse Rostering accumulate sum Null Pointer Exception

南楼画角 提交于 2019-12-25 09:10:06

问题


I try to modify the OptaPlanner nurse Roster example. So far, I want to write a rule in the drl file which reacts when an employee works more than 13 hours per day. I expanded the class shiftType to get hours per Shift.

At first look, the rule seems to work but when there is no feasible solution after a while I get a NullPointerException.

Here is some relevant code:

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

And part of the Exception

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:


I don't understand why but when i change the rule a little bit it works fine when i broke down the rule. Perhaps it has something to do with planning variables and planning entity,.. but I'm not sure. Here is the code how it looks like now:

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


来源:https://stackoverflow.com/questions/42665968/optaplanner-nurse-rostering-accumulate-sum-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!