最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java規(guī)則引擎Easy Rules的使用介紹

 更新時間:2020年06月11日 11:57:16   作者:廢物大師兄  
這篇文章主要介紹了Java規(guī)則引擎Easy Rules的使用介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. Easy Rules 概述

Easy Rules是一個Java規(guī)則引擎,靈感來自一篇名為《Should I use a Rules Engine?》的文章

規(guī)則引擎就是提供一種可選的計(jì)算模型。與通常的命令式模型(由帶有條件和循環(huán)的命令依次組成)不同,規(guī)則引擎基于生產(chǎn)規(guī)則系統(tǒng)。這是一組生產(chǎn)規(guī)則,每條規(guī)則都有一個條件(condition)和一個動作(action)———— 簡單地說,可以將其看作是一組if-then語句。

精妙之處在于規(guī)則可以按任何順序編寫,引擎會決定何時使用對順序有意義的任何方式來計(jì)算它們??紤]它的一個好方法是系統(tǒng)運(yùn)行所有規(guī)則,選擇條件成立的規(guī)則,然后執(zhí)行相應(yīng)的操作。這樣做的好處是,很多問題都很自然地符合這個模型:

if car.owner.hasCellPhone then premium += 100;
if car.model.theftRating > 4 then premium += 200;
if car.owner.livesInDodgyArea && car.model.theftRating > 2 then premium += 300;

規(guī)則引擎是一種工具,它使得這種計(jì)算模型編程變得更容易。它可能是一個完整的開發(fā)環(huán)境,或者一個可以在傳統(tǒng)平臺上工作的框架。生產(chǎn)規(guī)則計(jì)算模型最適合僅解決一部分計(jì)算問題,因此規(guī)則引擎可以更好地嵌入到較大的系統(tǒng)中。

你可以自己構(gòu)建一個簡單的規(guī)則引擎。你所需要做的就是創(chuàng)建一組帶有條件和動作的對象,將它們存儲在一個集合中,然后遍歷它們以評估條件并執(zhí)行這些動作。

Easy Rules它提供Rule抽象以創(chuàng)建具有條件和動作的規(guī)則,并提供RuleEngine API,該API通過一組規(guī)則運(yùn)行以評估條件并執(zhí)行動作。

Easy Rules簡單易用,只需兩步:

首先,定義規(guī)則,方式有很多種

方式一:注解

@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!");
  }
}

方式二:鏈?zhǔn)骄幊?/p>

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

方式三:表達(dá)式

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!\");");

方式四:yml配置文件

例如: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"));

接下來,應(yīng)用規(guī)則

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

入門案例:Hello Easy Rules

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

通過骨架創(chuàng)建maven項(xiàng)目:

mvn archetype:generate \
  -DarchetypeGroupId=org.jeasy \
  -DarchetypeArtifactId=easy-rules-archetype \
  -DarchetypeVersion=4.0.0

默認(rèn)給我們生成了一個HelloWorldRule規(guī)則,如下:

package com.cjs.example.rules;

import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Rule;

@Rule(name = "Hello World rule", description = "Always say hello world")
public class HelloWorldRule {

  @Condition
  public boolean when() {
    return true;
  }

  @Action
  public void then() throws Exception {
    System.out.println("hello world");
  }

}

2. 規(guī)則定義

2.1. 定義規(guī)則

大多數(shù)業(yè)務(wù)規(guī)則可以用以下定義表示:

  • Name : 一個命名空間下的唯一的規(guī)則名稱
  • Description : 規(guī)則的簡要描述
  • Priority : 相對于其他規(guī)則的優(yōu)先級
  • Facts : 事實(shí),可立即為要處理的數(shù)據(jù)
  • Conditions : 為了應(yīng)用規(guī)則而必須滿足的一組條件
  • Actions : 當(dāng)條件滿足時執(zhí)行的一組動作

Easy Rules為每個關(guān)鍵點(diǎn)提供了一個抽象來定義業(yè)務(wù)規(guī)則。

