前提准备:java 虚拟机
Drools核心库
Junit4.0以上版本
规则Excel模板表(将下面的图片的内容新建到Excel中)
典型的用法就是根据excel创建KnowledgeBase,然后将它丢给session执行,执行的参数和结果都在params里面
1 package com.xxx.yyyy;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.InputStream;
6 import java.util.Arrays;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.Map;
10 import junit.framework.Assert;
11 import org.drools.KnowledgeBase;
12 import org.drools.KnowledgeBaseFactory;
13 import org.drools.builder.DecisionTableConfiguration;
14 import org.drools.builder.DecisionTableInputType;
15 import org.drools.builder.KnowledgeBuilder;
16 import org.drools.builder.KnowledgeBuilderFactory;
17 import org.drools.builder.ResourceType;
18 import org.drools.definition.KnowledgePackage;
19 import org.drools.io.ResourceFactory;
20 import org.drools.runtime.StatelessKnowledgeSession;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.Parameterized;
24 import org.junit.runners.Parameterized.Parameters;
25
26 @RunWith(Parameterized.class)
27 public class IsP4PTest
28 {
29 private IsP4P param;
30
31 private String extected;
32
33 public IsP4PTest(IsP4P param, String extected)
34 {
35 this.param = param;
36 this.extected = extected;
37 }
38 public Map<String, Object> getParams()
39 {
40 Map<String, Object> params = new HashMap<String, Object>();
41
42 params.put("productLine", productLine);
43 params.put("productType", productType);
44 params.put("playType", playType);
45
46 return params;
47 }
48 public void testExcel(String fileName, Map<String, Object> params)
49 throws Exception
50 {
51 System.out.println("---------------begin------------------------");
52
53 DecisionTableConfiguration dtableconfiguration = KnowledgeBuilderFactory
54 .newDecisionTableConfiguration();
55 dtableconfiguration.setInputType(DecisionTableInputType.XLS);
56 final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
57 .newKnowledgeBuilder();
58 File file = new File(
59 "F:\\juny.qu\\rule_workspace\\com.deppon.rules\\src\\main\\java\\com\\xxx\\yyyy\\"
60 + fileName);
61 InputStream is = new FileInputStream(file);
62 kbuilder.add(ResourceFactory.newInputStreamResource(is, "UTF-8"),
63 ResourceType.DTABLE);
64 if (kbuilder.hasErrors())
65 {
66 System.out.println(kbuilder.getErrors().toString());
67 }
68 Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
69 KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
70 kbase.addKnowledgePackages(pkgs);
71
72 StatelessKnowledgeSession ksession = kbase
73 .newStatelessKnowledgeSession();
74 ksession.execute(Arrays.asList(new Object[] { params }));
75
76 System.out.println("---------------end------------------------");
77 }
78
79
80 @Parameters
81 public static Collection<?> contructData()
82 {
83 return Arrays.asList(new Object[][] {
84 { new IsP4P("搜索推广", "true", "null"), "true" },
85 { new IsP4P("网盟推广", "true", "null"), "true" },
86 { new IsP4P("掘金推广", "true", "非轮播"), "false" },
87 { new IsP4P("其他", "false", "其他"), "other" } });
88 }
89
90 @Test
91 public void testP4P() throws Exception
92 {
93 Map<String, Object> params = new HashMap<String, Object>();
94 params.putAll(param.getParams());
95 testExcel("ka/isP4P.xls", params);
96 Assert.assertEquals(extected, params.get("isP4P"));
97 }
98 }
99
100