how to fire all matched rules in the drl assest (drool workbench)

30 views
Skip to first unread message

Ramanathan M

unread,
Jun 3, 2024, 8:15:35 AMJun 3
to Drools Usage
package com.myspace.drl1;
import com.myspace.drl1.ActualPrice;


// Rules
rule "7"
when
    actPriceObj : ActualPrice(customer == "ramanathan", discount == "15")
then
    actPriceObj.setActualPrice(1400);
    actPriceObj.setCoupon(20);
end

rule "8"
when
    actPriceObj : ActualPrice(customer == "balaji", discount == "15")
then
    actPriceObj.setActualPrice(3550);
    actPriceObj.setCoupon(30);
end

rule "9"
ruleflow-group "execute-rules"
when
    actPriceObj : ActualPrice(customer == "balaji")
then
    actPriceObj.setCoupon(40);
end

rule "10"
ruleflow-group "execute-rules"
when
    actPriceObj : ActualPrice(customer == "ramanathan", discount == "15")
then
    actPriceObj.setActualPrice(2700);
    actPriceObj.setCoupon(50);
end

rule "11"
ruleflow-group "execute-rules"
when
    actPriceObj : ActualPrice(customer == "prithvi", discount == "20")
then
    actPriceObj.setActualPrice(4000);
    actPriceObj.setCoupon(60);
end

rule "12"
ruleflow-group "execute-rules"
when
    actPriceObj : ActualPrice(customer == "aksh", discount == "30")
then
    actPriceObj.setActualPrice(2200);
    actPriceObj.setCoupon(70);
end

rule "13"
ruleflow-group "execute-rules"
when
    actPriceObj : ActualPrice(customer == "ramanathan")
then
    actPriceObj.setActualPrice(3600);
    actPriceObj.setCoupon(80);
end

// if i give a input customer name = "ramanathan" and discount=15 it print only the output of 3600 and 80 i want to print all the matched rules

Ajay Chowdary Kandula

unread,
Jun 7, 2024, 8:08:36 AMJun 7
to drools...@googlegroups.com, Ramanathan M
To fire all the matched rules in the DRL asset, you need to use the `fireAllRules()` method provided by the KIE engine. Here's an example of how you can achieve this:

```java
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class RulesFiringExample {
    public static void main(String[] args) {
        // Load the KIE base and session
        KieServices kieServices = KieServices.Factory.get();
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        KieSession kieSession = kieContainer.newKieSession();

        // Create the input object
        ActualPrice actualPrice = new ActualPrice();
        actualPrice.setCustomer("ramanathan");
        actualPrice.setDiscount("15");

        // Insert the input object into the session
        kieSession.insert(actualPrice);

        // Fire all the rules
        kieSession.fireAllRules();

        // Print the updated object
        System.out.println("Actual Price: " + actualPrice.getActualPrice());
        System.out.println("Coupon: " + actualPrice.getCoupon());

        // Dispose the session
        kieSession.dispose();
    }
}
```

In this example, we first load the KIE base and create a new KIE session. Then, we create an instance of the `ActualPrice` class and set the customer and discount values.

Next, we insert the `ActualPrice` object into the KIE session using the `insert` method. This makes the object available for pattern matching in the rules.

After that, we call the `fireAllRules` method, which will fire all the rules that match the inserted object. This method will continue firing rules until no more rules can be fired (a fixed-point is reached).

Finally, we print the updated values of the `ActualPrice` object and dispose of the KIE session.

When you run this code with the input `customer = "ramanathan"` and `discount = "15"`, it should fire all the rules that match this input (rules 7, 10, and 13) and print the final values of `actualPrice` and `coupon` after applying all the matching rules.

Note that the order in which the rules are fired is not guaranteed, as the rule engine uses an optimized algorithm to determine the firing order based on the input data and rule conditions.[2][4]

Citations:
[1] https://stackoverflow.com/questions/40088432/drools-workbench-guided-rule-to-include-multiple-rules-in-one-file
[2] https://docs.drools.org/7.74.1.Final/drools-docs/html_single/
[3] https://groups.google.com/g/drools-setup/c/oeQROSzLtxc/m/j21EPjtcAwAJ
[4] https://docs.jboss.org/drools/release/6.2.0.CR3/drools-docs/html/ch06.html
[5] https://docs.drools.org/7.11.0.Final/drools-docs/html_single/


Thanks and Regards!!!
Ajay C. Kandula


--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/aea3383d-0f49-4792-b6db-3355762a9916n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages