Easy-rules

  easy-rules

Order

This paper mainly studies easy-rules.

Easy-rules is a lightweight rules engine.

maven

        <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 creation method

Based on mvel expression

Easy-rules integrates mvel expressions first, and SpEL may be integrated later.

  • Configuration file
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\");"
  • Load run
        //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);

Annotation method

@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 can label the name and description attributes. the name of each rule should be unique. if it is not specified, the RuleProxy will take the class name by default
  • @Condition is a condition judgment, requiring a boolean value to be returned to indicate whether the condition is satisfied
  • Triggered method after @Action annotation condition is established
  • @Priority marks the priority of the rule. the default value is Integer.MAX_VALUE-1. the smaller the value, the higher the priority.

Implement Rule interface

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

/**
 * 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;

}

Implementing this interface is also a form of creating a rule.

Source code analysis

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

Asrule method is used here.

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

As you can see, if there is a Rule interface implemented, it will be returned directly, otherwise (I.e. in the form of annotations), the dynamic proxy of JDK is used for packaging.

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

You can see here that invoke adapted the method

Let’s take getName as an example to see how to return according to the annotation.

    private String getRuleName() {
        org.jeasy.rules.annotation.Rule rule = getRuleAnnotation();
        return rule.name().equals(Rule.DEFAULT_NAME) ? getTargetClass().getSimpleName() : rule.name();
    }

You can see that the annotation is parsed here.

Summary

In essence, the purpose of the rule engine is to replace the hard-coded if else judgment in a loose and flexible way to achieve the purpose of decoupling, but the actual scene should pay extra attention to the safety of the rule expression.

doc