<br><font size=2 face="sans-serif">Hi Rules Users,</font>
<br>
<br><font size=2 face="sans-serif">I'm experiencing an issue with a Drools
4.0.7 rule.</font>
<br>
<br><font size=2 face="sans-serif">Here is a clip of the exception that
is thrown:</font>
<br>
<br><font size=2 face="sans-serif">Caused by: org.mvel.CompileException:
cannot invoke method</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.mvel.optimizers.impl.refl.MethodAccessor.getValue(MethodAccessor.java:54)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.mvel.optimizers.impl.refl.VariableAccessor.getValue(VariableAccessor.java:39)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.mvel.ast.VariableDeepPropertyNode.getReducedValueAccelerated(VariableDeepPropertyNode.java:22)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.mvel.ast.PropertyASTNode.getReducedValueAccelerated(PropertyASTNode.java:21)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.mvel.MVELRuntime.execute(MVELRuntime.java:90)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.mvel.CompiledExpression.getValue(CompiledExpression.java:111)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.mvel.MVEL.executeExpression(MVEL.java:235)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.base.mvel.MVELConsequence.evaluate(MVELConsequence.java:48)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:554)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; ...
80 more</font>
<br><font size=2 face="sans-serif">Caused by: java.lang.reflect.InvocationTargetException</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
sun.reflect.GeneratedMethodAccessor412.invoke(Unknown Source)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
java.lang.reflect.Method.invoke(Method.java:585)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.mvel.optimizers.impl.refl.MethodAccessor.getValue(MethodAccessor.java:46)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; ...
88 more</font>
<br><font size=2 face="sans-serif">Caused by: org.drools.RuntimeDroolsException:
java.lang.ClassCastException: com.demo.Rule_My_Rule_0$Accumulate0</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.rule.Accumulate.accumulate(Accumulate.java:131)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.reteoo.AccumulateNode.modifyTuple(AccumulateNode.java:352)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.reteoo.AccumulateNode.assertObject(AccumulateNode.java:248)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.reteoo.CompositeObjectSinkAdapter.propagateAssertObject(CompositeObjectSinkAdapter.java:318)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:162)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.reteoo.Rete.assertObject(Rete.java:175)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.reteoo.ReteooRuleBase.assertObject(ReteooRuleBase.java:192)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.reteoo.ReteooWorkingMemory.doInsert(ReteooWorkingMemory.java:71)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:911)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:883)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.base.DefaultKnowledgeHelper.insert(DefaultKnowledgeHelper.java:67)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.base.DefaultKnowledgeHelper.insert(DefaultKnowledgeHelper.java:61)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; ...
92 more</font>
<br><font size=2 face="sans-serif">Caused by: java.lang.ClassCastException:
com.demo.Rule_My_Rule_0$Accumulate0</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
com.demo.Rule_My_Rule_0Accumulate0Invoker.accumulate(Rule_My_Rule_0Accumulate0Invoker.java:43)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; at
org.drools.rule.Accumulate.accumulate(Accumulate.java:123)</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; ...
103 more</font>
<br>
<br><font size=2 face="sans-serif">The actually rule name and package are
a lot longer, I changed them here for brevity and because I don't believe
the names themselves are relevant.</font>
<br>
<br><font size=2 face="sans-serif">This exception is being thrown intermittently
when this rule, which contains an accumulate function is called. &nbsp;I
say intermittently because it does not happen every time the rule is run.
&nbsp;As far as I can tell the ClassCastExceptions only happens when the
computer that the rules are running on is under load.</font>
<br>
<br><font size=2 face="sans-serif">A few more details before I post the
rule itself. &nbsp;The rule base that I am building consists of 2 drl files.
&nbsp;Both of the files have the same package declaration at the top. &nbsp;The
2 packages are built separately using PackageBuilder then combined using
RuleBase.addPackage() on both packages. &nbsp;Also, I'm not sure if this
is relevant, but this scenario takes place in a web application where the
two package objects are only built once (for performance) but each user
session creates a new RuleBase and adds the packages. &nbsp;The individual
sessions then create stateful sessions from the rule base.</font>
<br>
<br><font size=2 face="sans-serif">This is the rule that I believe is causing
the issue.</font>
<br>
<br><font size=2 color=#a00000 face="Courier New"><b>rule</b></font><font size=2 face="Courier New">
</font><font size=2 color=#008000 face="Courier New">&quot;My Rule&quot;</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; </font><font size=2 color=#a00000 face="Courier New"><b>salience</b></font><font size=2 face="Courier New">
50</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; </font><font size=2 color=#a00000 face="Courier New"><b>dialect</b></font><font size=2 face="Courier New">
</font><font size=2 color=#008000 face="Courier New">&quot;java&quot;</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; </font><font size=2 color=#a00000 face="Courier New"><b>no-loop</b></font><font size=2 face="Courier New">
</font><font size=2 color=#a00000 face="Courier New"><b>true</b></font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; </font><font size=2 color=#a00000 face="Courier New"><b>when</b></font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; $module
: Module( selected == </font><font size=2 color=#a00000 face="Courier New"><b>false</b></font><font size=2 face="Courier New">,
required &gt; 1, $size : required )</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; $list :
ArrayList( )</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; </font><font size=2 color=#a00000 face="Courier New"><b>from</b></font><font size=2 face="Courier New">
</font><font size=2 color=#a00000 face="Courier New"><b>accumulate</b></font><font size=2 face="Courier New">(
PotentialMatch( module == $module, $slot : slot ),</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init( List result = </font><font size=2 color=#a00000 face="Courier New"><b>new</b></font><font size=2 face="Courier New">
ArrayList(); ),</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action( result.add($slot); ),</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reverse( result.remove((Integer)$slot
); ),</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result( result ) )</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; </font><font size=2 color=#a00000 face="Courier New"><b>eval</b></font><font size=2 face="Courier New">(
countThem($list, $size) != $module.setMatchCount() )</font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; </font><font size=2 color=#a00000 face="Courier New"><b>then</b></font>
<br><font size=2 face="Courier New">&nbsp; &nbsp; &nbsp; &nbsp; </font><font size=2 color=#a00000 face="Courier New"><b>modify</b></font><font size=2 face="Courier New">(
$module ) { setMatchCount(countThem($list, $size)) };</font>
<br><font size=2 color=#a00000 face="Courier New"><b>end</b></font>
<br>
<br><font size=2 face="sans-serif">As I'm typing this I realize that there
is another way to write this rule using collect. &nbsp;I'm not sure why
it was written this way, but since I have already come this far, has anyone
had any experience with the Drools 4 generated code throwing ClassCastExceptions?
</font>
<br>
<br><font size=2 face="sans-serif">Thanks,</font>
<br><font size=2 face="sans-serif"><br>
</font><font size=3>Steve Ronderos</font>