1.  Easy Rules 概述

Easy Rules是一个Java规则引擎,灵感来自一篇名为《Should I use a Rules Engine?》的文章

规则引擎就是提供一种可选的计算模型。与通常的命令式模型(由带有条件和循环的命令依次组成)不同,规则引擎基于生产规则系统。这是一组生产规则,每条规则都有一个条件(condition)和一个动作(action)———— 简单地说,可以将其看作是一组if-then语句。

精妙之处在于规则可以按任何顺序编写,引擎会决定何时使用对顺序有意义的任何方式来计算它们。考虑它的一个好方法是系统运行所有规则,选择条件成立的规则,然后执行相应的操作。这样做的好处是,很多问题都很自然地符合这个模型:

  1. if car.owner.hasCellPhone then premium += 100;
  2. if car.model.theftRating > 4 then premium += 200;
  3. if car.owner.livesInDodgyArea && car.model.theftRating > 2 then premium += 300;

规则引擎是一种工具,它使得这种计算模型编程变得更容易。它可能是一个完整的开发环境,或者一个可以在传统平台上工作的框架。生产规则计算模型最适合仅解决一部分计算问题,因此规则引擎可以更好地嵌入到较大的系统中。

你可以自己构建一个简单的规则引擎。你所需要做的就是创建一组带有条件和动作的对象,将它们存储在一个集合中,然后遍历它们以评估条件并执行这些动作。

Easy Rules它提供Rule抽象以创建具有条件和动作的规则,并提供RuleEngine API,该API通过一组规则运行以评估条件并执行动作。

Easy Rules简单易用,只需两步:

首先,定义规则,方式有很多种

方式一:注解

  1. @Rule(name = "weather rule", description = "if it rains then take an umbrella")
  2. public class WeatherRule {
  3.  
  4. @Condition
  5. public boolean itRains(@Fact("rain") boolean rain) {
  6. return rain;
  7. }
  8.  
  9. @Action
  10. public void takeAnUmbrella() {
  11. System.out.println("It rains, take an umbrella!");
  12. }
  13. }

方式二:链式编程

  1. Rule weatherRule = new RuleBuilder()
  2. .name("weather rule")
  3. .description("if it rains then take an umbrella")
  4. .when(facts -> facts.get("rain").equals(true))
  5. .then(facts -> System.out.println("It rains, take an umbrella!"))
  6. .build();

方式三:表达式

  1. Rule weatherRule = new MVELRule()
  2. .name("weather rule")
  3. .description("if it rains then take an umbrella")
  4. .when("rain == true")
  5. .then("System.out.println(\"It rains, take an umbrella!\");");

方式四:yml配置文件

例如:weather-rule.yml

  1. name: "weather rule"
  2. description: "if it rains then take an umbrella"
  3. condition: "rain == true"
  4. actions:
  5. - "System.out.println(\"It rains, take an umbrella!\");"
  1. MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
  2. Rule weatherRule = ruleFactory.createRule(new FileReader("weather-rule.yml"));

接下来,应用规则

  1. public class Test {
  2. public static void main(String[] args) {
  3. // define facts
  4. Facts facts = new Facts();
  5. facts.put("rain", true);
  6.  
  7. // define rules
  8. Rule weatherRule = ...
  9. Rules rules = new Rules();
  10. rules.register(weatherRule);
  11.  
  12. // fire rules on known facts
  13. RulesEngine rulesEngine = new DefaultRulesEngine();
  14. rulesEngine.fire(rules, facts);
  15. }
  16. }

入门案例:Hello Easy Rules

  1. <dependency>
  2. <groupId>org.jeasy</groupId>
  3. <artifactId>easy-rules-core</artifactId>
  4. <version>4.0.0</version>
  5. </dependency>

通过骨架创建maven项目:

  1. mvn archetype:generate \
  2. -DarchetypeGroupId=org.jeasy \
  3. -DarchetypeArtifactId=easy-rules-archetype \
  4. -DarchetypeVersion=4.0.

