📄 shopping.drl
字号:
#created on: 2009-3-3
package org.drools.hu.test
dialect "mvel" //定义Package中使用mvel,默认是使用java mvel是一种java的表达式语言
import org.drools.hu.test.Shopping.Customer
import org.drools.hu.test.Shopping.Purchase
import org.drools.hu.test.Shopping.Discount
import org.drools.hu.test.Shopping.Product
/* shopping例子中rules的执行顺序为
1. Purchase notification salience值越高的规则先激发
2. Apply 10% discount if total purcahses is over 100
LIFO: 后进先出,这里所谓的先后是指根据Fact对象的插入顺序引发规则来计数的,
越晚插入的Fact引起的规则越早执行
当LIFO顺序值相等时在Drl文件后面的规则先激发
3. Discount awarded notification
4. Discount removed notification
*/
//列出客户购买商品的情况
rule "Purchase notification"
salience 10 //优先级数字高的规则会比优先级低的规则先执行。
when
$c:Customer()
$p:Purchase(customer==$c)
then
System.out.println( "Customer " + $c.name+ "just purchased " + $p.product.name );
end
//当折扣取消时显示相关信息
rule "Discount removed notification"
when
$c : Customer()
not Discount( customer == $c )
then
$c.discount = 0 ;
System.out.println( "Customer " + $c.name + " now has a discount of " + $c.discount );
end
//当给与客户折扣时显示相关信息
rule "Discount awarded notification"
when
$c : Customer()
$d : Discount( customer == $c )
then
System.out.println( "Customer " + $c.name + " now has a discount of --" + $d.amount );
end
rule "Apply 10% discount if total purcahses is over 100"
no-loop true
dialect "java"
when
$c : Customer()
$i : Double(doubleValue > 100) from accumulate (Purchase( customer == $c, $price : product.price ),sum( $price ) )
then
$c.setDiscount( 10 );
insertLogical( new Discount($c, 10) ); //与insert类似,但是当没有更多的fact支持当前激发规则的真值状态时,对象自动删除
System.out.println( "Customer " + $c.getName() + " now has a shopping total of " + $i );
end
/* accumulate条件元素常用语法:
<result pattern> from accumulate( <source pattern>,
init( <init code> ),
action( <action code> ),
reverse( <reverse code> ),
result( <result expression> ) ) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -