package com.example.demo.Aviator;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
//https://blog.csdn.net/huanghuang9426/article/details/125314084
public class TestAviator {
@Test
void grayRule(){
Map<String,Object> map = new HashMap<>();
final Date date = new Date();
String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(date);
map.put("mobileTail","9");
map.put("userId",901);
map.put("registerTime",dateStr);
map.put("mobile","15600000269");
map.put("sex",1);
map.put("age",28);
// 手机尾号
String expression = "(string.endsWith('9,9',mobileTail) && userId>=901 && registerTime>'2021-06-25 00:00:00')";
Boolean flag = (Boolean) AviatorEvaluator.execute(expression, map);
Assert.assertTrue("规则验证通过",flag);
}
@Test
void grayRule1(){
String expression = "a=='河北省'&&((b=='男人'||c>=30)||d=='黄')";
Expression compiledExp = AviatorEvaluator.compile(expression);
Map<String, Object> map = new HashMap<>();
map.put("a", "河北省");
map.put("b", "男人");
map.put("c",1);
map.put("d","黄");
Boolean flag = (Boolean) AviatorEvaluator.execute(expression, map);
Assert.assertTrue("规则验证通过",flag);
}
@Test
void grayRule2(){
// exec执行方式,无需传递Map格式
String age = "18";
System.out.println(AviatorEvaluator.exec("'His age is '+ age +'!'", age));
// execute执行方式,需传递Map格式
Map<String, Object> map = new HashMap<String, Object>();
map.put("age", "18");
System.out.println(AviatorEvaluator.execute("'His age is '+ age +'!'", map));
}
@Test //函数
void grayRule3() {
Map<String,Object> map = new HashMap<>();
map.put("s1","123qwer");
map.put("s2","123");
System.out.println(AviatorEvaluator.execute("string.startsWith(s1,s2)",map));
}
@Test //计算算数值
void grayRule3_1() {
Map<String,Object> map = new HashMap<>();
map.put("s1",3);
map.put("s2",4);
map.put("s3",8);
map.put("s4",9);
System.out.println(AviatorEvaluator.execute("(s1+s2)",map));
System.out.println(AviatorEvaluator.execute("math.pow(s3,s4)",map));
System.out.println(AviatorEvaluator.execute("(s1+s2)*math.pow(s3,s4)",map));
}
@Test //自定义函数
void grayRule4() {
// 注册自定义函数
AviatorEvaluator.addFunction(new MultiplyFunction());
// 方式1
System.out.println(AviatorEvaluator.execute("multiply(12.23, -2.3)"));
// 方式2
Map<String, Object> params = new HashMap<>();
params.put("a", 12.23);
params.put("b", -2.3);
System.out.println(AviatorEvaluator.execute("multiply(a, b)", params));
}
@Test //嵌套式多种数据结构
void grayRule5() {
final List<String> list = new ArrayList<>();
list.add("hello");
list.add(" world");
final int[] array = new int[3];
array[0] = 0;
array[1] = 1;
array[2] = 3;
final Map<String, Date> map = new HashMap<>();
map.put("date", new Date());
Map<String, Object> env = new HashMap<>();
env.put("list", list);
env.put("array", array);
env.put("map", map);
System.out.println(AviatorEvaluator.execute(
"list[0]+':'+array[0]+':'+'today is '+map.date", env));
}
@Test //三元比较符
void grayRule6() {
Map<String, Object> env = new HashMap<String, Object>();
env.put("a", -5);
String result = (String) AviatorEvaluator.execute("a>0? 'yes':'no'", env);
System.out.println(result);
}
@Test //变量的语法糖衣
void grayRule7() {
User user = new User(1,"jack","18");
Map<String, Object> env = new HashMap<>();
env.put("user", user);
String result = (String) AviatorEvaluator.execute(" '[user id='+ user.id + ',name='+user.name + ',age=' +user.age +']' ", env);
System.out.println(result);
}
@Test //script脚本+参数
void grayRule8() throws IOException {
Expression compiledExp = AviatorEvaluator.getInstance().compileScript("src/test/resources/script.av");
//执行脚本,参数可以map,也可以通过newEnv kv对的方式塞入,最终还是map
final Object o = compiledExp.execute(compiledExp.newEnv("age", 12, "name", "yxk"));
System.out.println(o);
}
@Test //自定义函数,直接穿参数
void grayRule9() throws IOException {
Expression compiledExp = AviatorEvaluator.getInstance().compileScript("src/test/resources/script1.av");
//执行脚本,参数可以map,也可以通过newEnv kv对的方式塞入,最终还是map
final Object o = compiledExp.execute(compiledExp.newEnv("age", 12, "name", "yxk"));
System.out.println(o);
// 注册自定义函数
AviatorEvaluator.addFunction(new MultiplyFunction());
// 方式1
System.out.println(AviatorEvaluator.execute("multiply(12.23, -2.3)"));
final Object o1 = compiledExp.execute(compiledExp.newEnv("age", 45, "name", "yxk"));
System.out.println(o1);
}
@Test //自定义函数,用map送入
void grayRule10() throws IOException {
Expression compiledExp = AviatorEvaluator.getInstance().compileScript("src/test/resources/script1.av");
// 注册自定义函数
AviatorEvaluator.addFunction(new MultiplyFunction());
// 方式1
Map<String, Object> env = new HashMap<>();
env.put("age", 45);
env.put("name", "yxk");
final Object o2 = compiledExp.execute(env);
System.out.println(o2);
}
@Test //当场反射
void grayRule11() throws IOException, IllegalAccessException, NoSuchMethodException {
AviatorEvaluator.addInstanceFunctions("s", String.class);
// 方式1
Map<String, Object> env = new HashMap<>();
env.put("age", 70);
env.put("name", "yxk");
Expression compiledExp = AviatorEvaluator.getInstance().compileScript("src/test/resources/script1.av");
final Object o2 = compiledExp.execute(env);
System.out.println(o2);
}
@Test //动态组代码,然后执行
void grayRule12() throws IOException, IllegalAccessException, NoSuchMethodException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("AviatorScript");
// AviatorScript code in a String
String script = "fn hello(name) { print('Hello, ' + name); }";
// evaluate script
try {
engine.eval(script);
Invocable inv = (Invocable) engine;
// invoke the global function named "hello"
inv.invokeFunction("hello", "Scripting!!" );
} catch (ScriptException e) {
e.printStackTrace();
}
}
@Test //当场反射
void grayRule13() throws IOException, IllegalAccessException, NoSuchMethodException {
// 方式1
Map<String, Object> env = new HashMap<>();
env.put("age", 76);
env.put("name", "yxk");
Expression compiledExp = AviatorEvaluator.getInstance
snetlogon20
- 粉丝: 5
- 资源: 12
最新资源
- C#ASP.NET高校移动考勤webapp源码数据库 SQL2008源码类型 WebForm
- (2000-2023年)中国各、省、市、县、乡镇基尼系数数据(全新整理)
- JAVA的SpringBoot快速开发平台源码数据库 MySQL源码类型 WebForm
- java校园跑腿综合服务网平台小程序源码带部署搭建教程数据库 MySQL源码类型 WebForm
- 时间序列-白银-1分数据
- C#VS2015进销存管理系统源码数据库 SQL2008源码类型 WebForm
- java企业报表管理系统源码数据库 MySQL源码类型 WebForm
- 软考题库试题及其解析.docx
- (全新整理)中国同一带一路国家海关进出口数据(1994-2022年)
- java医院数据分析管理系统源码数据库 MySQL源码类型 WebForm
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