Sample Drools Program - rules engine - drools tutorial - business rules engine
- In this, we will generate a Drools project for the following problem statement:
- Depending upon the city and the kind of product find out the local tax related to that city.
- We have two DRL files for our Drools project.
- The two DRL files will indicate two cities (Lucknow and Noida) and four types of products (Gadgets, medicines, watches, and luxury goods).
- The tax on medicines in both the cities is considered as zero.
- For Gadgets, we have assumed a tax of Rs 2 in Lucknow and Rs 1 in Noida.
- We have used the same selling price to demonstrate different outputs. Note that all the rules are getting fired in the application.
Example:
DRL Files
- Here we used two DRL files: Lucknow.drl and Noida.drl.
Lucknow.drl
- This is the DRL file that executes rules for Lucknow city
Noida.drl
- This is the DRL file that executes rules for Noida city
- The DRL files based on city, as it gives us extensibility to add any number of rule files later if new cities are being added.
- To demonstrate that all the rules are getting triggered from our rule files, we have used two item types (medicines and Gadgets); and medicine is tax-free and Gadgets are taxed as per the city.
- Our test class loads the rule files, inserts the facts into the session, and produces the output.
Droolstest.java
Output
- For both Lucknow and Noida, when the item is a medicine, the local tax is zero; whereas when the item is a gadget product, the tax is as per the city. More rules can be added in the DRL files for other products.
Call an External Function form a DRL File
- Here we will demonstrate how to call a static function from a Java file within your DRL file.
- First of all, create a class HelloCity.java in the same package com.sample.
- Thereafter, add the import statement in the DRL file to call the writeHello method from the DRL file.
Example
Output
- The advantage to call a Java method is that we can write any utility/helper function in Java and call the same from a DRL file.