Using Maven and Without Drools Eclipse Plugin
Build one sample Drools Project using maven dependencies and this will not require any pre-setup of drools in your eclipse to build the project.
Join the DZone community and get the full member experience.Join For Free
Here we are going to build a sample Drools project using maven dependencies, and this will not require any pre-setup of drools in your eclipse to build the project.
In the sample demo project, we will be creating a Maven Project in which with the help of drools we will decide what discount is to be offered on which Bank Card. For example, if the user is using ABC Bankcard we will be giving him/her a 10% discount.
So let's follow the below steps to build the Maven Drools Project
- Open the eclipse and go to File -> New -> Project
- One window will open search for Maven -> Maven Project
- Click next until you get the “Enter a group id for the artifact.” Page
- Enter Artifact Id as “DroolsDemo”, Group Id as “com.demo” and click Finish
- After clicking on Finish project will be created. Now, Expand the project and open the pom.xml
- Under the dependencies tag, add below dependency
- Now we will create one POJO class and also remove App.java if its created by default. Go to src/main/java -> right click on com.demo.DroolsDemo -> New -> Class
- Enter the name of the class as “CardDetails” and click Finish
- Write below code in the CardDetails class
- Now we will add the rules. Go to src -> right click on main -> New -> Folder, Give the folder name as “resources”
- Now right click on resources New-> File, Give File name as “offers.drl”
- Write the below code in offers.drl file
- Now we will create one class to fire drools rule. Go to src/main/java -> right click on com.demo.DroolsDemo -> New -> Class
- Enter Name as DroolTest -> click on Finish and write below Code in the DroolTest.java
- Now Run the project as a java application. You will see the below output.
After completion, your project folder will look like below
Now let's see what exactly this file will do!
- CardDetails.java: The CardDetails.java file consists of our POJO class CardDetails. Which will help to store the data of the name of the Bank Card and the discount given against it
- Offers.drl: The offers.drl file consists of the rules of the drools. In which we are defining what discount should be given against which bank card. For more understanding on drools, rule syntax click here
- DroolTest.java: The DroolTest.java file consists of the main method as well as the method which will fire the rules and give the output back
Now let see how this flow will work.
- In DroolTest.java the main method will call the executeBussinessRule method.
- The business Rule method will set the value of bank card name in the POJO class by calling the method SetCard(“ABC”) (where ABC is the Bank Name) and fire the rule
- The rule written under offers.drl will set the discount value and return to executeBussinessRule method in DroolTest class which will then print the message
“The discount for the card of ABC Bank is 10%”
Opinions expressed by DZone contributors are their own.