当前位置:网站首页>Aviator an expression evaluation engine

Aviator an expression evaluation engine

2022-06-25 06:18:00 WD Technology

1. brief introduction
Aviator It's a high performance 、 Lightweight java Expression evaluation engine implemented by language , Mainly used for dynamic evaluation of various expressions . Now there are a lot of open source available java Expression evaluation engine , Why do we still need Avaitor Well ?
Aviator The design goal is lightweight and high performance , Compared with Groovy、JRuby It's heavy , Aviator A very small , Plus dependent packages 537K, Not counting dependent packages, there are only 70K; Of course , Aviator The syntax of is limited , It's not a complete language , It's just a small set of languages .
secondly , Aviator The implementation idea of is very different from other lightweight evaluators , Other evaluators generally run by interpretation , and Aviator It is to compile the expression directly into JVM Bytecode , hand JVM To carry out . Simply speaking , Aviator Is positioned between Groovy Such a heavyweight scripting language and IKExpression Between such lightweight expression engines .

2. User manual
(1)、Aviator The use of is centralized through com.googlecode.aviator.AviatorEvaluator This entry class handles , The simplest example , Perform a calculation 1+2+3 The expression of :

val result = AviatorEvaluator.execute("1+2+3")
println(result)     //6

(2)、 If it's on ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL Options , Then the floating-point numbers appearing in the expression will be resolved to BigDecimal, This is for the convenience of some users who require high-precision calculation , I don't want to add... To floating point numbers M The suffix is marked as BigDecimal

AviatorEvaluator.setOption(Options.ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL, true)

(3)、 from 4.0.0 Start , aviator Support semicolon ; Separated multiline expressions , The result of evaluating a multiline expression will be the result of the last expression , for example

val result = AviatorEvaluator.execute("println('hello world'); 1+2+3 ; 100-1")
println(result) 
// Output results :
//hello world
//99

3、 Execution example
First compile an expression containing variables , Then execute the environment with different variables . That's better performance

//compile Method can compile an expression into Expression The intermediate object of ,
// When the expression is to be executed, pass in env And call Expression Of execute The method can 
    public static void main(String[] args){
       String expStr="a*b";
       Expression expression=AviatorEvaluator.compile(expStr);
       Map<String,Object> env=new HashMap<>();// Notice that it must be <String,Object> Of 
       env.put("a",3L);
       env.put("b",2L);
 
        System.out.println("result:"+expression.execute(env));
    }
原网站

版权声明
本文为[WD Technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201237011260.html