当前位置: 首页 > 工具软件 > IKExpression > 使用案例 >

表达式引擎------ScriptEngine与IKExpression介绍与比较

蒋畅
2023-12-01

前言

之前的旧项目中一直在使用ScriptEngine表达式解析引擎,但是使用过程中发现有执行错误的现象,明明为true却返回false,而且不是必现,想了很多办法,最终并没有解决,想着是否换一个试试,就接触了IKExpression。

正文

使用方法

ScriptEngine

package com.lk.sydn.common.util;

import javax.script.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Silence on 2019/1/11.
 */
public class RunScript {

    private static ScriptEngineManager scriptEngineManager;
    private static ScriptEngine scriptEngine;
    private static Bindings bindings;
    static {
        scriptEngineManager = new ScriptEngineManager();
        scriptEngine = scriptEngineManager.getEngineByName("js");
        bindings = scriptEngine.createBindings();
    }

    public static Boolean run(String expression, String key, Object value) {

        Map<String, Object> params = new HashMap<>();
        params.put(key, value);
        return run(expression, params);

    }

    public static Boolean run(String expression, Map<String, Object> params) {

        Boolean result = false;
        expression = expression.replaceAll("and", "&&").replaceAll("or", "||").replace("AND", "&&").replace("OR", "||");
        bindings.putAll(params);
        if (scriptEngine instanceof Compilable) {
            Compilable compilableScriptEngine = (Compilable) scriptEngine;
            try {
                CompiledScript compiledScript = compilableScriptEngine.compile(expression);
                result = (Boolean) compiledScript.eval(bindings);
            } catch (ScriptException e) {
            }
        } else {
            try {
                result = (Boolean) scriptEngine.eval(expression, bindings);
            } catch (ScriptException e) {
                e.printStackTrace();
            }
        }

        return result;

    }

    public static void main(String[] args) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("Q19", "Y");
//        map.put("Q18", "N");
//        map.put("Q7", "Y");
//        Boolean status = run(" ( Q18 ==  'Y'  or Q19 ==  'Y'  or Q8 ==  'Y'  )  and Q7 ==  'Y' ", map);
//        System.out.println(status);
        Boolean status = RunScript.run("x == 3512 OR x==3513", "x", "3513");
        System.out.println(status);
    }

}

IKExpression

	<dependency>
      <groupId>org.wltea.expression</groupId>
        <artifactId>IKExpression</artifactId>
        <version>2.1.2-SNAPSHOT</version>
    </dependency>
package com.lk.sydn.augur;

import org.wltea.expression.ExpressionEvaluator;
import org.wltea.expression.datameta.Variable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silence on 2020/3/24.
 */
public class Test {

    public static void main(String[] args) {

        String expression = " x == '50701' ||  x == '50401' ||  x == '51001' ||  x == '50301' ||  x == '50204' ||  x == '50205' ||  x == '50305'";
        expression = expression.replaceAll("'", "\"");

//        String expression = "x>=60.5&&x<=80";

        List<Variable> variables = new ArrayList<Variable>();
        Variable variable = Variable.createVariable("x", "50701");
        variables.add(variable);

        Boolean result = (Boolean) ExpressionEvaluator.evaluate(expression, variables);
        System.out.println(result);

    }

}

区别

1.ScriptEngine如果不用比较复杂的表达式引擎比如groovy,就不需要额外引用包,IKExpression需要引入包
1.ScriptEngine对于类型并不强制要求,比如x==‘1234’,你传string和int结果是一样的,但是IKExpression不行,必须制定string才能执行过,否则报错

 类似资料: