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 FreeHere 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
xxxxxxxxxx
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>6.0.1.Final</version>
</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
xxxxxxxxxx
package com.demo.DroolsDemo;
public class CardDetails {
private String Card;
public String getCard() {
return Card;
}
public void setCard(String card) {
Card = card;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
private int discount;
}
- 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
xxxxxxxxxx
import com.demo.DroolsDemo.CardDetails
rule "Offer for ABC Bank"
when
offerObject: CardDetails(Card=="ABC Bank")
then
offerObject.setDiscount(10);
end
rule "Offer for XYZ Bank"
when
offerObject: CardDetails(Card=="XYZ Bank")
then
offerObject.setDiscount(15);
end
- 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
x
package com.demo.DroolsDemo;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.compiler.PackageBuilder;
import org.drools.core.RuleBase;
import org.drools.core.RuleBaseFactory;
import org.drools.core.WorkingMemory;
public class DroolTest {
public static void main(String[] args) throws DroolsParserException, IOException {
// TODO Auto-generated method stub
DroolTest demo = new DroolTest();
demo.executeBussinessRule();
}
public void executeBussinessRule() throws DroolsParserException, IOException {
PackageBuilder builder = new PackageBuilder();
String ruleFile = "/offers.drl";
InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);
Reader ruleReader = new InputStreamReader(resourceAsStream);
builder.addPackageFromDrl(ruleReader);
org.drools.core.rule.Package rulePackage = builder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(rulePackage);
WorkingMemory workingMemory = ruleBase.newStatefulSession();
CardDetails cardDetails = new CardDetails();
cardDetails.setCard("ABC Bank");
workingMemory.insert(cardDetails);
workingMemory.fireAllRules();
System.out.println("The discount for the card of " + cardDetails.getCard() + " is "
+ cardDetails.getDiscount() + "%");
}
}
- Now Run the project as a java application. You will see the below output.
File Description
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
Flow Execution
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.
Comments