Re: [rules-users] CEP accumulate unclear behavior

2013-12-09 Thread Wolfgang Laun
On 09/12/2013, ters  wrote:
> Wolfgang, hi again. I've tried your suggestion of the computation
> separation.
> But I tried to use global variable instead of additional fact-accumulator:

The Rule Engine is very narrow-minded when it comes to evaluating
globals on the LHS of a rule. Changes aren't "seen" by the engine.
(Read the doc:  "a global should never be used to establish conditions
in rules except when it has a constant immutable value.")

-W
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] CEP accumulate unclear behavior

2013-12-09 Thread ters
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.
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] CEP accumulate unclear behavior

2013-12-05 Thread ters
Agree with you, I also have think about such approach. I will try it later
and inform here about the results.
laune, thank you again for your quick and adequate responses, I'm really
appreciate.



--
View this message in context: 
http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027109.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] CEP accumulate unclear behavior

2013-12-05 Thread Wolfgang Laun
Try the following separation of the computation of the average from
the detection of critical events.

The first rule with a salience > 0 simply computes the average of all
Events and update a singular Fact (Average) containing a field with
the running average value.

A second rule matches on Average and the kind of Event you're
interested and tests the duration, e.g.

   Average( $v: value )
   Event( name == "MyService", duration > $v )

-W



On 05/12/2013, ters  wrote:
> laune,
> I've tried your proposed accumulate approach, the Result of
> test1_FireAllRules() is:
> -= 1 =-
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> $events avg duration = 2.0
> --FIRED--
> -= 3 =-
> $events avg duration = 7.0
> --FIRED--
> ---test1 end---
> Ok, it fires for each new inserted event (between fireAllRules calls) and
> calculates avg for all events witn name "MyService".
> But, as I wrote in first post, I need to fire a rule if last inserting
> event
> duration
> greater than value which depends on average duration of all events from the
> wm. Lets take $avg/10(/10 - take for convenience to show issue).
> So I need to add pattern along with accumulate:
> /Number( $avg: doubleValue ) from accumulate( 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 ) );
>   
>   $event : ServicePerformanceEvent(serviceName == "MyService", duration >
> */$avg/10/*);/
> Result of test1_FireAllRules():
> -= 1 =-
> $event.duration = 2
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> $event.duration = 2
> $events avg duration = 2.0
> --FIRED--
> -= 3 =-
> $event.duration = 12
> $events avg duration = 7.0
> --FIRED--
> $event.duration = 2
> $events avg duration = 7.0
> --FIRED--
> ---test1 end---
>
> As can see, it fires on both for newly inserted event and for old already
> matched events/facts.
> Ok, if rule analyzes all previous events/facts I decide to check previous
> rule with test:
> / @Test
>   public void test12_FireAllRules() throws RuleGeneratorException {
>   System.out.println("-= 1 =-");
>   ksession.insert(new ServicePerformanceEvent("MyService", 2));
>   ksession.fireAllRules();
>
>   System.out.println("-= 2 =-");
>   ksession.insert(new ServicePerformanceEvent("AnotherService", 
> 10));
>   ksession.fireAllRules();
>
>   System.out.println("-= 3 =-");
>   ksession.insert(new ServicePerformanceEvent("MyService", 12));
>   ksession.fireAllRules();
>
> */System.out.println("-= 4 =-");
>   ksession.fireAllRules(); // call firing without inserting event 
> after
> last
> fire/*
>   System.out.println("---test1 end---");
>   }/
> Result of test12_FireAllRules():
> -= 1 =-
> $event.duration = 2
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> $event.duration = 2
> $events avg duration = 2.0
> --FIRED--
> -= 3 =-
> $event.duration = 12
> $events avg duration = 7.0
> --FIRED--
> $event.duration = 2
> $events avg duration = 7.0
> --FIRED--
> /-= 4 =-
> ---test1 end---/
>
> And what I see, in case -3- the rule analyzed both new and old facts...
> but in case -4- the rule analyzed nothing! though, as in case -3-, there
> were potentially matched old facts.
> I completely don't understand such behavior...
> Again, what I need for last test is:
> -= 1 =-
> $event.duration = 2
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> -= 3 =-
> /$event.duration = 12
> $events avg duration = 7.0/
> --FIRED--
> -= 4 =-
> ---test1 end---
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027107.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> ___
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] CEP accumulate unclear behavior

2013-12-05 Thread ters
laune,
I've tried your proposed accumulate approach, the Result of
test1_FireAllRules() is:
-= 1 =-
$events avg duration = 2.0
--FIRED--
-= 2 =-
$events avg duration = 2.0
--FIRED--
-= 3 =-
$events avg duration = 7.0
--FIRED--
---test1 end---
Ok, it fires for each new inserted event (between fireAllRules calls) and
calculates avg for all events witn name "MyService".
But, as I wrote in first post, I need to fire a rule if last inserting event
duration 
greater than value which depends on average duration of all events from the
wm. Lets take $avg/10(/10 - take for convenience to show issue).
So I need to add pattern along with accumulate:
/Number( $avg: doubleValue ) from accumulate( 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 ) );

$event : ServicePerformanceEvent(serviceName == "MyService", duration >
*/$avg/10/*);/
Result of test1_FireAllRules():
-= 1 =-
$event.duration = 2
$events avg duration = 2.0
--FIRED--
-= 2 =-
$event.duration = 2
$events avg duration = 2.0
--FIRED--
-= 3 =-
$event.duration = 12
$events avg duration = 7.0
--FIRED--
$event.duration = 2
$events avg duration = 7.0
--FIRED--
---test1 end---

As can see, it fires on both for newly inserted event and for old already
matched events/facts.
Ok, if rule analyzes all previous events/facts I decide to check previous
rule with test:
/   @Test
public void test12_FireAllRules() throws RuleGeneratorException {
System.out.println("-= 1 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 2));
ksession.fireAllRules();

System.out.println("-= 2 =-");
ksession.insert(new ServicePerformanceEvent("AnotherService", 
10));
ksession.fireAllRules();

System.out.println("-= 3 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 12));
ksession.fireAllRules();

*/  System.out.println("-= 4 =-");
ksession.fireAllRules(); // call firing without inserting event 
after last
fire/*
System.out.println("---test1 end---");
}/
Result of test12_FireAllRules():
-= 1 =-
$event.duration = 2
$events avg duration = 2.0
--FIRED--
-= 2 =-
$event.duration = 2
$events avg duration = 2.0
--FIRED--
-= 3 =-
$event.duration = 12
$events avg duration = 7.0
--FIRED--
$event.duration = 2
$events avg duration = 7.0
--FIRED--
/-= 4 =-
---test1 end---/

And what I see, in case -3- the rule analyzed both new and old facts... 
but in case -4- the rule analyzed nothing! though, as in case -3-, there
were potentially matched old facts.
I completely don't understand such behavior...
Again, what I need for last test is:
-= 1 =-
$event.duration = 2
$events avg duration = 2.0
--FIRED--
-= 2 =-
-= 3 =-
/$event.duration = 12
$events avg duration = 7.0/
--FIRED--
-= 4 =-
---test1 end---



--
View this message in context: 
http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027107.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] CEP accumulate unclear behavior

2013-12-04 Thread Wolfgang Laun
On 04/12/2013, ters  wrote:
> laune, thanks for explanations.
>
> Could you please provide me with links/examples of how to /write your own
> accumulate function (using the
> init/action/result paradigm) /, and how to use this custom function instead
> of standard one?



rule manacc
when
Number( $avg: intValue )
from accumulate( Event( $source: source, $value: value),
 init( int sum = 0; int count = 0; )
 action( if( "server1".equals( $source ) ){
 sum += $value; count++;
 } )
 result( count != 0 ? sum/count : 0 ) )
then
 System.out.println("$events avg value = " + $avg );
end

You could add a
   reverse(   if( "server1".equals( $source ) ){
 sum -= $value; count--;
  })

Cheers
Wolfgang


>
> Regards!
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027090.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> ___
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] CEP accumulate unclear behavior

2013-12-04 Thread ters
laune, thanks for explanations.

Could you please provide me with links/examples of how to /write your own
accumulate function (using the
init/action/result paradigm) /, and how to use this custom function instead
of standard one?

Regards!



--
View this message in context: 
http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027090.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] CEP accumulate unclear behavior

2013-12-04 Thread Wolfgang Laun
Hi ters,

I've run a few tests and I think that you'll have to use an entirely
different approach to achieve what you want.

accumulate creates a "virtual object" maintaining the accumulated
value(s). This is updated with every change to its domain, i.e., the
set of matching facts. Thus, for every inserted "MyService" the rule
will fire. (If you add some other pattern, then all matching products
result in an activation. So my advice for seeing the average for every
insert was wrong.)

You could write your own accumulate function (using the
init/action/result paradigm) where you accumulate over all
ServicePerformanceEvent events but count and sum only the "MyService"
events.

Alternatively, use a fact of a class that contains a count and a sum
and use this to collect the average:


public class Accu {
private String trigger;
private int sum;
private int count;
public Accu( String t ){
trigger = t;
}
public String getTrigger(){ return trigger; }
public int getAverage(){ return sum/count; }
public void accumulate( int value ){
sum += value;
count++;
}
}

rule a when
 $event : Event( $source: source, $value: value)
 $accu: Accu( trigger == $source )
then
 $accu.accumulate( $value );
 System.out.println("$events avg value = " + $accu.getAverage() );
end

rule b when
 $event : Event( $source: source, $value: value)
 $accu: Accu( trigger != $source )
then
 System.out.println("$events avg value = " + $accu.getAverage() );
end

Cheers
-W

On 04/12/2013, ters  wrote:
> laune, thanks for fast reply! So from point to point:
>
> *-- "Are you using 5.4.0?"*
> Yes, I'm using 5.4.0.Final.
>
> *-- "But you can easily achieve what you want by adding the pattern
> ServicePerformanceEvent() in front of the accumulate CE."*
> I've tested this, LHS is:
> /ServicePerformanceEvent()
> accumulate(ServicePerformanceEvent(serviceName == "MyService",
> $thisDuration
> : duration);
>  $avg : average($thisDuration));/
>
> The result is:
> -= 1 =-
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> $events avg duration = 2.0
> --FIRED--
> -= 3 =-
> $events avg duration = 7.0
> --FIRED--
> $events avg duration = 7.0
> --FIRED--
> $events avg duration = 7.0
> --FIRED--
> ---test end---
>
> Now (e.g. in case -3-) rule fires on all already inserted/processed events!
> but I expect (in case -3-) firing only for last inserting event which is
> not
> processed before, that is:
> -= 1 =-
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> $events avg duration = 2.0
> --FIRED--
> /-= 3 =-
> $events avg duration = 7.0
> --FIRED--/
> ---test end---
>
> *--"Can you try with a later version?"*
> I've just tried with 6.0.0.Final version. Now result of Third rule becomes
> the same for both fireAllRules and fireUntilHalt tests (good point):
> /Third rule:
>   when
>   $event : ServicePerformanceEvent(serviceName == "MyService");
> accumulate(ServicePerformanceEvent(serviceName == "MyService",
> $thisDuration : duration);  $avg : average($thisDuration));
>   then
> System.out.println("$event.duration = " + $event.duration);
> System.out.println("$events avg duration = " + $avg);
> System.out.println("--FIRED--");
> end   /
>
> Result:
> -= 1 =-
> $event.duration = 2
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> -= 3 =-
> $event.duration = 2
> $events avg duration = 7.0
> --FIRED--
> $event.duration = 12
> $events avg duration = 7.0
> --FIRED--
> ---test end---
>
> As can see, in case -3-, rule fires 2 times - on already inserted and
> processed as fact event with duration=2 and
> on the event with duration=12 which inserted just now and was not processed
> as fact before.
>
> Actually what I want in this case - the rule should fire only on events
> restricted with serviceName constraint, /*which are not
> processed as fact/not-fact before (inserted between fireAllRules()
> calls)*/,
> but average should be calculated for all existed in wm events considering
> serviceName constraint.
> I.e. my expectation for Third rule result is:
> / -= 1 =-
> $event.duration = 2
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> *-= 3 =-
> $event.duration = 12
> $events avg duration = 7.0*
> --FIRED--
> ---test end---/
>
> How I can achieve this behavior???
>
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027088.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> ___
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>

Re: [rules-users] CEP accumulate unclear behavior

2013-12-04 Thread ters
laune, thanks for fast reply! So from point to point:

*-- "Are you using 5.4.0?"*
Yes, I'm using 5.4.0.Final.

*-- "But you can easily achieve what you want by adding the pattern
ServicePerformanceEvent() in front of the accumulate CE."*
I've tested this, LHS is:
/ServicePerformanceEvent()
accumulate(ServicePerformanceEvent(serviceName == "MyService", $thisDuration
: duration);
   $avg : average($thisDuration));/

The result is:
-= 1 =-
$events avg duration = 2.0
--FIRED--
-= 2 =-
$events avg duration = 2.0
--FIRED--
-= 3 =-
$events avg duration = 7.0
--FIRED--
$events avg duration = 7.0
--FIRED--
$events avg duration = 7.0
--FIRED--
---test end---

Now (e.g. in case -3-) rule fires on all already inserted/processed events!
but I expect (in case -3-) firing only for last inserting event which is not
processed before, that is:
-= 1 =-
$events avg duration = 2.0
--FIRED--
-= 2 =-
$events avg duration = 2.0
--FIRED--
/-= 3 =-
$events avg duration = 7.0
--FIRED--/
---test end---

*--"Can you try with a later version?"*
I've just tried with 6.0.0.Final version. Now result of Third rule becomes
the same for both fireAllRules and fireUntilHalt tests (good point):
/Third rule:
when
$event : ServicePerformanceEvent(serviceName == "MyService");
accumulate(ServicePerformanceEvent(serviceName == "MyService",
$thisDuration : duration);  $avg : average($thisDuration));
then
System.out.println("$event.duration = " + $event.duration);
System.out.println("$events avg duration = " + $avg);
System.out.println("--FIRED--");
end /

Result:
-= 1 =-
$event.duration = 2
$events avg duration = 2.0
--FIRED--
-= 2 =-
-= 3 =-
$event.duration = 2
$events avg duration = 7.0
--FIRED--
$event.duration = 12
$events avg duration = 7.0
--FIRED--
---test end---

As can see, in case -3-, rule fires 2 times - on already inserted and
processed as fact event with duration=2 and 
on the event with duration=12 which inserted just now and was not processed
as fact before. 

Actually what I want in this case - the rule should fire only on events
restricted with serviceName constraint, /*which are not 
processed as fact/not-fact before (inserted between fireAllRules() calls)*/,
but average should be calculated for all existed in wm events considering
serviceName constraint.
I.e. my expectation for Third rule result is:
/ -= 1 =-
$event.duration = 2
$events avg duration = 2.0
--FIRED--
-= 2 =-
*-= 3 =-
$event.duration = 12
$events avg duration = 7.0*
--FIRED--
---test end---/

How I can achieve this behavior???




--
View this message in context: 
http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027088.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] CEP accumulate unclear behavior

