Drools Kie - Example

From Training Material
Jump to navigation Jump to search
title
Drools Kie
author
Bernard Szlachta (NobleProg Ltd)

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⌘。

Jbpm orgaizing konwoledge.png

  1. Creating Organizational Unit
    1. Authoring/Administration
    2. Organisation Units/Manage Organisation Units/Add
    3. Name: npou / Owner: NobleProg Ltd
  2. Creating Repository
    1. Authoring/Administration
    2. Repositories/New Repository
    3. Repository Name: nprep/Organisational Unit: npou
  3. Creating Project
    1. Authoring/ Project Authoring
    2. New Item/Project
    3. Resource Name: npproj1
    4. 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
ClipCapIt-160823-145024.PNG
  • Create new field 新建 Field
    • Id: totalPrice
    • Label: Total Price
    • Type: BigDecimal
DroolsNewField.png
  • 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 输入下面的信息

ApplyDiscountRule.png

  • 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

ApplyDiscountTestPlan.png

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元

ApplyDiscountRuleRecusion.png

No-Loop Attribute ⌘

We need to add no-loop attribute
添加 no-loop 属性

ApplyDiscoutRuleRecursiveNoLoop.png

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
测试通过,会显示

ApplyDiscountRecursiveTest.png

Exercises ⌘。

Exercise 1.4

  1. to the shopping cart fact type add "discountRate" field,
    在购物车 fact type 里增加 "discountRate" field
  2. add rule to populate it accordingly rather than change totalPrice field directly
    添加新计算totalPrice的规则,不要直接改变totalPrice field
  3. create a test 添加测试

Exercise 1.5

  1. to the shopping cart fact type add "totalPriceAfterDiscount" field,
    在购物车 fact type 里增加 "totalPriceAfterDiscount" field
  2. add rule to calculate total price
    添加计算总价格的规则
  3. 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 讨论

Next Module

Drools Kie - Decision Table