Drools Tutorials- Integration with Spring
Overview
In this tutorial we will create a Spring MVC Application and integrate with JBoss Drools. Like previous drools examples we will create an application for a jewellery shop to calculate discount based on the type of jewellery. In another tutorial, we have integrated Spring Boot with DroolsJBoss Drools - Table of Contents
JBoss Drools Hello World JBoss Drools Hello World-Stateful Knowledge Session using KieSession JBoss Drools- Understanding Drools Decision Table using Simple Example Understand Drools Stateful vs Stateless Knowledge Session Drools Tutorials- Backward Chaining simple example Drools Tutorials- Understanding attributes salience, update statement and no-loop using Simple Example Drools Tutorials- Understanding Execution Control in Drools using Simple Example Drools Tutorials- Integration with Spring MVC Drools Tutorials- Integration with Spring Boot
Video
This tutorial is explained in the below Youtube Video.Lets Begin
The project structure is as follows-
The pom.xml will be as follows-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainuse</groupId> <artifactId>drools-spring</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.1.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version></version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version></version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-spring</artifactId> <version>5.4.0.Final</version> </dependency> </dependencies> </project>
Define the model class Product.java as follows.
package com.javainuse.model; public class Product { private String type; private int discount; public String getType() { return type; } public void setType(String type) { this.type = type; } public int getDiscount() { return discount; } public void setDiscount(int discount) { this.discount = discount; } }
Define the rules.drl as follows-
package rules import com.javainuse.model.Product 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