9.2 信用卡申请

全套代码及资料全部完整提供,点此处下载

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

9.2.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

9.2.2 实现步骤

第一步:创建maven工程creditCardApply并配置pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starters</artifactId>
  9. <version>2.0.6.RELEASE</version>
  10. </parent>
  11. <groupId>com.itheima</groupId>
  12. <artifactId>creditCardApply</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-aop</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-test</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>commons-lang</groupId>
  29. <artifactId>commons-lang</artifactId>
  30. <version>2.6</version>
  31. </dependency>
  32. <!--drools规则引擎-->
  33. <dependency>
  34. <groupId>org.drools</groupId>
  35. <artifactId>drools-core</artifactId>
  36. <version>7.6.0.Final</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.drools</groupId>
  40. <artifactId>drools-compiler</artifactId>
  41. <version>7.6.0.Final</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.drools</groupId>
  45. <artifactId>drools-templates</artifactId>
  46. <version>7.6.0.Final</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.kie</groupId>
  50. <artifactId>kie-api</artifactId>
  51. <version>7.6.0.Final</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.kie</groupId>
  55. <artifactId>kie-spring</artifactId>
  56. <exclusions>
  57. <exclusion>
  58. <groupId>org.springframework</groupId>
  59. <artifactId>spring-tx</artifactId>
  60. </exclusion>
  61. <exclusion>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>spring-beans</artifactId>
  64. </exclusion>
  65. <exclusion>
  66. <groupId>org.springframework</groupId>
  67. <artifactId>spring-core</artifactId>
  68. </exclusion>
  69. <exclusion>
  70. <groupId>org.springframework</groupId>
  71. <artifactId>spring-context</artifactId>
  72. </exclusion>
  73. </exclusions>
  74. <version>7.6.0.Final</version>
  75. </dependency>
  76. </dependencies>
  77. <build>
  78. <finalName>${project.artifactId}</finalName>
  79. <resources>
  80. <resource>
  81. <directory>src/main/java</directory>
  82. <includes>
  83. <include>**/*.xml</include>
  84. </includes>
  85. <filtering>false</filtering>
  86. </resource>
  87. <resource>
  88. <directory>src/main/resources</directory>
  89. <includes>
  90. <include>**/*.*</include>
  91. </includes>
  92. <filtering>false</filtering>
  93. </resource>
  94. </resources>
  95. <plugins>
  96. <plugin>
  97. <groupId>org.apache.maven.plugins</groupId>
  98. <artifactId>maven-compiler-plugin</artifactId>
  99. <version>2.3.2</version>
  100. <configuration>
  101. <source>1.8</source>
  102. <target>1.8</target>
  103. </configuration>
  104. </plugin>
  105. </plugins>
  106. </build>
  107. </project>

第二步:创建/resources/application.yml文件

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: creditCardApply

