Hello,
Recently I've been working with Drools version 5.0.1 to set up an event
processor. While doing so I have come
across difficulties with the accumulate function, in that it will always
throw a ClassCastException
when inserting events that are declared within my drl file. To illustrate
this I've set up an example which is
shown below. I've search Google and the mailing list for several hours, but
the only way past this problem
appears to be that I will have to declare a class externally and make sure
it implements Serializable. Does
anyone has suggestions for another approach? Secondly, I would have assumed
that since Drools was
compiling these internally declared classes, that Serializable would have
been implemented automatically. So
is this a result of the generated class not implementing Serializable, or
have I missed something in my rules
file? Thanks.
The important part of my drl file is:
declare TestEvent
@role( event )
@expires(10s)
id : String
end
#This rule works fine when 'Accumulate Count' is commented out, and is used
to make
#sure that the event inserting has worked.
rule "Is It There"
when
TestEvent() from entry-point "test"
then
System.out.println("Found a TestEvent.");
end
#This rule throws a ClassCastException when a TestEvent is inserted.
rule "Accumulate Count"
when
$n : Number() from accumulate($t : TestEvent() from entry-point
"test", count($t))
then
System.out.println("Found " + $n + " events.");
end
The unit test does this:
FactType testEventType =
session.getKnowledgeBase().getFactType("com.drools.test",
"TestEvent");
for(int i = 0; i < 10; i++){
Object testEvent = testEventType.newInstance();
testEventType.set(testEvent, "id", "id");
WorkingMemoryEntryPoint mainStream =
session.getWorkingMemoryEntryPoint("test");
mainStream.insert(event);
session.fireAllRules();
}
The exception I receive is:
org.drools.RuntimeDroolsException: java.lang.ClassCastException:
com.drools.test.TestEvent cannot be cast to java.io.Serializable
at org.drools.rule.Accumulate.accumulate(Accumulate.java:172)
at org.drools.reteoo.AccumulateNode.modifyTuple(AccumulateNode.java:424)
at
org.drools.reteoo.AccumulateNode.assertObject(AccumulateNode.java:284)
at
org.drools.reteoo.SingleObjectSinkAdapter.propagateAssertObject(SingleObjectSinkAdapter.java:42)
at
org.drools.reteoo.PropagationQueuingNode$AssertAction.execute(PropagationQueuingNode.java:326)
at
org.drools.reteoo.PropagationQueuingNode.propagateActions(PropagationQueuingNode.java:221)
at
org.drools.reteoo.PropagationQueuingNode$PropagateAction.execute(PropagationQueuingNode.java:394)
at
org.drools.common.AbstractWorkingMemory.executeQueuedActions(AbstractWorkingMemory.java:1486)
at org.drools.common.NamedEntryPoint.insert(NamedEntryPoint.java:158)
at org.drools.common.NamedEntryPoint.insert(NamedEntryPoint.java:122)
at org.drools.common.NamedEntryPoint.insert(NamedEntryPoint.java:80)
at org.drools.common.NamedEntryPoint.insert(NamedEntryPoint.java:28)
at com.drools.test.MyUnitTest.testEvents(EvaluationTest.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at
org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:160)
at
org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:233)
at
org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:333)
at
org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at
org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at
org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.ClassCastException: com.drools.test.TestEvent cannot be
cast to java.io.Serializable
at
org.drools.base.accumulators.JavaAccumulatorFunctionExecutor.accumulate(JavaAccumulatorFunctionExecutor.java:110)
at org.drools.rule.Accumulate.accumulate(Accumulate.java:164)
... 36 more