Drools Kie - Decision Table

From Training Material
Jump to navigation Jump to search
title
Drools Guvnor - Decision Table
author
Bernard Szlachta (NobleProg Ltd)

Drools Guvnor - Decision Table


Presentation and Exercises ⌘。

  1. Instructor does it himself and explains the steps
  2. The group does the exercise with the instructor

ShoopingCart Fact Type (from previous exercises) ⌘。

 package nppack;
 public class ShoppingCart implements java.io.Serializable
 {
   static final long serialVersionUID = 1L;
    @org.kie.api.definition.type.Label("Total Price")
    private java.math.BigDecimal totalPrice;  
 
   @org.kie.api.definition.type.Label("DiscountRate")
   private java.math.BigDecimal discountRate;

   @org.kie.api.definition.type.Label(value = "Total Price After Discount")
   private java.math.BigDecimal totalPriceAfterDiscount;

   public ShoppingCart()
   {
   }

   public java.math.BigDecimal getTotalPrice()
   {
      return this.totalPrice;
   }

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

   public java.math.BigDecimal getDiscountRate()
   {
      return this.discountRate;
   }

   public void setDiscountRate(java.math.BigDecimal discountRate)
   {
      this.discountRate = discountRate;
   }

   public java.math.BigDecimal getTotalPriceAfterDiscount()
   {
      return this.totalPriceAfterDiscount;
   }

   public void setTotalPriceAfterDiscount(
         java.math.BigDecimal totalPriceAfterDiscount)
   {
      this.totalPriceAfterDiscount = totalPriceAfterDiscount;
   }

   public ShoppingCart(java.math.BigDecimal totalPrice,
         java.math.BigDecimal discountRate,
         java.math.BigDecimal totalPriceAfterDiscount)
   {
      this.totalPrice = totalPrice;
      this.discountRate = discountRate;
      this.totalPriceAfterDiscount = totalPriceAfterDiscount;
   }
 }

Modification to previous scenario ⌘。

  1. Copy previous project to npproj2
  2. Make sure that the Group ID is nppack

Applying Progressive Discounts 应用累进折扣 - 决策表。⌘

We want to apply discounts according to the table below 根据下表应用折扣:

0           -  10,000  -> 0%
10,000(inc) -  50,000  -> 10%
50,000(inc) -  80,000  -> 12%
80,000(inc)  -  above  -> 15%
  • New Item -> Guided Decision Table
    • Name: Progressive Discount Table
ClipCapIt-160823-114756.PNG

Applying Progressive Discounts - Source 应用分段折扣 - 源码 。⌘

  • View Source
 package nppack;

 //from row number: 1
 rule "Row 1 Progressive Discount Table"
 	dialect "mvel"
 	when
 		$sc : ShoppingCart( totalPrice >= 10000B , totalPrice < 50000B )
 	then
 		modify( $sc ) {
 				setDiscountRate( 0.1B )
 		}
 end
 
 //from row number: 2
 rule "Row 2 Progressive Discount Table"
 	dialect "mvel"
 	when
 		$sc : ShoppingCart( totalPrice >= 50000B , totalPrice < 80000B )
 	then
 		modify( $sc ) {
 				setDiscountRate( 0.12B )
 		}
 end
 
 //from row number: 3
 rule "Row 3 Progressive Discount Table"
 	dialect "mvel"
	when
 		$sc : ShoppingCart( totalPrice >= 80000B )
 	then
		modify( $sc ) {
 				setDiscountRate( 0.15B )
 		}
 end

Creating Test 。⌘

ClipCapIt-160823-123006.PNG

Problem with recurrence 。⌘

ClipCapIt-160831-111552.PNG

Stop condition for rules 。⌘

package nppack;
dialect "mvel"
rule "Row 1 Progressive Discount Table"
	when
		$sc : ShoppingCart( totalPrice >= 10000B , totalPrice < 50000B , discountRate == null )
	then
		modify( $sc ) {	setDiscountRate( 0.1B ) }
end

rule "Row 2 Progressive Discount Table"
	when
		$sc : ShoppingCart( totalPrice >= 50000B , totalPrice < 80000B , discountRate == null )
	then
		modify( $sc ) {	setDiscountRate( 0.12B ) }
end

rule "Row 3 Progressive Discount Table"
	when
		$sc : ShoppingCart( totalPrice >= 80000B , discountRate == null )
	then
		modify( $sc ) {	setDiscountRate( 0.15B ) }
end

Calculating Total Price rule

package nppack;

import java.lang.Number;

rule "Calculate total price"
	dialect "mvel"
	when
		$sc : ShoppingCart(discountRate != null, priceAfterDiscount == null)
	then
		modify( $sc ) {
				setPriceAfterDiscount( $sc.totalPrice*(1-$sc.discountRate) ),
		}
end


Testing total Price

ClipCapIt-160831-111709.PNG

Exercises 3.1 ⌘

  1. Implement the decision table above 实现上面的决策表

Exercises 3.2 ⌘

  1. Create another rule which would calculate totalPrice after discount has been applied 建立一个规则,用以计算打折以后的 totalPrice

Exercise 3.3 (optional)* ⌘

  1. Add a field "Loyal Customer" in the shopping cart fact type and implement decision table, when if the customer is loyal customer, they have extra 1% off
  2. 在购物车的fact type里增加一个field "Loyal Customer" ,并且实现决策表, 当客户是loyal customer的时候, 他们再得到 1% 折扣

Next Module

Drools Kie - Rule Templates