AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

四個流行的Java開源規則引擎和入門

來源: 責編: 時間:2023-10-17 10:01:59 326觀看
導讀在許多業務系統中,會有大量的業務規則配置,而且隨著政策制度、業務流程甚至是管理者的決策發生變化,這些業務規則也需要進行更改。這種變化在一些行業特別頻繁,并且要求快速響應。規則引擎的作用是為了適應這種變更需求,實

在許多業務系統中,會有大量的業務規則配置,而且隨著政策制度、業務流程甚至是管理者的決策發生變化,這些業務規則也需要進行更改。這種變化在一些行業特別頻繁,并且要求快速響應。x6e28資訊網——每日最新資訊28at.com

x6e28資訊網——每日最新資訊28at.com

規則引擎的作用是為了適應這種變更需求,實現業務系統快速且低成本的更新。一般是將業務規則的配置單獨拿出來,使之與業務系統保持低耦合,如果這個用于配置的模塊做得足夠通用且獨立,那么它就可以成為一個規則引擎系統。通過規則引擎可以快速響應業務規則的變化。這種方式不需要修改代碼,減少了修改業務代碼之后出現錯誤的可能性,如果規則引擎提供前端操作界面,還能夠支持業務人員輕松上手配置業務規則。 x6e28資訊網——每日最新資訊28at.com

x6e28資訊網——每日最新資訊28at.com

本文主要分享一些基于Java的規則引擎,這些規則引擎是目前比較流行的項目,包括:Drolls、Easy RulesRuleBook、OpenL Tablets。并簡單介紹這些規則引擎的使用方式。x6e28資訊網——每日最新資訊28at.com

1.Drools

https://www.drools.org/x6e28資訊網——每日最新資訊28at.com

https://github.com/kiegroup/droolsx6e28資訊網——每日最新資訊28at.com

x6e28資訊網——每日最新資訊28at.com

Drools是一個業務規則管理系統(BRMS)。主要功能模塊包括:核心業務規則引擎(BRE)、Web創作和規則管理應用程序(Drools Workbench)、決策模型和符號(DMN)模型以及用于開發的IDE插件(idea、eclipse等)。x6e28資訊網——每日最新資訊28at.com

Drools體系架構如下圖所示:x6e28資訊網——每日最新資訊28at.com

x6e28資訊網——每日最新資訊28at.com

x6e28資訊網——每日最新資訊28at.com

Drools架構的執行步驟如下:x6e28資訊網——每日最新資訊28at.com

  • 將規則加載到規則庫中,該規則庫始終保持可用。
  • 事實(Facts)被保存到工作內存(Working Memory)中,它們可以被修改或撤回。
  • Pattern Matcher將新的或現有的事實與規則進行匹配, 這個過程稱為模式匹配,該過程由規則引擎執行。
  • agenda在沖突解決策略的幫助下管理沖突規則的執行順序。

x6e28資訊網——每日最新資訊28at.com

以下是SpringBoot的Drools使用例子。x6e28資訊網——每日最新資訊28at.com

(1)定義Pom.xml

創建一個基本的springBoot應用程序,并將drools依賴項添加到pom.xml。x6e28資訊網——每日最新資訊28at.com

<?xml version="1.0" encoding="UTF-8"?><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 https://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <parent>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-parent</artifactId>      <version>2.6.6</version>      <relativePath/> <!-- lookup parent from repository -->   </parent>   <groupId>com.praveen.drools.example</groupId>   <artifactId>springboot-drools-demo</artifactId>   <version>0.0.1-SNAPSHOT</version>   <name>springboot-drools-demo</name>   <description>Demo project for Spring Boot with Drools Engine</description>   <properties>      <java.version>11</java.version>      <drools.version>7.67.0.Final</drools.version>      <springfox-swagger2.version>3.0.0</springfox-swagger2.version>   </properties>   <dependencies>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>      </dependency>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-test</artifactId>         <scope>test</scope>      </dependency>      <dependency>         <groupId>org.drools</groupId>         <artifactId>drools-core</artifactId>         <version>${drools.version}</version>      </dependency>      <dependency>         <groupId>org.drools</groupId>         <artifactId>drools-compiler</artifactId>         <version>${drools.version}</version>      </dependency>      <dependency>         <groupId>org.drools</groupId>         <artifactId>drools-decisiontables</artifactId>         <version>${drools.version}</version>      </dependency>      <!-- swagger ui -->      <dependency>         <groupId>io.springfox</groupId>         <artifactId>springfox-boot-starter</artifactId>         <version>${springfox-swagger2.version}</version>      </dependency>      <dependency>         <groupId>io.springfox</groupId>         <artifactId>springfox-swagger-ui</artifactId>         <version>${springfox-swagger2.version}</version>      </dependency>   </dependencies>   <build>      <plugins>         <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>         </plugin>      </plugins>   </build></project>

x6e28資訊網——每日最新資訊28at.com

(2)創建一個名為DroolsConfig.java的java配置類。

package com.praveen.drools.example.configuration;import com.praveen.drools.example.service.CustomerCategorizeService;import org.kie.api.KieServices;import org.kie.api.builder.KieBuilder;import org.kie.api.builder.KieFileSystem;import org.kie.api.builder.KieModule;import org.kie.api.runtime.KieContainer;import org.kie.internal.io.ResourceFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * Drools Config. * @author Praveen.Nair */@Configurationpublic class DroolsConfig {    private static final String RULES_CUSTOMER_RULES_DRL = "rules/customer-category.drl";    @Bean    public KieContainer kieContainer() {         final KieServices kieServices = KieServices.Factory.get();        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();        kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_CUSTOMER_RULES_DRL));        KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);        kb.buildAll();        KieModule kieModule = kb.getKieModule();        return kieServices.newKieContainer(kieModule.getReleaseId());    }}

x6e28資訊網——每日最新資訊28at.com

這個配置類創建一個springbean KieContainer,通過加載應用程序/resources文件夾下的規則文件來構建規則引擎。x6e28資訊網——每日最新資訊28at.com

創建KieFileSystem實例并從應用程序的resources目錄加載DRL文件。接著使用KieService和KieBuilder創建KieContainer并將其配置為spring bean。x6e28資訊網——每日最新資訊28at.com

(3)創建模型類

創建名為CustomerRequest的Pojo類和字段。x6e28資訊網——每日最新資訊28at.com

package com.praveen.drools.example.model;import java.util.Objects;import java.util.StringJoiner;/** * Customer request POJO. * @author Praveen.Nair */public final class CustomerRequest {    private final long id;    private final Integer age;    private final String gender;    private final Integer numberOfOrders;    public CustomerRequest(long id, Integer age, String gender, Integer numberOfOrders) {        this.id = id;        this.age = age;        this.gender = gender;        this.numberOfOrders = numberOfOrders;    }    public long getId() {        return id;    }    public Integer getAge() {        return age;    }    public String getGender() {        return gender;    }    public Integer getNumberOfOrders() {        return numberOfOrders;    }    @Override    public boolean equals(Object o) {        if (this == o) {            return true;        }        if (o == null || getClass() != o.getClass()) {            return false;        }        CustomerRequest that = (CustomerRequest) o;        return id == that.id &&                Objects.equals(age, that.age) &&                Objects.equals(gender, that.gender) &&                Objects.equals(numberOfOrders, that.numberOfOrders);    }    @Override    public int hashCode() {        return Objects.hash(id, age, gender, numberOfOrders);    }    @Override    public String toString() {        return new StringJoiner(", ", CustomerRequest.class.getSimpleName() + "[", "]")                .add("id=" + id)                .add("age=" + age)                .add("gender=" + gender)                .add("numberOfOrders='" + numberOfOrders + "'")                .toString();    }}

我們將這個類作為請求對象參數傳給規則引擎,并且將字段作為輸入發送到定義的規則中,以便為派生customerType。x6e28資訊網——每日最新資訊28at.com

另外,再定義了一個名為CustomerCategory.java的java枚舉,用于保存客戶類別,規則引擎根據該值派生客戶類型。x6e28資訊網——每日最新資訊28at.com

package com.praveen.drools.example.model;/** * Customer Categories. */public enum CustomerCategory {    GENERAL, KIDS, SENIOR_CITIZEN, SUSPENDED;    public String getValue() {        return this.toString();    }}

創建一個名為CustomerType的響應POJO類,如下所示。x6e28資訊網——每日最新資訊28at.com

