• 第2-4-9章 规则引擎Drools实战(2)-信用卡申请


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

    
    <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.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-startersartifactId>
            <version>2.0.6.RELEASEversion>
        parent>
        <groupId>com.itheimagroupId>
        <artifactId>creditCardApplyartifactId>
        <version>1.0-SNAPSHOTversion>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-aopartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
            <dependency>
                <groupId>commons-langgroupId>
                <artifactId>commons-langartifactId>
                <version>2.6version>
            dependency>
            
            <dependency>
                <groupId>org.droolsgroupId>
                <artifactId>drools-coreartifactId>
                <version>7.6.0.Finalversion>
            dependency>
            <dependency>
                <groupId>org.droolsgroupId>
                <artifactId>drools-compilerartifactId>
                <version>7.6.0.Finalversion>
            dependency>
            <dependency>
                <groupId>org.droolsgroupId>
                <artifactId>drools-templatesartifactId>
                <version>7.6.0.Finalversion>
            dependency>
            <dependency>
                <groupId>org.kiegroupId>
                <artifactId>kie-apiartifactId>
                <version>7.6.0.Finalversion>
            dependency>
            <dependency>
                <groupId>org.kiegroupId>
                <artifactId>kie-springartifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframeworkgroupId>
                        <artifactId>spring-txartifactId>
                    exclusion>
                    <exclusion>
                        <groupId>org.springframeworkgroupId>
                        <artifactId>spring-beansartifactId>
                    exclusion>
                    <exclusion>
                        <groupId>org.springframeworkgroupId>
                        <artifactId>spring-coreartifactId>
                    exclusion>
                    <exclusion>
                        <groupId>org.springframeworkgroupId>
                        <artifactId>spring-contextartifactId>
                    exclusion>
                exclusions>
                <version>7.6.0.Finalversion>
            dependency>
        dependencies>
        <build>
            <finalName>${project.artifactId}finalName>
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.*include>
                    includes>
                    <filtering>falsefiltering>
                resource>
            resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>2.3.2version>
                    <configuration>
                        <source>1.8source>
                        <target>1.8target>
                    configuration>
                plugin>
            plugins>
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107

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

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

    第三步:编写配置类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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    第四步:编写实体类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 "审核不通过";
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130

    第五步:在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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    第六步:创建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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    第七步:创建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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    第八步:创建启动类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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    第九步:导入静态资源文件到resources/static目录下
    全套代码及资料全部完整提供,点此处下载

  • 相关阅读:
    【UI自动化测试】selenium + python3使用总结(二)
    【关于Java:认识异常】
    电阻:分压造成的流血事件
    你知道有什么好的文字翻译软件吗?这几个软件非常好用
    安卓(Android)面试题库(含答案)
    Linux之ssh
    springboot + redis实现签到与统计功能
    MySQL常见的数据查询慢甚至导致死锁问题
    TODOS案例
    Tomcat更换端口号部署多项目
  • 原文地址:https://blog.csdn.net/weixin_42208775/article/details/128110174