[rules-users] CEP accumulate unclear behavior

ters ters at ukr.net
Mon Dec 9 13:19:59 EST 2013


Wolfgang, hi again. I've tried your suggestion of the computation separation.
But I tried to use global variable instead of additional fact-accumulator:

/	@Test
	public void test1_FireAllRules() throws RuleGeneratorException {
		ksession.setGlobal("globalAccum", new ArrayList(Arrays.asList(0)));
		ksession.insert(new ServicePerformanceEvent("MyService", 100));
		ksession.fireAllRules();
	}/
....................................................................
/global java.util.ArrayList *globalAccum*;
rule "HELPER rule to calculate value and put into global variable"
dialect "mvel"
*salience 10*
	when
		Number( $avg: doubleValue )
		from accumulate( $e: ServicePerformanceEvent( $name: serviceName,
$thisDuration: duration),
                     init( double sum = 0; double count = 0;)
                     action( if("MyService".equals($name)){sum +=
$thisDuration; count++;})
                     result( count != 0 ? sum/count : 0 ) );
	then
		globalAccum.set(0, *$avg + 100*);
	        System.out.println("--------------------- HELPER RULE salience
10--------------------------------" );
	    System.out.println("RHS: HELPER RULE: globalAccum = " +
globalAccum.get(0));
end

rule "MAIN rule"
    dialect "mvel"
	when
               $event : ServicePerformanceEvent(*duration >
globalAccum.get(0)*);
	then
		System.out.println("---------------------- MAIN
RULE--------------------------------" );
                System.out.println("RHS: MAIN RULE: $event.duration = " +
$event.duration);
                System.out.println("RHS: MAIN RULE: globalAccum = " +
globalAccum.get(0));
end/

The output of invocation was unexpected for me:
--------------------- HELPER RULE salience
10--------------------------------
RHS: HELPER RULE: globalAccum = *200.0*
---------------------- MAIN RULE--------------------------------
RHS: MAIN RULE: $event.duration = *100*
RHS: MAIN RULE: globalAccum = *200.0*

I've found that the order of invocation is:
1 - HELPER rule LHS (calculating avg)
2 - MAIN rule LHS (comparison duration(100) > globalAccum(0))!
3 - HELPER rule RHS (assigning globalAccum with value 200)!
4 - MAIN rule RHS 
It was surprise for me, I've expected invocation of Main rule LHS after
Helper rule RHS.
So, as I understand, the only way to achieve desired behavior is insert
helper Fact (insert(new Average($avg))) 
in the HELPER rule RHS and then matching this fact into MAIN rule LHS, use
avg from it and further retract it.



--
View this message in context: http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027173.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list