本文整理汇总了Java中groovy.lang.GroovyShell.evaluate方法的典型用法代码示例。如果您正苦于以下问题:Java GroovyShell.evaluate方法的具体用法?Java GroovyShell.evaluate怎么用?Java GroovyShell.evaluate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类groovy.lang.GroovyShell
的用法示例。
在下文中一共展示了GroovyShell.evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
@Override
public void process(Network network, ComputationManager computationManager) throws Exception {
if (Files.exists(script)) {
LOGGER.debug("Execute groovy post processor {}", script);
try (Reader reader = Files.newBufferedReader(script, StandardCharsets.UTF_8)) {
CompilerConfiguration conf = new CompilerConfiguration();
Binding binding = new Binding();
binding.setVariable("network", network);
binding.setVariable("computationManager", computationManager);
GroovyShell shell = new GroovyShell(binding, conf);
shell.evaluate(reader);
}
}
}
示例2: applyConfigurationScript
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
private void applyConfigurationScript(File configScript, CompilerConfiguration configuration) {
VersionNumber version = parseGroovyVersion();
if (version.compareTo(VersionNumber.parse("2.1")) < 0) {
throw new GradleException("Using a Groovy compiler configuration script requires Groovy 2.1+ but found Groovy " + version + "");
}
Binding binding = new Binding();
binding.setVariable("configuration", configuration);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
try {
shell.evaluate(configScript);
} catch (Exception e) {
throw new GradleException("Could not execute Groovy compiler configuration script: " + configScript.getAbsolutePath(), e);
}
}
示例3: getGroovyAttributeValue
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
private static Object getGroovyAttributeValue(final String groovyScript,
final Map<String, Object> resolvedAttributes) {
try {
final Binding binding = new Binding();
final GroovyShell shell = new GroovyShell(binding);
binding.setVariable("attributes", resolvedAttributes);
binding.setVariable("logger", LOGGER);
LOGGER.debug("Executing groovy script [{}] with attributes binding of [{}]",
StringUtils.abbreviate(groovyScript, groovyScript.length() / 2), resolvedAttributes);
final Object res = shell.evaluate(groovyScript);
return res;
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
示例4: interpret
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
public static EObject interpret(String groovyScript) {
EDataType data = EcorePackage.eINSTANCE.getEString();
Binding binding = new Binding();
//Binding setVariable allow to pass a variable from the moonti arc model to the groovy interpreter
//binding.setVariable(name, value);
GroovyShell shell = new GroovyShell(binding);
Object result = shell.evaluate(groovyScript);
//Binding.getVariable get the new value of this variable.
// binding.getVariable(name)
// data.setName(""+(rand.nextInt(100)+1));
data.setName(""+result);
return data;
}
示例5: runGroovy
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
public static void runGroovy(int xmax, int ymax, int zmax) {
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
String expression = "x + y*2 - z";
Integer result = 0;
Date start = new Date();
for (int xval = 0; xval < xmax; xval++) {
for (int yval = 0; yval < ymax; yval++) {
for (int zval = 0; zval <= zmax; zval++) {
binding.setVariable("x", xval);
binding.setVariable("y", yval);
binding.setVariable("z", zval);
Integer cal = (Integer) shell.evaluate(expression);
result += cal;
}
}
}
Date end = new Date();
System.out.println("Groovy:time is : " + (end.getTime() - start.getTime()) + ",result is " + result);
}
示例6: main
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
public static void main(String[] args) {
List<String> rules = Lists.newArrayList();
rules.add("S <= 10");
rules.add("S + A <= 20");
rules.add("S + A + BP <= 55");
rules.add("C >= 5");
Binding binding = new Binding();
binding.setVariable("S", 5.0);
binding.setVariable("A", 5.0);
binding.setVariable("BP", 30.0);
binding.setVariable("B", 30.0);
binding.setVariable("C", 30.0);
GroovyShell shell = new GroovyShell(binding);
for (String rule : rules) {
Object result = shell.evaluate(rule);
System.out.println(result);
}
}
示例7: getAction
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
/**
* Extract the quest name from a context.
*
* @param ctx
* The configuration context.
* @return The quest name.
* @throws IllegalArgumentException
* If the quest attribute is missing.
*/
protected ChatAction getAction(final ConfigurableFactoryContext ctx) {
String value = ctx.getString("action", null);
if (value == null) {
return null;
}
Binding groovyBinding = new Binding();
final GroovyShell interp = new GroovyShell(groovyBinding);
try {
String code = "import games.stendhal.server.entity.npc.action.*;\r\n"
+ value;
return (ChatAction) interp.evaluate(code);
} catch (CompilationFailedException e) {
throw new IllegalArgumentException(e);
}
}
示例8: shouldFail
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
/**
* Asserts that the given script fails when it is evaluated
* and that a particular type of exception is thrown.
*
* @param clazz the class of the expected exception
* @param script the script that should fail
* @return the caught exception
*/
public static Throwable shouldFail(Class clazz, String script) {
Throwable th = null;
try {
GroovyShell shell = new GroovyShell();
shell.evaluate(script, genericScriptName());
} catch (GroovyRuntimeException gre) {
th = ScriptBytecodeAdapter.unwrap(gre);
} catch (Throwable e) {
th = e;
}
if (th == null) {
fail("Script should have failed with an exception of type " + clazz.getName());
} else if (!clazz.isInstance(th)) {
fail("Script should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + th);
}
return th;
}
示例9: testMarkupBug
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
public void testMarkupBug() throws Exception {
String[] lines =
{
"package groovy.xml",
"",
"b = new MarkupBuilder()",
"",
"b.root1(a:5, b:7) { ",
" elem1('hello1') ",
" elem2('hello2') ",
" elem3(x:7) ",
"}"};
String code = asCode(lines);
GroovyShell shell = new GroovyShell();
shell.evaluate(code);
}
示例10: testInvokeRawMethod
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
@Test
public void testInvokeRawMethod() throws Exception {
ListMethods itf = list -> {
JsonObject combined = new JsonObject();
list.forEach(combined::mergeIn);
return combined;
};
CompilerConfiguration config = new CompilerConfiguration();
Properties props = new Properties();
props.setProperty("groovy.disabled.global.ast.transformations", "io.vertx.lang.groovy.VertxTransformation");
config.configure(props);
GroovyShell shell = new GroovyShell(config);
shell.setProperty("itf", itf);
Object o = shell.evaluate("return itf.jsonList([new io.vertx.core.json.JsonObject().put('foo', 'foo_value'), new io.vertx.core.json.JsonObject().put('bar', 'bar_value')])");
JsonObject result = (JsonObject) o;
assertEquals(result, new JsonObject().put("foo", "foo_value").put("bar", "bar_value"));
}
示例11: doAssert
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
@Override
public AssertionResult doAssert(InputSource source) {
Document xmlDocument;
try {
xmlDocument = builder.parse(source);
} catch (Exception e) {
throw new RuntimeException(e);
}
Binding binding = new Binding();
binding.setVariable("document", xmlDocument);
GroovyShell shell = new GroovyShell(binding);
Object value = shell.evaluate(script);
if (value instanceof Boolean){
if ((Boolean)value){
return new AssertionResult(assertionName, AssertionType.SUCCESS);
} else {
return new AssertionResult(assertionName, AssertionType.FAILED);
}
} else {
throw new RuntimeException("value of type "+value.getClass()+" is returned from groovy script");
}
}
示例12: executeScript
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
public boolean executeScript(int pIndex, PrintStream outStream,
ErrorHandler pErrorHandler) {
Binding binding = new Binding();
binding.setVariable("c", null);
binding.setVariable("node", null);
GroovyShell shell = new GroovyShell(binding);
String script = getScript(pIndex).getScript();
// redirect output:
PrintStream oldOut = System.out;
Object value;
try {
System.setOut(outStream);
value = shell.evaluate(script);
} finally {
System.setOut(oldOut);
}
return true;
}
示例13: runScript
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
public static Object runScript(String script,
String scriptLanguage,
Object root)
throws Exception {
OgnlContext ognlContext = ElementsThreadLocals.getOgnlContext();
if ("ognl".equals(scriptLanguage)) {
return OgnlUtils.getValueQuietly(script, ognlContext, root);
} else if ("groovy".equals(scriptLanguage)) {
ognlContext.put("root", root);
Binding binding = new Binding(ognlContext);
GroovyShell shell = new GroovyShell(binding);
return shell.evaluate(script);
} else {
String msg = String.format(
"Unrecognised script language: %s", scriptLanguage);
throw new IllegalArgumentException(msg);
}
}
示例14: instantiateScriplet
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
private Object instantiateScriplet(String scripletFilename)
throws Exception {
String[] tokens = SorcerUtil.tokenize(scripletFilename, "|");
Object bean = null;
Object configurator = null;
GroovyShell shell = new GroovyShell();
bean = shell.evaluate(new File(tokens[0]));
for (int i = 1; i < tokens.length; i++) {
configurator = shell.evaluate(new File(tokens[i]));
if ((configurator instanceof Configurator)
&& (bean instanceof Configurable)) {
shell.setVariable("configurable", bean);
bean = ((Configurator) configurator).configure();
}
}
initBean(bean);
return bean;
}
示例15: getValue
import groovy.lang.GroovyShell; //导入方法依赖的package包/类
@Override
public String getValue(String orginData)
{
String value;
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
binding.setVariable("globalMap", globalMap);
Object resObj = null;
try
{
resObj = shell.evaluate(groovyCls + orginData);
if(resObj != null)
{
value = resObj.toString();
}
else
{
value = "groovy not return!";
}
}
catch(CompilationFailedException e)
{
value = e.getMessage();
logger.error("Groovy动态数据语法错误!", e);
}
return value;
}