首页  

规则引擎easyrules     所属分类 java 浏览量 2702
比drools 简单 
理解规则引擎 关键概念 术语  基本原理的好东西

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

You can build a simple rules engine yourself. 
All you need is to create a bunch of objects with conditions and actions, store them in a collection, 
and run through them to evaluate the conditions and execute the actions.


Lightweight library and easy to learn API
POJO based development with annotation programming model
Useful abstractions to define business rules and apply them easily with Java
The ability to create composite rules from primitive ones
The ability to define rules using an Expression Language (Like MVEL and SpEL)

SpEL(Spring Expression Language)


using annotations 注解

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

注册时 使用 注解扫描 动态代理类

 public void register(Object rule) {
        Objects.requireNonNull(rule);
        rules.add(RuleProxy.asRule(rule));
    }
    
    

programmatic way with a fluent API 使用流式API

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();
        
        
using an Expression Language 使用表达式语言

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!\");");
        
        
using a rule descriptor 使用规则描述文件
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"));


执行规则引擎 fire it

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


     
多条规则例子

能被5整除 输出 fizz
能被7整除 输出 buzz
同时能被5和7整除 输出 fizzbuzz


public class FizzBuzzRule extends UnitRuleGroup {

    public FizzBuzzRule(Object... rules) {
        for (Object rule : rules) {
            addRule(rule);
        }
    }

    @Override
    public int getPriority() {
        return 0;
    }
}

FizzBuzzRule 是 FizzRule 和 BuzzRule 的组合

public class UnitRuleGroup extends CompositeRule

UnitRuleGroup
A unit rule group is a composite rule that acts as a unit: Either all rules are applied or nothing is applied.



package org.jeasy.rules.tutorials.fizzbuzz; import org.jeasy.rules.api.Facts; import org.jeasy.rules.api.Rules; import org.jeasy.rules.api.RulesEngine; import org.jeasy.rules.core.DefaultRulesEngine; import org.jeasy.rules.core.RulesEngineParameters; public class FizzBuzzWithEasyRules { public static void main(String[] args) { // create rules engine RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true); RulesEngine fizzBuzzEngine = new DefaultRulesEngine(parameters); // create rules Rules rules = new Rules(); rules.register(new FizzRule()); rules.register(new BuzzRule()); rules.register(new FizzBuzzRule(new FizzRule(), new BuzzRule())); rules.register(new NonFizzBuzzRule()); // fire rules Facts facts = new Facts(); for (int i = 1; i <= 100; i++) { facts.put("number", i); fizzBuzzEngine.fire(rules, facts); System.out.println(); } } } skipOnFirstAppliedRule 设置为 true , 匹配规则后返回 多条规则 按 优先级 priority 执行 FizzBuzzRule priority 为 0 ,最先执行 public class Rules implements Iterable<Rule> { private Set<Rule> rules = new TreeSet<>(); 使用了有序的 TreeSet !!!
public class Facts implements Iterable> { private Map facts = new HashMap<>(); RulesEngine // Fire all registered rules on given facts. void fire(Rules rules, Facts facts); // Check rules without firing them. // 只匹配规则 condition ,不执行 action Map check(Rules rules, Facts facts); DefaultRulesEngine RulesEngineParameters parameters; List ruleListeners; List rulesEngineListeners; RulesEngineParameters // skip next applicable rules on first applied rule. // 匹配第一条规则后直接返回 skipOnFirstAppliedRule // skip next applicable rules on first failed rule. // 执行失败时返回 skipOnFirstFailedRule // skip next applicable rules on first non triggered rule. // 未匹配规则时直接返回 skipOnFirstNonTriggeredRule // threshold after which rules should be skipped. priorityThreshold

上一篇     下一篇
Java线程池ExecutorService与CompletionService

使用aql更新lua脚本

metaspace OOM 实例

Mysql锁机制

mysql连接信息查看

Java锁机制