默认给我们生成了一个HelloWorldRule规则,如下:

  1. package com.cjs.example.rules;
  2.  
  3. import org.jeasy.rules.annotation.Action;
  4. import org.jeasy.rules.annotation.Condition;
  5. import org.jeasy.rules.annotation.Rule;
  6.  
  7. @Rule(name = "Hello World rule", description = "Always say hello world")
  8. public class HelloWorldRule {
  9.  
  10. @Condition
  11. public boolean when() {
  12. return true;
  13. }
  14.  
  15. @Action
  16. public void then() throws Exception {
  17. System.out.println("hello world");
  18. }
  19.  
  20. }

2.  规则定义

2.1.  定义规则

大多数业务规则可以用以下定义表示:

  • Name : 一个命名空间下的唯一的规则名称
  • Description : 规则的简要描述
  • Priority : 相对于其他规则的优先级
  • Facts : 事实,可立即为要处理的数据
  • Conditions : 为了应用规则而必须满足的一组条件
  • Actions : 当条件满足时执行的一组动作

Easy Rules为每个关键点提供了一个抽象来定义业务规则。

在Easy Rules中,Rule接口代表规则

  1. public interface Rule {
  2.  
  3. /**
  4. * This method encapsulates the rule's conditions.
  5. * @return true if the rule should be applied given the provided facts, false otherwise
  6. */
  7. boolean evaluate(Facts facts);
  8.  
  9. /**
  10. * This method encapsulates the rule's actions.
  11. * @throws Exception if an error occurs during actions performing
  12. */
  13. void execute(Facts facts) throws Exception;
  14.  
  15. //Getters and setters for rule name, description and priority omitted.
  16.  
  17. }

evaluate方法封装了必须计算结果为TRUE才能触发规则的条件。execute方法封装了在满足规则条件时应该执行的动作。条件和操作由Condition和Action接口表示。

定义规则有两种方式:

  • 通过在POJO类上添加注解
  • 通过RuleBuilder API编程

可以在一个POJO类上添加@Rule注解,例如:

  1. @Rule(name = "my rule", description = "my rule description", priority = 1)
  2. public class MyRule {
  3.  
  4. @Condition
  5. public boolean when(@Fact("fact") fact) {
  6. //my rule conditions
  7. return true;
  8. }
  9.  
  10. @Action(order = 1)
  11. public void then(Facts facts) throws Exception {
  12. //my actions
  13. }
  14.  
  15. @Action(order = 2)
  16. public void finally() throws Exception {
  17. //my final actions
  18. }
  19.  
  20. }

@Condition注解指定规则条件
@Fact注解指定参数
@Action注解指定规则执行的动作

RuleBuilder支持链式风格定义规则,例如:

  1. Rule rule = new RuleBuilder()
  2. .name("myRule")
  3. .description("myRuleDescription")
  4. .priority(3)
  5. .when(condition)
  6. .then(action1)
  7. .then(action2)
  8. .build();

组合规则

CompositeRule由一组规则组成。这是一个典型地组合设计模式的实现。

组合规则是一个抽象概念,因为可以以不同方式触发组合规则。

Easy Rules自带三种CompositeRule实现:

  • UnitRuleGroup : 要么应用所有规则,要么不应用任何规则(AND逻辑)
  • ActivationRuleGroup : 它触发第一个适用规则,并忽略组中的其他规则(XOR逻辑)
  • ConditionalRuleGroup : 如果具有最高优先级的规则计算结果为true,则触发其余规则

复合规则可以从基本规则创建并注册为常规规则:

  1. //Create a composite rule from two primitive rules
  2. UnitRuleGroup myUnitRuleGroup = new UnitRuleGroup("myUnitRuleGroup", "unit of myRule1 and myRule2");
  3. myUnitRuleGroup.addRule(myRule1);
  4. myUnitRuleGroup.addRule(myRule2);
  5.  
  6. //Register the composite rule as a regular rule
  7. Rules rules = new Rules();
  8. rules.register(myUnitRuleGroup);
  9.  
  10. RulesEngine rulesEngine = new DefaultRulesEngine();
  11. rulesEngine.fire(rules, someFacts);

每个规则都有优先级。它代表触发注册规则的默认顺序。默认情况下,较低的值表示较高的优先级。可以重写compareTo方法以提供自定义优先级策略。

2.2.  定义事实

在Easy Rules中,Fact API代表事实

  1. public class Fact<T> {
  2. private final String name;
  3. private final T value;
  4. }

