Skip to content

zeromsi/Springboot-drools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Springboot-drools

Drools is an opensource brms engine Drools is a Business Rule Management System (BRMS) solution. It provides a rule engine which processes facts and produces output as a result of rules and facts processing. Centralization of business logic makes it possible to introduce changes fast and cheap.

Drools Basics

We are going to look at basic concepts of Drools:

  • Facts – represents data that serves as input for rules
  • Working Memory – a storage with Facts, where they are used for pattern matching and can be modified, inserted and removed
  • Rule – represents a single rule which associates Facts with matching actions. It can be written in Drools Rule Language in the .drl files or as Decision Table in an excel spreadsheet
  • Knowledge Session – it holds all the resources required for firing rules; all Facts are inserted into session, and then matching rules are fired
  • Knowledge Base – represents the knowledge in the Drools ecosystem, it has the information about the resources where Rules are found, and also it creates the Knowledge Session
  • Module – A module holds multiple Knowledge Bases which can hold different sessions

Springboot configuration

POM file

<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>6.4.0.Final</version>
</dependency><!-- https://mvnrepository.com/artifact/org.drools/drools-compiler -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>6.4.0.Final</version>
</dependency><!-- https://mvnrepository.com/artifact/org.kie/kie-api -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>6.4.0.Final</version>
</dependency>

rule file

  • create a folder inside src/main/resources
  • create a file that has .drl extension

ksession

  • create a folder named META-INF
  • create a file named kmodule.xml
  • copy and paste the following code
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
  <kbase name="rules" packages="com.rule">
       <ksession name="rulesSession"/>
   </kbase>
</kmodule>

kieContainer bean

  • Create a bean inside main class like following
	@Bean
	public KieContainer kieContainer() {
		return KieServices.Factory.get().getKieClasspathContainer();
	}

trigger rules

  • write service layer in the following format
	public Product getProductDiscount(Product product, Customer customer) {
		KieSession kieSession = kieContainer.newKieSession("rulesSession");
		kieSession.insert(product);
		kieSession.setGlobal("ProductService", this);
		kieSession.fireAllRules();
		kieSession.dispose();
		return product;
	}

Note: while instantiating kieSession we've passed kieSession name as rulesSession which was created inside kmodule.xml file previously and we've passed ProductService as ProductService through the session to use this isnside .drl file.

.drl file

  • a .drl file has three major parts

    1. package declaration
     package com.rule
    1. file/class importing
     import com.msi.model.Product
     import com.msi.service.ProductService
     import com.msi.model.Customer
    1. variable declaration
     global 	ProductService ProductService

    Note: We've passed ProductService as ProductService

    1. rules
rule "Offer for Diamond"
	when 
		productObject: Product(type=="diamond")
	then
		productObject.setDiscount(15);
	end
	
rule "Offer for Gold"
	when 
		productObject: Product(type=="gold")
	then
		productObject.setDiscount(25);
	end
	
rule "Offer for Gold"
	when 
		productObject: Product(type=="gold")
		customerObj: Customer()
	then
	  if(customerObj.getAge()>21){
	  ProductService.printMsg();
	  }
		productObject.setDiscount(27);		
	end

About

Drools is an opensource brms engine

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages