Drools Kie - Example
Jump to navigation
Jump to search
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.
Resource Structure⌘。
- Organisation Unit(access control, 管理用户的访问权限)
- Repository 1 (physical storage)
- Repository 2
- Project 1(build)
- Project 2
- Package 1(dependencies)
- Package 2
- Rule 1
- Process 1
- Data Object 1
- Test 1
Creating Organization Unit, repository, project and package⌘。
- Creating Organizational Unit
- Authoring/Administration
- Organisation Units/Manage Organisation Units/Add
- Name: npou / Owner: NobleProg Ltd
- Creating Repository
- Authoring/Administration
- Repositories/New Repository
- Repository Name: nprep/Organisational Unit: npou
- Creating Project
- Authoring/ Project Authoring
- New Item/Project
- Resource Name: npproj1
- Group ID: nppack <- THIS IS VERY IMPORTANT
Hello World ⌘。
- New Item -> New DRL file
- Name: Hello World
- Package: nppack
- Paste 粘贴
rule "Hello World" when eval(true) then System.out.println("Hello world"); end
- Save the rule 保存规则
- Testing Hello World Rule 测试规则
- New Item -> Test Scenario
- Name: Hello World Test
- Check the log files 查看日志
- e.g. 例如
cd /opt/jbpm-installer/wildfly-8.2.1.Final/standalone/log tail server.log -f
Apply Discount 打折
Creating a Fact Type 新建一个 Fact Type (Data Object) ⌘。
MAKE SURE YOU ARE IN npou/nprep/npproj1
- New Item/Data Object
- Identifier ShoppingCart
- Label: Shopping Cart
- Package: nppack
- Create new field 新建 Field
- Id: totalPrice
- Label: Total Price
- Type: BigDecimal
- Save (upper right corner) 保存 (右上角)
Looking at generated POJO ⌘。
- Source
package nppack;
/**
* 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 java.math.BigDecimal getTotalPrice()
{
return this.totalPrice;
}
public void setTotalPrice(java.math.BigDecimal totalPrice)
{
this.totalPrice = totalPrice;
}
public ShoppingCart(java.math.BigDecimal totalPrice)
{
this.totalPrice = totalPrice;
}
}
Creating a Rule 新建一个规则 。⌘
- Rule: If customer buys more than 10k, apply 10% discount
- 规则:如果客户买的东西超过一万元,打九折
- New Item -> Guided Rule
- Resource Name: Apply Tall Order Discount
- Enter details as below 输入下面的信息
- View Source should return 查看源代码
package nppack; import java.lang.Number; rule "Apply Tall Order Discount" dialect "mvel" when $sc : ShoppingCart( $tp : totalPrice >= 10000B ) then $sc.setTotalPrice( $tp*0.9 ); update( $sc ); end
Creating a Test Plan 。⌘
- New Item-> Test Scenario
- Resource Name:Apply Discount Test
- Run Scenario
Dealing with Recursion 递归 。⌘
- Let us try to add a test for order value of 15000 and set EXPECT section to 13500
- 试着为价格是15000元的订单添加一个test,并设定它的EXPECT为13500元
No-Loop Attribute ⌘
We need to add no-loop attribute
添加 no-loop 属性
package nppack;
import java.lang.Number;
rule "Apply Tall Order Discount"
no-loop true <-------
dialect "mvel"
when
$sc : ShoppingCart( $tp : totalPrice <= 10000B )
then
modify( $sc ) {
setTotalPrice( $tp*0.9 )
}
end
- Test correct test should look like that
- 测试通过,会显示
Exercises ⌘。
Exercise 1.4
- to the shopping cart fact type add "discountRate" field,
在购物车 fact type 里增加 "discountRate" field - add rule to populate it accordingly rather than change totalPrice field directly
添加新计算totalPrice的规则,不要直接改变totalPrice field - create a test 添加测试
Exercise 1.5
- to the shopping cart fact type add "totalPriceAfterDiscount" field,
在购物车 fact type 里增加 "totalPriceAfterDiscount" field - add rule to calculate total price
添加计算总价格的规则 - add a test 添加测试
Exercise 1.6 (optional - don't do it unless you ask to)
- to the shopping cart fact type add "discountApplied:Boolean" field,
在购物车 fact type 里增加 "discountApplied:Boolean" field, - set it to true after applying a discount
在应用折扣后设定为 true - modify rule so it will work without no-loop or other attributes
修改规则使得规则在没有no-loop或其他属性时一样工作 - discuss why 讨论