package com.praveen.drools.example.model;import java.util.Objects;import java.util.StringJoiner;/** * CustomerType Response model. * @author Praveen.Nair */public class CustomerType {    private CustomerCategory customerType;    public CustomerCategory getCustomerType() {        return customerType;    }    public void setCustomerType(CustomerCategory customerType) {        this.customerType = customerType;    }    @Override    public boolean equals(Object o) {        if (this == o) {            return true;        }        if (o == null || getClass() != o.getClass()) {            return false;        }        CustomerType that = (CustomerType) o;        return customerType == that.customerType;    }    @Override    public int hashCode() {        return Objects.hash(customerType);    }    @Override    public String toString() {        return new StringJoiner(", ", CustomerType.class.getSimpleName() + "[", "]")                .add("customerType=" + customerType)                .toString();    }}

(4) 定義Drools規則

創建一個名為customer-category.drl的drools規則文件,并將該文件放在目錄/src/main/resources/rules下。x6e28資訊網——每日最新資訊28at.com

import com.praveen.drools.example.model.CustomerRequestimport com.praveen.drools.example.model.CustomerCategory;global com.praveen.drools.example.model.CustomerType customerType;dialect "mvel"rule "Categorize customer based on age"    when        CustomerRequest(age < 20)    then        customerType.setCustomerType(CustomerCategory.KIDS);endrule "Categorize senior citizen customer based on age"    when        CustomerRequest(age > 50)    then      customerType.setCustomerType(CustomerCategory.SENIOR_CITIZEN);endrule "Categorize customer based on number of orders"    when        CustomerRequest(numberOfOrders == 0)    then        customerType.setCustomerType(CustomerCategory.SUSPENDED);endrule "Categorize customer general case"    when        CustomerRequest((gender == "M" || gender == "F") && age > 20 && age < 50)    then        customerType.setCustomerType(CustomerCategory.GENERAL);end

需要在DRL文件中import 使用到的模型。定義一個名為customerType的全局參數,作為多個規則之間共享數據。x6e28資訊網——每日最新資訊28at.com

DRL文件可以包含一個或多個規則。可以使用mvel語法來指定規則。此外,每個規則都可以使用rule關鍵字進行描述。x6e28資訊網——每日最新資訊28at.com

然后定義when-then語法來指定規則的條件。根據Customer請求的輸入值,我們將設置customerType結果。x6e28資訊網——每日最新資訊28at.com

(5) 添加服務層和控制層

創建一個名為CustomerCategorizeService的服務類,并添加以下內容。x6e28資訊網——每日最新資訊28at.com

package com.praveen.drools.example.service;import com.praveen.drools.example.model.CustomerRequest;import com.praveen.drools.example.model.CustomerType;import org.kie.api.runtime.KieContainer;import org.kie.api.runtime.KieSession;/** * Customer Categorization service. * @author Praveen.Nair */public class CustomerCategorizeService {    private final KieContainer kieContainer;    public CustomerCategorizeService(KieContainer kieContainer) {        this.kieContainer = kieContainer;    }    public CustomerType getCustomerType(CustomerRequest customerRequest) {        CustomerType customerType = new CustomerType();        KieSession kieSession = kieContainer.newKieSession();        kieSession.setGlobal("customerType", customerType);        kieSession.insert(customerRequest);        kieSession.fireAllRules();        kieSession.dispose();        return customerType;    }}

使用注入的KieContainer實例創建KieSession實例。返回一個CustomerType類型的全局參數,這個CustomerType將用于保存規則執行結果。x6e28資訊網——每日最新資訊28at.com

使用insert方法將customerRequest傳遞給DRL文件,然后我們通過調用fireAllRules方法觸發所有規則,最后通過調用KieSession的dispose方法終止會話。x6e28資訊網——每日最新資訊28at.com

接著開發一個Controller 將服務發布為一個API: /API/getCustomerType。API的入參為CustomerRequest對象,返回類型為CustomerType。Controller代碼如下所示:x6e28資訊網——每日最新資訊28at.com

packagecom.praveen.drools.example.web;import com.praveen.drools.example.model.CustomerRequest;import com.praveen.drools.example.model.CustomerType;import com.praveen.drools.example.service.CustomerCategorizeService;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/getCustomerType")public class CustomerCategorizeController {    private final CustomerCategorizeService customerCategorizeService;    public CustomerCategorizeController(            CustomerCategorizeService customerCategorizeService) {        this.customerCategorizeService = customerCategorizeService;    }    @PostMapping    public ResponseEntity<CustomerType> getCustomer(@RequestBody CustomerRequest customerRequest) {        CustomerType customerType = customerCategorizeService.getCustomerType(customerRequest);        return new ResponseEntity<>(customerType, HttpStatus.OK);    }}

x6e28資訊網——每日最新資訊28at.com

2.Easy Rules

https://github.com/j-easy/easy-rulesx6e28資訊網——每日最新資訊28at.com

x6e28資訊網——每日最新資訊28at.com

EasyRule是一個輕量級的規則引擎。它提供了用于創建規則的抽象以及規則引擎API,它通過運行一組規則來檢測條件并執行操作。x6e28資訊網——每日最新資訊28at.com

以下是EasyRule的一些核心特性:x6e28資訊網——每日最新資訊28at.com

  • 輕量級類庫和容易上手
  • 基于POJO的開發與注解的編程模型
  • 基于MVEL表達式的編程模型(適用于極簡單的規則,一般不推薦)
  • 支持根據簡單的規則創建組合規則
  • 方便且適用于java的抽象的業務模型規則

x6e28資訊網——每日最新資訊28at.com

以下是Java中使用EasyRules的例子:x6e28資訊網——每日最新資訊28at.com

(1)在Maven中引入依賴包

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

x6e28資訊網——每日最新資訊28at.com

(2)定義規則

Easy Rules提供了一些選項來創建規則:x6e28資訊網——每日最新資訊28at.com

  • 聲明性注解;
  • API;
  • 表達式語言;
  • 規則描述符。

x6e28資訊網——每日最新資訊28at.com

定義方式如下面Java代碼:x6e28資訊網——每日最新資訊28at.com

@Rule(name = "cart total rule", description = "Give 10% off when shopping cart is greater than $200" )public class CartTotalRule {    @Condition    public boolean cartTotal(@Fact("cart") Cart cart) {        return cart.isGreaterThanTwoHundered;    }    @Action    public void giveDiscount(@Fact("cart") Cart cart) {       cart.setTotalDiscount(200);    }}

x6e28資訊網——每日最新資訊28at.com

(3)最后基于事實數據執行規則引擎

public class CartTotalRuleTest {    public static void main(String[] args) {        // define facts        Facts facts = new Facts();        facts.put("cart", get_customer_cart);        // define rules        Rule cartTotalRUle = CartTotalRule        Rules rules = new Rules();        rules.register(cartTotalRUle);        // fire rules on known facts        RulesEngine rulesEngine = new DefaultRulesEngine();        rulesEngine.fire(rules, facts);    }}

x6e28資訊網——每日最新資訊28at.com

3.RuleBook

https://github.com/deliveredtechnologies/rulebookx6e28資訊網——每日最新資訊28at.com

x6e28資訊網——每日最新資訊28at.com

RuleBook提供了一個簡單、靈活并且直觀的DSL。RuleBook提供了易于使用的Lambda特定語言或POJO方式來定義規則,Java開發人員可以通過帶注解的POJO來組織大規模規則集合,替代那些又臭又長的“if/else”。x6e28資訊網——每日最新資訊28at.com

以下是在Java使用RuleBook的Demo。x6e28資訊網——每日最新資訊28at.com

(1)Maven

<dependency>    <groupId>com.deliveredtechnologies</groupId>    <artifactId>rulebook-core</artifactId>    <version>${version}</version></dependency>

(2)Java定義規則

public class Cart{    private double cartTotal;    private String cartId;    private Customer user;    private List cartEntries;    //getter and setter}public class ShoppingCartRule extends CoRRuleBook {    @Override    public void defineRules() {        //give 10% off when cart total is greater than $200      addRule(RuleBuilder.create().withFactType(Cart.class).withResultType(Double.class)        .when(facts -> facts.getOne().getCartTotal() > 200)        .then((facts, result) -> result.setValue(20))        .stop()        .build());}

x6e28資訊網——每日最新資訊28at.com

(3)執行規則:

public class CartPromotionRule {    public static void main(String[] args) {      RuleBook cartPromotion = RuleBookBuilder.create(ShoppingCartRule.class).withResultType(Double.class)        .withDefaultResult(0.0)        .build();      NameValueReferableMap facts = new FactMap();      facts.setValue("cart", new Cart(450.0, 123456, customer, entries));      cartPromotion.run(facts);    }}

x6e28資訊網——每日最新資訊28at.com

4.OpenL Tablets

http://openl-tablets.org/x6e28資訊網——每日最新資訊28at.com

https://github.com/openl-tablets/openl-tabletsx6e28資訊網——每日最新資訊28at.com

x6e28資訊網——每日最新資訊28at.com

OpenL Tablets 是一個基于 Java和Excel決策表工具的業務規則引擎(BRE)和業務規則管理系統(BRMS)。x6e28資訊網——每日最新資訊28at.com

主要包括以下幾個部分:x6e28資訊網——每日最新資訊28at.com

  • Business Rules Engines(業務規則引擎)
  • WebStudio
  • Web services(web服務)
  • Rule repository(基于JCR的實現的規則庫)

x6e28資訊網——每日最新資訊28at.com

以下是在Java中使用OpenL Tablets的例子。x6e28資訊網——每日最新資訊28at.com

(1)Maven

<dependency>    <groupId>org.openl</groupId>    <artifactId>org.openl.core</artifactId>    <version>${version}</version></dependency><dependency>    <groupId>org.openl.rules</groupId>    <artifactId>org.openl.rules</artifactId>    <version>${version}</version></dependency>

x6e28資訊網——每日最新資訊28at.com

(2)java實現x6e28資訊網——每日最新資訊28at.com

public class Main {    private CartPromotionRules instance;    public static void main(String[] args) {        Main rules = new Main();        // setup user and case here        rules.process(aCase);    }    public void process(Case aCase) {        EngineFactory engineFactory = new RulesEngineFactory(          getClass().getClassLoader()            .getResource("rules.xls"), CartPromotionRules.class);        instance = engineFactory.newEngineInstance();        instance.executePromotion(aCase, new Response());    }}

本文鏈接:http://www.tebozhan.com/showinfo-26-13643-0.html四個流行的Java開源規則引擎和入門

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 客服發送一條消息背后的技術和思考

下一篇: 如何將電腦上的“小電影”隱藏為一張圖片?這波操作絕了!!

標簽:
  • 熱門焦點
  • 天貓精靈Sound Pro體驗:智能音箱沒有音質?來聽聽我的

    這幾年除了手機作為智能生活終端最主要的核心之外,第二個可以成為中心點的產品是什么?——是智能音箱。 手機在執行命令的時候有兩種操作方式,手和智能語音助手,而智能音箱只
  • 6月安卓手機性價比榜:Note 12 Turbo斷層式碾壓

    6月份有一個618,雖然這是京東周年慶的日子,但別的電商也都不約而同的跟進了,反正促銷沒壞處,廠商和用戶都能滿意。618期間一些產品也出現了歷史低價,那么各個價位段的產品性價比
  • “又被陳思誠騙了”

    作者|張思齊 出品|眾面(ID:ZhongMian_ZM)如今的國產懸疑電影,成了陳思誠的天下。最近大爆電影《消失的她》票房突破30億斷層奪魁暑期檔,陳思誠再度風頭無兩。你可以說陳思誠的
  • 猿輔導與新東方的兩種“歸途”

    作者|卓心月 出品|零態LT(ID:LingTai_LT)如何成為一家偉大企業?答案一定是對&ldquo;勢&rdquo;的把握,這其中最關鍵的當屬對企業戰略的制定,且能夠站在未來看現在,即使這其中的
  • ESG的面子與里子

    來源 | 光子星球撰文 | 吳坤諺編輯 | 吳先之三伏大幕拉起,各地高溫預警不絕,但處于厄爾尼諾大&ldquo;烤&rdquo;之下的除了眾生,還有各大企業發布的ESG報告。ESG是&ldquo;環境保
  • 阿里瓴羊One推出背后,零售企業迎數字化新解

    作者:劉曠近年來隨著數字經濟的高速發展,各式各樣的SaaS應用服務更是層出不窮,但本質上SaaS大多局限于單一業務流層面,對用戶核心關切的增長問題等則沒有提供更好的解法。在Saa
  • 華為發布HarmonyOS 4:更好玩、更流暢、更安全

    在8月4日的華為開發者大會2023(HDC.Together)大會上,HarmonyOS 4正式發布。自2019年發布以來,HarmonyOS一直以用戶為中心,經歷四年多的發展HarmonyOS已
  • iQOO Neo8系列今日官宣:首發天璣9200+ 全球安卓最強芯!

    在昨日舉行的的聯發科新一代旗艦芯片天璣9200+的發布會上,iQOO官方也正式宣布,全新的iQOO Neo8系列新品將全球首發搭載這款當前性能最強大的移動平臺
  • 2022爆款:ROG魔霸6 冰川散熱系統持續護航

    喜逢開學季,各大商家開始推出自己的新產品,進行打折促銷活動。對于忠實的端游愛好者來說,能夠擁有一款夢寐以求的筆記本電腦是一件十分開心的事。但是現在的
Top