目录

1、计算规则

2、实现步骤

2.1、创建maven工程creditCardApply并配置pom.xml文件

2.2、创建/resources/application.yml文件

2.3、编写配置类DroolsConfig

2.4、编写实体类CreditCardApplyInfo

2.5、在resources/rules下创建规则文件creditCardApply.drl文件

2.6、创建RuleService

2.7、创建RuleController

2.8、创建启动类DroolsApplication

2.9、导入静态资源文件到resources/static目录下


通过Drools规则引擎来根据规则进行申请人的合法性检查,检查通过后再根据规则确定信用卡额度,最终页面效果如下:

1、计算规则

合法性检查规则如下:

规则编号 名称 描述
1 检查学历与薪水1 如果申请人既没房也没车,同时学历为大专以下,并且月薪少于5000,那么不通过
2 检查学历与薪水2 如果申请人既没房也没车,同时学历为大专或本科,并且月薪少于3000,那么不通过
3 检查学历与薪水3 如果申请人既没房也没车,同时学历为本科以上,并且月薪少于2000,同时之前没有信用卡的,那么不通过
4 检查申请人已有的信用卡数量 如果申请人现有的信用卡数量大于10,那么不通过

信用卡额度确定规则:

规则编号 名称 描述
1 规则1 如果申请人有房有车,或者月收入在20000以上,那么发放的信用卡额度为15000
2 规则2 如果申请人没房没车,但月收入在10000~20000之间,那么发放的信用卡额度为6000
3 规则3 如果申请人没房没车,月收入在10000以下,那么发放的信用卡额度为3000
4 规则4 如果申请人有房没车或者没房但有车,月收入在10000以下,那么发放的信用卡额度为5000
5 规则5 如果申请人有房没车或者是没房但有车,月收入在10000~20000之间,那么发放的信用卡额度为8000

2、实现步骤

2.1、创建maven工程creditCardApply并配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starters</artifactId><version>2.0.6.RELEASE</version></parent><groupId>org.example</groupId><artifactId>creditCardApply</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><!--drools规则引擎--><dependency><groupId>org.drools</groupId><artifactId>drools-core</artifactId><version>7.6.0.Final</version></dependency><dependency><groupId>org.drools</groupId><artifactId>drools-compiler</artifactId><version>7.6.0.Final</version></dependency><dependency><groupId>org.drools</groupId><artifactId>drools-templates</artifactId><version>7.6.0.Final</version></dependency><dependency><groupId>org.kie</groupId><artifactId>kie-api</artifactId><version>7.6.0.Final</version></dependency><dependency><groupId>org.kie</groupId><artifactId>kie-spring</artifactId><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></exclusion></exclusions><version>7.6.0.Final</version></dependency></dependencies><build><finalName>${project.artifactId}</finalName><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes><filtering>false</filtering></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
</project>

2.2、创建/resources/application.yml文件

server:port: 8080
spring:application:name: creditCardApply

2.3、编写配置类DroolsConfig

package com.itheima.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.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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;import java.io.IOException;/*** 规则引擎配置类*/
@Configuration
public class DroolsConfig
{//指定规则文件存放的目录private static final String RULES_PATH = "rules/";private final KieServices kieServices = KieServices.Factory.get();@Bean@ConditionalOnMissingBeanpublic 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;}@Bean@ConditionalOnMissingBeanpublic KieContainer kieContainer() throws IOException {KieRepository kieRepository = kieServices.getRepository();kieRepository.addKieModule(kieRepository::getDefaultReleaseId);KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());kieBuilder.buildAll();return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());}@Bean@ConditionalOnMissingBeanpublic KieBase kieBase() throws IOException {return kieContainer().getKieBase();}@Bean@ConditionalOnMissingBeanpublic KModuleBeanFactoryPostProcessor kiePostProcessor() {return new KModuleBeanFactoryPostProcessor();}
}

2.4、编写实体类CreditCardApplyInfo

package com.itheima.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 "审核不通过";}}
}

2.5、在resources/rules下创建规则文件creditCardApply.drl文件

//当前规则文件负责处理两类规则:用户信息合法性检查、确定信用卡额度
package creditCardApplyimport com.itheima.drools.entity.CreditCardApplyInfo//用户信息合法性检查---共四个规则rule "如果申请人既没房也没车,同时学历为大专以下,并且月薪少于5000,那么不通过"salience 100no-loop truewhen$c:CreditCardApplyInfo(hasHouse == false && hasCar == false && education == CreditCardApplyInfo.EDUCATION_1 && monthlyIncome < 5000)then$c.setCheckResult(false);drools.halt();
endrule "如果申请人既没房也没车,同时学历为大专或本科,并且月薪少于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以上,那么发放的信用卡额度为15000"salience 1no-loop trueactivation-group "quota_group"when$c:CreditCardApplyInfo(checkResult == true &&((hasHouse == true && hasCar == true) ||(monthlyIncome > 20000)))then$c.setQuota(15000);
end
rule "如果申请人没房没车,但月收入在10000~20000之间,那么发放的信用卡额度为6000"salience 1no-loop trueactivation-group "quota_group"when$c:CreditCardApplyInfo(checkResult == true &&hasHouse == false &&hasCar == false &&monthlyIncome >= 10000 &&monthlyIncome <= 20000)then$c.setQuota(6000);
end
rule "如果申请人没房没车,月收入在10000以下,那么发放的信用卡额度为3000"salience 1no-loop trueactivation-group "quota_group"when$c:CreditCardApplyInfo(checkResult == true &&hasHouse == false &&hasCar == false &&monthlyIncome < 10000)then$c.setQuota(3000);
end
rule "如果申请人有房没车或者没房但有车,月收入在10000以下,那么发放的信用卡额度为5000"salience 1no-loop trueactivation-group "quota_group"when$c:CreditCardApplyInfo(checkResult == true &&((hasHouse == true && hasCar == false) ||(hasHouse == false && hasCar == true)) &&monthlyIncome < 10000)then$c.setQuota(5000);
end
rule "如果申请人有房没车或者是没房但有车,月收入在10000~20000之间,那么发放的信用卡额度为8000"salience 1no-loop trueactivation-group "quota_group"when$c:CreditCardApplyInfo(checkResult == true &&((hasHouse == true && hasCar == false) ||(hasHouse == false && hasCar == true)) &&monthlyIncome >= 10000 &&monthlyIncome <= 20000)then$c.setQuota(8000);
end

2.6、创建RuleService

package com.itheima.drools.service;import com.itheima.drools.entity.CreditCardApplyInfo;
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;//调用Drools规则引擎实现信用卡申请public CreditCardApplyInfo creditCardApply(CreditCardApplyInfo creditCardApplyInfo){KieSession session = kieBase.newKieSession();session.insert(creditCardApplyInfo);session.fireAllRules();session.dispose();return creditCardApplyInfo;}
}

2.7、创建RuleController

package com.itheima.drools.controller;import com.itheima.drools.entity.CreditCardApplyInfo;
import com.itheima.drools.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("rule")
public class RuleController
{@Autowiredprivate RuleService ruleService;@RequestMapping("/creditCardApply")public CreditCardApplyInfo creditCardApply(@RequestBody  CreditCardApplyInfo creditCardApplyInfo){creditCardApplyInfo = ruleService.creditCardApply(creditCardApplyInfo);return creditCardApplyInfo;}}

2.8、创建启动类DroolsApplication

package com.itheima.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);}
}

2.9、导入静态资源文件到resources/static目录下

前端测试页面:

