SpringBoot集成easy-rules规则引擎的流程是什么

其他教程   发布日期:2024年05月04日   浏览次数:357

这篇文章主要讲解了“SpringBoot集成easy-rules规则引擎的流程是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot集成easy-rules规则引擎的流程是什么”吧!

一、概述

通过将业务规则配置的配置文件中,可以精简代码,同时已于维护,当规则修改时,只需要修改配置文件即可。easy-rules是一个小巧的规则引擎,支持spring的SPEL表达式,同时还支持 Apache JEXL 表达式和 MVL 表达式。

二、项目中加入依赖

在项目的gradle中增加依赖关系。

build.gradle:

plugins {
id 'org.springframework.boot' version '3.0.5'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}

group = 'cn.springcamp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
compileOnly {
extendsFrom annotationProcessor
}
testCompileOnly {
extendsFrom testAnnotationProcessor
}
}

repositories {
mavenCentral()
}

dependencies {
implementation "org.springframework.boot:spring-boot-starter-json"
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.jeasy:easy-rules-core:4.1.0'
implementation 'org.jeasy:easy-rules-spel:4.1.0'
implementation 'org.jeasy:easy-rules-support:4.1.0'
annotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation 'org.junit.vintage:junit-vintage-engine'
testImplementation 'org.junit.vintage:junit-vintage-engine'
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2022.0.1"
}
}

test {
useJUnitPlatform()
}

三、配置文件

示例程序将业务规则放到配置文件中,业务规则配置文件(demo-rule.yml)代码:

name: "age rule"
description: ""
priority: 1
condition: "#person.getAdult() == false"
actions:
- "T(java.lang.System).out.println("Shop: Sorry, you are not allowed to buy alcohol")"
- "#person.setAdult(true)"
- "#person.setAge(18)"
---
name: "alcohol rule"
description: ""
priority: 1
condition: "#person.getAdult() == true"
actions:
- "T(java.lang.System).out.println("Shop: you are now allowed to buy alcohol")"

配置文件中的规则通过 condition 进行配置,当满足规则时,会调用 actions 中配置的动作。

示例项目使用了spring的SPEL表达式进行规则配置,配置文件中配置了2个规则,第一个规则通过

  1. person
这个spring bean中的getAdult()判断是否满足规则,满足规则时调用三个方法。

在spring-boot本身的配置文件中 application.yml 配置规则文件:

rule:
skip-on-first-failed-rule: true
skip-on-first-applied-rule: false
skip-on-first-non-triggered-rule: true
rules:
- rule-id: "demo"
rule-file-location: "classpath:demo-rule.yml"

四、代码中对规则引擎进行配置

通过

  1. RuleEngineConfig
这个spring的配置类对规则引擎进行配置:
  1. @Slf4j
  2. @EnableConfigurationProperties(RuleEngineConfigProperties.class)
  3. @Configuration
  4. public class RuleEngineConfig implements BeanFactoryAware {
  5. @Autowired(required = false)
  6. private List<RuleListener> ruleListeners;
  7. @Autowired(required = false)
  8. private List<RulesEngineListener> rulesEngineListeners;
  9. private BeanFactory beanFactory;
  10. @Bean
  11. public RulesEngineParameters rulesEngineParameters(RuleEngineConfigProperties properties) {
  12. RulesEngineParameters parameters = new RulesEngineParameters();
  13. parameters.setSkipOnFirstAppliedRule(properties.isSkipOnFirstAppliedRule());
  14. parameters.setSkipOnFirstFailedRule(properties.isSkipOnFirstFailedRule());
  15. parameters.setSkipOnFirstNonTriggeredRule(properties.isSkipOnFirstNonTriggeredRule());
  16. return parameters;
  17. }
  18. @Bean
  19. public RulesEngine rulesEngine(RulesEngineParameters rulesEngineParameters) {
  20. DefaultRulesEngine rulesEngine = new DefaultRulesEngine(rulesEngineParameters);
  21. if (!CollectionUtils.isEmpty(ruleListeners)) {
  22. rulesEngine.registerRuleListeners(ruleListeners);
  23. }
  24. if (!CollectionUtils.isEmpty(rulesEngineListeners)) {
  25. rulesEngine.registerRulesEngineListeners(rulesEngineListeners);
  26. }
  27. return rulesEngine;
  28. }
  29. @Bean
  30. public BeanResolver beanResolver() {
  31. return new BeanFactoryResolver(beanFactory);
  32. }
  33. @Bean
  34. public RuleEngineTemplate ruleEngineTemplate(RuleEngineConfigProperties properties, RulesEngine rulesEngine) {
  35. RuleEngineTemplate ruleEngineTemplate = new RuleEngineTemplate();
  36. ruleEngineTemplate.setBeanResolver(beanResolver());
  37. ruleEngineTemplate.setProperties(properties);
  38. ruleEngineTemplate.setRulesEngine(rulesEngine);
  39. return ruleEngineTemplate;
  40. }
  41. @Bean
  42. public RuleListener defaultRuleListener() {
  43. return new RuleListener() {
  44. @Override
  45. public boolean beforeEvaluate(Rule rule, Facts facts) {
  46. return true;
  47. }
  48. @Override
  49. public void afterEvaluate(Rule rule, Facts facts, boolean b) {
  50. log.info("-----------------afterEvaluate-----------------");
  51. log.info(rule.getName() + rule.getDescription() + facts.toString());
  52. }
  53. @Override
  54. public void beforeExecute(Rule rule, Facts facts) {
  55. log.info("-----------------beforeExecute-----------------");
  56. log.info(rule.getName() + rule.getDescription() + facts.toString());
  57. }
  58. @Override
  59. public void onSuccess(Rule rule, Facts facts) {
  60. log.info("-----------------onSuccess-----------------");
  61. log.info(rule.getName() + rule.getDescription() + facts.toString());
  62. }
  63. @Override
  64. public void onFailure(Rule rule, Facts facts, Exception e) {
  65. log.info("-----------------onFailure-----------------");
  66. log.info(rule.getName() + "----------" + rule.getDescription() + facts.toString() + e.toString());
  67. }
  68. };
  69. }
  70. @Override
  71. public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  72. this.beanFactory = beanFactory;
  73. }
  74. }

配置文件中配置了

  1. ruleEngineTemplate
这个spring bean,通过ruleEngineTemplate触发规则引擎的执行。

五、执行规则引擎

  1. ruleEngineTemplate
配置好后,我们可以在业务代码中执行规则引擎,处理配置文件中配置的业务规则:

最为演示,我们将规则引擎的执行代码放到了 Application 的 run 方法中,程序启动后立即执行规则引擎:

  1. @SpringBootApplication
  2. public class Application implements CommandLineRunner {
  3. @Autowired
  4. RuleEngineTemplate ruleEngineTemplate;
  5. public static void main(String[] args) {
  6. SpringApplication.run(Application.class, args);
  7. }
  8. @Override
  9. public void run(String... args) {
  10. Person person = new Person();
  11. Facts facts = new Facts();
  12. facts.put("person", person);
  13. ruleEngineTemplate.fire("demo", facts);
  14. }
  15. }

程序执行后可以看到控制台里打印了

  1. Shop: Sorry, you are not allowed to buy alcohol
,这个内容对应的是我们在规则文件中的actions中配置的
  1. "T(java.lang.System).out.println("Shop: Sorry, you are not allowed to buy alcohol")"
,说明规则成功执行了。

以上就是SpringBoot集成easy-rules规则引擎的流程是什么的详细内容,更多关于SpringBoot集成easy-rules规则引擎的流程是什么的资料请关注九品源码其它相关文章!