1.之前我们讲了Urule的快速初始化
https://blog.csdn.net/liuchao_123456/article/details/129197082?spm=1001.2014.3001.5502
2.现在我们来了解一下Urule的库文件 [变量库、常量库、参数库、动作库]
我们可以鼠标右击选中 “库”,右边会出现【添加变量库、常量库、参数库、动作库】的选项
![](https://img-blog.csdnimg.cn/img_convert/1c047aa01193859c80fec0bdabed762d.png)
2.1 点击添加变量库->添加变量->添加字段
![](https://img-blog.csdnimg.cn/img_convert/a18f9e3448c3622b22fb816cd732ca4a.png)
但这样一个一个添加变量字段会很麻烦,Urule提供了根据类路劲自动生成字段的功能。
我们可以这样操作,在代码中写一个类:
package com.bstek.demo.urule.entity;
import com.bstek.urule.model.Label;
import lombok.Data;
import java.math.BigDecimal;
@Data //必须提供get、set方法
public class Employee {
@Label("编号") //@Label 对应页面上的字段名称
private long id;
@Label("姓名")
private String name;
@Label("标签")
private String title;
@Label("性别")
private String sex;
@Label("年龄")
private Integer age;
@Label("地址")
private String address;
@Label("电话")
private String phone;
@Label("地区得分")
private BigDecimal areaScore;
@Label("员工评级")
private String employeErate;
@Label("最终评分")
private BigDecimal finalScore;
}
然后选中该变量,右击选择 根据类路劲生成字段,就会自动生成好java实体类对应的属性字段了。
![](https://img-blog.csdnimg.cn/img_convert/2d0111580f2789b2f492749f7e3d662e.png)
2.2 点击添加常量库
![](https://img-blog.csdnimg.cn/img_convert/125418d50f17b741490cdfa76ac9ffb8.png)
Date | 时间类型,输入时间格式有:yyyy-MM-dd HH:mm:ss、yyyy-MM-dd HH:mm、yyyy-MM-dd |
List | ArrayList集合,值的设置方式为: 基础类型集合:s4,s1,s2,s3 复杂类型集合:[{"id":"4","name":"user4"},{"id":"1","name":"user1"}] |
Set | TreeSet集合,值的设置方式为: 基础类型集合:s8,s5,s6,s7 复杂类型集合:[{"a1":"s8"},{"a1":"s5"},{"a1":"s6"},{"a1":"s7"}] |
Map | HashMap集合,值的设置方式为:JSON格式,比如:{"a":"1","b":"2","c":"3"} |
Enum | 任意值 |
![](https://img-blog.csdnimg.cn/img_convert/73bfaffd2f259431b3199eb540094385.png)
2.3 点击添加参数库
参数库选择数据类型时是可以选择变量库中的变量,如图我选择的数据类型为员工。
![](https://img-blog.csdnimg.cn/img_convert/1f7f84b657bb02c95efa1c2480537e01.png)
![](https://img-blog.csdnimg.cn/img_convert/f4508824ad2d6d645a6f9320c87b21f0.png)
2.4 添加动作库
首先需要在代码中添加一个方法类,然后在Urule页面上右击选中选择方法,双击选中你要添加的方法
当然urule中自带了很多方法,能满足大多数的设计需求,若urule自带的方法无法满足时可以添加自己的方法。
package com.bstek.demo.urule.test;
import java.lang.reflect.Member;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.bstek.urule.model.ExposeAction;
import org.springframework.stereotype.Component;
@Component //必须配置到spring中,让其成为一个标准的bean
public class MethodTest {
@ExposeAction("helloKey") //@ExposeAction对应页面上显示时的名称
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(Member m){
System.out.println("Hello "+m.getName()+", has house:"+m.isSynthetic());
}
@ExposeAction("是否能整除")
public Boolean test4and6(int a,int b){
if(b==0) return false;
if(a%b==0) return true;
return false;
}
}
![](https://img-blog.csdnimg.cn/img_convert/34705b287656464b9d39cb0d3b04ca41.png)
![](https://img-blog.csdnimg.cn/img_convert/209b9e5972e3d266840b606fad3e1047.png)
![](https://img-blog.csdnimg.cn/img_convert/0c5eaf9b5461a623b5894c10198506dc.png)
2.5自定义内置动作
package com.bstek.urule.runtime.builtinaction;
import com.bstek.urule.model.library.action.annotation.ActionBean;
import com.bstek.urule.model.library.action.annotation.ActionMethod;
import com.bstek.urule.model.library.action.annotation.ActionMethodParameter;
/**
* @author Jacky.gao
* @since 2015年11月27日
*/
@ActionBean(name="字符串")
public class StringAction {
@ActionMethod(name="去空格")
@ActionMethodParameter(names={"目标字符串"})
public String trim(String str){
if(str==null){
return str;
}
return str.trim();
}
@ActionMethod(name="指定起始的字符串截取")
@ActionMethodParameter(names={"目标字符串","开始位置","结束位置"})
public String substring(String str,int start,int end){
return str.substring(start, end);
}
......
}
总结:
库文件中存放的是规则设计中用到的变量和方法,相当于规则设计的必要工具,可以理解为java中写接口时需要用到的实体类和工具包,给后面规则设计的时候使用。