1

I need to write a rule that counts the number of Facts I've received in my stream over the last 10 seconds if they match a certain criteria.

So, for instance, if 2 black cars pass through an intersection in the last 10 seconds I want to alert someone.

I have the following rule:

rule "check black cars in 10 seconds" dialect "java"
when

    $car : Car(color == Color.BLACK) over window:time(10s);
then

    System.out.println("got something");

This is working when I pass in a Black car, however, I don't want it to fire unless there are 2 black cars. I cannot find a good example of this.

Thanks.

El Guapo
  • 5,581
  • 7
  • 54
  • 82

1 Answers1

1
when
accumulate( Car(color == Color.BLACK) over window:time(10s);
            $cnt: count(1); $cnt == 2 )
then

This will fire when the second car arrives within 10 seconds of the first car, and again when a third car arrives within 10 seconds of the second car but later than 10 seconds after the first car, but that's what can be derived from your (vague) spec.

laune
  • 31,114
  • 3
  • 29
  • 42
  • So... in trying to understand this... what exactly does "$cnt: count(1)" do??? And what do I do if I didn't want to wait 10 seconds after the 1st car to be notified about the 3rd? – El Guapo Feb 17 '14 at 21:23
  • Question: let's assume I don't know the values of possible colors and i want the rule to fire if two cars with them same color (no matter what the exact color is) occur in a 10s window. i came up with when $c : Car( $color : color ) accumulate( Car(color == $color) over window:time(10s); $cnt: count(1); $cnt == 2 ) then .... this solution fires to may times... – Scholle Jun 16 '18 at 14:32
  • @Scholle I suggest you ask this as a new question. – laune Jul 04 '18 at 19:37