Java规则引擎-Easy Rules使用


规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策。接受数据输入,解释业务规则,并根据业务规则做出业务决策。
开源的规则引擎有很多,像Drools,本文中用到的Easy Rules,Apache Camel,等等。
今天我們來说说easy rule的使用

Easy Rules 介绍

Easy Rules是Java规则引擎,其灵感来自名为“我应该使用规则引擎?”的文章。 马丁·福勒(Martin Fowler)在其中说:

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

这正是Easy Rules所做的,它提供Rule抽象以创建带有条件和动作的规则,并提供RuleEngine API,该API通过一组规则运行以评估条件和执行动作。
github地址: https://github.com/j-easy/easy-rules

Easy Rules特性

  • 轻量级库和易于学习的API
  • 基于POJO的开发与注释编程模型
  • 有用的抽象定义业务规则并通过Java轻松应用
  • 从原始规则创建复合规则的能力
  • 使用表达式语言(如MVEL,SpEL和JEXL)定义规则的能力

Easy Rules的使用

使用注解的方式进行申明规则和使用

创建一个maven项目,项目名为:easy-rules-example,结构如下:
在这里插入图片描述
在pom.xml中引入相关的包,如下:

 <dependency>
     <groupId>org.jeasy</groupId>
     <artifactId>easy-rules-core</artifactId>
 </dependency>
 <dependency>
     <groupId>org.jeasy</groupId>
     <artifactId>easy-rules-support</artifactId>
 </dependency>

创建一个OperationRules.java,代码如下:

/**
 * 定义一个相关的规则
 *
 * @author tony
 * @date 2021/1/14 10:28
 */
 //注释以将类标记为规则
@Rule(name = "Operation rule", description = "根据不同的操作进行处理相关事务")
public class OperationRules {
	//将方法标记为规则条件的注释。 
	//必须注释任何不带参数的公共方法,并且该方法返回布尔值。
	//@Fact注解:将参数标记为事实的注释。 比对operation这个属性值是否为true,为true将进行action的操作
    @Condition
    public boolean operation(@Fact("operation") boolean operation) {
        return operation;
    }
    //将方法标记为规则操作的注释。 
    //必须注释任何不带参数的公共方法。 该方法的返回值将被引擎忽略。
    //注解中的order属性定义action的顺序
    @Action
    public void operationDesc() {
        System.out.println("当操作为true的时候会使用进来");
    }
}

创建一个测试类:TestQuickStart.java,代码如下:

public static void main(String[] args) {
        //定义相应的facts 一组事实并表示一个事实名称空间。事实在事实对象中具有唯一的名称。
        Facts facts = new Facts();
        //看相关的源码,是一个set集合保存fact,这里是需要全局唯一的name
        facts.put("operation", false);
        //此类封装了一组规则并表示规则名称空间。 规则在规则名称空间中必须具有唯一的名称。 
        //规则将基于Comparable.compareTo(Object)方法相互比较,因此Rule的实现应正确
        //实现compareTo以确保单个命名空间中的唯一规则名称。
        Rules rules = new Rules();
        //register方法注册一个或多个新规则
        rules.register(new OperationRules());
        //申明一个规则引擎
        RulesEngine engine = new DefaultRulesEngine();
        engine.fire(rules, facts);
    }

运行上面的代码,将facts中operation设置不同的值,将会有不同的结果。

使用MVL表达式进行规则的定义

在pom.xml中引入相关的包,如下:

 <dependency>
     <groupId>org.jeasy</groupId>
     <artifactId>easy-rules-mvel</artifactId>
 </dependency>

创建名为:OperationMVLExpression.java,代码如下:

/**
 * 使用mvl 表达式进行操作
 *
 * @author tony
 * @date 2021/1/14 11:31
 */
public class OperationMVLExpression {
    public static Rule operationRule() {
        return new MVELRule()
                .name("operation rule")
                .description("这是一个规则描述")
                .when("operation == true")
                .then("System.out.println(\"进行了相关操作\");");
    }
}

创建一个测试类:TestOperationMVLExpression.java,代码如下:

