Search code examples
javaspring-bootdroolsrule-engineentry-point

how to prevent drools engine looping on entry point data


I'm using drools to raise alarm on streaming transaction data. The drools engine is STREAM and ACTIVE mode. I also use an entry point (OM-TRANS) to transmit data to the rule engine. I have written a simple rule to test the behaviour of the engine. I got some results but i don't understand them and their are not what i expect.

This is the first simple rule:

rule "My rule"
    no-loop true
    when
        $transaction: TransactionOmDto($msisdn: msisdn) from entry-point "OM-TRANS"
    then
        System.out.println("------------ an alarm on "+$msisdn+" ----------");
    end

These are the results i got after testing:

  1. transaction from msisdn msisdn_1:

------------ an alarm on msisdn_1 ----------

  1. transaction from msisdn msisdn_1 again:

------------ an alarm on msisdn_1 ----------

  1. transaction from msisdn msisdn_2:

------------ an alarm on msisdn_2 ----------

  1. transaction from msisdn msisdn_2 again:

------------ an alarm on msisdn_2 ----------

  1. transaction from msisdn msisdn_3:

------------ an alarm on msisdn_3 ----------

This is now the second version of the rule:

rule "My rule"
    no-loop true
    when
        $transaction: TransactionOmDto($msisdn: msisdn) from entry-point "OM-TRANS"
        $datasTransaction: List() from collect(TransactionOmDto(msisdn == $msisdn) from entry-point "OM-TRANS")
    then
        System.out.println("------------ an alarm on "+$msisdn+", total Transaction: "+$datasTransaction.size()+" ----------");
    end

and i got the following results and what i expect:

  1. transaction from msisdn msisdn_1:

------------ an alarm on msisdn_1, total Transaction: 1 ---------- (this is the one expected)

  1. transaction from msisdn msisdn_1 again:
------------ an alarm on msisdn_1, total Transaction: 2 ----------
------------ an alarm on msisdn_1, total Transaction: 2 ----------

What i expected is : A sigle row ------------ an alarm on msisdn_1, total Transaction: 2 ----------

  1. transaction from msisdn msisdn_2:

------------ an alarm on msisdn_2, total Transaction: 1 ---------- (as expected)

  1. transaction from msisdn msisdn_2 again:
------------ an alarm on msisdn_2, total Transaction: 2 ----------
------------ an alarm on msisdn_2, total Transaction: 2 ----------

What i expected is : A sigle row ------------ an alarm on msisdn_2, total Transaction: 2 ----------

  1. transaction from msisdn msisdn_3:

------------ an alarm on msisdn_3, total Transaction: 1 ---------- (this is the one expected)

  1. transaction from msisdn msisdn_3 again:
------------ an alarm on msisdn_3, total Transaction: 2 ----------
------------ an alarm on msisdn_3, total Transaction: 2 ----------

What i expected is : A sigle row ------------ an alarm on msisdn_3, total Transaction: 2 ----------

  1. transaction from msisdn msisdn_3 again:
------------ an alarm on msisdn_3, total Transaction: 3 ----------
------------ an alarm on msisdn_3, total Transaction: 3 ----------
------------ an alarm on msisdn_3, total Transaction: 3 ----------

What i expected is : A sigle row ------------ an alarm on msisdn_3, total Transaction: 3 ----------

Can someone explain to me why i got those result? Is it possible to get what i expect ? Thanks you.


Solution

  • Because your events expire only in 24h, older ones do participate in the rule logic.
    Usually this can be fixed with sliding length window

     over window:length(1) from entry-point "OM-TRANS"
    

    full snippet

    rule "My rule2"
    no-loop true
    when
        $transaction: TransactionOmDto($msisdn: msisdn) over window:length(1) from entry-point "OM-TRANS"
        $datasTransaction: List() from collect(TransactionOmDto(msisdn == $msisdn) from entry-point "OM-TRANS")
    then
        System.out.println("------------ an alarm on "+$msisdn+", total Transaction: "+$datasTransaction.size()+" ----------");
    end
    

    output

    ------------ an alarm on msisdn_1, total Transaction: 1 ----------
    ------------ an alarm on msisdn_1, total Transaction: 2 ----------
    ------------ an alarm on msisdn_1, total Transaction: 3 ----------