zoukankan      html  css  js  c++  java
  • 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);
    
    
        }
    
    
    }
    
    

  • 相关阅读:
    【P000-004】交易费计算系统,功能类规划
    【P000-003】交易费计算系统,从股票信息网络接口获取信息
    ASP页面的执行顺序
    Python ImportError: DLL load failed: %1 不是有效的 Win32 应用程序
    VSCode运行已有代码
    WPF MVVM-TreeView数据源添加了节点,UI没有刷新
    MapGIS二次开发注意事项
    把echarts嵌入winform窗口注意事项
    host is not allowed to connect mysql解决方法
    SqlDbx连接Oracle数据库
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/14814892.html
Copyright © 2011-2022 走看看