idea项目中使用URule 规则引擎的简单例子

如需转载分享,请标明出处,且不用于盈利为目的,谢谢合作!   

                                                        idea项目中使用URule 规则引擎的简单例子

 

简介

URule是一款纯Java规则引擎,它以RETE算法为基础,提供了向导式规则集、脚本式规则集、决策表、交叉决策表(PRO版提供)、决策树、评分卡及决策流共六种类型的规则定义方式,配合基于WEB的设计器,可快速实现规则的定义、维护与发布。

URule提供了两个版本:一个是基于Apache-2.0协议开源免费版本,URule开源版本第一款基于Apache-2.0协议开源的中式规则引擎;另一个是商用PRO版本,点击http://www.bstek.com 了解更多关于URule商用Pro版更多信息。

URULE PRO版与开源版主要功能比较

特性

URULE PRO版

URULE开源版

向导式决策集

脚本式决策集

决策树

决策流

决策表

交叉决策表

复杂评分卡

文件名、项目名重构

参数名、变量常量名重构

Excel决策表导入

规则集模版保存与加载

中文项目名和文件名支持

服务器推送知识包到客户端功能的支持

知识包优化与压缩的支持

客户端服务器模式下大知识包的推拉支持

规则集中执行组的支持

规则流中所有节点向导式条件与动作配置的支持

循环规则多循环单元支持

循环规则中无条件执行的支持

导入项目自动重命名功能

规则树构建优化

对象查找索引支持

规则树中短路计算的支持

规则条件冗余计算缓存支持

基于方案的批量场景测试功能

知识包调用监控

更为完善的文件读写权限控制

知识包版本控制

SpringBean及Java类的热部署

技术支持

SpringBoot项目中使用(最新版本2.1.7)

项目结构:

  1. 在pom.xml中添加依赖:
    <dependency>
        <groupId>com.bstek.urule</groupId>
        <artifactId>urule-console</artifactId>
        <version>2.1.7</version>
    </dependency>

     

  2. 在yml中添加:
    server:
      port: 9999
    urule:
      repository:
        dir: d:/urulerepo

     

  3. 启动类上添加:
    @SpringBootApplication
    @ImportResource({"classpath:urule-console-context.xml"})

     

  4. IndexServlet类
    package com.bdqn.cn.config;
    
    import com.bdqn.cn.entity.Customer;
    import com.bstek.urule.ClassUtils;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.IOException;
    
    /**
     * @author sunhao
     * @since 
     */
    public class IndexServlet extends HttpServlet {
       private static final long serialVersionUID = 9155627652423910928L;
    
       @Override
       protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          resp.sendRedirect(req.getContextPath()+"/urule/frame");
       }
       public static void main(String[] args) {
          File file=new File("d:/customer.xml");
          ClassUtils.classToXml(Customer.class, file);
       }
    }

     

  5. PropertiesConfiguration类
    package com.bdqn.cn.config;
    
    import com.bstek.urule.URulePropertyPlaceholderConfigurer;
    import org.springframework.beans.factory.InitializingBean;
    
    import java.util.Properties;
    
    /**
     * @author sunhao
     * @since
     */
    //@Component
    public class PropertiesConfiguration extends URulePropertyPlaceholderConfigurer implements InitializingBean {
       @Override
       public void afterPropertiesSet() throws Exception {
          Properties props=new Properties();
          props.setProperty("urule.repository.xml", "classpath:mysql.xml");  
          setProperties(props);
       }
    }

     

  6. URuleServletRegistration类
    package com.bdqn.cn.config;
    
    import com.bdqn.cn.test.MethodTest;
    import com.bstek.urule.console.servlet.URuleServlet;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    import javax.servlet.http.HttpServlet;
    
    /**
     * @author sunhao
     * @since
     */
    @Component
    public class URuleServletRegistration {
       @Bean
       public ServletRegistrationBean<HttpServlet> registerURuleServlet(){
          return new ServletRegistrationBean<HttpServlet>(new URuleServlet(),"/urule/*");
       }
       @Bean
       public ServletRegistrationBean<HttpServlet> registerIndexServlet(){
          return new ServletRegistrationBean<HttpServlet>(new IndexServlet(),"/");
       }
       @Bean
       public MethodTest methodTest(){
          return new MethodTest();
       }
    }

     

  7. Customer类
    package com.bdqn.cn.entity;
    
    import com.bstek.urule.model.Label;
    import lombok.Data;
    
    import java.util.Date;
    
    @Data
    public class Customer {
       @Label("名称")
       private String name;
       @Label("年龄")
       private int age;
       @Label("出生日期")
       private Date birthday;
       @Label("等级")
       private int level;
       @Label("手机号")
       private String mobile;
       @Label("性别")
       private boolean gender;
       @Label("是否有车")
       private boolean car;
       @Label("婚否")
       private boolean married;
       @Label("是否有房")
       private boolean house;
    }

     

  8. MethodTest类
    package com.bdqn.cn.test;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import com.bdqn.cn.entity.Customer;
    import com.bstek.urule.action.ActionId;
    import com.bstek.urule.model.ExposeAction;
    /**
     * @author sunhao
     */
    public class MethodTest {
       @ActionId("helloKey")
       public String hello(String username){
          System.out.println("hello "+username);
          return "hello"+username;
       }
       @ExposeAction("方法1")
       public boolean evalTest(String username){
          if(username==null){
             return false;
          }else if(username.equals("张三")){
             return true;
          }
          return false;
       }
    
       @ExposeAction("测试Int")
       public int testInt(int a,int b){
          return a+b;
       }
       public int testInteger(Integer a,int b){
          return a+b*10;
       }
    
       @ExposeAction("打印内容")
       public void printContent(String username,Date birthday){
          SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          if(birthday!=null){
             System.out.println(username+"今年已经"+sd.format(birthday)+"岁了!");
          }else{
             System.out.println("Hello "+username+"");
          }
       }
       @ExposeAction("打印Member")
       public void printUser(Customer m){
          System.out.println("Hello "+m.getName()+", has house:"+m.isHouse());
       }
    }

     

  9. 运行访问UI地址
    http://localhost:9999/urule/frame 

    附: d盘下的customer.xml

<?xml version="1.0" encoding="utf-8"?>

<variables clazz="com.bdqn.cn.entity.Customer">
  <variable name="age" label="年龄" type="Integer" act="InOut"/>
  <variable name="birthday" label="出生日期" type="Date" act="InOut"/>
  <variable name="car" label="是否有车" type="Boolean" act="InOut"/>
  <variable name="gender" label="性别" type="Boolean" act="InOut"/>
  <variable name="house" label="是否有房" type="Boolean" act="InOut"/>
  <variable name="level" label="等级" type="Integer" act="InOut"/>
  <variable name="married" label="婚否" type="Boolean" act="InOut"/>
  <variable name="mobile" label="手机号" type="String" act="InOut"/>
  <variable name="name" label="名称" type="String" act="InOut"/>
</variables>

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值