第三步:编写配置类DroolsConfig

  1. package com.itheima.drools.config;
  2. import org.kie.api.KieBase;
  3. import org.kie.api.KieServices;
  4. import org.kie.api.builder.KieBuilder;
  5. import org.kie.api.builder.KieFileSystem;
  6. import org.kie.api.builder.KieRepository;
  7. import org.kie.api.runtime.KieContainer;
  8. import org.kie.api.runtime.KieSession;
  9. import org.kie.internal.io.ResourceFactory;
  10. import org.kie.spring.KModuleBeanFactoryPostProcessor;
  11. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  12. import org.springframework.context.annotation.Bean;
  13. import org.springframework.context.annotation.Configuration;
  14. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  15. import org.springframework.core.io.support.ResourcePatternResolver;
  16. import org.springframework.core.io.Resource;
  17. import java.io.IOException;
  18. /**
  19. * 规则引擎配置类
  20. */
  21. @Configuration
  22. public class DroolsConfig {
  23. //指定规则文件存放的目录
  24. private static final String RULES_PATH = "rules/";
  25. private final KieServices kieServices = KieServices.Factory.get();
  26. @Bean
  27. @ConditionalOnMissingBean
  28. public KieFileSystem kieFileSystem() throws IOException {
  29. KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
  30. ResourcePatternResolver resourcePatternResolver =
  31. new PathMatchingResourcePatternResolver();
  32. Resource[] files =
  33. resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*");
  34. String path = null;
  35. for (Resource file : files) {
  36. path = RULES_PATH + file.getFilename();
  37. kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
  38. }
  39. return kieFileSystem;
  40. }
  41. @Bean
  42. @ConditionalOnMissingBean
  43. public KieContainer kieContainer() throws IOException {
  44. KieRepository kieRepository = kieServices.getRepository();
  45. kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
  46. KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
  47. kieBuilder.buildAll();
  48. return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
  49. }
  50. @Bean
  51. @ConditionalOnMissingBean
  52. public KieBase kieBase() throws IOException {
  53. return kieContainer().getKieBase();
  54. }
  55. @Bean
  56. @ConditionalOnMissingBean
  57. public KModuleBeanFactoryPostProcessor kiePostProcessor() {
  58. return new KModuleBeanFactoryPostProcessor();
  59. }
  60. }

第四步:编写实体类CreditCardApplyInfo

  1. package com.itheima.drools.entity;
  2. /**
  3. * 信用卡申请信息
  4. */
  5. public class CreditCardApplyInfo {
  6. public static final String EDUCATION_1 = "专科以下";
  7. public static final String EDUCATION_2 = "专科";
  8. public static final String EDUCATION_3 = "本科";
  9. public static final String EDUCATION_4 = "本科以上";
  10. private String name;
  11. private String sex;
  12. private int age;
  13. private String education;
  14. private String telephone;
  15. private double monthlyIncome = 0;//月收入
  16. private String address;
  17. private boolean hasHouse = false;//是否有房
  18. private boolean hasCar = false;//是否有车
  19. private int hasCreditCardCount = 0;//现持有信用卡数量
  20. private boolean checkResult = true;//审核是否通过
  21. private double quota = 0;//额度
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public String getSex() {
  29. return sex;
  30. }
  31. public void setSex(String sex) {
  32. this.sex = sex;
  33. }
  34. public int getAge() {
  35. return age;
  36. }
  37. public void setAge(int age) {
  38. this.age = age;
  39. }
  40. public String getEducation() {
  41. return education;
  42. }
  43. public void setEducation(String education) {
  44. this.education = education;
  45. }
  46. public String getTelephone() {
  47. return telephone;
  48. }
  49. public void setTelephone(String telephone) {
  50. this.telephone = telephone;
  51. }
  52. public double getMonthlyIncome() {
  53. return monthlyIncome;
  54. }
  55. public void setMonthlyIncome(double monthlyIncome) {
  56. this.monthlyIncome = monthlyIncome;
  57. }
  58. public String getAddress() {
  59. return address;
  60. }
  61. public void setAddress(String address) {
  62. this.address = address;
  63. }
  64. public boolean isHasHouse() {
  65. return hasHouse;
  66. }
  67. public void setHasHouse(boolean hasHouse) {
  68. this.hasHouse = hasHouse;
  69. }
  70. public boolean isHasCar() {
  71. return hasCar;
  72. }
  73. public void setHasCar(boolean hasCar) {
  74. this.hasCar = hasCar;
  75. }
  76. public int getHasCreditCardCount() {
  77. return hasCreditCardCount;
  78. }
  79. public void setHasCreditCardCount(int hasCreditCardCount) {
  80. this.hasCreditCardCount = hasCreditCardCount;
  81. }
  82. public boolean isCheckResult() {
  83. return checkResult;
  84. }
  85. public void setCheckResult(boolean checkResult) {
  86. this.checkResult = checkResult;
  87. }
  88. public double getQuota() {
  89. return quota;
  90. }
  91. public void setQuota(double quota) {
  92. this.quota = quota;
  93. }
  94. public String toString() {
  95. if(checkResult){
  96. return "审核通过,信用卡额度为:" + quota;
  97. }else {
  98. return "审核不通过";
  99. }
  100. }
  101. }

