java规则引擎easy-rules使用指南 1 - 基本用法

本文介绍了Java轻量级规则引擎Easy-Rules的使用,包括如何引入、创建规则和动作、使用Rule的多种方式(如RuleBuilder、注解、表达式语言和YML配置)、复合规则的实现及如何使用Facts。通过示例展示了如何在Java应用程序中灵活地应用规则引擎。
摘要由CSDN通过智能技术生成

规则引擎能干什么

规则引擎的工作方式有点像if-else,它允许你设置一些条件和动作,然后在程序运行时判断某些动作该不该执行。
easy-rules是一款轻量级的java规则引擎,目前它的长期支持版本是4.1.x,所以我们就以4.1.0版本来看一下如何使用。

如何引入

如果使用maven,可以直接在pom中加入:

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

如果需要对MVEL, SpEL和JEXL表达式的支持,还需要引入相应的支持包:

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

<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-rules-spel</artifactId>
    <version>4.1.0</version>
</dependency>

<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-rules-jexl</artifactId>
    <version>4.1.0</version>
</dependency>

一个简单的例子

使用easy-rules非常简单,只需要两个步骤:

  • 创建规则和动作
  • 运行引擎

以下是一个简单的例子:

public class Test {
    public static void main(String[] args) {
		    // define rules 
				Rule weatherRule = new RuleBuilder()
        .name("weather rule")
        .description("if it rains then take an umbrella")
        .when(facts -> facts.get("rain").equals(true))
        .then(facts -> System.out.println("It rains, take an umbrella!"))
        .build();
        Rules rules = new Rules();
        rules.register(weatherRule);
		
        // define facts
        Facts facts = new Facts();
 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值