public static void main(String[] args) {
        Facts facts = new Facts();
        facts.put("operation", false);
        Rules rules = new Rules();
        rules.register(OperationMVLExpression.operationRule());
        RulesEngine engine = new DefaultRulesEngine();
        engine.fire(rules, facts);
    }

运行程序进行验证即可

使用fluent进行创建使用规则

在pom.xml中引入相关的包,如下:

 <dependency>
     <groupId>org.jeasy</groupId>
     <artifactId>easy-rules-core</artifactId>
 </dependency>
 <dependency>
     <groupId>org.jeasy</groupId>
     <artifactId>easy-rules-support</artifactId>
 </dependency>

创建一个类:OperationRuleFluent.java,代码如下:

/**
 * 使用fluent进行操作
 *
 * @author tony
 * @date 2021/1/14 11:23
 */
public class OperationRuleFluent {

    public static Rule operationRule() {
    	//生成器来创建规则实例
        return new RuleBuilder()
                .name("operation rule")
                .description("这是一个规则描述")
                .when(facts -> facts.get("operation").equals(true))
                .then(facts -> System.out.println("进行了相关操作"))
                .build();
    }
}

创建一个测试类:TestOperationRuleFluent.java,代码如下:

 public static void main(String[] args) {
    Facts facts = new Facts();
    facts.put("operation", true);
    //定义一个规则
    Rules rules = new Rules();
    rules.register(OperationRuleFluent.operationRule());
    //申明一个规则引擎
    RulesEngine engine = new DefaultRulesEngine();
    engine.fire(rules, facts);
}

加载yml进行配置规则

创建一个yml文件:operation-rule.yml

name: "operation rule"
description: "这是规则的描述"
condition: "operation == true"
actions:
  - "System.out.println(\"进行了相关的操作\");"

进行加载文件,进行定义规则:

public static Rule operation() throws Exception {
        MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
        return ruleFactory.createRule(new FileReader("operation-rule.yml"));
    }

创建一个测试类:TestOperationYml.java,代码如下:

public static void main(String[] args) throws Exception {
        Facts facts = new Facts();
        facts.put("operation", false);
        Rules rules = new Rules();
        rules.register(OperationRuleYmlFile.operation());
        RulesEngine engine = new DefaultRulesEngine();
        engine.fire(rules, facts);
    }

上面是Easy Rules使用的几种方式,有已进行多action的定义,规则满足的时候可以同事进行多个操作。还提供相关的监听机制,在规则触发之前和之后执行什么操作等,感觉使用起来还是比较简单你,大家可以试试,一起学习!

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Easy-rules是一个轻量级的Java规则引擎,可以帮助我们在应用程序中使用规则来进行业务逻辑处理。在Spring-boot中使用Easy-rules需要以下步骤: 1. 添加Easy-rules依赖 在pom.xml文件中添加Easy-rules依赖: ```xml <dependency> <groupId>org.easyrules</groupId> <artifactId>easy-rules-core</artifactId> <version>4.1.0</version> </dependency> ``` 2. 创建规则 创建一个规则类,继承自`org.easyrules.core.BasicRule`,并实现`org.easyrules.api.Rules`接口中的方法。例如: ```java public class AdultRule extends BasicRule { private Person person; public AdultRule(Person person) { super("AdultRule", "Check if person is an adult", 1); this.person = person; } @Override public boolean evaluate() { return person.getAge() >= 18; } @Override public void execute() throws Exception { System.out.println(person.getName() + " is an adult"); } } ``` 3. 配置规则引擎 在Spring-boot的配置类中配置规则引擎: ```java @Configuration public class RuleEngineConfiguration { @Bean public RulesEngine rulesEngine() { return new DefaultRulesEngine(); } } ``` 4. 执行规则 在需要执行规则的地方,注入规则引擎,然后将规则添加到规则引擎中并执行: ```java @Autowired private RulesEngine rulesEngine; public void executeRules(Person person) { Rules rules = new Rules(); rules.register(new AdultRule(person)); rulesEngine.fire(rules); } ``` 这样就可以在Spring-boot中使用Easy-rules规则引擎了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值