第五步:在resources/rules下创建规则文件creditCardApply.drl文件

  1. package com.itheima.creditCardApply
  2. import com.itheima.drools.entity.CreditCardApplyInfo
  3. //合法性检查
  4. rule "如果申请人既没房也没车,同时学历为大专以下,并且月薪少于5000,那么不通过"
  5. salience 10
  6. no-loop true
  7. when
  8. $c:CreditCardApplyInfo(hasCar == false &&
  9. hasHouse == false &&
  10. education == CreditCardApplyInfo.EDUCATION_1 &&
  11. monthlyIncome < 5000)
  12. then
  13. $c.setCheckResult(false);
  14. drools.halt();
  15. end
  16. rule "如果申请人既没房也没车,同时学历为大专或本科,并且月薪少于3000,那么不通过"
  17. salience 10
  18. no-loop true
  19. when
  20. $c:CreditCardApplyInfo(hasCar == false &&
  21. hasHouse == false &&
  22. (education == CreditCardApplyInfo.EDUCATION_2 ||
  23. education == CreditCardApplyInfo.EDUCATION_3) &&
  24. monthlyIncome < 3000)
  25. then
  26. $c.setCheckResult(false);
  27. drools.halt();
  28. end
  29. rule "如果申请人既没房也没车,同时学历为本科以上,并且月薪少于2000,同时之前没有信用卡的,那么不通过"
  30. salience 10
  31. no-loop true
  32. when
  33. $c:CreditCardApplyInfo(hasCar == false &&
  34. hasHouse == false &&
  35. education == CreditCardApplyInfo.EDUCATION_4 &&
  36. monthlyIncome < 2000 &&
  37. hasCreditCardCount == 0)
  38. then
  39. $c.setCheckResult(false);
  40. drools.halt();
  41. end
  42. rule "如果申请人现有的信用卡数量大于10,那么不通过"
  43. salience 10
  44. no-loop true
  45. when
  46. $c:CreditCardApplyInfo(hasCreditCardCount > 10)
  47. then
  48. $c.setCheckResult(false);
  49. drools.halt();
  50. end
  51. //--------------------------------------------------------------------------
  52. //确定额度
  53. rule "如果申请人有房有车,或者月收入在20000以上,那么发放的信用卡额度为15000"
  54. salience 1
  55. no-loop true
  56. activation-group "quota_group"
  57. when
  58. $c:CreditCardApplyInfo(checkResult == true &&
  59. ((hasHouse == true && hasCar == true) ||
  60. (monthlyIncome > 20000)))
  61. then
  62. $c.setQuota(15000);
  63. end
  64. rule "如果申请人没房没车,但月收入在10000~20000之间,那么发放的信用卡额度为6000"
  65. salience 1
  66. no-loop true
  67. activation-group "quota_group"
  68. when
  69. $c:CreditCardApplyInfo(checkResult == true &&
  70. hasHouse == false &&
  71. hasCar == false &&
  72. monthlyIncome >= 10000 &&
  73. monthlyIncome <= 20000)
  74. then
  75. $c.setQuota(6000);
  76. end
  77. rule "如果申请人没房没车,月收入在10000以下,那么发放的信用卡额度为3000"
  78. salience 1
  79. no-loop true
  80. activation-group "quota_group"
  81. when
  82. $c:CreditCardApplyInfo(checkResult == true &&
  83. hasHouse == false &&
  84. hasCar == false &&
  85. monthlyIncome < 10000)
  86. then
  87. $c.setQuota(3000);
  88. end
  89. rule "如果申请人有房没车或者没房但有车,月收入在10000以下,那么发放的信用卡额度为5000"
  90. salience 1
  91. no-loop true
  92. activation-group "quota_group"
  93. when
  94. $c:CreditCardApplyInfo(checkResult == true &&
  95. ((hasHouse == true && hasCar == false) ||
  96. (hasHouse == false && hasCar == true)) &&
  97. monthlyIncome < 10000)
  98. then
  99. $c.setQuota(5000);
  100. end
  101. rule "如果申请人有房没车或者是没房但有车,月收入在10000~20000之间,那么发放的信用卡额度为8000"
  102. salience 1
  103. no-loop true
  104. activation-group "quota_group"
  105. when
  106. $c:CreditCardApplyInfo(checkResult == true &&
  107. ((hasHouse == true && hasCar == false) ||
  108. (hasHouse == false && hasCar == true)) &&
  109. monthlyIncome >= 10000 &&
  110. monthlyIncome <= 20000)
  111. then
  112. $c.setQuota(8000);
  113. end

