网站开发开题报告,php编程语言,做网站电信运营许可证,开一个素材设计网站怎么做文章目录 系列文章索引十、个人所得税计算器实战1、名词解释2、计算规则3、实现步骤 十一、信用卡申请实战1、计算规则2、实现 十二、保险产品准入规则实战1、决策表2、基于决策表的入门案例3、保险产品规则介绍4、实现步骤5、资料 系列文章索引
规则引擎Drools使用#xff0… 文章目录 系列文章索引十、个人所得税计算器实战1、名词解释2、计算规则3、实现步骤 十一、信用卡申请实战1、计算规则2、实现 十二、保险产品准入规则实战1、决策表2、基于决策表的入门案例3、保险产品规则介绍4、实现步骤5、资料 系列文章索引
规则引擎Drools使用0基础入门规则引擎Drools一基础入门 规则引擎Drools使用0基础入门规则引擎Drools二高级语法 规则引擎Drools使用0基础入门规则引擎Drools三整合springboot 规则引擎Drools使用0基础入门规则引擎Drools四WorkBench控制台 规则引擎Drools使用0基础入门规则引擎Drools五实战决策表
十、个人所得税计算器实战
我们需要通过Drools规则引擎来根据规则计算个人所得税最终页面效果如下
1、名词解释
税前月收入即税前工资指交纳个人所得税之前的总工资
应纳税所得额指按照税法规定确定纳税人在一定期间所获得的所有应税收入减除在该纳税期间依法允许减除的各种支出后的余额
税率是对征税对象的征收比例或征收额度
速算扣除数指为解决超额累进税率分级计算税额的复杂技术问题而预先计算出的一个数据可以简化计算过程
扣税额是指实际缴纳的税额
税后工资是指扣完税后实际到手的工资收入
2、计算规则
要实现个人所得税计算器需要了解如下计算规则
规则编号 名称 描述
1计算应纳税所得额应纳税所得额为税前工资减去35002设置税率应纳税所得额1500税率为0.03速算扣除数为03设置税率应纳税所得额在1500至4500之间税率为0.1速算扣除数为1054设置税率应纳税所得额在4500志9000之间税率为0.2速算扣除数为5555设置税率应纳税所得额在9000志35000之间税率为0.25速算扣除数为10056设置税率应纳税所得额在35000至55000之间税率为0.3速算扣除数为27557设置税率应纳税所得额在55000至80000之间税率为0.35速算扣除数为55058设置税率应纳税所得额在80000以上税率为0.45速算扣除数为135059计算税后工资扣税额应纳税所得额*税率-速算扣除数 税后工资税前工资-扣税额
3、实现步骤
本实战案例我们基于Spring Boot整合Drools的方式来实现。
第一步创建maven工程calculation并配置pom.xml文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starters/artifactIdversion2.0.6.RELEASE/version/parentgroupIdcn.itcast/groupIdartifactIdcalculation/artifactIdversion1.0-SNAPSHOT/versiondependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactId/dependencydependencygroupIdcommons-lang/groupIdartifactIdcommons-lang/artifactIdversion2.6/version/dependency!--drools规则引擎--dependencygroupIdorg.drools/groupIdartifactIddrools-core/artifactIdversion7.6.0.Final/version/dependencydependencygroupIdorg.drools/groupIdartifactIddrools-compiler/artifactIdversion7.6.0.Final/version/dependencydependencygroupIdorg.drools/groupIdartifactIddrools-templates/artifactIdversion7.6.0.Final/version/dependencydependencygroupIdorg.kie/groupIdartifactIdkie-api/artifactIdversion7.6.0.Final/version/dependencydependencygroupIdorg.kie/groupIdartifactIdkie-spring/artifactIdexclusionsexclusiongroupIdorg.springframework/groupIdartifactIdspring-tx/artifactId/exclusionexclusiongroupIdorg.springframework/groupIdartifactIdspring-beans/artifactId/exclusionexclusiongroupIdorg.springframework/groupIdartifactIdspring-core/artifactId/exclusionexclusiongroupIdorg.springframework/groupIdartifactIdspring-context/artifactId/exclusion/exclusionsversion7.6.0.Final/version/dependency/dependenciesbuildfinalName${project.artifactId}/finalNameresourcesresourcedirectorysrc/main/java/directoryincludesinclude**/*.xml/include/includesfilteringfalse/filtering/resourceresourcedirectorysrc/main/resources/directoryincludesinclude**/*.*/include/includesfilteringfalse/filtering/resource/resourcespluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion2.3.2/versionconfigurationsource1.8/sourcetarget1.8/target/configuration/plugin/plugins/build
/project第二步创建/resources/application.yml文件
server:port: 8080
spring:application:name: calculation第三步编写配置类DroolsConfig
package com.drools.config;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.Resource;
import java.io.IOException;
/*** 规则引擎配置类*/
Configuration
public class DroolsConfig {//指定规则文件存放的目录private static final String RULES_PATH rules/;private final KieServices kieServices KieServices.Factory.get();BeanConditionalOnMissingBeanpublic KieFileSystem kieFileSystem() throws IOException {System.setProperty(drools.dateformat,yyyy-MM-dd);KieFileSystem kieFileSystem kieServices.newKieFileSystem();ResourcePatternResolver resourcePatternResolver new PathMatchingResourcePatternResolver();Resource[] files resourcePatternResolver.getResources(classpath*: RULES_PATH *.*);String path null;for (Resource file : files) {path RULES_PATH file.getFilename();kieFileSystem.write(ResourceFactory.newClassPathResource(path, UTF-8));}return kieFileSystem;}BeanConditionalOnMissingBeanpublic KieContainer kieContainer() throws IOException {KieRepository kieRepository kieServices.getRepository();kieRepository.addKieModule(kieRepository::getDefaultReleaseId);KieBuilder kieBuilder kieServices.newKieBuilder(kieFileSystem());kieBuilder.buildAll();return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());}BeanConditionalOnMissingBeanpublic KieBase kieBase() throws IOException {return kieContainer().getKieBase();}BeanConditionalOnMissingBeanpublic KModuleBeanFactoryPostProcessor kiePostProcessor() {return new KModuleBeanFactoryPostProcessor();}
}第四步编写实体类Calculation
package com.drools.entity;
public class Calculation {private double wage;//税前工资private double wagemore;//应纳税所得额private double cess;//税率private double preminus;//速算扣除数private double wageminus;//扣税额private double actualwage;//税后工资
public double getWage() {return wage;}
public void setWage(double wage) {this.wage wage;}
public double getActualwage() {return actualwage;}
public void setActualwage(double actualwage) {this.actualwage actualwage;}
public double getWagemore() {return wagemore;}
public void setWagemore(double wagemore) {this.wagemore wagemore;}
public double getCess() {return cess;}
public void setCess(double cess) {this.cess cess;}
public double getPreminus() {return preminus;}
public void setPreminus(double preminus) {this.preminus preminus;}
public double getWageminus() {return wageminus;}
public void setWageminus(double wageminus) {this.wageminus wageminus;}
Overridepublic String toString() {return Calculation{ wage wage , actualwage actualwage , wagemore wagemore , cess cess , preminus preminus , wageminus wageminus };}
}第五步在resources/rules下创建规则文件calculation.drl文件
package calculation
import com.drools.entity.Calculation
rule 个人所得税计算应纳税所得额enabled truesalience 3no-loop truedate-effective 2021-09-01 //生效日期when$cal : Calculation(wage0)then$cal.setWagemore($cal.getWage()-3500);update($cal);
end
rule 个人所得税设置税率--应纳税所得额1500salience 2no-loop trueactivation-group SETCess_Groupwhen$cal : Calculation(wagemore 1500)then$cal.setCess(0.03);$cal.setPreminus(0);update($cal);
end
rule 个人所得税设置税率--应纳税所得额在1500至4500之间salience 2no-loop trueactivation-group SETCess_Groupwhen$cal : Calculation(wagemore 1500 wagemore 4500)then$cal.setCess(0.1);$cal.setPreminus(105);update($cal);
end
rule 个人所得税设置税率--应纳税所得额在4500志9000之间salience 2no-loop trueactivation-group SETCess_Groupwhen$cal : Calculation(wagemore 4500 wagemore 9000)then$cal.setCess(0.2);$cal.setPreminus(555);update($cal);
end
rule 个人所得税设置税率--应纳税所得额在9000志35000之间salience 2no-loop trueactivation-group SETCess_Groupwhen$cal : Calculation(wagemore 9000 wagemore 35000)then$cal.setCess(0.25);$cal.setPreminus(1005);update($cal);
end
rule 个人所得税设置税率--应纳税所得额在35000至55000之间salience 2no-loop trueactivation-group SETCess_Groupwhen$cal : Calculation(wagemore 35000 wagemore 55000)then$cal.setCess(0.3);$cal.setPreminus(2755);update($cal);
end
rule 个人所得税设置税率--应纳税所得额在55000至80000之间salience 2no-loop trueactivation-group SETCess_Groupwhen$cal : Calculation(wagemore 55000 wagemore 80000)then$cal.setCess(0.35);$cal.setPreminus(5505);update($cal);
end
rule 个人所得税设置税率--应纳税所得额在80000以上salience 2no-loop trueactivation-group SETCess_Groupwhen$cal : Calculation(wagemore 80000)then$cal.setCess(0.45);$cal.setPreminus(13505);update($cal);
end
rule 个人所得税计算税后工资salience 1when$cal : Calculation(wage 0 wagemore 0 wagemore 0 cess 0)then$cal.setWageminus($cal.getWagemore()*$cal.getCess()-$cal.getPreminus());$cal.setActualwage($cal.getWage()-$cal.getWageminus());
System.out.println(-----税前工资$cal.getWage());System.out.println(-----应纳税所得额$cal.getWagemore());System.out.println(-----税率 $cal.getCess());System.out.println(-----速算扣除数 $cal.getPreminus());System.out.println(-----扣税额 $cal.getWageminus());System.out.println(-----税后工资 $cal.getActualwage());
end第六步创建RuleService
package com.drools.service;
import com.itheima.drools.entity.Calculation;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/*** 调用规则引擎执行规则*/
Service
public class RuleService {Autowiredprivate KieBase kieBase;
//个人所得税计算public Calculation calculate(Calculation calculation){KieSession kieSession kieBase.newKieSession();kieSession.insert(calculation);kieSession.fireAllRules();kieSession.dispose();return calculation;}
}第七步创建RuleController
package com.drools.controller;
import com.itheima.drools.entity.Calculation;
import com.itheima.drools.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
RestController
RequestMapping(/rule)
public class RuleController {Autowiredprivate RuleService ruleService;
RequestMapping(/calculate)public Calculation calculate(double wage){Calculation calculation new Calculation();calculation.setWage(wage);calculation ruleService.calculate(calculation);System.out.println(calculation);return calculation;}
}第八步创建启动类DroolsApplication
package com.drools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
SpringBootApplication
public class DroolsApplication {public static void main(String[] args) {SpringApplication.run(DroolsApplication.class);}
}第九步准备前端页面
十一、信用卡申请实战
我们需要通过Drools规则引擎来根据规则进行申请人的合法性检查检查通过后再根据规则确定信用卡额度最终页面效果如下
1、计算规则
合法性检查规则如下
规则编号名称描述1检查学历与薪水1如果申请人既没房也没车同时学历为大专以下并且月薪少于5000那么不通过2检查学历与薪水2如果申请人既没房也没车同时学历为大专或本科并且月薪少于3000那么不通过3检查学历与薪水3如果申请人既没房也没车同时学历为本科以上并且月薪少于2000同时之前没有信用卡的那么不通过4检查申请人已有的信用卡数量如果申请人现有的信用卡数量大于10那么不通过
信用卡额度确定规则
规则编号名称描述1规则1如果申请人有房有车或者月收入在20000以上那么发放的信用卡额度为150002规则2如果申请人没房没车但月收入在10000~20000之间那么发放的信用卡额度为60003规则3如果申请人没房没车月收入在10000以下那么发放的信用卡额度为30004规则4如果申请人有房没车或者没房但有车月收入在10000以下那么发放的信用卡额度为50005规则5如果申请人有房没车或者是没房但有车月收入在10000~20000之间那么发放的信用卡额度为8000
2、实现
编写实体类CreditCardApplyInfo
package com.drools.entity;
/*** 信用卡申请信息*/
public class CreditCardApplyInfo {public static final String EDUCATION_1 专科以下;public static final String EDUCATION_2 专科;public static final String EDUCATION_3 本科;public static final String EDUCATION_4 本科以上;
private String name;private String sex;private int age;private String education;private String telephone;private double monthlyIncome 0;//月收入private String address;
private boolean hasHouse false;//是否有房private boolean hasCar false;//是否有车private int hasCreditCardCount 0;//现持有信用卡数量
private boolean checkResult true;//审核是否通过private double quota 0;//额度
public String getName() {return name;}
public void setName(String name) {this.name name;}
public String getSex() {return sex;}
public void setSex(String sex) {this.sex sex;}
public int getAge() {return age;}
public void setAge(int age) {this.age age;}
public String getEducation() {return education;}
public void setEducation(String education) {this.education education;}
public String getTelephone() {return telephone;}
public void setTelephone(String telephone) {this.telephone telephone;}
public double getMonthlyIncome() {return monthlyIncome;}
public void setMonthlyIncome(double monthlyIncome) {this.monthlyIncome monthlyIncome;}
public String getAddress() {return address;}
public void setAddress(String address) {this.address address;}
public boolean isHasHouse() {return hasHouse;}
public void setHasHouse(boolean hasHouse) {this.hasHouse hasHouse;}
public boolean isHasCar() {return hasCar;}
public void setHasCar(boolean hasCar) {this.hasCar hasCar;}
public int getHasCreditCardCount() {return hasCreditCardCount;}
public void setHasCreditCardCount(int hasCreditCardCount) {this.hasCreditCardCount hasCreditCardCount;}
public boolean isCheckResult() {return checkResult;}
public void setCheckResult(boolean checkResult) {this.checkResult checkResult;}
public double getQuota() {return quota;}
public void setQuota(double quota) {this.quota quota;}
public String toString() {if(checkResult){return 审核通过信用卡额度为 quota;}else {return 审核不通过;}}
}在resources/rules下创建规则文件creditCardApply.drl文件
package com.creditCardApply
import com.drools.entity.CreditCardApplyInfo
//合法性检查
rule 如果申请人既没房也没车同时学历为大专以下并且月薪少于5000那么不通过salience 10no-loop truewhen$c:CreditCardApplyInfo(hasCar false hasHouse false education CreditCardApplyInfo.EDUCATION_1 monthlyIncome 5000)then$c.setCheckResult(false);drools.halt();
end
rule 如果申请人既没房也没车同时学历为大专或本科并且月薪少于3000那么不通过salience 10no-loop truewhen$c:CreditCardApplyInfo(hasCar false hasHouse false (education CreditCardApplyInfo.EDUCATION_2 ||education CreditCardApplyInfo.EDUCATION_3) monthlyIncome 3000)then$c.setCheckResult(false);drools.halt();
end
rule 如果申请人既没房也没车同时学历为本科以上并且月薪少于2000同时之前没有信用卡的那么不通过salience 10no-loop truewhen$c:CreditCardApplyInfo(hasCar false hasHouse false education CreditCardApplyInfo.EDUCATION_4 monthlyIncome 2000 hasCreditCardCount 0)then$c.setCheckResult(false);drools.halt();
end
rule 如果申请人现有的信用卡数量大于10那么不通过salience 10no-loop truewhen$c:CreditCardApplyInfo(hasCreditCardCount 10)then$c.setCheckResult(false);drools.halt();
end
//--------------------------------------------------------------------------
//确定额度
rule 如果申请人有房有车或者月收入在20000以上那么发放的信用卡额度为15000salience 1no-loop trueactivation-group quota_groupwhen$c:CreditCardApplyInfo(checkResult true ((hasHouse true hasCar true) ||(monthlyIncome 20000)))then$c.setQuota(15000);
end
rule 如果申请人没房没车但月收入在10000~20000之间那么发放的信用卡额度为6000salience 1no-loop trueactivation-group quota_groupwhen$c:CreditCardApplyInfo(checkResult true hasHouse false hasCar false monthlyIncome 10000 monthlyIncome 20000)then$c.setQuota(6000);
end
rule 如果申请人没房没车月收入在10000以下那么发放的信用卡额度为3000salience 1no-loop trueactivation-group quota_groupwhen$c:CreditCardApplyInfo(checkResult true hasHouse false hasCar false monthlyIncome 10000)then$c.setQuota(3000);
end
rule 如果申请人有房没车或者没房但有车月收入在10000以下那么发放的信用卡额度为5000salience 1no-loop trueactivation-group quota_groupwhen$c:CreditCardApplyInfo(checkResult true ((hasHouse true hasCar false) ||(hasHouse false hasCar true)) monthlyIncome 10000)then$c.setQuota(5000);
end
rule 如果申请人有房没车或者是没房但有车月收入在10000~20000之间那么发放的信用卡额度为8000salience 1no-loop trueactivation-group quota_groupwhen$c:CreditCardApplyInfo(checkResult true ((hasHouse true hasCar false) ||(hasHouse false hasCar true)) monthlyIncome 10000 monthlyIncome 20000)then$c.setQuota(8000);
end十二、保险产品准入规则实战
1、决策表
前面的课程中我们编写的规则文件都是drl形式的文件Drools除了支持drl形式的文件外还支持xls格式的文件即Excel文件。这种xls格式的文件通常称为决策表decision table。
决策表decision table是一个“精确而紧凑的”表示条件逻辑的方式非常适合商业级别的规则。决策表与现有的drl文件可以无缝替换。Drools提供了相应的API可以将xls文件编译为drl格式的字符串。
一个决策表的例子如下 决策表语法
关键字说明是否必须RuleSet相当于drl文件中的package必须只能有一个。如果没有设置RuleSet对应的值则使用默认值rule_tableSequential取值为Boolean类型。true表示规则按照表格自上到下的顺序执行false表示乱序可选Import相当于drl文件中的import如果引入多个类则类之间用逗号分隔可选Variables相当于drl文件中的global用于定义全局变量如果有多个全局变量则中间用逗号分隔可选RuleTable它指示了后面将会有一批ruleRuleTable的名称将会作为以后生成rule的前缀必须CONDITION规则条件关键字相当于drl文件中的when。下面两行则表示 LHS 部分第三行则为注释行不计为规则部分从第四行开始每一行表示一条规则每个规则表至少有一个ACTION规则结果关键字相当于drl文件中的then每个规则表至少有一个NO-LOOP相当于drl文件中的no-loop可选AGENDA-GROUP相当于drl文件中的agenda-group可选
在决策表中还经常使用到占位符语法为$后面加数字用于替换每条规则中设置的具体值。
上面的决策表例子转换为drl格式的规则文件内容如下
package rules;
import com.drools.entity.PersonInfoEntity;
import java.util.List;
global java.util.List listRules;
rule personCheck_10salience 65535agenda-group signwhen$person : PersonInfoEntity(sex ! 男)thenlistRules.add(性别不对);
end
rule personCheck_11salience 65534agenda-group signwhen$person : PersonInfoEntity(age 22 || age 25)thenlistRules.add(年龄不合适);
end
rule personCheck_12salience 65533agenda-group signwhen$person : PersonInfoEntity(salary 10000)thenlistRules.add(工资太低了);
end要进行决策表相关操作需要导入如下maven坐标
dependencygroupIdorg.drools/groupIdartifactIddrools-decisiontables/artifactIdversion7.10.0.Final/version
/dependency通过下图可以发现由于maven的依赖传递特性在导入drools-decisiontables坐标后drools-core和drools-compiler等坐标也被传递了过来 Drools提供的将xls文件编译为drl格式字符串的API如下
String realPath C:\\testRule.xls;//指定决策表xls文件的磁盘路径
File file new File(realPath);
InputStream is new FileInputStream(file);
SpreadsheetCompiler compiler new SpreadsheetCompiler();
String drl compiler.compile(is, InputType.XLS);Drools还提供了基于drl格式字符串创建KieSession的API
KieHelper kieHelper new KieHelper();
kieHelper.addContent(drl, ResourceType.DRL);
KieSession session kieHelper.build().newKieSession();2、基于决策表的入门案例
第一步创建maven工程drools_decisiontable_demo并配置pom.xml文件
dependencygroupIdorg.drools/groupIdartifactIddrools-decisiontables/artifactIdversion7.10.0.Final/version
/dependency
dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version
/dependency第二步创建实体类PersonInfoEntity
package com.drools.entity;
public class PersonInfoEntity {private String sex;private int age;private double salary;
public String getSex() {return sex;}
public void setSex(String sex) {this.sex sex;}
public int getAge() {return age;}
public void setAge(int age) {this.age age;}
public double getSalary() {return salary;}
public void setSalary(double salary) {this.salary salary;}
}第三步创建xls规则文件可以直接使用资料中提供的testRule.xls文件
第四步创建单元测试
Test
public void test1() throws Exception{String realPath d:\\testRule.xls;//指定决策表xls文件的磁盘路径File file new File(realPath);InputStream is new FileInputStream(file);SpreadsheetCompiler compiler new SpreadsheetCompiler();String drl compiler.compile(is, InputType.XLS);
System.out.println(drl);KieHelper kieHelper new KieHelper();kieHelper.addContent(drl, ResourceType.DRL);KieSession session kieHelper.build().newKieSession();
PersonInfoEntity personInfoEntity new PersonInfoEntity();personInfoEntity.setSex(男);personInfoEntity.setAge(35);personInfoEntity.setSalary(1000);
ListString list new ArrayListString();session.setGlobal(listRules,list);
session.insert(personInfoEntity);session.getAgenda().getAgendaGroup(sign).setFocus();session.fireAllRules();
for (String s : list) {System.out.println(s);}session.dispose();
}3、保险产品规则介绍
各保险公司针对人身、财产推出了不同的保险产品作为商业保险公司筛选出符合公司利益最大化的客户是非常重要的即各保险产品的准入人群是不同的也就是说保险公司会针对不同的人群特征制定不同的产品缴费和赔付规则。
我们来看一下某保险产品准入规则的简化版当不满足以下规则时系统模块需要返回准入失败标识和失败原因 规则1 保险公司是PICC 规则2 销售区域是北京、天津 规则3 投保人年龄0 ~ 17岁 规则4 保险期间是20年、25年、30年 规则5 缴费方式是趸交一次性交清或年交 规则6 保险期与交费期规则一保险期间为20年期交费期间最长10年交且不能选择[趸交] 规则7 保险期与交费期规则二保险期间为25年期交费期间最长15年交且不能选择[趸交] 规则8 保险期与交费期规则三保险期间为30年期交费期间最长20年交且不能选择[趸交] 规则9 被保人要求投保年龄保险期间不得大于40周岁 规则10 保险金额规则投保时约定最低为5万元超过部分必须为1000元的整数倍 规则11 出单基本保额限额规则线上出单基本保额限额62.5万元超62.5万元需配合契调转线下出单 在本案例中规则文件是一个Excel文件业务人员可以直接更改这个文件中指标的值系统不需要做任何变更。
4、实现步骤
本案例还是基于Spring Boot整合Drools的架构来实现。
第一步创建maven工程insuranceInfoCheck并配置pom.xml文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starters/artifactIdversion2.0.6.RELEASE/version/parentgroupIdcom.itheima/groupIdartifactIdinsuranceInfoCheck/artifactIdversion1.0-SNAPSHOT/versiondependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactId/dependencydependencygroupIdcommons-lang/groupIdartifactIdcommons-lang/artifactIdversion2.6/version/dependency!--drools规则引擎--dependencygroupIdorg.drools/groupIdartifactIddrools-core/artifactIdversion7.6.0.Final/version/dependencydependencygroupIdorg.drools/groupIdartifactIddrools-compiler/artifactIdversion7.6.0.Final/version/dependencydependencygroupIdorg.drools/groupIdartifactIddrools-templates/artifactIdversion7.6.0.Final/version/dependencydependencygroupIdorg.kie/groupIdartifactIdkie-api/artifactIdversion7.6.0.Final/version/dependencydependencygroupIdorg.kie/groupIdartifactIdkie-spring/artifactIdexclusionsexclusiongroupIdorg.springframework/groupIdartifactIdspring-tx/artifactId/exclusionexclusiongroupIdorg.springframework/groupIdartifactIdspring-beans/artifactId/exclusionexclusiongroupIdorg.springframework/groupIdartifactIdspring-core/artifactId/exclusionexclusiongroupIdorg.springframework/groupIdartifactIdspring-context/artifactId/exclusion/exclusionsversion7.6.0.Final/version/dependency/dependenciesbuildfinalName${project.artifactId}/finalNameresourcesresourcedirectorysrc/main/java/directoryincludesinclude**/*.xml/include/includesfilteringfalse/filtering/resourceresourcedirectorysrc/main/resources/directoryincludesinclude**/*.*/include/includesfilteringfalse/filtering/resource/resourcespluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion2.3.2/versionconfigurationsource1.8/sourcetarget1.8/target/configuration/plugin/plugins/build
/project第二步创建/resources/application.yml文件
server:port: 8080
spring:application:name: insuranceInfoCheck第三步创建实体类InsuranceInfo
/*** 保险信息*/
public class InsuranceInfo {private String param1;//保险公司private String param2;//方案代码private String param3;//渠道号private String param4;//销售区域private String param5;//投保年龄private String param6;//保险期间private String param7;//缴费期间private String param8;//缴费方式private String param9;//保障类型private String param10;//等待期private String param11;//犹豫期private String param12;//职业类型private String param13;//保额限制private String param14;//免赔额private String param15;//主险保额private String param16;//主险保费private String param17;//附加险保额private String param18;//附加险保费private String param19;//与投保人关系private String param20;//与被保人关系private String param21;//性别private String param22;//证件private String param23;//保费private String param24;//保额
//getter setter省略
}第四步创建决策表文件也可以直接使用实战资料中提供的insuranceInfoCheck.xls文件 第五步封装工具类KieSessionUtils
package com.drools.utils;
import com.drools.entity.InsuranceInfo;
import com.drools.entity.PersonInfoEntity;
import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class KieSessionUtils {private KieSessionUtils() {
}// 把xls文件解析为Stringpublic static String getDRL (String realPath) throws FileNotFoundException {File file new File(realPath); // 例如C:\\abc.xlsInputStream is new FileInputStream(file);SpreadsheetCompiler compiler new SpreadsheetCompiler();String drl compiler.compile(is, InputType.XLS);System.out.println(drl);return drl;}
// drl为含有内容的字符串public static KieSession createKieSessionFromDRL(String drl) throws Exception{KieHelper kieHelper new KieHelper();kieHelper.addContent(drl, ResourceType.DRL);Results results kieHelper.verify();if (results.hasMessages(Message.Level.WARNING, Message.Level.ERROR)) {ListMessage messages results.getMessages(Message.Level.WARNING, Message.Level.ERROR);for (Message message : messages) {System.out.println(Error: message.getText());}// throw new IllegalStateException(Compilation errors were found. Check the logs.);}return kieHelper.build().newKieSession();}
// realPath为Excel文件绝对路径public static KieSession getKieSessionFromXLS(String realPath) throws Exception {return createKieSessionFromDRL(getDRL(realPath));}
}第六步创建RuleService类
package com.drools.service;
import com.drools.entity.InsuranceInfo;
import com.drools.utils.KieSessionUtils;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
Service
public class RuleService {public ListString insuranceInfoCheck(InsuranceInfo insuranceInfo) throws Exception{KieSession session KieSessionUtils.getKieSessionFromXLS(D:\\rules.xls);session.getAgenda().getAgendaGroup(sign).setFocus();
session.insert(insuranceInfo);
ListString listRules new ArrayList();session.setGlobal(listRules, listRules);
session.fireAllRules();return listRules;}
}第七步创建RuleController类
package com.drools.controller;
import com.drools.entity.InsuranceInfo;
import com.drools.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
RestController
RequestMapping(/rule)
public class RuleController {Autowiredprivate RuleService ruleService;
RequestMapping(/insuranceInfoCheck)public Map insuranceInfoCheck(){Map map new HashMap();//模拟数据实际应为页面传递过来InsuranceInfo insuranceInfo new InsuranceInfo();insuranceInfo.setParam1(picc);insuranceInfo.setParam4(上海);insuranceInfo.setParam5(101);insuranceInfo.setParam6(12);insuranceInfo.setParam7(222);insuranceInfo.setParam8(1);insuranceInfo.setParam13(3);try {ListString list ruleService.insuranceInfoCheck(insuranceInfo);if(list ! null list.size() 0){map.put(checkResult,false);map.put(msg,准入失败);map.put(detail,list);}else{map.put(checkResult,true);map.put(msg,准入成功);}return map;} catch (Exception e) {e.printStackTrace();map.put(checkResult,false);map.put(msg,未知错误);return map;}}
}第八步创建启动类DroolsApplication
package com.drools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
SpringBootApplication
public class DroolsApplication {public static void main(String[] args) {SpringApplication.run(DroolsApplication.class);}
}5、资料
https://download.csdn.net/download/A_art_xiang/88561393