前提准备:java 虚拟机
Drools核心库
Junit4.0以上版本
规则Excel模板表(将下面的图片的内容新建到Excel中)
典型的用法就是根据excel创建KnowledgeBase,然后将它丢给session执行,执行的参数和结果都在params里面
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
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 }
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 }
运行结果如下:
package rule_table;
//generated from Decision Table
import java.util.*;
// rule values at B22, header at B17
rule "ContractP4P_22"
when
$constract:java.util.HashMap($constract.get("productLine")=="搜索推广")
$constract.get("productType")=="true"
$constract.get("playType")=="null"
then
$constract.put("isP4P","true");
end
// rule values at B23, header at B17
rule "ContractP4P_23"
when
$constract.get("playType")=="null"
then
$constract.put("isP4P","true");
end
// rule values at B24, header at B17
rule "ContractP4P_24"
when
$constract:java.util.HashMap($constract.get("productLine")=="掘金推广")
$constract.get("productType")=="true"
$constract.get("playType")=="非轮播"
then
$constract.put("isP4P","false");
end
// rule values at B25, header at B17
rule "ContractP4P_25"
when
$constract:java.util.HashMap($constract.get("productLine")=="其他")
$constract.get("productType")=="false"
$constract.get("playType")=="其他"
then
$constract.put("isP4P","other");
end