附加CompiledScript或添加两个CompiledScript

时间:2018-02-18 10:29:19

标签: java groovy groovyscriptengine

您好我使用Groovy引擎来评估某个脚本。问题是执行时间有点大,所以我想将我的脚本分成两个脚本。第一个是静态的,可以在@PostConstruct方法内编译,第二个是变量,它取决于用户选择的一些参数。这就是我想要做的事情:

Bindings bindings;
CompiledScript scriptC;
String script1="Static Script";
String script2="Variable Script";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("groovy");
scriptC = ((Compilable) engine).compile(script1); 
//Execute some instruction to generate 'script2'
scriptC += ((Compilable) engine).compile(script2);//Please note the += operator

Bindings bindings;
CompiledScript scriptC;
String script1="Static Script";
String script2="Variable Script";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("groovy");
scriptC = ((Compilable) engine).compile(script1); 
//Execute some instruction to generate 'script2'
scriptC.append(((Compilable) engine).compile(script2));//Please note the append function

这两个代码无法确定,我用它们只是为了说明我试图做的事情。是否可以将两个脚本合并为一个脚本?

1 个答案:

答案 0 :(得分:1)

你不能像那两个CompiledScript对象那样组合。如果涉及到Groovy - 当Groovy脚本被编译时,它会创建一个扩展groovy.lang.Script的类,里面有几个方法。在这种情况下,相互添加两个脚本意味着合并两个Java类。

考虑将分离中的两个脚本作为替代方案进行评估。看一下下面的例子:

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

public class ScriptTest {

    public static void main(String[] args) throws ScriptException {
        final ScriptEngineManager manager = new ScriptEngineManager();
        final ScriptEngine engine = manager.getEngineByName("Groovy");

        final Bindings bindings = new SimpleBindings();
        bindings.put("a", 3);

        final String script1 = "println 'Running first script...'; def c = 2; def d = c + a; return d";
        final CompiledScript compiledScript1 = ((Compilable) engine).compile(script1);

        //Execute some instruction to generate 'script2'
        final String script2 = "println 'Running second script...';";
        final CompiledScript compiledScript2 = ((Compilable) engine).compile(script2);

        Integer returnedValue = (Integer) compiledScript1.eval(bindings);
        System.out.println("Returned value from the first script: " + returnedValue);

        if (returnedValue > 1) {
            compiledScript2.eval(bindings);
        }
    }
}

创建CompiledScript对象是一回事。第二件事是对这些脚本的评估。如果您始终立即评估script1script2,那么您只需要:

compiledScript1.eval(bindings);
compiledScript2.eval(bindings);

但是script1可能会返回一些值,根据该值,您可以决定是否应评估script2。我们假设script1返回Integer,只有在返回的值大于1时才会评估script2

Integer returnedValue = (Integer) compiledScript1.eval(bindings);
if (returnedValue > 1) {
    compiledScript2.eval(bindings);
}

或者,您可以在运行script1后基于绑定值。让我们说script1修改绑定ab并将其设置为true(假设它们保持boolean类型):

compiledScript1.eval(bindings);
if (bindings.get("a") && bindings.get("b")) {
    compiledScript2.eval(bindings);
}

script1

调用script2中定义的函数

让我们说script1定义如下:

import groovy.json.JsonOutput

def json(Map map) {
    return new JsonOutput().toJson(map)
}

println json([test: 1])

如果您必须在json(map)内调用script2功能 - 您可以执行此操作。看一下这个例子:

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

public class ScriptTest {

    public static void main(String[] args) throws ScriptException {
        final ScriptEngineManager manager = new ScriptEngineManager();
        final ScriptEngine engine = manager.getEngineByName("Groovy");

        final Bindings bindings = new SimpleBindings();
        bindings.put("a", 3);

        final String script1 = "import groovy.json.JsonOutput\n" +
                "\n" +
                "def json(Map map) {\n" +
                "    return new JsonOutput().toJson(map)\n" +
                "}\n" +
                "\n" +
                "println json([test: 1])";

        final CompiledScript compiledScript1 = ((Compilable) engine).compile(script1);

        //Execute some instruction to generate 'script2'
        final String script2 = "println 'Running second script...'; println json([test: 2]);";
        final CompiledScript compiledScript2 = ((Compilable) engine).compile(script2);

        compiledScript1.eval(bindings);
        compiledScript2.eval(bindings);
    }
}

script2定义为:

println 'Running second script...';
println json([test: 2]);

当你运行它时,你会在控制台中看到:

{"test":1}
Running second script...
{"test":2}

前两行来自script1,而最后一行来自script2。请记住,CompiledScript都拥有对同一引擎的引用,因此您可以引用先前评估过的脚本中定义的函数。