引用官方的话
决策表是一个“精确而紧凑的”表示条件逻辑的方式,非常适合商业级别的规则。 目前决策表支持xls格式和csv格式。决策表与现有的drools drl文件使用可以无缝替换。
RuleSet 和 drl 文件中的 package 是一样
首先需要引入maven的pom依赖:
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>7.0.0.Final</version>
</dependency>
可以通过以下两个方式来对决策表进行解析生成drl文件:
@Test
public void checkDrl() throws FileNotFoundException {
File file = new File("E:\\GitHub\\respo\\drools-lesson\\src\\main\\resources\\decision.xls");
InputStream is = new FileInputStream(file);
SpreadsheetCompiler compiler = new SpreadsheetCompiler();
String drl = compiler.compile(is, InputType.XLS);
System.out.println(drl);
}
@Test
public void checkDrl2() throws FileNotFoundException {
SpreadsheetCompiler compiler = new SpreadsheetCompiler();
String drl = compiler.compile(ResourceFactory.newClassPathResource("com/decision/decision.xls"), InputType.XLS);
System.out.println(drl);
}
可以通过查看生成的drl内容确定决策表的语法及业务是预期的。
像配置drl文件格式的规则一样,我们要执行一个xls决策表操作非常简单,只用把drl文件替换为对应的xls文件即可。
具体配置如下: kmodule.xml中配置xls所在的resource目录对应的session:
<kbase name="decision-kbase" packages="com.decision">
<ksession name="decision-rules"/>
</kbase>
同时将决策表的xls文件放置在com.decision包中,为了减少不必要的麻烦,决策表中定义的RuleSet值也应该是com.decision。
同样的,在代码中只需像正常调用代码一样获取KieSession进行后续业务处理即可:
@Test
public void testDecision(){
KieSession kieSession = this.getKieSessionBySessionName("decision-rules");
kieSession.fireAllRules();
kieSession.dispose();
}
打印的结果与drl文件无二。
同时也可查看github上的代码:https://github.com/secbr/drools
如果想获得更多相关技术交流可加入QQ技术交流群:593177274