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文件

<?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>com.itheima</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>

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

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

第三步:编写配置类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.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();
@Bean
@ConditionalOnMissingBean
public KieFileSystem kieFileSystem() throws IOException {
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
@ConditionalOnMissingBean
public 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
@ConditionalOnMissingBean
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
@ConditionalOnMissingBean
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
return new KModuleBeanFactoryPostProcessor();
}
}

第四步:编写实体类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 "审核不通过";
}
}
}

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

package com.itheima.creditCardApply
import com.itheima.drools.entity.CreditCardApplyInfo //合法性检查
rule "如果申请人既没房也没车,同时学历为大专以下,并且月薪少于5000,那么不通过"
salience 10
no-loop true
when
$c:CreditCardApplyInfo(hasCar == false &&
hasHouse == false &&
education == CreditCardApplyInfo.EDUCATION_1 &&
monthlyIncome < 5000)
then
$c.setCheckResult(false);
drools.halt();
end
rule "如果申请人既没房也没车,同时学历为大专或本科,并且月薪少于3000,那么不通过"
salience 10
no-loop true
when
$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 10
no-loop true
when
$c:CreditCardApplyInfo(hasCar == false &&
hasHouse == false &&
education == CreditCardApplyInfo.EDUCATION_4 &&
monthlyIncome < 2000 &&
hasCreditCardCount == 0)
then
$c.setCheckResult(false);
drools.halt();
end
rule "如果申请人现有的信用卡数量大于10,那么不通过"
salience 10
no-loop true
when
$c:CreditCardApplyInfo(hasCreditCardCount > 10)
then
$c.setCheckResult(false);
drools.halt();
end
//--------------------------------------------------------------------------
//确定额度
rule "如果申请人有房有车,或者月收入在20000以上,那么发放的信用卡额度为15000"
salience 1
no-loop true
activation-group "quota_group"
when
$c:CreditCardApplyInfo(checkResult == true &&
((hasHouse == true && hasCar == true) ||
(monthlyIncome > 20000)))
then
$c.setQuota(15000);
end
rule "如果申请人没房没车,但月收入在10000~20000之间,那么发放的信用卡额度为6000"
salience 1
no-loop true
activation-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 1
no-loop true
activation-group "quota_group"
when
$c:CreditCardApplyInfo(checkResult == true &&
hasHouse == false &&
hasCar == false &&
monthlyIncome < 10000)
then
$c.setQuota(3000);
end
rule "如果申请人有房没车或者没房但有车,月收入在10000以下,那么发放的信用卡额度为5000"
salience 1
no-loop true
activation-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 1
no-loop true
activation-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

第六步:创建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 {
@Autowired
private KieBase kieBase; //调用Drools规则引擎实现信用卡申请
public CreditCardApplyInfo creditCardApply(CreditCardApplyInfo creditCardApplyInfo){
KieSession session = kieBase.newKieSession();
session.insert(creditCardApplyInfo);
session.fireAllRules();
session.dispose();
return creditCardApplyInfo;
}
}

第七步:创建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 {
@Autowired
private RuleService ruleService; @RequestMapping("/creditCardApply")
public CreditCardApplyInfo creditCardApply(@RequestBody
CreditCardApplyInfo creditCardApplyInfo){
creditCardApplyInfo = ruleService.creditCardApply(creditCardApplyInfo);
return creditCardApplyInfo;
}
}

第八步:创建启动类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);
}
}

第九步:导入静态资源文件到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. 【微服务】- Nacos - 注册中心

    微服务 - 注册中心 - Nacos 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 上一 ...

  2. Kibana:在Kibana中对数据进行深入分析

  3. 第五章:Admin管理后台 - 3:Admin文档生成器

    Django的admindocs应用可以从模型.视图.模板标签等地方获得文档内容. 一.概览 要激活admindocs,请按下面的步骤操作: 在INSTALLED_APPS内添加django.cont ...

  4. 《深入理解Elasticsearch》读书笔记 ---重点概念汇总

    文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247483918&idx=1&sn=a9f2ad3 ...

  5. 媒介查询兼容各种端口的响应式范围取值(移动端、PC端、ipad、移动端侧屏)

    !!!(chrome作者亲测)!!!数据仅供参考 /*ipad*/@media screen and (min-width:760px) and (max-width:1000px) /*移动端*/@ ...

  6. PHP全栈开发(七):PHP与MySQL存储交互(1.连接、创建数据库;创建数据表)

    在Linux服务器中可以使用命令 mysqladmin -u root password beijing2007; 来修改MySQL的root用户的密码. 但是在我们自己安装了wampserver的电 ...

  7. 洛谷P4638 SHOI2011 银行 ( 最大流)

    类似题目(一模一样):http://poj.org/problem?id=1149 我这里以poj1149的PIGS为例, 新建源点s和汇点t,n个顾客作为中间的点,,对于每个顾客,他可以解锁一定的猪 ...

  8. SpringBoot(三) - Slf4j+logback 日志,异步请求,定时任务

    1.Slf4j+logback 日志 SpringBoot框架的默认日志实现:slf4j + logback: 默认日志级别:info,对应了实际生产环境日志级别: 1.1 日志级别 # 常见的日志框 ...

  9. uoj220【NOI2016】网格

    刚了几个小时啊,这tm要是noi我怕不是直接滚粗了.我判答案为1的情况试了几种做法,最后终于想到了一个靠谱的做法,然后细节巨多,调了好久,刚拿到97分时代码有6.2KB了,后来发现有些东西好像没啥用就 ...

  10. vue中将验证表单输入框的方法写在一个js文件中(表达式验证邮箱、身份证、号码、两次输入的密码是否一致)

    文章目录 1.实现的效果 2.编写的js文件(这里写在了api文件下) 3.在vue页面中引入(script) 4.页面代码 1.实现的效果 20220606_154646 2.编写的js文件(这里写 ...