简体   繁体   中英

Drools: Sequential rule firing with dynamic input

I am the following condition to follow:

  • Fetch data from CSV
  • If the data fetched from CSV is not null, then store it in a map
  • Then, insert the above data in database. Note: This is a very simple example that I am trying to put forward.

I tried with using salience, but the problem is, the output from my first rule will be treated as input for the condition for the second rule. As I understand, what is happening is, the condition part of all the rules mentioned in the same DRL file is executed first. Thus, for the second rule, in the condition, the variable is null.

Example:

rule "1st rule"
salience 50
when
    $bean : Bean(a == 1)
then
    $bean.setB(10);
end

rule "2nd rule"
salience 40
when
    $bean : Bean(b == 10)
then
    System.out.println("2nd rule success");
end

What I am trying to point above is, the second rule will execute only when the first rule is executed. But, as I understand, the "IF" part of the rules are executed before the "THEN" part, thus, the 2nd rule fails.

If I understood correctly, the 2nd rule in your example is not executed. This is because you're not letting the engine know that something has changed. Use update or modify for this.

For example

// update
when
    $bean : Bean(a == 1)
then
    $bean.setB(10);
    update($bean);
end

// or with modify
when
    $bean : Bean(a == 1)
then
    modify($bean) {
      setB(10)
    };
end

More information available in the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM