{{announcement.body}}
{{announcement.title}}

Using Maven and Without Drools Eclipse Plugin

DZone 's Guide to

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.

· Java Zone ·
Free Resource

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

    New project
  • Click next until you get the “Enter a group id for the artifact.” Page

    new maven project
  • 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
XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
        <groupId>org.drools</groupId>
3
        <artifactId>drools-compiler</artifactId>
4
        <version>6.0.1.Final</version>
5
</dependency>
6
 
          



  • 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

    java class
  • Write below code in the CardDetails class
Java
 




xxxxxxxxxx
1
20


 
1
package com.demo.DroolsDemo;
2
 
          
3
public class CardDetails {
4
    
5
    private String Card;
6
    public String getCard() {
7
        return Card;
8
    }
9
    public void setCard(String card) {
10
        Card = card;
11
    }
12
    public int getDiscount() {
13
        return discount;
14
    }
15
    public void setDiscount(int discount) {
16
        this.discount = discount;
17
    }
18
    private int discount;
19
}
20
 
          



  • 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
Java
 




xxxxxxxxxx
1
15


 
1
import com.demo.DroolsDemo.CardDetails
2
 
          
3
rule "Offer for ABC Bank"
4
    when
5
        offerObject: CardDetails(Card=="ABC Bank")
6
    then
7
        offerObject.setDiscount(10);
8
    end
9
rule "Offer for XYZ Bank"
10
    when
11
        offerObject: CardDetails(Card=="XYZ Bank")
12
    then
13
        offerObject.setDiscount(15);
14
    end
15
 
          



  • 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
Java
 




x


1
package com.demo.DroolsDemo;
2
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.InputStreamReader;
6
import java.io.Reader;
7
8
import org.drools.compiler.compiler.DroolsParserException;
9
import org.drools.compiler.compiler.PackageBuilder;
10
import org.drools.core.RuleBase;
11
import org.drools.core.RuleBaseFactory;
12
import org.drools.core.WorkingMemory;
13
14
15
16
17
public class DroolTest {
18
19
    public static void main(String[] args) throws DroolsParserException, IOException {
20
        // TODO Auto-generated method stub
21
        DroolTest demo = new DroolTest();
22
        demo.executeBussinessRule();
23
    }
24
    
25
public void executeBussinessRule() throws DroolsParserException, IOException {
26
        
27
        PackageBuilder builder = new PackageBuilder();
28
        String ruleFile = "/offers.drl";
29
        InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);
30
        
31
        Reader ruleReader = new InputStreamReader(resourceAsStream);
32
        builder.addPackageFromDrl(ruleReader);
33
        org.drools.core.rule.Package rulePackage = builder.getPackage();
34
        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
35
        ruleBase.addPackage(rulePackage);
36
        
37
        WorkingMemory workingMemory = ruleBase.newStatefulSession();
38
        
39
        CardDetails cardDetails = new CardDetails();
40
        cardDetails.setCard("ABC Bank");
41
        workingMemory.insert(cardDetails);
42
        workingMemory.fireAllRules();
43
        
44
        
45
        System.out.println("The discount for the card of " + cardDetails.getCard() + " is "
46
                + cardDetails.getDiscount() + "%");
47
        
48
        
49
    }
50
51
}



  • Now Run the project as a java application. You will see the below output.

    SLF4J

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%”
Topics:
drools ,eclipse plugin ,java ,maven ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}