DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • DGS GraphQL and Spring Boot
  • How to Build a New API Quickly Using Spring Boot and Maven
  • Configurable Feign Client Retry With Reusable Library and DRY
  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3

Trending

  • Testing SingleStore's MCP Server
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  1. DZone
  2. Coding
  3. Frameworks
  4. Using Maven and Without Drools Eclipse Plugin

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.

By 
Dikshant Dwivedi user avatar
Dikshant Dwivedi
·
Updated May. 11, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
8.1K Views

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

    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%”
Drools Apache Maven Eclipse

Opinions expressed by DZone contributors are their own.

Related

  • DGS GraphQL and Spring Boot
  • How to Build a New API Quickly Using Spring Boot and Maven
  • Configurable Feign Client Retry With Reusable Library and DRY
  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!