第六步:创建RuleService

  1. package com.itheima.drools.service;
  2. import com.itheima.drools.entity.CreditCardApplyInfo;
  3. import org.kie.api.KieBase;
  4. import org.kie.api.runtime.KieSession;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class RuleService {
  9. @Autowired
  10. private KieBase kieBase;
  11. //调用Drools规则引擎实现信用卡申请
  12. public CreditCardApplyInfo creditCardApply(CreditCardApplyInfo creditCardApplyInfo){
  13. KieSession session = kieBase.newKieSession();
  14. session.insert(creditCardApplyInfo);
  15. session.fireAllRules();
  16. session.dispose();
  17. return creditCardApplyInfo;
  18. }
  19. }

第七步:创建RuleController

  1. package com.itheima.drools.controller;
  2. import com.itheima.drools.entity.CreditCardApplyInfo;
  3. import com.itheima.drools.service.RuleService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping("/rule")
  10. public class RuleController {
  11. @Autowired
  12. private RuleService ruleService;
  13. @RequestMapping("/creditCardApply")
  14. public CreditCardApplyInfo creditCardApply(@RequestBody
  15. CreditCardApplyInfo creditCardApplyInfo){
  16. creditCardApplyInfo = ruleService.creditCardApply(creditCardApplyInfo);
  17. return creditCardApplyInfo;
  18. }
  19. }

第八步:创建启动类DroolsApplication

  1. package com.itheima.drools;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class DroolsApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DroolsApplication.class);
  8. }
  9. }

第九步:导入静态资源文件到resources/static目录下

全套代码及资料全部完整提供,点此处下载

第2-4-9章 规则引擎Drools实战(2)-信用卡申请的更多相关文章

  1. 开源规则引擎 drools

    java语言开发的开源业务规则引擎 DROOLS(JBOSS RULES )具有一个易于访问企业策略.易于调整以及易于管理的开源业务规则引擎,符合业内标准,速度快.效率高.业务分析师或审核人员可以利用 ...

  2. 规则引擎drools封装

    一.前言 网上规则引擎drools介绍很多,并且有很多细致的说明,作者也不敢托大说自己的好用,但作者经过2个项目使用过规则引擎后,自己对规则引擎的理解并进行封装,对规则内容及如何使用,有自己的一番实践 ...

  3. 开源规则引擎 Drools 学习笔记 之 -- 1 cannot be cast to org.drools.compiler.kie.builder.impl.InternalKieModule

    直接进入正题 我们在使用开源规则引擎 Drools 的时候, 启动的时候可能会抛出如下异常: Caused by: java.lang.ClassCastException: cn.com.cheng ...

  4. [Drools]JAVA规则引擎 -- Drools 2

    上一篇文章 http://blog.csdn.net/quzishen/archive/2011/01/25/6163012.aspx 描述了一些常用的drools的语法标签和一个模拟实例即发送积分的 ...

  5. 使用规则引擎Drools计算圆周率PI

    实际上是使用规则引擎能够更新工作内存区重新匹配规则实现迭代功能. 使用了策略模式实现. <规则引擎与RETE算法介绍> PPT : http://files.cnblogs.com/lov ...

  6. JAVA规则引擎 -- Drools

    Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...

  7. [Drools]JAVA规则引擎 -- Drools

    Drools是一个基于Java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...

  8. 小明历险记:规则引擎drools教程一

    小明是一家互联网公司的软件工程师,他们公司为了吸引新用户经常会搞活动,小明常常为了做活动加班加点很烦躁,这不今天呀又来了一个活动需求,我们大家一起帮他看看. 小明的烦恼 活动规则是根据用户购买订单的金 ...

  9. 规则引擎 drools

    https://www.jianshu.com/p/725811f420db 深入了解Drools 简单介绍 笔者正在做风控系统,风控系统里边存在非常多的规则(比如:age < 16 || ag ...

  10. 规则引擎drools的简单使用

    规则引擎适用于有复杂多变的规则,如商品满减.积分赠送.考勤规则等 一.引入maven依赖 <dependency> <groupId>org.drools</groupI ...

