what happens in rete algorithm when there are multiple "then"

472 views Asked by At

Using this as an example, let's say that there is a condition which says-

if the flyer's status is silver, then allow free upgrade to business class **and** give a free drink

how should rete network be constructed? At the bottom of the network, there will be two nodes- 1) free upgrade 2) free drink

How should these two nodes be linked to "silver" node?

All of the examples I've seen related to Rete algorithm talks about one "then"

1

There are 1 answers

0
Esteban Aliverti On BEST ANSWER

If the 2 actions are performed in the same rule, then only 1 Action node will be constructed after the "silver" alpha node. I.e.

rule "Allow free upgrade to business class and give a free drink to Silver flyers"
no-loop true
when
    $a: Account (status == "SILVER")
then
    modify($a){
        .setFreeUpgrade(true);
        .setFreeDrinks(true);
    }
end

Drools treats the RHS of the rules as a black box. A RHS is always represented as an Action node in the RETE network. This is true even for rules having the exact same RHS: two Action nodes will be created.

If you implement the same business rule as 2 separate rules, then 2 Action nodes are going to be created:

rule "Allow free upgrade to business class to Silver flyers"
lock-on-active true
when
    $a: Account (status == "SILVER")
then
    modify($a){
        .setFreeUpgrade(true);            
    }
end

rule "Give a free drink to Silver flyers"
lock-on-active true
when
    $a: Account (status == "SILVER")
then
    modify($a){
        .setFreeDrinks(true);
    }
end

We can further discuss which approach is better, but to answer your question I think this is enough: The RETE network will contain AS MANY Action nodes AS rules you have in your kbase

If you are using Drools' eclipse plugin, you can see the RETE network created for a single .DRL file. When editing the DRL you will notice a tab in the bottom part of the editor to check the RETE network being generated.

Hope it helps,