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

  • 相关阅读:
    m.baidu.com/?tn=simple 开始有广告了。。。
    一些话
    sublime text3中如何使用PHP编译系统
    遇到了一个特别有意思的题
    RVS PA-1800 功放参数
    TP框架修改后台路径方法
    换手机号之前需要看看
    layui跨域问题的解决
    Send me
    单细胞文章分享:Molecular Diversity of Midbrain Development in Mouse, Human, and Stem Cells
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/14814892.html
Copyright © 2020-2023  润新知