Drools Kie - Decision Table
Jump to navigation
Jump to search
Drools Guvnor - Decision Table
Copyright Notice
Copyright © 2004-2023 by NobleProg Limited All rights reserved.
This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
Presentation and Exercises ⌘。
- Instructor does it himself and explains the steps
- 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 ⌘。
- Copy previous project to npproj2
- 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
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 。⌘
Problem with recurrence 。⌘
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
Exercises 3.1 ⌘
- Implement the decision table above 实现上面的决策表
Exercises 3.2 ⌘
- Create another rule which would calculate totalPrice after discount has been applied 建立一个规则,用以计算打折以后的 totalPrice
Exercise 3.3 (optional)* ⌘
- 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
- 在购物车的fact type里增加一个field "Loyal Customer" ,并且实现决策表, 当客户是loyal customer的时候, 他们再得到 1% 折扣