举个栗子:

  1. Fact<String> fact = new Fact("foo", "bar");
  2. Facts facts = new Facts();
  3. facts.add(fact);

或者,也可以用这样简写形式

  1. Facts facts = new Facts();
  2. facts.put("foo", "bar");

用@Fact注解可以将Facts注入到condition和action方法中

  1. @Rule
  2. class WeatherRule {
  3.  
  4. @Condition
  5. public boolean itRains(@Fact("rain") boolean rain) {
  6. return rain;
  7. }
  8.  
  9. @Action
  10. public void takeAnUmbrella(Facts facts) {
  11. System.out.println("It rains, take an umbrella!");
  12. // can add/remove/modify facts
  13. }
  14.  
  15. }

2.3.  定义规则引擎

Easy Rules提供两种RulesEngine接口实现:

  • DefaultRulesEngine : 根据规则的自然顺序应用规则
  • InferenceRulesEngine : 持续对已知事实应用规则,直到不再适用任何规则为止

创建规则引擎:

  1. RulesEngine rulesEngine = new DefaultRulesEngine();
  2.  
  3. // or
  4.  
  5. RulesEngine rulesEngine = new InferenceRulesEngine();

然后,注册规则

  1. rulesEngine.fire(rules, facts);

规则引擎有一些可配置的参数,如下图所示:

举个栗子:

  1. RulesEngineParameters parameters = new RulesEngineParameters()
  2. .rulePriorityThreshold(10)
  3. .skipOnFirstAppliedRule(true)
  4. .skipOnFirstFailedRule(true)
  5. .skipOnFirstNonTriggeredRule(true);
  6.  
  7. RulesEngine rulesEngine = new DefaultRulesEngine(parameters);

2.4. 定义规则监听器

通过实现RuleListener接口

  1. public interface RuleListener {
  2.  
  3. /**
  4. * Triggered before the evaluation of a rule.
  5. *
  6. * @param rule being evaluated
  7. * @param facts known before evaluating the rule
  8. * @return true if the rule should be evaluated, false otherwise
  9. */
  10. default boolean beforeEvaluate(Rule rule, Facts facts) {
  11. return true;
  12. }
  13.  
  14. /**
  15. * Triggered after the evaluation of a rule.
  16. *
  17. * @param rule that has been evaluated
  18. * @param facts known after evaluating the rule
  19. * @param evaluationResult true if the rule evaluated to true, false otherwise
  20. */
  21. default void afterEvaluate(Rule rule, Facts facts, boolean evaluationResult) { }
  22.  
  23. /**
  24. * Triggered on condition evaluation error due to any runtime exception.
  25. *
  26. * @param rule that has been evaluated
  27. * @param facts known while evaluating the rule
  28. * @param exception that happened while attempting to evaluate the condition.
  29. */
  30. default void onEvaluationError(Rule rule, Facts facts, Exception exception) { }
  31.  
  32. /**
  33. * Triggered before the execution of a rule.
  34. *
  35. * @param rule the current rule
  36. * @param facts known facts before executing the rule
  37. */
  38. default void beforeExecute(Rule rule, Facts facts) { }
  39.  
  40. /**
  41. * Triggered after a rule has been executed successfully.
  42. *
  43. * @param rule the current rule
  44. * @param facts known facts after executing the rule
  45. */
  46. default void onSuccess(Rule rule, Facts facts) { }
  47.  
  48. /**
  49. * Triggered after a rule has failed.
  50. *
  51. * @param rule the current rule
  52. * @param facts known facts after executing the rule
  53. * @param exception the exception thrown when attempting to execute the rule
  54. */
  55. default void onFailure(Rule rule, Facts facts, Exception exception) { }
  56.  
  57. }

3.  示例

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.cjs.example</groupId>
  5. <artifactId>easy-rules-quickstart</artifactId>
  6. <version>1.0.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.jeasy</groupId>
  11. <artifactId>easy-rules-core</artifactId>
  12. <version>4.0.0</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.jeasy</groupId>
  16. <artifactId>easy-rules-support</artifactId>
  17. <version>4.0.0</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.jeasy</groupId>
  21. <artifactId>easy-rules-mvel</artifactId>
  22. <version>4.0.0</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.slf4j</groupId>
  26. <artifactId>slf4j-simple</artifactId>
  27. <version>1.7.30</version>
  28. </dependency>
  29. </dependencies>
  30. </project>

