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.