easy-rules:轻量级Java规则引擎,支持注解、表达式语言及复合规则

The simple, stupid rules engine for Java

分支1Tags18
当前项目代码仓暂无内容

Easy Rules
简单、愚蠢的Java™规则引擎

MIT许可证 构建状态 Maven Central Javadoc 项目状态


项目状态

截至2020年12月,Easy Rules处于维护模式。这意味着从现在开始只会处理错误修复。 版本4.1.x是唯一受支持的版本。请尽早考虑升级到此版本。

最新消息

  • 2020年12月6日:版本4.1发布,新增模块支持Apache JEXL作为额外的表达式语言!您可以在发布说明中找到其他更改的详细信息。

什么是Easy Rules?

Easy Rules是一个受Martin Fowler文章*“我应该使用规则引擎吗?”*启发的Java规则引擎,其中Martin说:

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

这正是Easy Rules所做的,它提供了Rule抽象来创建带有条件和动作的规则,以及RulesEngine API,该API遍历一组规则以评估条件并执行动作。

核心特性

  • 轻量级库和易于学习的API
  • 基于POJO的开发,带有注解编程模型
  • 有用的抽象来定义业务规则并使用Java轻松应用它们
  • 能够从基本规则创建复合规则
  • 能够使用表达式语言(如MVEL、SpEL和JEXL)定义规则

示例

1. 首先,定义你的规则..

或者使用注解以声明式方式:

@Rule(name = "weather rule", description = "if it rains then take an umbrella")
public class WeatherRule {

    @Condition
    public boolean itRains(@Fact("rain") boolean rain) {
        return rain;
    }
    
    @Action
    public void takeAnUmbrella() {
        System.out.println("It rains, take an umbrella!");
    }
}

或者通过流畅的 API 以编程方式实现:

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

或者使用表达式语言:

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

或者使用规则描述符:

如下所示,在 weather-rule.yml 示例文件中:

name: "weather rule"
description: "if it rains then take an umbrella"
condition: "rain == true"
actions:
  - "System.out.println(\"It rains, take an umbrella!\");"

当然,我会按照您的要求进行翻译。请您提供需要翻译的英文文本,我会将其转换成通俗、专业、优雅且流畅的中文内容,同时保持原始的 Markdown 格式。

MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
Rule weatherRule = ruleFactory.createRule(new FileReader("weather-rule.yml"));

2. 然后,发射它!

public class Test {
    public static void main(String[] args) {
        // define facts
        Facts facts = new Facts();
        facts.put("rain", true);

        // define rules
        Rule weatherRule = ...
        Rules rules = new Rules();
        rules.register(weatherRule);

        // fire rules on known facts
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
    }
}

这是 Easy Rules 的“Hello World”示例。您可以在维基中找到其他示例,如商店空调Web应用教程。

贡献

我们欢迎您通过 GitHub 的 pull request 为项目做出贡献。请注意,Easy Rules 目前处于维护模式,这意味着只有针对错误修复的 pull request 会得到考虑。

如果您认为发现了错误或有任何疑问,请使用问题跟踪器

精彩贡献者

感谢大家的贡献!

Easy Rules 在其他语言中的版本

谁在使用 Easy Rules?

致谢

YourKit Java Profiler

非常感谢 YourKit, LLC 为 Easy Rules 的开发提供免费的 YourKit Java Profiler 许可。

许可

Easy Rules 根据麻省理工学院许可协议发布。

The MIT License (MIT)

Copyright (c) 2021 Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

当然,我会按照您的要求进行翻译。不过,您似乎没有提供要翻译的文本内容。请您提供需要翻译的原文,我将会把它翻译成中文,同时保持 Markdown 格式和您所要求的其他特点。

项目介绍

Java 简洁而实用的规则引擎【此简介由AI生成】

定制我的领域
2165.26 K1.1 K访问 GitHub