4.  扩展

规则本质上是一个函数,如y=f(x1,x2,..,xn)

规则引擎就是为了解决业务代码和业务规则分离的引擎,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离。

还有一种常见的方式是Java+Groovy来实现,Java内嵌Groovy脚本引擎进行业务规则剥离。

https://github.com/j-easy/easy-rules/wiki

Java规则引擎 Easy Rules的更多相关文章

  1. JAVA规则引擎JSR-94笔札

    JAVA规则引擎JSR-94笔札 JSR-94 是由JCP(Java Community Process)组织所制定的java规则引擎API的java请求规范.它主要定义了规则引擎在java运行时的一 ...

  2. Java规则引擎及JSR-94[转]

      规则引擎简介 Java规则引擎是推理引擎的一种,它起源于基于规则的专家系统.       Java规则引擎将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.Java规则引擎接 ...

  3. [Drools]JAVA规则引擎 -- Drools- 转http://blog.csdn.net/quzishen/article/details/6163012

    [Drools]JAVA规则引擎 -- Drools 标签: java引擎exceptiongetterstringsetter 2011-01-25 14:33 113340人阅读 评论(35) 收 ...

  4. 【java规则引擎】之规则引擎解释

    转载:http://www.open-open.com/lib/view/open1417528754230.html 现实生活中,规则无处不在.法律.法规和各种制度均是:对于企业级应用来说,在IT技 ...

  5. JAVA规则引擎 -- Drools

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

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

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

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

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

  8. java 规则引擎资料汇集

    1. ibm的developworks中较早的一篇关于规则引擎的文章 https://www.ibm.com/developerworks/cn/java/j-java-rules/ 2. 一篇硕士论 ...

  9. 【java规则引擎】drools6.5.0版本中kmodule.xml解析

    kmodule.xml文件存放在src/main/resources/META-INF/文件夹下. <?xml version="1.0" encoding="UT ...

随机推荐

  1. 「雕爷学编程」Arduino动手做(11)——金属触摸模块

    37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...

  2. Django之ORM配置与单表操作

    ORM数据库操作流程: 1.    配置数据库(项目同名包中settings.py和__init__.py) 2.    定义类(app包中models.py),执行建表命令(Tools---> ...

  3. zabbix监控redis多实例cpu mem-自动发现

    1.自动发现实例端口脚本,用于zbx item prototypes #!/bin/bash REDIS_PORT=`ps aux |grep redis-server | grep -v 'grep ...

  4. ftp服务器搭建(二)

    1.已经安装好了vsftpd  进入到根目录下的/etc目录 ls查看一下 2.拷贝一下上面的两个配置文件 我拷贝到了我新建的目录中了 3.查看现在的网络连接方式——我的是-net方式 当然其他方式也 ...

  5. Spring BeanFactory 容器

    Spring 的 BeanFactory 容器 这是一个最简单的容器,它主要的功能是为依赖注入 (DI) 提供支持,这个容器接口在 org.springframework.beans.factory. ...

  6. Node.js NPM Tutorial

    Node.js NPM Tutorial – How to Get Started with NPM? NPM is the core of any application that is devel ...

  7. docker的file内容解释

    关键字---重点啊) FROM 基础镜像,当前新镜像是基于哪个镜像的 MAINTAINER  镜像维护者的姓名和邮箱地址 RUN  容器构建时需要运行的命令 EXPOSE 当前容器对外暴露的端口 WO ...

  8. Docker容器启动时初始化Mysql数据库

    1. 前言 Docker在开发中使用的越来越多了,最近搞了一个Spring Boot应用,为了方便部署将Mysql也放在Docker中运行.那么怎么初始化 SQL脚本以及数据呢? 我这里有两个传统方案 ...

  9. Network Motif 文献调研

    Network Motif 文献调研 概述:Network motifs,可以认为是网络中频繁出现的子图模式,是复杂网络的"构建块".有两篇发表在science上的论文给出moti ...

  10. React-Native 爬坑爬坑

    出现函数找不到问题一般都是this的指向问题,一般是用箭头函数解决,解决不了就传入this 在setState里面一定不能要直接写state引入的值,如: this.setState({now: th ...