easy rules简单使用

参考文章:http://tech.dianwoda.com/2019/06/05/gui-ze-yin-qing-easy-rulejie-shao/

easy rules的特点

  • 轻量级类库和容易上手
  • 基于POJO的开发与注解的编程模型
  • 基于mvel表达式的编程模型
  • 支持根据简单的规则创建组合规则
  • 方便且适用于java的抽象的业务模型规则

基于POJO开发与注解的编程模型:判断3或者8整除的数

  1. maven引入easy-rules
<dependency>
        <groupId>org.jeasy</groupId>
        <artifactId>easy-rules-core</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.jeasy</groupId>
        <artifactId>easy-rules-mvel</artifactId>
        <version>3.3.0</version>
    </dependency>
  1. 编写规则POJO
@Rule(name = "被3整除", description = "number如果被3整除,打印:number is three")
public class ThreeRule {  
    @Condition //条件判断注解:如果return true, 执行Action
    public boolean isThree(@Fact("number") int number){
        return number % 3 == 0;
    }

    @Action //执行方法注解
    public void threeAction(@Fact("number") int number){
        System.out.print(number + " is three");
    }

    @Priority //优先级注解:return 数值越小,优先级越高
    public int getPriority(){
        return 1;
    }
}
@Rule(name = "被8整除",description="number被8整除,则输出:number is eight")
public class EightRule {

    @Condition
    public boolean isEight(@Fact("number") int number){
        return number % 8 == 0;
    }

    @Action
    public void eightAction(@Fact("number") int number){
        System.out.print(number + " is eight");
    }

    @Priority
    public int getPriority(){
        return 2;
    }
}
@Rule(name = "被3和8同时整除",  description = "这是一个组合规则")
public class ThreeEightRuleUnitGroup extends UnitRuleGroup {  
    public ThreeEightRuleUnitGroup(Object... rules) {
        for (Object rule : rules) {
            addRule(rule);
        }
    }

    @Override
    public int getPriority() {
        return 0;
    }
}
  1. 执行规则
public static void main(String[] args) {
        /**
         * 创建规则执行引擎
         * 注意: skipOnFirstAppliedRule意思是,只要匹配到第一条规则就跳过后面规则匹配
         */
        RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true);
        RulesEngine rulesEngine = new DefaultRulesEngine(parameters);
        //创建规则
        Rules rules = new Rules();
        rules.register(new EightRule());
        rules.register(new ThreeRule());
        rules.register(new ThreeEightRuleUnitGroup(new EightRule(), new ThreeRule()));
        rules.register(new OtherRule());
        Facts facts = new Facts();
        for (int i=1 ; i<=30 ; i++){
            //规则因素,对应的name,要和规则里面的@Fact 一致
            facts.put("number", i);
            //执行规则
            rulesEngine.fire(rules, facts);
            System.out.println();
        }
    }
  1. 运行结果
1
2
3 is three
4
5
6 is three
7
8 is eight
9 is three
10
11
12 is three
13
14
15 is three
16 is eight
17
18 is three
19
20
21 is three
22
23
24 is three24 is eight
25
26
27 is three
28
29
30 is three

基于mvel表达式的编程模型

要求:禁止18岁以下的人购买酒精。

  1. 顾客Person类
public class Person { 
	
    public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
    
    private String name;

    private boolean adult;

	private int age;
    //getter, setter 省略

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isAdult() {
		return adult;
	}

	public void setAdult(boolean adult) {
		this.adult = adult;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", adult=" + adult + ", age=" + age + "]";
	}
    
}

  1. 定义规则
Rule ageRule = new MVELRule()
                .name("age rule")
                .description("Check if person's age is > 18 and marks the person as adult")
                .priority(1)
                .when("person.age > 18")
                .then("person.setAdult(true);");
 Rule adultAlcoho = new MVELRule()
			.name("adultAlcoho rule")
			.description("adult are allowed to buy alcohol")
			.priority(2)
			.when("person.isAdult() == true")
			.then("System.out.println(\"Shop: You are allowed to buy alcohol\");");

alcohol-rule.yml中存放规则

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\");"
  1. 执行规则
public static void main(String[] args) throws Exception {
		//创建map
		 Map<String, Integer> map = new HashMap<String, Integer>();
			map.put("A", 0);
			map.put("B", 48);
			map.put("C", 11);
			map.put("D", 70);
			map.put("E", 10);
			map.put("F", 8);
        //创建一个Person实例(Fact)
        Person tom = new Person("Tom", 23);
        Facts facts = new Facts();
        facts.put("person", tom);
        facts.put("map",map);

        //创建规则1
        Rule ageRule = new MVELRule()
                .name("age rule")
                .description("Check if person's age is > 18 and marks the person as adult")
                .priority(1)
                .when("person.age > 18")
                .then("person.setAdult(true);");
        //创建规则2
        Rule alcoholRule = MVELRuleFactory.createRuleFrom(new FileReader("alcohol-rule.yml"));	

        //创建规则3
		Rule adultAlcoho = new MVELRule()
			.name("adultAlcoho rule")
			.description("adult are allowed to buy alcohol")
			.priority(2)
			.when("person.isAdult() == true")
			.then("System.out.println(\"Shop: You are allowed to buy alcohol\");");
		//创建规则4
		Rule rule4 = new MVELRule()
			.name("mvel rule")
			.description("test mvel")
			.priority(3)
			.when("map.get(\"A\") >= 0")
			.then("System.out.println(\"true\");");
        Rules rules = new Rules();
        rules.register(ageRule);
        rules.register(alcoholRule);
        rules.register(adultAlcoho);
        //创建规则执行引擎,并执行规则
        RulesEngine rulesEngine = new DefaultRulesEngine();
        System.out.println("Tom: Hi! can I have some Vodka please?");
        rulesEngine.fire(rules, facts);
    }

:在new FileReader(“alcohol-rule.yml”)中我将alcohol-rule.yml放在程序跟目录下
easy rules简单使用_第1张图片
4. 输出结果
在这里插入图片描述

你可能感兴趣的:(java,easy,rules,java)