3Drools-信用卡额度实例 · SpringCloud微服务实战 · 看云

计算规则

合法性检查规则如下:


规则编号名称描述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

实现步骤

创建新模块fw-cloud-drools-example

第一步 创建应用配置application.yml

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

第二步 创建DroolsConfig

这里主要配置了Drools中使用的时间格式、规则文件存储的位置、Kie容器和知识库创建


@Configuration
public class DroolsConfig {

    static {
        System.setProperty("drools.dateformat", "yyyy-MM-dd HH:mm");
    }

    
    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();
    }
}

第三步 信用卡实体创建

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

第四步 创建 creditCardApply.drl规则文件

package com.yisu.drools.example.entity
import com.yisu.drools.example.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

@Service
public class RuleService {
    @Autowired
    private KieBase kieBase;

    
    public CreditCardApplyInfo creditCardApply(CreditCardApplyInfo creditCardApplyInfo){
        KieSession session = kieBase.newKieSession();
        session.insert(creditCardApplyInfo);
        session.fireAllRules();
        session.dispose();
        return creditCardApplyInfo;
    }
}

第六步 创建 RuleController

@RestController
@RequestMapping("/rule")
public class RuleController {
    @Autowired
    private RuleService ruleService;

    @RequestMapping("/creditCardApply")
    public CreditCardApplyInfo creditCardApply(@RequestBody
                                                       CreditCardApplyInfo creditCardApplyInfo){
        creditCardApplyInfo = ruleService.creditCardApply(creditCardApplyInfo);
        return creditCardApplyInfo;
    }
}

第七步 创建启动类

@SpringBootApplication
public class DroolsExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(DroolsExampleApplication.class);
    }
}

启动测试

Postman 输入 localhost:8080/rule/creditCardApply  进行测试,请求内容如下

{
	"name":"fwcloud",
	"sex":"男",
	"age":18,
	"education":"本科",
	"telephone":"18888888888",
	"monthlyIncome":"200000",
	"address":"火星",
	"hasHouse":true,
	"hasCar":true,
	"hasCreditCardCount":"9"
}

c4324abf389e876ce2897b81c74c8a13_MD5.png

返回内容如下

52c30cea4531614287a481db5a7387df_MD5.png

{
    "name": "fwcloud",
    "sex": "男",
    "age": 18,
    "education": "本科",
    "telephone": "18888888888",
    "monthlyIncome": 200000,
    "address": "火星",
    "hasHouse": true,
    "hasCar": true,
    "hasCreditCardCount": 9,
    "checkResult": true,
    "quota": 15000
}