Unlock 10% Discounts in 5 Minutes: Learn How to Build a Drools Project with Maven Today!

Comentarios · 45 Puntos de vista

In this tutorial, we’ll embark on building a sample Drools project, leveraging Maven dependencies, which eliminates the need for pre-configuring Drools in your Eclipse environment to get the project up and running.

Building a Drools Project with Maven in 5 Easy Steps In this tutorial, we will guide you through the process of creating a sample Drools project using Maven dependencies, eliminating the need for pre-configuring Drools in your Eclipse environment. Our demo project will utilize Drools to determine the discount offered on various Bank Cards. For instance, if the user is employing an ABC Bankcard, we will provide a 10% discount. Follow the steps below to build the Maven Drools Project: Step 1-2: Create a New Maven Project Launch Eclipse and navigate to File - New - Project. A new window will open; search for Maven - Maven Project. Click next until you reach the "Enter a group id for the artifact" page. Step 3: Configure the Project Enter the Artifact Id as "DroolsDemo", Group Id as "com.demo", and click Finish. After clicking Finish, the project will be created. Step 4: Add Dependencies Expand the project and open the pom.xml file. Under the dependencies tag, add the following dependency: xml org.drools drools-compiler 6.0.1.Final By following these easy steps, you can unlock 10% discounts and build a Drools project with Maven. For more information, visit computerstechnicians.

  • Proceed by creating a POJO class and deleting the default App.java file if present. Navigate to src/main/java, right-click on com.demo.DroolsDemo, and select New - Class
  • Specify the class name as “CardDetails” and click Finishjava class creation
  • Insert the following code into the CardDetails class

 

package com.demo.DroolsDemo;public class CardDetails {		private String cardType;	public String getCardType() {		return cardType;	}	public void setCardType(String cardType) {		this.cardType = cardType;	}	public int getDiscountAmount() {		return discountAmount;	}	public void setDiscountAmount(int discountAmount) {		this.discountAmount = discountAmount;	}	private int discountAmount;}
  • Next, we will establish the rules. Navigate to src, right-click on main, and select New - Folder. Name the folder “resources”.
  • Right-click on resources and select New- File, naming the file “offers.drl”.
  • Insert the following code into the offers.drl file:
import com.demo.DroolsDemo.CardDetailsrule "ABC Bank Promotion"	when		offerObject: CardDetails(cardType=="ABC Bank")	then		offerObject.setDiscountAmount(10);	endrule "XYZ Bank Promotion"	when		offerObject: CardDetails(cardType=="XYZ Bank")	then		offerObject.setDiscountAmount(15);	end
  • Now, we will create a class to activate the Drools rule. Go to src/main/java, right-click on com.demo.DroolsDemo, and select New - Class.
  • Enter the name as DroolTest, click on Finish, and insert the following code into the DroolTest.java file:
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() + "%");					}}
  • Initiate the project as a Java application, and you will be presented with the following output.SLF4J

Project Structure Overview

Upon completion, your project folder will be organized as follows:

Let’s dive into the details of this file!

  • CardDetails.java: This file houses our POJO class, CardDetails, which serves as a data repository for the bank card name and corresponding discount.
  • Offers.drl: This file encompasses the rules governing Drools, where we define the discounts applicable to specific bank cards. For a more in-depth understanding of Drools and its rule syntax, click here.
  • DroolTest.java: This file comprises the main method, as well as the method responsible for firing the rules and generating output.

Flow Execution Sequence

Now, let’s examine how this flow unfolds.

  • In DroolTest.java, the main method invokes the executeBusinessRule method.
  • The business rule method sets the bank card name value in the POJO class by calling the SetCard(“ABC”) method (where ABC represents the bank name) and triggers the rule.
  • The rule defined in offers.drl sets the discount value and returns it to the executeBusinessRule method in the DroolTest class, which then prints the message “The discount for the card of ABC Bank is 10%.”
Comentarios