在Easy Rules中,Rule接口代表規(guī)則

public interface Rule {

  /**
  * This method encapsulates the rule's conditions.
  * @return true if the rule should be applied given the provided facts, false otherwise
  */
  boolean evaluate(Facts facts);

  /**
  * This method encapsulates the rule's actions.
  * @throws Exception if an error occurs during actions performing
  */
  void execute(Facts facts) throws Exception;

  //Getters and setters for rule name, description and priority omitted.

}

evaluate方法封裝了必須計(jì)算結(jié)果為TRUE才能觸發(fā)規(guī)則的條件。execute方法封裝了在滿足規(guī)則條件時應(yīng)該執(zhí)行的動作。條件和操作由Condition和Action接口表示。

定義規(guī)則有兩種方式:

  • 通過在POJO類上添加注解
  • 通過RuleBuilder API編程

可以在一個POJO類上添加@Rule注解,例如:

@Rule(name = "my rule", description = "my rule description", priority = 1)
public class MyRule {

  @Condition
  public boolean when(@Fact("fact") fact) {
    //my rule conditions
    return true;
  }

  @Action(order = 1)
  public void then(Facts facts) throws Exception {
    //my actions
  }

  @Action(order = 2)
  public void finally() throws Exception {
    //my final actions
  }
}

@Condition注解指定規(guī)則條件
@Fact注解指定參數(shù)
@Action注解指定規(guī)則執(zhí)行的動作

RuleBuilder支持鏈?zhǔn)斤L(fēng)格定義規(guī)則,例如:

Rule rule = new RuleBuilder()
        .name("myRule")
        .description("myRuleDescription")
        .priority(3)
        .when(condition)
        .then(action1)
        .then(action2)
        .build();

組合規(guī)則

CompositeRule由一組規(guī)則組成。這是一個典型地組合設(shè)計(jì)模式的實(shí)現(xiàn)。

組合規(guī)則是一個抽象概念,因?yàn)榭梢砸圆煌绞接|發(fā)組合規(guī)則。

Easy Rules自帶三種CompositeRule實(shí)現(xiàn):

  • UnitRuleGroup : 要么應(yīng)用所有規(guī)則,要么不應(yīng)用任何規(guī)則(AND邏輯)
  • ActivationRuleGroup : 它觸發(fā)第一個適用規(guī)則,并忽略組中的其他規(guī)則(XOR邏輯)
  • ConditionalRuleGroup : 如果具有最高優(yōu)先級的規(guī)則計(jì)算結(jié)果為true,則觸發(fā)其余規(guī)則

復(fù)合規(guī)則可以從基本規(guī)則創(chuàng)建并注冊為常規(guī)規(guī)則:

//Create a composite rule from two primitive rules
UnitRuleGroup myUnitRuleGroup = new UnitRuleGroup("myUnitRuleGroup", "unit of myRule1 and myRule2");
myUnitRuleGroup.addRule(myRule1);
myUnitRuleGroup.addRule(myRule2);

//Register the composite rule as a regular rule
Rules rules = new Rules();
rules.register(myUnitRuleGroup);

RulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.fire(rules, someFacts);

每個規(guī)則都有優(yōu)先級。它代表觸發(fā)注冊規(guī)則的默認(rèn)順序。默認(rèn)情況下,較低的值表示較高的優(yōu)先級。可以重寫compareTo方法以提供自定義優(yōu)先級策略。

2.2. 定義事實(shí)

在Easy Rules中,F(xiàn)act API代表事實(shí)

public class Fact<T> {
   private final String name;
   private final T value;
}

舉個栗子:

Fact<String> fact = new Fact("foo", "bar");
Facts facts = new Facts();
facts.add(fact);

或者,也可以用這樣簡寫形式

Facts facts = new Facts();
facts.put("foo", "bar");

用@Fact注解可以將Facts注入到condition和action方法中