<!DOCTYPE html>
<html>
<head><!-- 页面meta --><meta charset="utf-8"><title>XX银行信用卡申请</title><meta name="description" content="个人所得税计算"><meta name="keywords" content="个人所得税计算"><meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
</head>
<body class="mainBg">
<div id="app"><h3 align="center">XX银行信用卡申请-by/Gjs</h3><table align="center" width="50%" border="0"><tr><td>姓名</td><td><input type="text" v-model="creditCardApplyInfo.name"></td><td>性别</td><td><input type="text" v-model="creditCardApplyInfo.sex"></td></tr><tr><td>年龄</td><td><input type="text" v-model="creditCardApplyInfo.age"></td><td>手机号</td><td><input type="text" v-model="creditCardApplyInfo.telephone"></td></tr><tr><td>住址</td><td><input type="text" v-model="creditCardApplyInfo.address"></td><td>学历</td><td><select v-model="creditCardApplyInfo.education"><option value="专科以下">专科以下</option><option value="专科">专科</option><option value="本科">本科</option><option value="本科以上">本科以上</option></select></td></tr><tr><td>月收入</td><td><input type="text" v-model="creditCardApplyInfo.monthlyIncome"></td><td>现持有信用卡数量</td><td><input type="text" v-model="creditCardApplyInfo.hasCreditCardCount"></td></tr><tr><td>是否有房</td><td><select v-model="creditCardApplyInfo.hasHouse"><option value="true">是</option><option value="false">否</option></select></td><td>是否有车</td><td><select v-model="creditCardApplyInfo.hasCar"><option value="true">是</option><option value="false">否</option></select></td></tr><tr><td colspan="4" align="center"><br><input type="button" value="   申请   " @click="creditCardApply()"></td></tr><tr><td colspan="4" align="center">{{applyResultMessage}}</td></tr></table>
</div>
</body>
<!-- 引入组件库 -->
<script src="js/vue.js"></script>
<script src="js/axios.js"></script>
<script>new Vue({el: '#app',data:{creditCardApplyInfo:{education:"本科",hasHouse:true,hasCar:true},applyResultMessage:''},methods: {creditCardApply(){axios.post("/rule/creditCardApply",this.creditCardApplyInfo).then((res) => {if(res.data.checkResult){//审核通过this.applyResultMessage = "恭喜你信用卡申请成功,信用卡额度为:" + res.data.quota;}else{//审核失败this.applyResultMessage = "抱歉,您提交的信息审核未通过,您不能申请我行信用卡!";}});}}});
</script>
</html>

Drools实战-信用卡申请相关推荐

  1. 第2-4-8章 规则引擎Drools实战(1)-个人所得税计算器

    文章目录 9. Drools实战 9.1 个人所得税计算器 9.1.1 名词解释 9.1.2 计算规则 9.1.2.1 新税制主要有哪些变化? 9.1.2.2 资较高人员本次个税较少,可能到年底扣税增 ...

  2. 规则引擎Drools示例:个人所得税计算器、信用卡申请、保险产品准入规则

    Drools小示例 一.Drools实战 1.1 个人所得税计算器 1.1.1 名词解释 1.1.2 计算规则 1.1.3 实现步骤 1.2 信用卡申请 1.2.1 计算规则 1.2.2 实现步骤 1 ...

  3. 规则引擎drools系列(一)

    规则引擎 Drools 1. 问题引出 现有一个在线申请信用卡的业务场景,用户需要录入个人信息,如下图所示: //此处为伪代码 ​ //检查用户信息合法性,返回true表示检查通过,返回false表示 ...

  4. 别再说你不会,规则引擎Drools了

    一.为什么要使用规则引擎 有一天运营想弄一个积分策略,计算额外积分金额 ,规则如下: 订单原价金额 100以下, 不加分: 100-500 加100分: 500-1000 加500分: 1000 以上 ...

  5. Drools 规则引擎

    官网:https://www.drools.org/ 累了听听歌:http://www.hy57.com/p/158102.html 1. 快速度入门 1. 导入依赖 <dependencies ...

  6. Drools动态规则

    动态规则,是做规则引擎最想知道的问题,小编也加过一些群,大部分的人都喜欢问这样的问题. 规则只能写在文件里嘛 规则引擎能做什么 规则可动态配置嘛 在项目中使用规则引擎,业务人员怎么用呢? 规则变化了怎 ...

  7. 15、Drools自然语言DSL,DSLR的说明——6.4版本

    Drools自然语言:是业务人员通过dslr文件编写的规则文件,业务人员可能不懂技术.通过用文字描述实现业务规则. 但要将DSLR文件解析成机器能读懂的程序,必须要在dslr文件中引用解析业务人员所写 ...

  8. 15、Analyzer分析器之中文分析器的扩展

    其实在第五章节里已经有介绍过下面的分析器了,只是没有做例子,今天将下面没有做过例子分析器进行一个例子说明 paoding: 庖丁解牛最新版在  https://code.google.com/p/pa ...

  9. drools金融风险管理实战-郑圣杰-专题视频课程

    drools金融风险管理实战-42人已学习 课程介绍         本课程主要讲解drools.hbase等框架在金融风险管理行业的实际应用.包含drools规则的编写和调试:hbase数据存储.数 ...

最新文章

  1. 企业日常选择网站制作公司大多从这三方面着手
  2. AT4513-[AGC030D]InversionSum【dp】
  3. 大数据到底有多大,人工智能到底有多能
  4. python面试题(6)--- read、readline和readlines的区别
  5. Android 系统(224)---如何不显示开机SIM卡欢迎语
  6. linux make乱码,linux乱码
  7. jhu研究生录取 计算机,成绩一般被JHU信息系统管理MIS硕士录取
  8. MQ学习(一)----JMS规范(转发整合)
  9. c语言 笔试 多选题,全国计算机等级考试C语言十六个选择题类高频知识点
  10. 让知识构建未来—知识图谱技术与应用 | AI TIME-33
  11. 大数据分析的道与术总结
  12. java毕业设计二手交易系统Mybatis+系统+数据库+调试部署
  13. linux Systemd详解
  14. 【二十八宿】又叫二十八舍或二十八星,是今人为观测日、
  15. “在 System.Threading.ThreadAbortException 中第一次偶然出现的‘mscorlib.dll’类型的异常”的解决方法
  16. UNION ALL用法 以及 UNION ALL和UNION的区别
  17. 【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
  18. 【非集中申请期】国家自然科学基金最新申请指南情况汇总
  19. 选择国企背景的期货公司开户
  20. java vector实现的接口_java中List接口的实现类 ArrayList,LinkedList,Vector 的区别 list实现类源码分析...

热门文章

  1. uni-app:新大陆PDA盘点RFID
  2. Gnome桌面扩展安装
  3. ftp服务器上 批量移动文件路径,ftp服务器上 批量移动文件
  4. JS数组的push()、pop()、shift()和unshift()方法
  5. 【基于UDS服务的BootLoader架构和刷写流程】
  6. 30句必备餐饮英语口语交流无障碍
  7. python 立体图 交叉 平面_如何绘制相交平面?
  8. nvm下node安装;node环境变量配置
  9. U3D-亡命时速游戏制作(五)
  10. 什么是中小板它与创业板有什么不同