随机推荐

  1. 从云AK泄露利用看企业特权管理

    从云AK泄露利用看企业特权管理 目录 - 缘起 - 当前主流AK泄露检测方式 - 防止AK滥用的关键要素? - 哪些算特权账号管理? - 如何做特权账号管理? - 特权管理与堡垒机.IAM.零信任的关 ...

  2. 万星开源项目强势回归「GitHub 热点速览 v.22.38」

    本周霸榜的 GitHub 项目多为老项目,比如:老面孔的 theatre 凭借极其优秀的动画功底连续三天霸榜 TypeScript 类目.借 Figma 被 Adobe 收购之风,又出现在 GitHu ...

  3. Java SE 代码块

    1.代码块 基本语法 [修饰符]{ 代码 }; 修饰符 可选,要写的话,也只能写 static 代码块分为两类,使用static修饰的叫静态代码块,没有static修饰的,叫普通代码块/非静态代码块 ...

  4. day03-3私聊功能

    多用户即时通讯系统03 4.编码实现02 4.4功能实现-私聊功能实现 4.4.1思路分析 客户端 - 发送者: 用户在控制台输入信息,客户端接收内容 将消息构建成Messgae对象,通过对应的soc ...

  5. 使用mtr来判断网络丢包和网络延迟

    转载自:https://mp.weixin.qq.com/s/UsjzMS1_rdxenw0TPlqwyQ 常用的 ping,tracert,nslookup 一般用来判断主机的网络连通性,其实 Li ...

  6. 清理rook-ceph

    官方步骤文档:https://rook.io/docs/rook/v1.8/ceph-teardown.html 请注意需要清理的以下资源: rook-ceph namespace: The Rook ...

  7. MySql的InnoDB的三层B+树可以存储两千万左右条数据的计算逻辑

    总结/朱季谦 B+树是一种在非叶子节点存放排序好的索引而在叶子节点存放数据的数据结构,值得注意的是,在叶子节点中,存储的并非只是一行表数据,而是以页为单位存储,一个页可以包含多行表记录.非叶子节点存放 ...

  8. My life of Honker Security Commando

    红客突击队 && 红客突击分队 红客突击队,于2019年,由队长k龙联合国内多位顶尖高校研究生牵头成立.其团队从成立至今多次参加国际网络安全竞赛并取得良好成绩,积累了丰富的竞赛经验.团 ...

  9. 为Azure-云准备一个基于Red Hat 8.x 的虚拟机镜像

    由于公司最近要求部分项目上线到Azure云上,要求操作系统使用的Redhat 8.x,而且必须加固 而在Azure官网提供的镜像中,又没有Redhat,于是只有自己自定义Redhat镜像,最后加固,作 ...

  10. POJ2104 K-th number (整体二分)

    刚学了整体二分,用这种解法来解决这道题. 首先对于每个询问时可以二分解决的,这也是可以使用整体二分的前提.将原来的序列看成是插入操作,和询问操作和在一起根据值域进行二分.用树状数组来检验二分值. 1 ...