idea项目中使用URule 规则引擎的简单例子
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)
项目结构:
- 在pom.xml中添加依赖:
<dependency><groupId>com.bstek.urule</groupId><artifactId>urule-console</artifactId><version>2.1.7</version> </dependency>
- 在yml中添加:
server:port: 9999 urule:repository:dir: d:/urulerepo
- 启动类上添加:
@SpringBootApplication @ImportResource({"classpath:urule-console-context.xml"})
- 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;@Overrideprotected 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);} }
- 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 {@Overridepublic void afterPropertiesSet() throws Exception {Properties props=new Properties();props.setProperty("urule.repository.xml", "classpath:mysql.xml"); setProperties(props);} }
- 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 {@Beanpublic ServletRegistrationBean<HttpServlet> registerURuleServlet(){return new ServletRegistrationBean<HttpServlet>(new URuleServlet(),"/urule/*");}@Beanpublic ServletRegistrationBean<HttpServlet> registerIndexServlet(){return new ServletRegistrationBean<HttpServlet>(new IndexServlet(),"/");}@Beanpublic MethodTest methodTest(){return new MethodTest();} }
- 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; }
- 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());} }
- 运行访问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>