@Rule
class WeatherRule {

  @Condition
  public boolean itRains(@Fact("rain") boolean rain) {
    return rain;
  }

  @Action
  public void takeAnUmbrella(Facts facts) {
    System.out.println("It rains, take an umbrella!");
    // can add/remove/modify facts
  }

}

2.3. 定義規(guī)則引擎

Easy Rules提供兩種RulesEngine接口實(shí)現(xiàn):

  • DefaultRulesEngine : 根據(jù)規(guī)則的自然順序應(yīng)用規(guī)則
  • InferenceRulesEngine : 持續(xù)對已知事實(shí)應(yīng)用規(guī)則,直到不再適用任何規(guī)則為止

創(chuàng)建規(guī)則引擎:

RulesEngine rulesEngine = new DefaultRulesEngine();

// or

RulesEngine rulesEngine = new InferenceRulesEngine();

然后,注冊規(guī)則

rulesEngine.fire(rules, facts);

規(guī)則引擎有一些可配置的參數(shù),如下圖所示:

舉個栗子:

RulesEngineParameters parameters = new RulesEngineParameters()
  .rulePriorityThreshold(10)
  .skipOnFirstAppliedRule(true)
  .skipOnFirstFailedRule(true)
  .skipOnFirstNonTriggeredRule(true);

RulesEngine rulesEngine = new DefaultRulesEngine(parameters);

2.4. 定義規(guī)則監(jiān)聽器

通過實(shí)現(xiàn)RuleListener接口

public interface RuleListener {

  /**
   * Triggered before the evaluation of a rule.
   *
   * @param rule being evaluated
   * @param facts known before evaluating the rule
   * @return true if the rule should be evaluated, false otherwise
   */
  default boolean beforeEvaluate(Rule rule, Facts facts) {
    return true;
  }

  /**
   * Triggered after the evaluation of a rule.
   *
   * @param rule that has been evaluated
   * @param facts known after evaluating the rule
   * @param evaluationResult true if the rule evaluated to true, false otherwise
   */
  default void afterEvaluate(Rule rule, Facts facts, boolean evaluationResult) { }

  /**
   * Triggered on condition evaluation error due to any runtime exception.
   *
   * @param rule that has been evaluated
   * @param facts known while evaluating the rule
   * @param exception that happened while attempting to evaluate the condition.
   */
  default void onEvaluationError(Rule rule, Facts facts, Exception exception) { }

  /**
   * Triggered before the execution of a rule.
   *
   * @param rule the current rule
   * @param facts known facts before executing the rule
   */
  default void beforeExecute(Rule rule, Facts facts) { }

  /**
   * Triggered after a rule has been executed successfully.
   *
   * @param rule the current rule
   * @param facts known facts after executing the rule
   */
  default void onSuccess(Rule rule, Facts facts) { }

  /**
   * Triggered after a rule has failed.
   *
   * @param rule the current rule
   * @param facts known facts after executing the rule
   * @param exception the exception thrown when attempting to execute the rule
   */
  default void onFailure(Rule rule, Facts facts, Exception exception) { }

}

3. 示例

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cjs.example</groupId>
  <artifactId>easy-rules-quickstart</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <dependencies>
    <dependency>
      <groupId>org.jeasy</groupId>
      <artifactId>easy-rules-core</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.jeasy</groupId>
      <artifactId>easy-rules-support</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.jeasy</groupId>
      <artifactId>easy-rules-mvel</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.30</version>
    </dependency>
  </dependencies>
</project>

4. 擴(kuò)展

規(guī)則本質(zhì)上是一個函數(shù),如y=f(x1,x2,..,xn)

規(guī)則引擎就是為了解決業(yè)務(wù)代碼和業(yè)務(wù)規(guī)則分離的引擎,是一種嵌入在應(yīng)用程序中的組件,實(shí)現(xiàn)了將業(yè)務(wù)決策從應(yīng)用程序代碼中分離。

還有一種常見的方式是Java+Groovy來實(shí)現(xiàn),Java內(nèi)嵌Groovy腳本引擎進(jìn)行業(yè)務(wù)規(guī)則剝離。

https://github.com/j-easy/easy-rules/wiki

到此這篇關(guān)于Java規(guī)則引擎Easy Rules的使用介紹的文章就介紹到這了,更多相關(guān)Java規(guī)則引擎Easy Rules內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mybatis-plus 處理大數(shù)據(jù)插入太慢的解決

    mybatis-plus 處理大數(shù)據(jù)插入太慢的解決

    這篇文章主要介紹了mybatis-plus 處理大數(shù)據(jù)插入太慢的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 淺談Java編程中string的理解與運(yùn)用

    淺談Java編程中string的理解與運(yùn)用

    這篇文章主要介紹了淺談Java編程中string的理解與運(yùn)用,還是比較不錯的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • windows環(huán)境下java開發(fā)工具maven的安裝教程圖解

    windows環(huán)境下java開發(fā)工具maven的安裝教程圖解

    Maven是一個項(xiàng)目管理和綜合工具。Maven提供了開發(fā)人員構(gòu)建一個完整的生命周期框架。這篇文章主要介紹了windows環(huán)境下java開發(fā)工具maven的安裝,非常不錯對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • easyExcel分批導(dǎo)入文件方式

    easyExcel分批導(dǎo)入文件方式

    這篇文章主要介紹了easyExcel分批導(dǎo)入文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Java多線程之線程安全問題詳情

    Java多線程之線程安全問題詳情

    這篇文章主要介紹了Java多線程之線程安全問題詳情,線程安全問題是指因多線程搶占式執(zhí)行而導(dǎo)致程序出現(xiàn)bug的問題。內(nèi)容介紹詳細(xì)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • 簡單學(xué)習(xí)Java API 設(shè)計(jì)實(shí)踐

    簡單學(xué)習(xí)Java API 設(shè)計(jì)實(shí)踐

    API(Application Programming Interface,應(yīng)用程序編程接口)是一些預(yù)先定義的函數(shù),目的是提供應(yīng)用程序與開發(fā)人員基于某軟件或硬件的以訪問一組例程的能力,而又無需訪問源碼,或理解內(nèi)部工作機(jī)制的細(xì)節(jié)。需要的可以了解一下
    2019-06-06
  • Java連接操作Oracle數(shù)據(jù)庫代碼詳解

    Java連接操作Oracle數(shù)據(jù)庫代碼詳解

    這篇文章主要介紹了Java連接操作Oracle數(shù)據(jù)庫代碼詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • 極簡的Resty服務(wù)端和客戶端RESTful框架

    極簡的Resty服務(wù)端和客戶端RESTful框架

    這篇文章主要為大家介紹了極簡的Resty服務(wù)端和客戶端RESTful框架的好處及作用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Java 異常的知識整理

    Java 異常的知識整理

    這篇文章主要介紹了Java 異常的知識整理的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 阿里面試Nacos配置中心交互模型是push還是pull原理解析

    阿里面試Nacos配置中心交互模型是push還是pull原理解析

    這篇文章主要為大家介紹了阿里面試Nacos配置中心交互模型是push還是pull原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07

最新評論

汝城县| 浑源县| 威信县| 南和县| 镇安县| 永平县| 郯城县| 阿巴嘎旗| 长治县| 华容县| 定边县| 普陀区| 鹤岗市| 临泉县| 郓城县| 简阳市| 通江县| 沅陵县| 安吉县| 商河县| 通山县| 湖南省| 商都县| 丰镇市| 娱乐| 天门市| 繁峙县| 屏边| 江陵县| 和静县| 驻马店市| 鄂尔多斯市| 临朐县| 腾冲县| 娱乐| 舟山市| 施秉县| 盐山县| 华蓥市| 稻城县| 恩平市|