easy rules
v4.1.0
截至 2020 年 12 月,Easy Rules 处于维护模式。这意味着从现在开始仅修复错误。版本 4.1.x 是唯一受支持的版本。请考虑尽早升级到此版本。
Easy Rules 是一个 Java 规则引擎,灵感来自一篇名为“我应该使用规则引擎吗?”的文章。马丁·福勒 (Martin Fowler) 的著作中马丁说:
您可以自己构建一个简单的规则引擎。您所需要做的就是创建一堆具有条件和操作的对象,将它们存储在集合中,并运行它们以评估条件并执行操作。
这正是 Easy Rules 所做的,它提供了Rule
抽象来创建带有条件和操作的规则,以及运行一组规则来评估条件和执行操作的RulesEngine
API。
@ 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!" );
}
}
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! " ); "
MVELRuleFactory ruleFactory = new MVELRuleFactory ( new YamlRuleDefinitionReader ());
Rule weatherRule = ruleFactory . createRule ( new FileReader ( "weather-rule.yml" ));
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 的你好世界。您可以在 wiki 中找到其他示例,例如 Shop、Airco 或 WebApp 教程。
欢迎您通过 GitHub 上的拉取请求为该项目做出贡献。请注意,Easy Rules 处于维护模式,这意味着仅考虑错误修复的拉取请求。
如果您认为发现了错误或有任何疑问,请使用问题跟踪器。
感谢大家的贡献!
非常感谢 YourKit, LLC 提供 YourKit Java Profiler 的免费许可证以支持 Easy Rules 的开发。
Easy Rules 是根据 MIT 许可证条款发布的:
The MIT License (MIT)
Copyright (c) 2021 Mahmoud Ben Hassine ([email protected])
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.