Springboot配合easy-rules简单使用:案例1--购物

实现规则

1、 一个人去买酒
2、 如果年龄大于18岁,则是成年人;小于18岁是未成年人
3、 如果未成年人去买酒,拒绝


步骤一: 导入依赖

        <dependency>
            <groupId>org.jeasy</groupId>
            <artifactId>easy-rules-core</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.jeasy</groupId>
            <artifactId>easy-rules-mvel</artifactId>
            <version>3.4.0</version>
        </dependency>


创建实体类

package com.example.testeasyrule.domain;

import org.springframework.stereotype.Component;


@Component
public class Person {


    private String name;
    private int age;
    private boolean adult;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public boolean isAdult() {
        return adult;
    }

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



}


编写一个Rule相关的配置类,方便等会通过@Autowired引用

package com.example.testeasyrule.config;

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.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RuleBean {

    @Bean
    public Facts getFacts() {
        Facts facts = new Facts();
        return facts;
    }

    @Bean
    public Rules getRules() {
        Rules rules = new Rules();
        return rules;
    }

    @Bean
    public RulesEngine getRulesEngine() {
        RulesEngine rulesEngine = new DefaultRulesEngine();
        return rulesEngine;
    }


}


创建规则引擎并触发

package com.example.testeasyrule.controller;


import com.example.testeasyrule.domain.Person;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.mvel.MVELRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestRule {

    @Autowired
    Person person;

    @Autowired
    Facts facts;

    @Autowired
    Rules rules;

    @Autowired
    RulesEngine rulesEngine;


    @RequestMapping("/shop")
    @ResponseBody
    public void shop() {

//        创建一个实例
        person.setName("Liu");
        person.setAge(14);

        Facts facts = new Facts();
        facts.put("person", person);

        //创建规则一
        Rule ageRule = new MVELRule().name("年龄规则")
                .description("如果一个人的年龄大于18岁就是成年人")
                .priority(1)
                .when("person.getAge() > 18")
                .then("person.setAdult(true)");
        //创建规则二
        Rule alcoholRule = new MVELRule().name("酒规则")
                .description("小孩不允许买酒")
                .priority(2)
                .when("person.isAdult()==false")
                .then("System.out.println("商家:  小屁孩,你在想屁吃")");

        //创建一个规则集
        rules.register(ageRule);
        rules.register(alcoholRule);

        System.out.println("哟哟哟,给我来点酒");

        //创建默认规则引擎并根据已知事实触发规则
        rulesEngine.fire(rules, facts);


    }


}

原文地址:https://www.cnblogs.com/lyd447113735/p/14814892.html