Drools 7.23 rule execution order

31 views
Skip to first unread message

Nikolay Shuvaev

unread,
Oct 6, 2020, 10:10:52 AM10/6/20
to Drools Setup
Hello! I installed Drools Workbench and Kie Server 7.23.0.Final-showcase. The documentation (https://docs.jboss.org/drools/release/7.23.0.Final/drools-docs/html_single) says that salience value affects rules execution order so the higher the value the higher the priority.
But in my example DRL file with two rules with same conditions, the rule with less salience is selected. It's confirmed by Test Scenarios and KIE API calls.
What can i do to change execution order as stated in documentation?

My DRL file:
package com.main.bucket;
import com.main.bucket.lead;

package com.main.bucket;
import com.main.bucket.lead;

rule "test 2"
salience 2
when
$lead:lead(utmSource == "test")
then
$lead.setBucket("test 2");
end

rule "test 1"
salience 1
when
$lead:lead(utmSource == "test")
then
$lead.setBucket("test 1");
end

My KIE Server API request:
{
  "lookup": null,
  "commands": [
    {
      "insert": {
        "object": {
          "com.main.bucket.lead": {
                "utmSource": "test"
          }
        },
        "out-identifier": 1,
        "return-object": true
      }
    },
    {
      "fire-all-rules": {
        "max": -1
      }
    }
  ]
}

and response:
"type": "SUCCESS",
    "msg": "Container bucket_1.0.0-SNAPSHOT successfully called.",
    "result": {
        "execution-results": {
            "results": [
                {
                    "value": {
                        "com.main.bucket.lead": {
                            "bucket": "test 1",
                            "utmSource": "test"
                        }
                    },
                    "key": "1"
                }
            ],
            "facts": [
                {
                    "value": {
                        "org.drools.core.common.DefaultFactHandle": {
                            "external-form": "0:44:675213773:675213773:44:DEFAULT:NON_TRAIT:com.main.bucket.lead"
                        }
                    },
                    "key": "1"
                }
            ]
        }
    }
}

canuc...@gmail.com

unread,
Oct 6, 2020, 12:42:03 PM10/6/20
to Drools Setup
This is correct lol. The last element to set is 1.

1. $lead.setBucket("test 2");
2.  $lead.setBucket("test 1");  

You can always put print statements to see the order in the then part. OR use a drools listener.

rule "test 2"
salience 2
when
$lead:lead(utmSource == "test")
then
System.out.println("test 2");   //print
$lead.setBucket("test 2");
end

mcpe...@gmail.com

unread,
Oct 6, 2020, 12:57:53 PM10/6/20
to Drools Setup
Hi,

The docs can be considered as correct though the wording allows different interpretations. Higher priority - the rule will be fired first. Lower salience - the rule will be fired last and will overwrite previous changes of the fact.

Regards,
Yuriy

On Tuesday, October 6, 2020 at 8:10:52 AM UTC-6 n.shuv...@gmail.com wrote:
...The documentation (https://docs.jboss.org/drools/release/7.23.0.Final/drools-docs/html_single) says that salience value affects rules execution order so the higher the value the higher the priority.
...

Nikolay Shuvaev

unread,
Oct 7, 2020, 5:36:17 AM10/7/20
to Drools Setup
Wow, I missed the fact that the rules are executed one by one and change the state of the same object.
I solved this problem by setting the "max" parameter to 1 in the "fire-all-rules" command. Thanks for the help!

вторник, 6 октября 2020 г. в 19:57:53 UTC+3, mcpe...@gmail.com:
Reply all
Reply to author
Forward
0 new messages