---
title: "Drools4：对Shopping示例的分析"
author: "hxpwork"
date: "2007-08-03T23:05:54Z"
tags: []
categories: ["企业架构"]
slug: "drools4-shopping"
draft: false
lastmod: "2007-08-03T23:05:54Z"
id: 108689
url: "https://hxpwork.iteye.com/blog/108689"
---

01. /*
02. Shopping:为订单内商品总金额大于100元的订单给与10%的折扣
03. 本例要点：
04. 1、语言Mvel和Java的切换
05. 在Package级别定义的是Mvel语言，而在"Apply..."规则中定义的是Java语言；
06. 两种语言的细节区别有很多，这里不详细说明。简单的说$c.name是Mvel语法，而$c.getName()则是Java语法
07. 2、使用了not这样的非存在性逻辑，留意"Discount..."这两个规则
08. 3、使用了from accumulate这种语法，accumulate的用法在手册中有详细说明
09. 4、使用了insertLogical方法
10. 这里留意的是"Apply..."规则插入了Discount对象，但是在代码和规则中都没有显式的删除Discount操作，
11. 那"Discount removed notification"规则为何可以激活呢？
12. 原因就在于insertLogical和session.retract( hatPurchaseHandle );这行代码的调用
13. retract删除的对象导致"Apply..."规则激活的条件变为无效，而Discount因为是insertLogical方法加入，
14. 在这种情况下自动被删除，导致了"Discount removed notification"规则激活
15. 思考：
16. 1、为何在执行时"Apply..."规则先于"Discount removed notification"规则调用？
17. 我们知道，Drools规则引擎对规则执行冲突的解决方案如下：
18. a) salience，拥有更高值的规则先激发
19. b) LIFO：后进先出，这里所谓的先后是指根据Fact对象的插入顺序引发规则来计数的，越晚插入的Fact引起的规则越早执行
20. c) 当LIFO顺序值相等时在Drl文件后面的规则先激发，这在Fibonacci例子中已经说明
21. 现在回过头来看一下"Apply..."规则和"Discount removed..."规则，
22. 激活"Apply..."规则的是最后的Purchase对象插入，因此按照LIFO原则"Apply..."规则先激发
23. 假设你定义一个测试类TestRete，并在"Discount removed..."规则的条件中增加TestRete()，
24. 你会发现当你将TestRete放在Purchase之后插入时，"Discount removed..."规则会先激发，这进一步验证了LIFO原则。
25. \*/

Fact 插入

01. Customer mark = new Customer( "mark",
02. 0 );
03. session.insert( mark );
04. Product shoes = new Product( "shoes",
05. 60 );
06. session.insert( shoes );
07. Product hat = new Product( "hat",
08. 60 );
09. session.insert( hat );
10. session.insert( new Purchase( mark,
11. shoes ) );
12. FactHandle hatPurchaseHandle = session.insert( new Purchase( mark,
13. hat ) );
14. session.fireAllRules();
15. session.retract( hatPurchaseHandle );
16. System.out.println( "Customer mark has returned the hat" );
17. session.fireAllRules();

规则

01. package org.drools.examples
02. # 定义Package中使用mvel，默认是使用java
03. dialect "mvel"
04. # 列出客户购买商品的情况
05. rule "Purchase notification"
06. salience 10
07. when
08. $c : Customer()
09. $p : Purchase( customer == $c)
10. then
11. System.out.println( "Customer " + $c.name + " just purchased " + $p.product.name );
12. end
13. # 当给与客户折扣时显示相关信息
14. rule "Discount awarded notification"
15. when
16. $c : Customer()
17. $d : Discount( customer == $c )
18. then
19. System.out.println( "Customer " + $c.name + " now has a discount of " + $d.amount );
20. end
21. # 当折扣取消时显示相关信息
22. rule "Discount removed notification"
23. when
24. $c : Customer()
25. not Discount( customer == $c )
26. then
27. $c.setDiscount( 0 );
28. System.out.println( "Customers " + $c.name + " now has a discount of " + $c.discount );
29. end
30. # 如果客户购买的商品超过100元给与折扣
31. rule "Apply 10% discount if total purcahses is over 100"
32. no-loop true
33. dialect "java"
34. when
35. $c : Customer()
36. $i : Double(doubleValue  &gt; 100) from accumulate ( Purchase( customer == $c, $price : product.price ),
37. sum( $price ) )
38. then
39. $c.setDiscount( 10 );
40. insertLogical( new Discount($c, 10) );
41. System.out.println( "Customer " + $c.getName() + " now has a shopping total of " + $i );
42. end