Different result for query with and without using agenda-group

37 Views Asked by At

I have the rule. Since it is autogenerated, it tends to be verbose.

rule "Rule Filter"
    agenda-group "Rule-ag"
when
    checkOrderItem(  )
then
    drools.setFocus("Action-ag");
end

query "checkOrderItem"  
    (
        calculateValue( $calculatedValue, $orderItem; )  and
        Optional( $calculatedValue.orElse(null) >  10.0 ) from Optional.empty()
    )
end

query "calculateValue" ( Optional $calculatedValue, OrderItem $oi ) 
    (
        OrderItem( $optionalPrice := Optional.of(price) ) from $oi and
        (
             (
                Optional( !this.isEmpty(), $optionalPriceVal : this.get() ) from $optionalPrice and
                Double( $modifiedPrice : (this *3) ) from $optionalPriceVal and
                Optional( $calculatedValue := this ) from Optional.ofNullable($modifiedPrice)
              )
             or
             (
                Optional( this.isEmpty() ) from $optionalPrice and
                Optional( $calculatedValue := this ) from $optionalPrice
             )

        )
    )
end

rule "Rule Action"
    agenda-group "Action-ag"
when
then
    insert(new Result(true, "rule-with-agenda-group1.drl"));
end

There are three steps in "calculateValue" query:

  1. set up value of $optionalPrice as price property of OrderItem

    OrderItem( $optionalPrice := Optional.of(price) ) from $oi 
    
  2. then if $optionalPrice is not empty do some calculation against value and then set up $calculatedValue

 Optional( !this.isEmpty(), $optionalPriceVal : this.get() ) from $optionalPrice and
 Double( $modifiedPrice : (this *3) ) from $optionalPriceVal and
 Optional( $calculatedValue := this ) from Optional.ofNullable($modifiedPrice)
  1. Otherwise do nothing and set up $calculatedValue as is.

    Optional( this.isEmpty() ) from $optionalPrice and
    Optional( $calculatedValue := this ) from $optionalPrice
    

I found out that result of the query depend on:

  1. agenda-group usage

  2. order of or conditions of the query

query works as expected

query "calculateValue" ( Optional $calculatedValue, OrderItem $oi ) 
    (
        OrderItem( $optionalPrice := Optional.of(price) ) from $oi and
        (
             (
                Optional( !this.isEmpty(), $optionalPriceVal : this.get() ) from $optionalPrice and
                Double( $modifiedPrice : (this *3) ) from $optionalPriceVal and
                Optional( $calculatedValue := this ) from Optional.ofNullable($modifiedPrice)
              )
             or
             (
                Optional( this.isEmpty() ) from $optionalPrice and
                Optional( $calculatedValue := this ) from $optionalPrice
             )

        )
    )
end

works as expected only without agenda-group usage. Only difference is order of or conditions

query "calculateValue" ( Optional $calculatedValue, OrderItem $oi ) 
    (
        OrderItem( $optionalPrice := Optional.of(price) ) from $oi and
        (
             
             (
                Optional( this.isEmpty() ) from $optionalPrice and
                Optional( $calculatedValue := this ) from $optionalPrice
             )
             or 
             (
                Optional( !this.isEmpty(), $optionalPriceVal : this.get() ) from $optionalPrice and
                Double( $modifiedPrice : (this *3) ) from $optionalPriceVal and
                Optional( $calculatedValue := this ) from Optional.ofNullable($modifiedPrice)
              )

        )
    )
end

Here you can find example of described testcase https://github.com/YuliaStrakhova/drools/tree/master . On project you can find 4 slightly different rules:

  1. https://github.com/YuliaStrakhova/drools/blob/master/src/main/resources/org/yulia/agenda1/rule-with-agenda-group1.drl - not null case first with agenda-group

  2. https://github.com/YuliaStrakhova/drools/tree/master/src/main/resources/org/yulia/agenda2 - null case first with agenda-group

  3. https://github.com/YuliaStrakhova/drools/blob/master/src/main/resources/org/yulia/plain1/rule-without-agenda-group1.drl - not null case first without agenda-group

  4. https://github.com/YuliaStrakhova/drools/blob/master/src/main/resources/org/yulia/plain2/rule-without-agenda-group2.drl - null case first without agenda-group

Case (2) do not work as expected (Rule then part do not executed). Cases (1), (3) and (4) executed as expected and then part produce result.

Please help me understand why result of query depend on order of conditions or usage of agenda-group.

<drools.version>7.52.0.Final</drools.version>

0

There are 0 best solutions below