Drools KIE introduction

From Training Material
Jump to navigation Jump to search

Drools Kie

Drools Kie Overview

Business Rules Manager ⌘

  • allows people to manage rules in a multi user environment
  • single point of truth for business rules

Kie

  • Common terms for combining common libraries and other parts of jBPM, Drools and Fusion
  • Stands for Knowledge Is Everything

When to use Kie

  • versions/deployment of rules,
  • let non-technical people create/update/view rules
  • embedded in existing applications
  • no existing infrastructure
  • lots of "business" rules
  • can be used on its own, or with the IDE

When to not use Kie

  • mostly technical rules
  • part of an application rather than corporate solution
  • existing infrastructure and GUI

Kie Users

  • Business Analysts
  • Rule experts
  • Developers
  • Administrators
  • Managers (as documentation and analysis tool)

Features

  • Multiple types of rule editors (GUI, text)
  • Version control (historical assets)
  • Build and deploy
  • Store multiple rule "assets" together as a package
  • Human Task console

Starting KIE

Starting KIE:

cd /opt/jbpm-server-7.XX.0.Final-dist/bin/
. standalone.sh

open a web browser and go to

localhost:8080/jbpm-console (drools 7.10)

localhost:8080/business-central (drools 7.19+)


Apply Discount

Creating a Fact Type ⌘

  • Add Asset -> Data Object
    • Data Object: ShoppingCart
ClipCapIt-170903-175815.PNG
  • Create new field
    • Id: totalPrice
    • Label: Total Price
    • Type: BigDecimal (not supported in test scenarios in version 7.19)
ClipCapIt-170903-175930.PNG
  • Save (upper right corner)

Looking at generated POJO

  • Switch to Source tab
 package npou.npproj;
 /**
 * This class was automatically generated by the data modeler tool.
 */
 public class ShoppingCart implements java.io.Serializable {
 
 static final long serialVersionUID = 1L;
 
 @org.kie.api.definition.type.Label(value = "Total Price")
 private java.math.BigDecimal totalPrice;
 
 public ShoppingCart() {
 }
 
 public ShoppingCart(java.math.BigDecimal totalPrice) {
 this.totalPrice = totalPrice;
 }
  
 public java.math.BigDecimal getTotalPrice() {
 return this.totalPrice;
 }

 public void setTotalPrice( java.math.BigDecimal totalPrice ) {
 this.totalPrice = totalPrice;
 }
 }

Drools Kie - Example

Creating a Rule

  • Add Asset -> Guided Rule
  • Resource Name: Apply Tall Order Discount
  • Enter details as below

ClipCapIt-170903-180656.PNG


  • View Source should return
package npou.npproj;

import java.lang.Number;

rule "Apply Tall Order Discount"
	dialect "mvel"
	when
		$sc : ShoppingCart( $tp : totalPrice >= 10000.0B )
	then
		modify( $sc ) {
				setTotalPrice( $tp*0.9 )
		}
end

Creating a Test Scenario (Legacy)

  • Create New Asset-> Test Scenario (Legacy)
  • Resource Name: Apply Discount Test
  • Run Scenario

ClipCapIt-170903-181355.PNG

Creating a Test Scenario

  • Create New Asset-> Test Scenario
  • Resource Name: Apply Discount Test 2
  • Run Scenario

ClipCapIt-191210-145729.PNG ClipCapIt-191210-150035.PNG


Dealing with Recursion

  • Let us try to add a test for order value of 15000 and set EXPECT section to 13500

ClipCapIt-170903-181553.PNG

No-Loop Attribute

We need to ad no-loop attribute

ClipCapIt-170903-191346.PNG

package com.nobleprog.npproj;
 
import java.lang.Number;
 
rule "Apply Tall Order Discount"
	dialect "mvel"
	no-loop true
	when
		$sc : ShoppingCart( $tp : totalPrice >= 10000.0B )
	then
		modify( $sc ) {
				setTotalPrice( $tp*0.9 )
		}
end
  • Correct test should look like that

ClipCapIt-170903-182204.PNG

Exercises

