Java中最流行的几种业务规则引擎简介


在本文中,我们将检查Java中一些最受欢迎的规则引擎。规则引擎使将业务逻辑与源代码分离变得容易。我们可能会将规则引擎视为复杂的if/then语句,在Java中,大多数流行的规则引擎都实现JSR94。
 
介绍
想象一下规则引擎是一个将数据和规则作为输入的系统。它将这些规则应用于数据,并根据规则定义为我们提供输出。让我们以一个在线购物为例,我们希望为客户提供某些促销或折扣。

  • 如果购物车总额超过$ 400,可为客户提供20%的折扣。
  • 第一次下单给10%。

上面将购物车和客户定义为满足规则集中定义的条件的规则将在其中执行的数据。
使用规则引擎有几个优点:
  • 它提供了极大的灵活性,我们可以在不对源代码进行重大更改的情况下更改规则。
  • 由于我们不再需要在源代码中构建规则引擎逻辑,因此降低了复杂性。
  • 通过将规则引擎分开,它提供了更大的可重用性。

让我们看一下Java中一些流行的规则引擎。
 
1.Drools
Drools是业务规则管理系统(BRMS)解决方案。它提供了核心业务规则引擎(BRE),Web创作和规则管理应用程序(Drools Workbench),对符合性级别3的决策模型和注释(DMN)模型的完整运行时支持,以及用于核心开发的Eclipse IDE插件。
 
2.EasyRule
EasyRule是轻量级的规则引擎API。它提供Rule抽象来创建带有条件和动作的规则,以及RulesEngine通过一组规则运行以测试条件和执行动作的API。以下是EasyRule的一些核心功能:
  • 轻量级规则引擎API。
  • 基于注释和POJO。
  • 支持复合规则构造。
  • 支持表达语言(如MVEL和SpEL)来定义规则。

让我们概述一下Easy Rules API。加入依赖:
<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-rules-core</artifactId>
    <version>3.3.0</version>
</dependency>

Easy Rules 提供以下创建规则的选项
  • 声明式地使用注释。
  • 务实地使用流利的API。
  • 使用表达语言 
  • 使用规则描述符。

让我们看几个例子:
@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);
    }
}

规则引擎的最后一部分是基于事实和规则数据执行规则。

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

 
3. RuleBook
RuleBook提供了灵活的简单直观的DSL。如果您有更多规则集,我们可以将其构建为带注释的POJO,并且RuleBook可以将整个包立即转换为RuleBook。它提供了一个易于使用的启用Lambda的领域特定语言或使用POJO,让我们使用RuleBook来查看一个简单的规则定义:
要在您的项目中添加RuleBook,请在pom.xml文件中添加以下依赖项:
<dependency>
    <groupId>com.deliveredtechnologies</groupId>
    <artifactId>rulebook-core</artifactId>
    <version>${version}</version>
</dependency>

让我们看一个例子:
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());
}

让我们执行规则:
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);
    }
}

 
4. OpenL平板电脑
OpenL Tablets业务规则引擎(BRE)和业务规则管理系统(BRMS)。它包含以下主要组件:
  • 业务规则引擎
  • 网络工作室
  • 网页服务
  • 规则存储库(基于JCR的实现)

要添加依赖项,请将以下内容添加到pom.xml中:
<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>

使用代码:
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());
    }
}

注意

  1. Jess –一种类似于Clips的Rule引擎,可通过Java使用Common Lisp的全部功能进行访问
  2.  JLisa –小型,轻便,快速的规则引擎和完全用Java编写的脚本环境;允许访问所有Java API。

概括
在本文中,我们讨论了Java中流行的规则引擎。这些规则引擎为业务逻辑抽象提供了灵活性。在所有这些规则引擎中,Drools是最先进,最活跃的规则引擎。