前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >easy-rules小试牛刀

easy-rules小试牛刀

作者头像
code4it
发布2018-09-17 16:04:21
1.9K0
发布2018-09-17 16:04:21
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究下easy-rules。

easy-rules是一款轻量级的规则引擎。

maven

代码语言:javascript
复制
        <dependency>
            <groupId>org.jeasy</groupId>
            <artifactId>easy-rules-core</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.jeasy</groupId>
            <artifactId>easy-rules-mvel</artifactId>
            <version>3.1.0</version>
        </dependency>

Rule创建方式

基于mvel表达式

easy-rules首先集成了mvel表达式,后续可能集成SpEL

  • 配置文件 name: "alcohol rule" description: "children are not allowed to buy alcohol" priority: 2 condition: "person.isAdult() == false" actions: - "System.out.println(\"Shop: Sorry, you are not allowed to buy alcohol\");"
  • 加载运行 //create a person instance (fact) Person tom = new Person("Tom", 14); Facts facts = new Facts(); facts.put("person", tom); MVELRule alcoholRule = MVELRuleFactory.createRuleFrom(new File(getClass().getClassLoader().getResource("alcohol-rule.yml").getFile())); // create a rule set Rules rules = new Rules(); rules.register(alcoholRule); //create a default rules engine and fire rules on known facts RulesEngine rulesEngine = new DefaultRulesEngine(); System.out.println("Tom: Hi! can I have some Vodka please?"); rulesEngine.fire(rules, facts);

注解方式

代码语言:javascript
复制
@Rule
public class BuzzRule {

    @Condition
    public boolean isBuzz(@Fact("number") Integer number) {
        return number % 7 == 0;
    }

    @Action
    public void printBuzz() {
        System.out.println("buzz");
    }

    @Priority
    public int getPriority() {
        return 2;
    }
}
  • @Rule可以标注name和description属性,每个rule的name要唯一,如果没有指定,则RuleProxy则默认取类名
  • @Condition是条件判断,要求返回boolean值,表示是否满足条件
  • @Action标注条件成立之后触发的方法
  • @Priority标注该rule的优先级,默认是Integer.MAX_VALUE - 1,值越小越优先

实现Rule接口

easy-rules-core-3.1.0-sources.jar!/org/jeasy/rules/api/Rule.java

代码语言:javascript
复制
/**
 * Abstraction for a rule that can be fired by the rules engine.
 *
 * Rules are registered in a rule set of type <code>Rules</code> in which they must have a <strong>unique</strong> name.
 *
 * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 */
public interface Rule extends Comparable<Rule> {

    /**
     * Default rule name.
     */
    String DEFAULT_NAME = "rule";

    /**
     * Default rule description.
     */
    String DEFAULT_DESCRIPTION = "description";

    /**
     * Default rule priority.
     */
    int DEFAULT_PRIORITY = Integer.MAX_VALUE - 1;

    /**
     * Getter for rule name.
     * @return the rule name
     */
    String getName();

    /**
     * Getter for rule description.
     * @return rule description
     */
    String getDescription();

    /**
     * Getter for rule priority.
     * @return rule priority
     */
    int getPriority();

    /**
     * Rule conditions abstraction : this method encapsulates the rule's conditions.
     * <strong>Implementations should handle any runtime exception and return true/false accordingly</strong>
     *
     * @return true if the rule should be applied given the provided facts, false otherwise
     */
    boolean evaluate(Facts facts);

    /**
     * Rule actions abstraction : this method encapsulates the rule's actions.
     * @throws Exception thrown if an exception occurs during actions performing
     */
    void execute(Facts facts) throws Exception;

}

实现这个接口,也是创建rule的一种形式。

源码解析

  • register easy-rules-core-3.1.0-sources.jar!/org/jeasy/rules/api/Rules.java /** * Register a new rule. * * @param rule to register */ public void register(Object rule) { Objects.requireNonNull(rule); rules.add(RuleProxy.asRule(rule)); } 这里使用RuleProxy.asRule方法
  • RuleProxy easy-rules-core-3.1.0-sources.jar!/org/jeasy/rules/core/RuleProxy.java /** * Makes the rule object implement the {@link Rule} interface. * * @param rule the annotated rule object. * @return a proxy that implements the {@link Rule} interface. */ public static Rule asRule(final Object rule) { Rule result; if (rule instanceof Rule) { result = (Rule) rule; } else { ruleDefinitionValidator.validateRuleDefinition(rule); result = (Rule) Proxy.newProxyInstance( Rule.class.getClassLoader(), new Class[]{Rule.class, Comparable.class}, new RuleProxy(rule)); } return result; } 可以看到,如果有实现Rule接口,则直接返回,没有的话(即基于注解的形式),则利用JDK的动态代理进行包装。
  • invoke @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { String methodName = method.getName(); switch (methodName) { case "getName": return getRuleName(); case "getDescription": return getRuleDescription(); case "getPriority": return getRulePriority(); case "compareTo": return compareToMethod(args); case "evaluate": return evaluateMethod(args); case "execute": return executeMethod(args); case "equals": return equalsMethod(args); case "hashCode": return hashCodeMethod(); case "toString": return toStringMethod(); default: return null; } } 可以看到这里invoke对方法进行了适配

下面以getName为例看下如何根据注解来返回

代码语言:javascript
复制
    private String getRuleName() {
        org.jeasy.rules.annotation.Rule rule = getRuleAnnotation();
        return rule.name().equals(Rule.DEFAULT_NAME) ? getTargetClass().getSimpleName() : rule.name();
    }

可以看到这里对注解进行了解析

小结

从本质上看,规则引擎的目的就是要以松散灵活的方式来替代硬编码式的if else判断,来达到解耦的目的,不过实际场景要额外注意规则表达式的安全问题。

doc

  • mvel
  • easy-rules
  • RulesEngine
  • 规则引擎选型及应用
  • 规则引擎之EasyRules
  • expression-language-support
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-03-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • maven
  • Rule创建方式
    • 基于mvel表达式
      • 注解方式
        • 实现Rule接口
        • 源码解析
        • 小结
        • doc
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档