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:
set up value of $optionalPrice as price property of OrderItem
OrderItem( $optionalPrice := Optional.of(price) ) from $oithen 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)
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:
agenda-group usage
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:
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
https://github.com/YuliaStrakhova/drools/tree/master/src/main/resources/org/yulia/agenda2 - null case first with agenda-group
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
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>