您的位置:首页 > 其它

Drools规则入门程序:喝汽水问题(^_^)

2010-11-17 14:58 453 查看

Drools规则入门程序:小学生喝汽水问题

上一次将Drools的开发环境配置好之后,我们这一篇用一个简单的问题来入门。
首先来看问题:
1、小明的钱可以购买50瓶汽水。
2、老板搞促销,两个空汽水瓶子可以换一瓶汽水。
问:小明最多能喝多少瓶汽水?

思路一:

先是50瓶,然后用50个空瓶换来25瓶,喝完后用25个空瓶换12瓶再多个空瓶,

喝完后用13个空瓶换6瓶,然后是7个空瓶换3瓶,然后是4个空瓶换2瓶,
然后是2个空瓶换1瓶,最后问老板借1个空瓶,再用2个空瓶换1瓶,
剩下来那个空瓶还给老板。所以喝到50+25+12+6+3+2+1+1=100

思路二:

50块买50瓶汽水,全部喝掉后,得到50个空瓶子。

跟老板借50个空瓶子+自己的50个=100个空瓶子

100个空瓶子再换50瓶汽水。50+50=100.

(可怜的小明...)

我们采用drools引擎规则来模拟一下思路一:

代码清单:

Customer.java

package com.pccw.drools;
public class Customer {
private String name;
private int money;
private int drinkSum;
private int blankCup;
public Customer(String name, int money) {
super();
this.name = name;
this.money = money;
this.drinkSum=0;
this.blankCup =0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getDrinkSum() {
return drinkSum;
}
public void setDrinkSum(int drinkSum) {
this.drinkSum = drinkSum;
}
public int getBlankCup() {
return blankCup;
}
public void setBlankCup(int blankCup) {
this.blankCup = blankCup;
}

}


SodaWater.drl

#created on: 2010-11-17
package com.sodawater
dialect "mvel"
import com.pccw.drools.Customer;
#当钱大于0时,买汽水喝掉,喝的总数+1,并且空瓶数量+1.
rule "buy a soda water and drink"

when
$c: Customer(money > 0,$m:money,$b:blankCup,$d:drinkSum)
then
$c.money = $m-1;
$c.blankCup = $b+1;
$c.drinkSum = $d+1;
System.out.println("Customer "+$c.name+" now buy a soda water and drink:money="+$c.money+" and blankCup="+$c.blankCup);
update($c);
end
#如果空瓶数量>1,2个空瓶=1瓶汽水=1元,钱总数+1,继续上面的规则
rule "sale blank cup and get money"
when
$c : Customer(blankCup>1,$b:blankCup,$m:money)
then
$c.blankCup = $b-2;
$c.money = $m+1;
System.out.println("Customer " + $c.name + " now sale 2 cups and get money: money=" + $c.money + ", blankCup=" + $c.blankCup);
update($c);
end
#空瓶数量<2时,结束流程
rule "finish drink"
no-loop true
dialect "java"
when
$c:Customer(blankCup<2,money==0)
then
System.out.println( "Customer " + $c.getName() + " finished drink, and drink number is " + $c.getDrinkSum() + " blankCup=" + $c.getBlankCup());
end
#最后剩了1个空瓶,计算机不会借瓶子! -_-!。


KnowledgeBaseUtil.java (创建KnowledgeBase的工具类)

package com.pccw.util;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
public class KnowledgeBaseUtil {
public static KnowledgeBase createKnowledgeBase(String fileName){
/**KnowledgeBuilder用来在业务代码中收集已经编写好的规则
* 	 然后对这些规则进行编译,产生一批编译好的规则包:KnowledgePackage
*/
KnowledgeBuilder  builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder.add(ResourceFactory.newClassPathResource(fileName), ResourceType.DRL);
/**
* 编译的过程中可以通过hasErrors()方法得到编译规则过程中发现规则是否有错误
* 如果有错误,可以通过getErrors()得到错误信息
*/
if(builder.hasErrors()){
throw new RuntimeException(builder.getErrors().toString());
}
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(builder.getKnowledgePackages());
return knowledgeBase;
}
}


SodaWater.java 测试类

package com.pccw.drools;
import java.io.IOException;
import java.io.InputStreamReader;
import org.drools.KnowledgeBase;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.runtime.StatefulKnowledgeSession;
import com.pccw.util.KnowledgeBaseUtil;
public class SodaWater {
public static void main(String[] args) {
KnowledgeBase knowledgeBase = KnowledgeBaseUtil.createKnowledgeBase("SodaWater.drl");
StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
Customer customer = new Customer("XiaoMing",50);
session.insert(customer);
session.fireAllRules();
}
}


运行结果:

Customer XiaoMing now buy a soda water and drink:money=49 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=48 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=49, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=48 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=47 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=48, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=47 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=46 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=47, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=46 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=45 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=46, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=45 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=44 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=45, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=44 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=43 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=44, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=43 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=42 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=43, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=42 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=41 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=42, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=41 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=40 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=41, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=40 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=39 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=40, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=39 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=38 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=39, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=38 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=37 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=38, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=37 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=36 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=37, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=36 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=35 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=36, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=35 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=34 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=35, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=34 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=33 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=34, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=33 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=32 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=33, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=32 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=31 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=32, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=31 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=30 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=31, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=30 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=29 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=30, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=29 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=28 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=29, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=28 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=27 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=28, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=27 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=26 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=27, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=26 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=25 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=26, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=25 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=24 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=25, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=24 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=23 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=24, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=23 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=22 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=23, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=22 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=21 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=22, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=21 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=20 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=21, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=20 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=19 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=20, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=19 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=18 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=19, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=18 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=17 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=18, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=17 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=16 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=17, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=16 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=15 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=16, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=15 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=14 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=15, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=14 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=13 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=14, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=13 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=12 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=13, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=12 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=11 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=12, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=11 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=10 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=11, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=10 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=9 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=10, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=9 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=8 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=9, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=8 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=7 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=8, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=7 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=6 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=7, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=6 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=5 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=6, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=5 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=4 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=5, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=4 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=3 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=4, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=3 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=2 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=3, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=2 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=1 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=2, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=1 and blankCup=1
Customer XiaoMing now buy a soda water and drink:money=0 and blankCup=2
Customer XiaoMing now sale 2 cups and get money: money=1, blankCup=0
Customer XiaoMing now buy a soda water and drink:money=0 and blankCup=1
Customer XiaoMing finished drink, and drink number is 99 blankCup=1


参考文档:

http://lcllcl987.javaeye.com/blog/257039
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: