花落成泥

学习是进步的源泉

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  1 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks
转自http://www.iteye.com/topic/257039

同学帮他侄儿问我一个问题:
资源:
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

不过最近在学习规则引擎, 于是就想到了怎么用规则引擎来实现。
我的想法是:
1: 假设汽水一元一瓶, 则小明最初有50元。
2: 如果兜里有超过一元钱, 则买一瓶汽水喝, 此时:
钱减少一元,同时拥有的空瓶增加一个。
3: 如果拥有至少两个空瓶, 则两个空瓶卖给老板, 兜里的钱加一。
根据如上想法, 有了如下规则引擎的实现的实现(Drool):

java文件SodaWater.java:

Java代码 复制代码 收藏代码
  1. package org.drools.examples; 
  2.  
  3. import java.io.InputStreamReader; 
  4. import java.io.Reader; 
  5.  
  6. import org.drools.FactHandle; 
  7. import org.drools.RuleBase; 
  8. import org.drools.RuleBaseFactory; 
  9. import org.drools.StatefulSession; 
  10. import org.drools.WorkingMemory; 
  11. import org.drools.compiler.PackageBuilder; 
  12. import org.drools.rule.Package; 
  13.  
  14. public class SodaWater 
  15.     public static final void main(String[] args) throws Exception { 
  16.         final PackageBuilder builder = new PackageBuilder(); 
  17.         builder.addPackageFromDrl( new InputStreamReader( SodaWater.class.getResourceAsStream( "SodaWater.drl" ) ) ); 
  18.  
  19.         final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); 
  20. ruleBase.addPackage( builder.getPackage() );
  21.  
  22.         final StatefulSession session = ruleBase.newStatefulSession(); 
  23.  
  24.         Customer customer = new Customer( "XiaoMing", 50); 
  25.         session.insert(customer); 
  26.         session.fireAllRules(); 
  27.     } 
  28.     
  29.     public static class Customer 
  30.     { 
  31.         private String name; 
  32.         
  33.         private int money; 
  34.         
  35.         private int drinkSum; 
  36.         
  37.         private int blankCup; 
  38.         
  39.         public Customer(String name, int money) 
  40.         { 
  41.             this.name = name; 
  42.             this.money = money; 
  43.             this.drinkSum = 0
  44.             this.blankCup = 0
  45.         }        
  46.  
  47.         public int getMoney() 
  48.         { 
  49.             return money; 
  50.         } 
  51.         
  52.         public void setMoney(int money) 
  53.         { 
  54.             this.money = money; 
  55.         } 
  56.         
  57.         public int getBlankCup() 
  58.         { 
  59.             return blankCup; 
  60.         } 
  61.  
  62.         public void setBlankCup(int blankCup) 
  63.         { 
  64.             this.blankCup = blankCup; 
  65.         } 
  66.  
  67.         public int getDrinkSum() 
  68.         { 
  69.             return drinkSum; 
  70.         } 
  71.  
  72.         public void setDrinkSum(int drinkSum) 
  73.         { 
  74.             this.drinkSum = drinkSum; 
  75.         } 
  76.  
  77.         public String getName() 
  78.         { 
  79.             return name; 
  80.         } 
  81.  
  82.         public void setName(String name) 
  83.         { 
  84.             this.name = name; 
  85.         }       
  86.         
  87.     } 



rule文件:

Sql代码 复制代码 收藏代码
  1. package org.drools.examples 
  2.  
  3. dialect "mvel" 
  4.  
  5. import org.drools.examples.SodaWater.Customer 
  6.   
  7.  
  8. rule "buy a soda water and drink" 
  9.     when 
  10.         $c : Customer(money > 0, $m:money, $b:blankCup, $d:drinkSum) 
  11.     then 
  12.         $c.money = $m - 1; 
  13.         $c.blankCup = $b + 1; 
  14.         $c.drinkSum = $d + 1; 
  15.         System.out.println( "Customer " + $c.name + " now buy a soda water and drink: money=" + $c.money + " and blankCup=" + $c.blankCup); 
  16.         update($c); 
  17. end 
  18.  
  19. rule "sale blank cup and get money" 
  20.     when 
  21.         $c : Customer(blankCup > 1, $b:blankCup, $m:money ) 
  22.     then 
  23.         $c.blankCup = $b - 2; 
  24.         $c.money = $m + 1; 
  25.         System.out.println("Customer " + $c.name + " now sale 2 cups and get money: money=" + $c.money + ", blankCup=" + $c.blankCup); 
  26.         update($c); 
  27. end 
  28.  
  29. rule "finish drink"            
  30.     no-loop true    
  31.     dialect "java" 
  32.     when 
  33.         $c : Customer(blankCup < 2, money == 0) 
  34.     then 
  35.         System.out.println( "Customer " + $c.getName() + " finished drink, and drink number is " + $c.getDrinkSum() + " blankCup=" + $c.getBlankCup()); 
  36. end 



运行结果如下:
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 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

不过小明最后手上还有最后一个瓶子, 计算机只能到此了。
向老板借一个瓶子换一瓶汽水,喝完在还一个空瓶子,估计就是人类智慧来。

此程序需要drools的jar包,具体可见:

http://lcllcl987.iteye.com/blog/254381


看看大家还有什么好的算法。


posted on 2011-09-02 14:39 WYH-THINK 阅读(161) 评论(0)  编辑  收藏 所属分类: 规则引擎

只有注册用户登录后才能发表评论。


网站导航: