请教Drools怎么处理超市打折促销

abirdman 2013-11-22
鄙人刚刚开始研究Drools,但看了些例子,仍然不知道怎么处理下面的业务:
超市打折促销,某种商品(按照类别code)促销规则:
   1.在指定日期区间内(比如1月1日-1月3日),
   2.买第1件按原价,第2件则价格打6折。第3件原价,第4件打6折... ...

现在顾客推着购物车来消费,里面有很多种商品,其中上面促销的那种有5件,结果应该是3件原价,两件要打折。

哪位大大提供个思路,怎么用drools来给顾客的购物票算总额。
asialee 2013-11-24
我好久没用drools,看见没人回复,我就翻了下原来的代码,希望能帮助到你。
测试父类
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;

public class KATestBase {
	public void testExcel(String fileName, Map<String, Object> params)
			throws Exception {
		System.out.println("---------------begin------------------------");
		
		DecisionTableConfiguration dtableconfiguration = KnowledgeBuilderFactory
				.newDecisionTableConfiguration();
		dtableconfiguration.setInputType(DecisionTableInputType.XLS);
		final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		File file = new File("E:\\workspace\\epct\\rule\\src\\test\\resources\\"
				+ fileName);
		InputStream is = new FileInputStream(file);

		//InputStream is = new ClassPathResource(fileName).getInputStream();
		kbuilder.add(ResourceFactory.newInputStreamResource(is,"UTF-8"),
				ResourceType.DTABLE);
		if (kbuilder.hasErrors()) {
			System.out.println(kbuilder.getErrors().toString());
		}
		Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(pkgs);

		StatelessKnowledgeSession ksession = kbase
				.newStatelessKnowledgeSession();
		ksession.execute(Arrays.asList(new Object[] {params}));
		
		System.out.println("---------------end------------------------");
	}


测试子类:

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)  
public class DroolsTest extends KATestBase{
	 private KADest param;  
	 private String extected;  
	      
	 public DroolsTest(KADest param,String extected) {  
	        this.param = param;  
	        this.extected = extected;  
	 }  
	    
	public static class KADest {
		private String orderTime;
		private String orderTotal;
		
		public KADest(String orderTime, String orderTotal) {
			this.orderTime = orderTime;
			this.orderTotal = orderTotal;
		}
		
		public Map<String,Object> getParams(){
			Map<String,Object> params = new HashMap<String,Object>();
			
			
			params.put("orderTime", orderTime);
			params.put("orderTotal", orderTotal);
			params.put("discountTotal", 0);
			
			return params;
		}
	} 
      
    @Parameters
    public static Collection<?> contructData(){  
        return Arrays.asList(new Object[][]{  
                {new KADest("2013-01-02 12:30:31","6"),"3"},
                {new KADest("2013-01-02 12:30:31","1"),"0"},
                {new KADest("2013-01-02 12:30:31","2"),"1"},
                {new KADest("2013-01-01 12:30:31","2"),"1"},
                {new KADest("2013-01-01 12:30:31","2"),"1"},
                {new KADest("2013-01-01 12:30:31","2"),"1"},
                {new KADest("2013-01-04 12:30:31","2"),"0"},
                {new KADest("2013-01-04 12:30:31","2"),"0"},
                {new KADest("2013-01-04 12:30:31","2"),"0"},
               });
    }  
    
    @Test
    public void testKaDest() throws Exception{
    	Map<String,Object> params = new HashMap<String,Object>();
    	params.putAll(param.getParams());
    	System.out.println(params);
    	super.testExcel("ka/drools.xls", params);
    	System.out.println(params);
        Assert.assertEquals(extected, "" +params.get("discountTotal"));  
    }


asialee 2013-11-24
完了,这个貌似上传不了文件,那我写一篇博客吧。
asialee 2013-11-24
详细请看我的博客,我帮你解决了下:http://asialee.iteye.com/blog/1980004
Global site tag (gtag.js) - Google Analytics