2013-12-03 Thread Wolfgang Laun
As to the second test, what you observe is correct. All hell would
break loose in most KBs if rules *not* referring to a certain fact would
fire when such a fact is inserted! But you can easily achieve what
you want by adding the pattern
   ServicePerformanceEvent()
in front of the accumulate CE.

The result of the third rule and test looks like a bug to me. There's
no version 5.0.4. Are you using 5.4.0? Can you try with a later
version?

-W


On 03/12/2013, ters  wrote:
> Hi all. Respected experts of drools, please help me to understand how
> pattern
> + accumulate behave.
> I believe that an example is the best way to explain the problem, maybe it
> will look bulky, but suppose be enough.
> I use CEP and EventProcessingOption.Stream.
>
> Event class:
> /public class ServicePerformanceEvent {
>   private String serviceName;
>   private Integer duration;
>   public ServicePerformanceEvent(String name, Integer duration) {
>   serviceName = name;
>   this.duration = duration;
>   }
> }/
>
> There are 2 tests, 1st - with fireAllRules invocation, 2nd - with
> firwUntilHalt invocation.
> / @Test
>   public void *test1_FireAllRules() *throws RuleGeneratorException {
>   System.out.println("-= 1 =-");
>   ksession.insert(new ServicePerformanceEvent("MyService", 2));
>   ksession.fireAllRules();
>
>   System.out.println("-= 2 =-");
>   ksession.insert(new ServicePerformanceEvent("AnotherService", 
> 10));
>   ksession.fireAllRules();
>
>   System.out.println("-= 3 =-");
>   ksession.insert(new ServicePerformanceEvent("MyService", 12));
>   ksession.fireAllRules();
>   System.out.println("---test1 end---");
>   }
>   @Test
>   public void *test2_FireUntilHalt()* throws RuleGeneratorException,
> InterruptedException {
>   System.out.println("-= 1 =-");
>   ksession.insert(new ServicePerformanceEvent("MyService", 2));
>
>   runFireUntilHaltThread();
>   Thread.sleep(1000);
>
>   System.out.println("-= 2 =-");
>   ksession.insert(new ServicePerformanceEvent("AnotherService", 
> 10));
>   Thread.sleep(1000);
>
>   System.out.println("-= 3 =-");
>   ksession.insert(new ServicePerformanceEvent("MyService", 12));
>   Thread.sleep(1000);
>   System.out.println("---test2 end---");
>   }/
>
>
> There are 3 simple rules for which I performing tests:
>
> *First Rule:*
> declare com.test.event.ServicePerformanceEvent
> @role( event )
> end
> rule "ServicePerformanceEvent test rule1"
> dialect "mvel"
>   when
>   $event : ServicePerformanceEvent(serviceName == "MyService");
>   then
> System.out.println("$event.duration = " + $event.duration);
> System.out.println("--FIRED--");
> end
>
> Invocation results of the test1_FireAllRules() and test2_FireUntilHalt()
> the
> same:
> -= 1 =-
> $event.duration = 2
> --FIRED--
> -= 2 =-
> -= 3 =-
> $event.duration = 12
> --FIRED--
> ---test end---
>
> Actual: As expected rule fires for each new inserted event with desired
> serviceName "MyService" (- behavior is clear).
>
> *Second rule:*
> rule "ServicePerformanceEvent test rule2"
> dialect "mvel"
>   when
> accumulate(ServicePerformanceEvent(serviceName == "MyService",
> $thisDuration : duration);  $avg : average($thisDuration));
>   then
> System.out.println("$events avg duration = " + $avg);
> System.out.println("--FIRED--");
> end
>
> Invocation results of the test1_FireAllRules() and the
> test2_FireUntilHalt()
> the same again:
> -= 1 =-
> $events avg duration = 2.0
> --FIRED--
> -= 2 =-
> -= 3 =-
> $events avg duration = 7.0
> --FIRED--
> ---test end---
>
> Actual: Now rule accumulates average only for events from working memory
> with desired serviceName (-this is clear) but fires only for inserted
> events
> with expected serviceName "MyService" (- not clear)
> *My expectation*: rule must fire for each currently inserted event and
> average must be calculated for all events from working memory with
> serviceName=="MyService".
>
> *Third rule:*
> /rule "ServicePerformanceEvent test rule3"
> dialect "mvel"
>   when
>   $event : ServicePerformanceEvent(serviceName == "MyService");
> accumulate(ServicePerformanceEvent(serviceName ==
> "MyService", $thisDuration : duration);
>  $avg : average($thisDuration));
>   then
> System.out.println("$event.duration = " + $event.duration);
> System.out.println("$events avg duration = " + $avg);
> System.out.println("--FIRED--");
> end/
>
> Invocation results of the test1_FireAllR

Re: [rules-users] CEP accumulate unclear behavior

2013-12-03 Thread ters
Forgot to mention, I use drools version 5.0.4 Final.



--
View this message in context: 
http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp4027069p4027072.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


[rules-users] CEP accumulate unclear behavior

2013-12-03 Thread ters
Hi all. Respected experts of drools, please help me to understand how pattern
+ accumulate behave.
I believe that an example is the best way to explain the problem, maybe it
will look bulky, but suppose be enough.
I use CEP and EventProcessingOption.Stream.

Event class:
/public class ServicePerformanceEvent {
private String serviceName;
private Integer duration;
public ServicePerformanceEvent(String name, Integer duration) {
serviceName = name;
this.duration = duration;
}
}/

There are 2 tests, 1st - with fireAllRules invocation, 2nd - with
firwUntilHalt invocation.
/   @Test
public void *test1_FireAllRules() *throws RuleGeneratorException {
System.out.println("-= 1 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 2));
ksession.fireAllRules();

System.out.println("-= 2 =-");
ksession.insert(new ServicePerformanceEvent("AnotherService", 
10));
ksession.fireAllRules();

System.out.println("-= 3 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 12));
ksession.fireAllRules();
System.out.println("---test1 end---");
}
@Test
public void *test2_FireUntilHalt()* throws RuleGeneratorException,
InterruptedException {
System.out.println("-= 1 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 2));

runFireUntilHaltThread();
Thread.sleep(1000);

System.out.println("-= 2 =-");
ksession.insert(new ServicePerformanceEvent("AnotherService", 
10));
Thread.sleep(1000);

System.out.println("-= 3 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 12));
Thread.sleep(1000);
System.out.println("---test2 end---");
}/


There are 3 simple rules for which I performing tests:

*First Rule:*
declare com.test.event.ServicePerformanceEvent
@role( event )
end
rule "ServicePerformanceEvent test rule1"
dialect "mvel"
when
$event : ServicePerformanceEvent(serviceName == "MyService"); 
then
System.out.println("$event.duration = " + $event.duration);
System.out.println("--FIRED--");
end

Invocation results of the test1_FireAllRules() and test2_FireUntilHalt() the
same:
-= 1 =-
$event.duration = 2
--FIRED--
-= 2 =-
-= 3 =-
$event.duration = 12
--FIRED--
---test end---

Actual: As expected rule fires for each new inserted event with desired
serviceName "MyService" (- behavior is clear).

*Second rule:*
rule "ServicePerformanceEvent test rule2"
dialect "mvel"
when
accumulate(ServicePerformanceEvent(serviceName == "MyService",
$thisDuration : duration);  $avg : average($thisDuration));
then
System.out.println("$events avg duration = " + $avg);
System.out.println("--FIRED--");
end

Invocation results of the test1_FireAllRules() and the test2_FireUntilHalt()
the same again:
-= 1 =-
$events avg duration = 2.0
--FIRED--
-= 2 =-
-= 3 =-
$events avg duration = 7.0
--FIRED--
---test end---

Actual: Now rule accumulates average only for events from working memory
with desired serviceName (-this is clear) but fires only for inserted events
with expected serviceName "MyService" (- not clear)
*My expectation*: rule must fire for each currently inserted event and
average must be calculated for all events from working memory with
serviceName=="MyService".

*Third rule:*
/rule "ServicePerformanceEvent test rule3"
dialect "mvel"
when
$event : ServicePerformanceEvent(serviceName == "MyService"); 
accumulate(ServicePerformanceEvent(serviceName ==
"MyService", $thisDuration : duration);  
 $avg : average($thisDuration));
then
System.out.println("$event.duration = " + $event.duration);
System.out.println("$events avg duration = " + $avg);
System.out.println("--FIRED--");
end/

Invocation results of the test1_FireAllRules():
-= 1 =-
$events avg duration = 2.0
--FIRED--
-= 2 =-
-= 3 =-
$events avg duration = 7.0
--FIRED--
$events avg duration = 7.0
--FIRED--
---test1 end---

Actual: In case -=3=- rule fires for each event into working memory during
average calculation (- not clear)
*My expectation*: firing rule only for current/last inserted event and
calculation average for all events into working memory considering
sevriceName=="MyService".

Invocation results of the test2_FireUntilHalt():
-= 1 =-
$events avg duration = 2.0
--FIRED--
-= 2 =-
-= 3 =-
$events avg duration = 2.0
--