- hxpwork
- 等级:
- 性别:
- 文章: 38
- 积分: 460
- 来自: 广州
|
Fact 插入
- Customer mark = new Customer( "mark",
- 0 );
- session.insert( mark );
-
- Product shoes = new Product( "shoes",
- 60 );
- session.insert( shoes );
-
- Product hat = new Product( "hat",
- 60 );
- session.insert( hat );
-
- session.insert( new Purchase( mark,
- shoes ) );
- FactHandle hatPurchaseHandle = session.insert( new Purchase( mark,
- hat ) );
-
- session.fireAllRules();
-
- session.retract( hatPurchaseHandle );
- System.out.println( "Customer mark has returned the hat" );
- session.fireAllRules();
规则
- package org.drools.examples
-
- # 定义Package中使用mvel,默认是使用java
- dialect "mvel"
-
- # 列出客户购买商品的情况
- 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 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 "Discount removed notification"
- when
- $c : Customer()
- not Discount( customer == $c )
- then
- $c.setDiscount( 0 );
- System.out.println( "Customers " + $c.name + " now has a discount of " + $c.discount );
- end
-
- # 如果客户购买的商品超过100元给与折扣
- 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) );
- System.out.println( "Customer " + $c.getName() + " now has a shopping total of " + $i );
- end
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- john_summer
- 等级: 初级会员
- 性别:
- 文章: 1
- 积分: 30
- 来自: 广西
|
发现一个问题,我分析了很久也没有结果,麻烦各位大虾帮想想 如果把两个Product的价格都改为160,在retract之后,居然也激活了"Discount removed notification"了这个规则,但是此时"Apply 10% discount if total purcahses is over 100"的条件依然是满足的呀,insertLogical进去的Discout对象不应该被删除的呀,很是奇怪,麻烦各位帮解答一下,谢谢了
|
返回顶楼 |
|
|