Aviator 一种表达式求值引擎
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
1.简介
2.特性
3.依赖
com.googlecode.aviator
aviator
{version}
4.使用手册
val result = AviatorEvaluator.execute("1+2+3")
println(result) //6
AviatorEvaluator.setOption(Options.ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL, true)
val result = AviatorEvaluator.execute("println('hello world'); 1+2+3 ; 100-1")
println(result)
//输出结果:
//hello world
//99
val instance = AviatorEvaluator.newInstance
//接下来使用 instance,几乎跟 AviatorEvaluator 没有不同,只是换成了实例方法
val world = "world"
val env = new util.HashMap[String,Object]()
env.put("world",world)
val result =AviatorEvaluator.execute(" 'hello ' + world ",env)
println(result) // hello world
AviatorEvaluator.execute(" 'a\"b' ") // 字符串 a"b
AviatorEvaluator.execute(" \"a\'b\" ") // 字符串 a'b
AviatorEvaluator.execute(" 'hello ' + 3 ") // 字符串 hello 3
AviatorEvaluator.execute(" 'hello '+ unknow ") // 字符串 hello null
val value = "world"
AviatorEvaluator.exec("'hello' + value",value) // hello world
AviatorEvaluator.execute("string.length('hello')")
AviatorEvaluator.execute("string.contains(\"test\", string.substring('hello', 1, 2))") // true
object test {
def main(args: Array[String]): Unit = {
AviatorEvaluator.addFunction(new AddFunction)
System.out.println(AviatorEvaluator.execute("add(1, 2)")) // 3.0
System.out.println(AviatorEvaluator.execute("add(add(1, 2), 100)")) // 103.0
}
}
class AddFunction extends AbstractFunction{
override def call(env: java.util.Map[String, Object], arg1: AviatorObject, arg2: AviatorObject): AviatorObject = {
val left = FunctionUtils.getNumberValue(arg1, env)
val right = FunctionUtils.getNumberValue(arg2, env)
new AviatorDouble(left.doubleValue + right.doubleValue)
}
def getName = "add"
}
object test {
def main(args: Array[String]): Unit = {
AviatorEvaluator.addFunction(new GetFirstNonNullFunction)
System.out.println(AviatorEvaluator.execute("getFirstNonNull(1)")) // 1
System.out.println(AviatorEvaluator.execute("getFirstNonNull(1,2,3,4,nil,5)")) // 1
System.out.println(AviatorEvaluator.execute("getFirstNonNull(a,b,c,d)")) // null
}
}
class GetFirstNonNullFunction extends AbstractVariadicFunction {
override def variadicCall(env: util.Map[String, AnyRef], args: AviatorObject*): AviatorObject = {
if (args != null) for (arg <- args) {
if (arg.getValue(env) != null) return arg
}
new AviatorString(null)
}
override def getName = "getFirstNonNull"
}
AviatorEvaluator.defineFunction("add", "lambda (x,y) -> x + y end")
AviatorEvaluator.exec("add(1,2)") //3
class function extends FunctionLoader{
/**
* Invoked when function not found
*
* @param name function name
*/
override def onFunctionNotFound(name: String): AviatorFunction = ???
}
AviatorEvaluator.addOpFunction(OperatorType.BIT_AND, new AbstractFunction() {
override def call(env: util.Map[String, Object], arg1: AviatorObject, arg2: AviatorObject) = new AviatorString(arg1.getValue(env).toString + arg2.getValue(env).toString)
override def getName = "&"
})
val map = new util.HashMap[String,Object]()
map.put("a","4")
val result = AviatorEvaluator.execute("a&3", map)
println(result) //43
val map1 = new util.HashMap[String,Object]()
map1.put("a","hello")
val result1 = AviatorEvaluator.execute("a&' world'", map1)
println(result1) //hello world
val expression = "a-(b-c)>100"
// 编译表达式
val compiledExp = AviatorEvaluator.compile(expression)
val env = new util.HashMap[String,Object]()
env.put("a", 100.3.asInstanceOf[Object])
env.put("b", new Integer(45))
env.put("c", (-199.100).asInstanceOf[Object])
// 执行表达式
val result = compiledExp.execute(env).asInstanceOf[Boolean]
println(result) // false
public static Expression compile(final String expression, final boolean cached)
public static void invalidateCache(String expression)
val list = new util.ArrayList[String]
list.add("hello")
list.add(" world")
val array = new Array[Int](3)
array(0) = 0
array(1) = 1
array(2) = 3
val map = new util.HashMap[String,Object]()
map.put("date", new Date())
val env = new util.HashMap[String,Object]()
env.put("list", list)
env.put("array", array)
env.put("mmap", map)
println(AviatorEvaluator.execute("list[0]+list[1]", env)) // hello world
println(AviatorEvaluator.execute("'array[0]+array[1]+array[2]=' + (array[0]+array[1]+array[2])", env)) // array[0]+array[1]+array[2]=4
println(AviatorEvaluator.execute("'today is ' + mmap.date ", env)) // today is Tue Mar 19 11:42:51 CST 2019
println(AviatorEvaluator.exec("string.split(s,',')[0]", "a,b,c,d")) //a
println(AviatorEvaluator.exec("a>0? 'yes':'no'", new Integer(1))) // yes
val email = "killme2008@gmail.com"
val env = new util.HashMap[String,Object]()
env.put("email", email)
val username = AviatorEvaluator.execute("email=~/([\\w0-8]+)@\\w+[\\.\\w+]+/ ? $1 : 'unknow' ", env).asInstanceOf[String]
println(username) // killme2008
object test {
def main(args: Array[String]): Unit = {
val foo = new Foo(100, 3.14f, new Date())
val env = new util.HashMap[String,Object]()
env.put("foo", foo)
println(AviatorEvaluator.execute("'foo.i = '+foo.i", env)) // foo.i = 100
println(AviatorEvaluator.execute("'foo.f = '+foo.f", env)) // foo.f = 3.14
println(AviatorEvaluator.execute("'foo.date.year = '+(foo.date.year+1900)", env)) // foo.date.year = 2019
}
}
class Foo(var i: Int, var f: Float, var date: Date) {
def getI: Int = i
def setI(i: Int): Unit = {
this.i = i
}
def getF: Float = f
def setF(f: Float): Unit = {
this.f = f
}
def getDate: Date = date
def setDate(date: Date): Unit = {
this.date = date
}
}
AviatorEvaluator.execute("nil == nil"); //true
AviatorEvaluator.execute(" 3> nil"); //true
AviatorEvaluator.execute(" true!= nil"); //true
AviatorEvaluator.execute(" ' '>nil "); //true
AviatorEvaluator.execute(" a==nil "); //true, a 是 null
val env = new util.HashMap[String,Object]()
val date = new Date()
val dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(date)
env.put("date", date)
env.put("dateStr", dateStr)
var result = AviatorEvaluator.execute("date==dateStr", env).asInstanceOf[Boolean]
println(result) // true
result = AviatorEvaluator.execute("date > '2010-12-20 00:00:00:00' ", env).asInstanceOf[Boolean]
println(result)// true
result = AviatorEvaluator.execute("date < '2200-12-20 00:00:00:00' ", env).asInstanceOf[Boolean]
println(result)// true
result = AviatorEvaluator.execute("date==date ", env).asInstanceOf[Boolean]
println(result)// true
println(AviatorEvaluator.execute("99999999999999999999999999999999 + 99999999999999999999999999999999"))
//结果:199999999999999999999999999999998
AviatorEvaluator.setOption(Options.ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL, true)
var rt = AviatorEvaluator.exec("9223372036854775807100.356M * 2")
println(rt + " " + rt.getClass) // 18446744073709551614200.712 class java.math.BigDecimal
rt = AviatorEvaluator.exec("92233720368547758074+1000")
println(rt + " " + rt.getClass) // 92233720368547759074 class java.math.BigInteger
val a = new BigInteger(String.valueOf(Long.MaxValue) + String.valueOf(Long.MaxValue))
val b = new java.math.BigDecimal("3.2")
val c = new java.math.BigDecimal("9999.99999")
rt = AviatorEvaluator.exec("a+10000000000000000000", a)
println(rt + " " + rt.getClass) // 92233720368547758089223372036854775807 class java.math.BigInteger
rt = AviatorEvaluator.exec("b+c*2", b, c)
println(rt + " " + rt.getClass) // 20003.19998 class java.math.BigDecimal
rt = AviatorEvaluator.exec("a*b/c", a, b, c)
println(rt + " " + rt.getClass) // 2.951479054745007313280155218459508E+34 class java.math.BigDecimal
AviatorEvaluator.setOption(Options.MATH_CONTEXT, MathContext.DECIMAL64)
val env = new util.HashMap[String,Object]()
val list = new util.ArrayList[Integer]
list.add(3)
list.add(20)
list.add(10)
env.put("list", list)
var result = AviatorEvaluator.execute("count(list)", env)
println(result) // 3
result = AviatorEvaluator.execute("reduce(list,+,0)", env)
println(result) // 33
result = AviatorEvaluator.execute("filter(list,seq.gt(9))", env)
println(result) // [20, 10]
result = AviatorEvaluator.execute("include(list,10)", env)
println(result) // true
result = AviatorEvaluator.execute("sort(list)", env)
println(result) // [3, 10, 20]
AviatorEvaluator.execute("map(list,println)", env)
AviatorEvaluator.setOption(Options.OPTIMIZE_LEVEL, AviatorEvaluator.EVAL)
AviatorEvaluator.setOption(Options.OPTIMIZE_LEVEL, AviatorEvaluator.COMPILE)
AviatorEvaluator.setOption(Options.TRACE, true)
AviatorEvaluator.setTraceOutputStream(new FileOutputStream(new File("aviator.log")))
5.语法手册
数据类型
操作符
6.内置函数
7.选项列表
8.JavaDoc
9.4.0功能详解
10.参考链接
示例:
https://github.com/killme2008/aviatorscript/tree/master/src/test/java/com/googlecode/aviator/example
官方指南:
https://github.com/killme2008/aviatorscript/wiki
作者 | 墨小柒
来源 | csdn.net/sinat_37690778/article/details/88668639
评论