前提准备: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
1 package com.xxx.yyyy;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.InputStream;
7
8 import org.drools.decisiontable.InputType;
9 import org.drools.decisiontable.SpreadsheetCompiler;
10 import org.junit.Test;
11
12 public class SpreadsheetCompilerTest {
13
14 @Test
15 public void compile() throws FileNotFoundException{
16 //File file = new File("F:\\juny.qu\\rule_workspace\\com.deppon.rules\\src\\main\\java\\com\\xxx\\yyyy\\ka\\isP4P.xls");
17 //File file = new File("F:\\collections_study\\规则引擎系统开发小组\\规则引擎doc\\02开发\\06-详细设计\\增值服务折扣测试1.xls");
18 File file = new File("F:\\rules.xlsx");
19
20 InputStream is = new FileInputStream(file);
21
22 SpreadsheetCompiler converter = new SpreadsheetCompiler();
23 String drl = converter.compile(is, InputType.XLS); //--------exception here--------
24 System.out.println("\n\n" + drl);
25
26 }
27
28 }
如果规则可以表示成 templates+data(模板+数据),可以考虑使用 decision tables。在决策表的每一行,采集数据和模板一起生成规则。使用基于决策表的Spreadsheet的API在Drools-decisiontables模块中。只有一个类:SpreadsheetCompiler. 这个类可以操作各种格式的Spreadsheet,并生成DRL规则(然后就可以常规的方式使用)。
一个典型的规则表的格式