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


    }


}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM