Accumulate Function ClassCastException

53 views
Skip to first unread message

Eugene Shvartsman

unread,
Jul 2, 2015, 12:36:51 PM7/2/15
to drools...@googlegroups.com
I am getting a ClassCastException with the following LHS side of the rule. The sample I am posting is the simplest version of the rule that I could create while still retaining the error. 

when

$employee : Employee () 

$contractLine : MinMaxContractLine( contractLineType == ContractLineType.HOURS_PER_DAY, $maxMinutesValue : maximumValue, $weight : maximumWeight, 

maximumEnabled == true)

$info : EmployeeRosterInfo( $type : profileDayType, $firstDayOfWeek : firstDayOfWeek )

$shiftDate : ShiftDate(isInPlanningWindow($info))

accumulate ( $shift : ShiftAssignment( employee != null, employee == $employee)

           ,$minutesInDay  : sum($shift.getShiftDurationMinutesByProfileType($type, $shiftDate));

            $minutesInDay > 0

)

then


During the execution of the rules I get the following stack trace:


java.lang.RuntimeException: java.lang.ClassCastException: com.x.engine.domain.ShiftDate cannot be cast to com.x.engine.domain.dto.ProfileDayType

at org.drools.core.rule.SingleAccumulate.accumulate(SingleAccumulate.java:90)

at org.drools.core.phreak.PhreakAccumulateNode.addMatch(PhreakAccumulateNode.java:759)

at org.drools.core.phreak.PhreakAccumulateNode.doLeftInserts(PhreakAccumulateNode.java:163)

at org.drools.core.phreak.PhreakAccumulateNode.doNode(PhreakAccumulateNode.java:80)

at org.drools.core.phreak.RuleNetworkEvaluator.switchOnDoBetaNode(RuleNetworkEvaluator.java:569)

at org.drools.core.phreak.RuleNetworkEvaluator.evalBetaNode(RuleNetworkEvaluator.java:540)

at org.drools.core.phreak.RuleNetworkEvaluator.innerEval(RuleNetworkEvaluator.java:336)

at org.drools.core.phreak.RuleNetworkEvaluator.outerEval(RuleNetworkEvaluator.java:163)

at org.drools.core.phreak.RuleNetworkEvaluator.evaluateNetwork(RuleNetworkEvaluator.java:118)

at org.drools.core.phreak.RuleExecutor.reEvaluateNetwork(RuleExecutor.java:194)

at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:62)

at org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:974)

at org.drools.core.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1283)

at org.drools.core.impl.StatefulKnowledgeSessionImpl.internalFireAllRules(StatefulKnowledgeSessionImpl.java:1325)

at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1308)

at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1287)

at com.x.engine.drools.hardconstraints.MaxHoursPerDayTester.testMaxHoursTwoShiftsViolatedStartDateType(MaxHoursPerDayTester.java:244)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: java.lang.ClassCastException: com.x.engine.domain.ShiftDate cannot be cast to com.x.engine.domain.dto.ProfileDayType

at com.x.engine.solver.rules.constraints.Rule_maxHoursPerDay1799802206AccumulateExpression0Invoker.evaluate(Rule_maxHoursPerDay1799802206AccumulateExpression0Invoker.java:17)

at org.drools.core.base.accumulators.JavaAccumulatorFunctionExecutor.accumulate(JavaAccumulatorFunctionExecutor.java:109)

at org.drools.core.rule.SingleAccumulate.accumulate(SingleAccumulate.java:82)

... 41 more


If I replace $shift.getShiftDurationMinutesByProfileType($type, $shiftDate) with $shift.getShiftDurationMinutesByProfileType($type, $shift.getShiftDate()) the issue goes away for some reason. Sadly this does not work as a workaround in my real case. 


Any ideas before I raise a JIRA issue?  


Pavan Kumar J S

unread,
Jun 27, 2016, 7:11:26 AM6/27/16
to Drools Usage
I'm able to reproduce the same issue in version "6.3.0.Final".

The workaround that you have suggested works in my case.

Thanks

murali mohan

unread,
Jun 28, 2016, 11:02:07 AM6/28/16
to Drools Usage
accumulate will work only on the elements of the fact it is collecting/matching. In your example, $shift : ShiftAssignment( ), is the fact that it is working on and it can only apply accumulate functions on the fields of ShiftAssignment. This explains why the workaround works.
Reply all
Reply to author
Forward
0 new messages