drools入门(二)-----规则引擎Drools解决汽水问题

喝汽水问题:
1元钱一瓶汽水,喝完后两个空瓶换一瓶汽水,问:你有50元钱,最多可以喝到几瓶汽水?(不能借空瓶,呵呵无商不奸)

 

answer:
1: 如果有超过一元钱, 则买一瓶汽水喝, 此时: 钱减少一元,同时拥有的空瓶增加一个。
2: 如果拥有至少两个空瓶, 则两个空瓶卖给老板兜里的钱加一(或是两个空瓶卖给老板换来一瓶汽水)。

SoftDrink.drl

#created on: 2011-10-18
package com.jsptpd.rjy.zyj.main
dialect "mvel"
#list any import classes here.
import com.jsptpd.rjy.zyj.main.SoftDrink.Customer

#declare any global variables here

rule "和了一瓶汽水"
    when
        $c:Customer(money>0,$m:money, $e:emptyBottle, $d :drinkBottleSum);
    then
        $c.money=$m-1;
        $c.emptyBottle=$e+1;
        $c.drinkBottleSum=$d+1;
        System.out.println($c.getName()+"有 "+$c.getMoney()+" 元, 有空瓶 "+$c.getEmptyBottle()+" 个, 喝了 "+$c.getDrinkBottleSum()+" 瓶。");
        update($c);
end

rule "用两个空瓶换了一瓶汽水"
    when
        $c:Customer(emptyBottle>1,$e:emptyBottle, $d:drinkBottleSum);
    then
        $c.emptyBottle=$e-2+1;
        $c.drinkBottleSum=$d+1;
         System.out.println($c.getName()+"有 "+$c.getMoney()+" 元, 有空瓶 "+$c.getEmptyBottle()+" 个, 喝了 "+$c.getDrinkBottleSum()+" 瓶。");
        update($c);
end

rule "结束"
    no-loop true	
    when
         $c:Customer(money<=0,emptyBottle<2);
    then
         System.out.println("计算结束:"+$c.getName()+"有 "+$c.getMoney()+" 元, 有空瓶 "+$c.getEmptyBottle()+" 个, 喝了 "+$c.getDrinkBottleSum()+" 瓶。");

end

SoftDrink.java

 

package com.jsptpd.rjy.zyj.main;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

public class SoftDrink {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			KnowledgeBase knowledgeBase = readKnowledgeBase();
			StatefulKnowledgeSession statefulKnowledgeSession = knowledgeBase
					.newStatefulKnowledgeSession();
			KnowledgeRuntimeLogger knowledgeRuntimeLogger = KnowledgeRuntimeLoggerFactory
					.newFileLogger(statefulKnowledgeSession, "test");
			Customer customer=new Customer("小黄", 50, 0, 0);
			statefulKnowledgeSession.insert(customer);
			statefulKnowledgeSession.fireAllRules();
			knowledgeRuntimeLogger.close();
			statefulKnowledgeSession.dispose();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		knowledgeBuilder.add(ResourceFactory
				.newClassPathResource("SoftDrink.drl"), ResourceType.DRL);
		KnowledgeBuilderErrors knowledgeBuilderErrors = knowledgeBuilder
				.getErrors();
		if (knowledgeBuilderErrors.size() > 0) {
			for (KnowledgeBuilderError knowledgeBuilderError : knowledgeBuilderErrors) {
				System.out.println(knowledgeBuilderError);
			}
			throw new IllegalArgumentException("KnowledgeBuilder创建失败!");
		}

		KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
		knowledgeBase.addKnowledgePackages(knowledgeBuilder
				.getKnowledgePackages());
		return knowledgeBase;
	}

	public static class Customer {
		private String name = "";
		private int money = 0;
		private int emptyBottle = 0;
		private int drinkBottleSum = 0;

		public Customer(String name, int money, int emptyBottle,
				int drinkBottleSum) {
			super();
			this.name = name;
			this.money = money;
			this.emptyBottle = emptyBottle;
			this.drinkBottleSum = drinkBottleSum;
		}

		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 getEmptyBottle() {
			return emptyBottle;
		}

		public void setEmptyBottle(int emptyBottle) {
			this.emptyBottle = emptyBottle;
		}

		public int getDrinkBottleSum() {
			return drinkBottleSum;
		}

		public void setDrinkBottleSum(int drinkBottleSum) {
			this.drinkBottleSum = drinkBottleSum;
		}
	}
}

 

运行结果:

....

....

 

小黄有 2 元, 有空瓶 2 个, 喝了 94 瓶。

小黄有 2 元, 有空瓶 1 个, 喝了 95 瓶。

小黄有 1 元, 有空瓶 2 个, 喝了 96 瓶。

小黄有 1 元, 有空瓶 1 个, 喝了 97 瓶。

小黄有 0 元, 有空瓶 2 个, 喝了 98 瓶。

小黄有 0 元, 有空瓶 1 个, 喝了 99 瓶。

计算结束:小黄有 0 元, 有空瓶 1 个, 喝了 99 瓶。

 

 

 

---------------------------------------------------------------------------------------------------------------------------------------- 

附件直接导入Eclipse中运行.

附件drl文件有java实现,这个是mvel实现。例外还有个重写的。

 

 

 

你可能感兴趣的:(Drools,drools实例)