Okey I got a little further: This works:  

rule "count old"
when
$le : LastEvent($ts : event.timestamp)
$old : Event(this before [1ms , 5s] $ts)
then
System.out.println($old);
end

but then why does this not work ?!

rule "count old"
when
$le : LastEvent($event : event)
$old : Event(this before [1ms , 5s] $event.timestamp)
then
System.out.println($old);
end

To me this seems to be a bug, that occurs as soon as I set an event as member of a fact (the LastEvent) -> implementing the peephole technique. 
At least I got it to do what I want...but not as "niceley..." as expected.

 

On 02 Nov 2013, at 18:11, Alexander Wolf <mail@alexander-wolf.net> wrote:

Drools 5.5.0 + Fusion / stateful session / STREAM mode

Hi,

I try to improve my rules using peephole technique (Thanks @W)
I think I got it right, but now I am stuck with a problem.

I want to keep a reference to my latest inserted event. Problem:  the rule "print old" gives me an Exception (see below). 
I think the peephole itself works pretty well, but somehow my Event Object (POJO with: Date timestamp, int value) is making problems when I try to use "before".  (see marked line, when commenting it out, the rules run alright)

dialect "mvel"

import java.util.Date
import com.testmodel.Event


//declarations
declare Event
@role(event)
@timestamp( timestamp )
end

declare LastEvent
event: Event
end


rule "init"
when
//once...
then
System.out.println("init");
LastEvent l = new LastEvent();
Event e = new Event();
e.timestamp = new Date();
e.value = 0;
l.event = e;
insert(l);
end

rule "update LastEvent"
no-loop
salience 99
when
$event : Event()
$last : LastEvent()
then
System.out.println("update LastEvent");
retract ($last)
LastEvent l = new LastEvent();
l.event = $event;
insert(l);
end


rule "count old"
when
LastEvent($last : event)
$old : Event(this before [0 , 5s] $last)   //<<<<<<<<<<  THIS IS EVIL?!
then
System.out.println($old);
end

My test code looks like this:

Event event = new Event(new Date(), 1);
ksession.insert(event);
ksession.fireAllRules();

//sleep  1+ sec
Thread.sleep(1001);

Event event2 = new Event(new Date(), 2);
ksession.insert(event2);
ksession.fireAllRules();


And this is the Exception I get:

Exception executing consequence for rule "init" in testEasierLife: [Error: drools.insert(l): com.testmodel.Event cannot be cast to java.lang.Number]
[Near : {... System.out.println("init"); ....}]
             ^
[Line: 1, Column: 1]
at org.drools.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)
at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1297)
at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:1221)
at org.drools.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1456)
at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:710)
at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:674)
at org.drools.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:230)
at com.Test.test(PeepholeTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
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.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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: [Error: drools.insert(l): com.testmodel.Event cannot be cast to java.lang.Number]
[Near : {... System.out.println("init"); ....}]
             ^
[Line: 1, Column: 1]
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:416)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:143)
at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85)
at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
at org.mvel2.MVEL.executeExpression(MVEL.java:930)
at org.drools.base.mvel.MVELConsequence.evaluate(MVELConsequence.java:104)
at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1287)
... 29 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:1104)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:987)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:377)
... 39 more
Caused by: java.lang.ClassCastException: com.testmodel.Event cannot be cast to java.lang.Number
at org.drools.base.evaluators.BeforeEvaluatorDefinition$BeforeEvaluator.evaluateCachedLeft(BeforeEvaluatorDefinition.java:337)
at org.drools.rule.constraint.EvaluatorConstraint.isAllowedCachedLeft(EvaluatorConstraint.java:67)
at org.drools.common.SingleBetaConstraints.isAllowedCachedLeft(SingleBetaConstraints.java:125)
at org.drools.reteoo.JoinNode.propagateFromLeft(JoinNode.java:105)
at org.drools.reteoo.JoinNode.assertLeftTuple(JoinNode.java:95)
at org.drools.reteoo.SingleLeftTupleSinkAdapter.doPropagateAssertLeftTuple(SingleLeftTupleSinkAdapter.java:196)
at org.drools.reteoo.SingleLeftTupleSinkAdapter.createAndPropagateAssertLeftTuple(SingleLeftTupleSinkAdapter.java:145)
at org.drools.reteoo.LeftInputAdapterNode.assertObject(LeftInputAdapterNode.java:154)
at org.drools.reteoo.CompositeObjectSinkAdapter.doPropagateAssertObject(CompositeObjectSinkAdapter.java:497)
at org.drools.reteoo.CompositeObjectSinkAdapter.propagateAssertObject(CompositeObjectSinkAdapter.java:382)
at org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:235)
at org.drools.reteoo.EntryPointNode.assertObject(EntryPointNode.java:240)
at org.drools.common.NamedEntryPoint.insert(NamedEntryPoint.java:350)
at org.drools.common.NamedEntryPoint.insert(NamedEntryPoint.java:311)
at org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:903)
at org.drools.base.DefaultKnowledgeHelper.insert(DefaultKnowledgeHelper.java:192)
at org.drools.base.DefaultKnowledgeHelper.insert(DefaultKnowledgeHelper.java:186)
... 46 more