Exercise 1

  • to the shopping cart fact type add "discountApplied:Boolean" field,
  • set it to true after applying a discount
  • modify rule so it will work without no-loop or other attributes

Drools Kie - Decision Table

Applying Progressive Discounts

We want to apply discounts according to the table below:

0      -  9,999  -> 0%
10,000 -  49,999 -> 10%
50,000 -  79,999 -> 12%
80,000 -  above  -> 15%

Modification to previous scenario

  • In Data Objects -> ShoppingCart add a new field discount:Double

Guided Decision Table

  • Create New Asset -> Guided Decision Table
    • Name: Progressive Discount Table
    • Hit Policy - None

ClipCapIt-170905-132403.PNG

Exercises

Exercise 1

  1. Implement the decision table above
  2. Test if it works

Exercise 2

  1. Create another rule which would calculate totalPrice after discount has been applied

Exercise 3

  1. Add a condition to stop looping (2 solutions)

Applying Progressive Discounts - Source

  • View Source
package npou.npproj;

//from row number: 1
rule "Row 1 Progressive Decision Table"
	dialect "mvel"
	when
		$sc : ShoppingCart( totalPrice >= 80000.0B )
	then
		modify( $sc ) {
				setDiscount( 0.15B )
		}
end

//from row number: 2
rule "Row 2 Progressive Decision Table"
	dialect "mvel"
	when
		$sc : ShoppingCart( totalPrice >= 50000.0B , totalPrice < 80000.0B )
	then
		modify( $sc ) {
				setDiscount( 0.12B )
		}
end

//from row number: 3
rule "Row 3 Progressive Decision Table"
	dialect "mvel"
	when
		$sc : ShoppingCart( totalPrice >= 10000.0B , totalPrice < 50000.0B )
	then
		modify( $sc ) {
				setDiscount( 0.10B )
		}
end

//from row number: 4
rule "Row 4 Progressive Decision Table"
	dialect "mvel"
	when
		$sc : ShoppingCart( totalPrice < 10000B )
	then
		modify( $sc ) {
				setDiscount( 0B )
		}
end

Knowledge Session

Stateless Knowledge Session

  • A stateless session can be called like a function passing it some data and then receiving some results back.
  • Some common use cases for stateless sessions are, but not limited to:
    • Validation
      • Is this person eligible for a mortgage?
    • Calculation
      • Compute a mortgage premium.
    • Routing and Filtering
      • Filter incoming messages, such as emails, into folders.
  • Stateless sessions do not support iterative insertions and the method call fireAllRules()
  • The act of calling execute() is a single-shot method that will internally instantiate a KieSession, add all the user data and execute user commands, call fireAllRules(), and then call dispose()

Stateful Knowledge Session

  • Stateful Sessions are long lived and allow iterative changes over time.
  • Some common use cases for Stateful Sessions are, but not limited to:
    • Monitoring
      • Stock market monitoring and analysis for semi-automatic buying.
    • Diagnostics
      • Fault finding, medical diagnostics
    • Logistics
      • Parcel tracking and delivery provisioning
    • Compliance
      • Validation of legality for market trades.

kmodule.xml

The kmodule.xml file is the descriptor that selects resources to knowledge bases and configures those knowledge bases and sessions.

<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">

  <kbase name="kbase1">
    <ksession name="ksession1"/>
  </kbase>

  <kbase name="kbase2" packages="org.some.pkg">
    <ksession name="ksession2"/>
  </kbase>

  <kbase name="kbase3" includes="kbase2" packages="org.some.pkg2">
    <ksession name="ksession3"/>
  </kbase>

  <kbase name="kbase4" packages="org.some.pkg, org.other.pkg">
    <ksession name="ksession4"/>
  </kbase>

  <kbase name="kbase5" packages="org.*">
    <ksession name="ksession5"/>
  </kbase>

  <kbase name="kbase6" packages="org.some.*">
    <ksession name="ksession6"/>
  </kbase>

</kmodule>
  • 'kbase1' includes all resources from the KieModule.
  • The other KieBases include resources from other selected folders, via the 'packages' attribute.
  • Note the use wildcard '*' use, to select this package and all packages below it.