加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 03:09:29 所属栏目:大数据 来源:网络整理
导读: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 │ ? │ ? └─
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内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.ttz.demo</groupId>
  <artifactId>easyruledemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>easyruledemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
    <repository>
      <id>ossrh</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.jeasy</groupId>
    	<artifactId>easy-rules-core</artifactId>
    	<version>3.0.0</version>
    </dependency>
  </dependencies>
</project>


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 如果有多条规则,手工指定下规则的优先级,这样可以让若干规则以按照优先级次序逐个执行。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读