Drools 决策表的使用与说明(一)

Drools决策表的使用

官方文档决策表说明

Drools 决策表的使用 16.7. Spreadsheet decision tables

决策表使用方式

在Drools中,决策表通常是以Excel(.xls或.xlsx)或者CSV格式存储的,这些格式的决策表并不能直接被Drools执行。它们需要被转换成Drools规则语言(DRL)的文本格式,之后才能被编译和执行。
Drools提供了工具来帮助完成这一转换,主要使用org.drools.decisiontable.SpreadsheetCompiler类

以下是一个基本的转换过程

这是我的决策表:
表一( Pricing bracket)
在这里插入图片描述
表二(Discounts)
在这里插入图片描述
文件名称:ExamplePolicyPricing.xls

项目的结构目录:
在这里插入图片描述

	 // 加载决策表文件
        InputStream is = DecisionTableToDRL.class.getResourceAsStream("/path/to/decisiontable.xls");

        // 创建SpreadsheetCompiler实例
        SpreadsheetCompiler compiler = SpreadsheetCompilerFactory.newCompiler();

        // 转换决策表为DRL
        String drl = compiler.compile(is, "UTF-8", "decisiontable");

        // 打印DRL规则
        System.out.println(drl);

xls转为drl结果为:

package com.mk.examples.decisiontable;
//generated from Decision Table
import com.mking.drools.bean.decisiontable.*;
// rule values at B9, header at B4
rule "Pricing bracket_9"
	when
		Driver(age >= 18, age <= 24, locationRiskProfile == "LOW", priorClaims == "1")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.setBasePrice(450);
end

// rule values at B10, header at B4
rule "Pricing bracket_10"
	when
		Driver(age >= 18, age <= 24, locationRiskProfile == "MED")
		policy: Policy(type == "FIRE_THEFT")
	then
		policy.setBasePrice(200);
		System.out.println("Priors not relevant");
end

// rule values at B11, header at B4
rule "Pricing bracket_11"
	when
		Driver(age >= 18, age <= 24, locationRiskProfile == "MED", priorClaims == "0")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.setBasePrice(300);
end

// rule values at B12, header at B4
rule "Pricing bracket_12"
	when
		Driver(age >= 18, age <= 24, locationRiskProfile == "LOW")
		policy: Policy(type == "FIRE_THEFT")
	then
		policy.setBasePrice(150);
end

// rule values at B13, header at B4
rule "Pricing bracket_13"
	when
		Driver(age >= 18, age <= 24, locationRiskProfile == "LOW", priorClaims == "0")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.setBasePrice(150);
		System.out.println("Safe driver discount");
end

// rule values at B14, header at B4
rule "Pricing bracket_14"
	when
		Driver(age >= 18, age <= 24, locationRiskProfile == "MED", priorClaims == "1")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.setBasePrice(700);
end

// rule values at B15, header at B4
rule "Pricing bracket_15"
	when
		Driver(age >= 18, age <= 24, locationRiskProfile == "HIGH", priorClaims == "0")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.setBasePrice(700);
		System.out.println("Location risk");
end

// rule values at B16, header at B4
rule "Pricing bracket_16"
	when
		Driver(age >= 18, age <= 24, locationRiskProfile == "HIGH")
		policy: Policy(type == "FIRE_THEFT")
	then
		policy.setBasePrice(550);
		System.out.println("Location risk");
end

// rule values at B17, header at B4
rule "Pricing bracket_17"
	when
		Driver(age >= 25, age <= 30, priorClaims == "0")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.setBasePrice(120);
		System.out.println("Cheapest possible");
end

// rule values at B18, header at B4
rule "Pricing bracket_18"
	when
		Driver(age >= 25, age <= 30, priorClaims == "1")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.setBasePrice(300);
end

// rule values at B19, header at B4
rule "Pricing bracket_19"
	when
		Driver(age >= 25, age <= 30, priorClaims == "2")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.setBasePrice(590);
end

// rule values at B20, header at B4
rule "Pricing bracket_20"
	when
		Driver(age >= 25, age <= 35, priorClaims == "3")
		policy: Policy(type == "THIRD_PARTY")
	then
		policy.setBasePrice(800);
		System.out.println("High risk");
end

// rule values at B27, header at B22
rule "Discounts_27"
	when
		Driver(age >= 20, age <= 24, priorClaims == "0")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.applyDiscount(1);
end

// rule values at B28, header at B22
rule "Discounts_28"
	when
		Driver(age >= 18, age <= 24, priorClaims == "0")
		policy: Policy(type == "FIRE_THEFT")
	then
		policy.applyDiscount(2);
end

// rule values at B29, header at B22
rule "Discounts_29"
	when
		Driver(age >= 25, age <= 30, priorClaims == "1")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.applyDiscount(5);
end

// rule values at B30, header at B22
rule "Discounts_30"
	when
		Driver(age >= 25, age <= 30, priorClaims == "2")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.applyDiscount(1);
end

// rule values at B31, header at B22
rule "Discounts_31"
	when
		Driver(age >= 25, age <= 30, priorClaims == "0")
		policy: Policy(type == "COMPREHENSIVE")
	then
		policy.applyDiscount(20);
end

执行drl代码及结果

在这里插入图片描述

动态加载drl可以参考该文:Drools实践与动态加载
以下是drl解析过程:

 KieHelper kieHelper = new KieHelper();
 kieHelper.addContent(drl, ResourceType.DRL);

 KieSession ksession = kieHelper.build().newKieSession();
 Driver def = new Driver();
 Policy policy = new Policy();
 ksession.insert(def);
 ksession.insert(policy);
 int count = ksession.fireAllRules();
 ksession.dispose();

添加微信,_ 不定时更新代码demo示例
添加 回复 “drools-demo” 获取示例代码
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值