[bigdata-091] 规则引擎 easyrules 开发

1. easyrules轻量级规则引擎 java开发的。


2. 官网
http://www.easyrules.org/index.html 很快要关闭切换到github
https://github.com/j-easy/easy-rules/wiki


3. 一个最简单的例子
3.1 目录结构
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── ttz
│   │               └── demo
│   │                   └── easyruledemo
│   │                       ├── App.java
│   │                       └── HelloWorldRule.java


3.2 pom.xml内容


  4.0.0

  com.ttz.demo
  easyruledemo
  0.0.1-SNAPSHOT
  jar

  easyruledemo
  http://maven.apache.org

  
    UTF-8
  
  
  
    
      
        false
      
      central
      Central Repository
      https://repo.maven.apache.org/maven2
    
    
      ossrh
      https://oss.sonatype.org/content/repositories/snapshots
    
  

  
    
      junit
      junit
      3.8.1
      test
    
    
    	org.jeasy
    	easy-rules-core
    	3.0.0
    
  



3.3 App.java内容


package com.ttz.demo.easyruledemo;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;

/**
 * Hello world!
 *
 */

public class App 
{
    public static void main( String[] args )
    {
    	// create facts
        Facts facts = new Facts();

        // create rules
        Rules rules = new Rules();
        rules.register(new HelloWorldRule());

        // create a rules engine and fire rules on known facts
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
    }
}



3.4 HellowWroldRule.java内容

package com.ttz.demo.easyruledemo;

import org.jeasy.rules.annotation.Rule;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Action;

@Rule(name = "Hello World rule", description = "Always say hello world")
public class HelloWorldRule {

    @Condition
    public boolean when() {
        return true;
    }

    @Action
    public void then() throws Exception {
        System.out.println("hello world");
    }

}




4. 原理分析

这个项目在github上有400多星,简直不可思议,其实原理简单的不得了

4.1 规则是一个interface接口,里面有俩函数:一个用来判断条件是否达成,返回true/false,另一个是如果返回是true,则执行一些动作修改某些值

4.2 如果有多条规则,手工指定下规则的优先级,这样可以让若干规则以按照优先级次序逐个执行。


你可能感兴趣的:([bigdata-091] 规则引擎 easyrules 开发)