use of org.apache.commons.jexl3.internal.Script in project iotdb by apache.
the class UDTFJexl method beforeStart.
@Override
public void beforeStart(UDFParameters parameters, UDTFConfigurations configurations) throws UDFInputSeriesDataTypeNotValidException, UDFOutputSeriesDataTypeNotValidException, MetadataException {
String expr = parameters.getString("expr");
JexlEngine jexl = new JexlBuilder().create();
script = jexl.createScript(expr);
inputDataType = new TSDataType[inputSeriesNumber];
for (int i = 0; i < inputSeriesNumber; i++) {
inputDataType[i] = parameters.getDataType(i);
}
outputDataType = probeOutputDataType();
if (inputSeriesNumber == 1) {
switch(inputDataType[0]) {
case INT32:
evaluator = new EvaluatorIntInput();
break;
case INT64:
evaluator = new EvaluatorLongInput();
break;
case FLOAT:
evaluator = new EvaluatorFloatInput();
break;
case DOUBLE:
evaluator = new EvaluatorDoubleInput();
break;
case TEXT:
evaluator = new EvaluatorStringInput();
break;
case BOOLEAN:
evaluator = new EvaluatorBooleanInput();
break;
default:
throw new UDFInputSeriesDataTypeNotValidException(0, inputDataType[0], TSDataType.INT32, TSDataType.INT64, TSDataType.FLOAT, TSDataType.DOUBLE, TSDataType.TEXT, TSDataType.BOOLEAN);
}
} else {
evaluator = new EvaluatorMulInput();
}
configurations.setAccessStrategy(new RowByRowAccessStrategy()).setOutputDataType(outputDataType);
}
use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.
the class Closure method setCaptured.
/**
* Sets the captured index of a given symbol, ie the target index of a parent
* captured symbol in this closure's frame.
* <p>This is meant to allow a locally defined function to "see" and call
* itself as a local (captured) variable;
* in other words, this allows recursive call of a function.
* @param symbol the symbol index (in the caller of this closure)
* @param value the value to set in the local frame
*/
public void setCaptured(final int symbol, final Object value) {
if (script instanceof ASTJexlLambda) {
final ASTJexlLambda lambda = (ASTJexlLambda) script;
final Scope scope = lambda.getScope();
if (scope != null) {
final Integer reg = scope.getCaptured(symbol);
if (reg != null) {
frame.set(reg, value);
}
}
}
}
use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.
the class JexlScriptEngine method eval.
@Override
public Object eval(final String script, final ScriptContext context) throws ScriptException {
// This is mandated by JSR-223 (see SCR.5.5.2 Methods)
if (script == null || context == null) {
throw new NullPointerException("script and context must be non-null");
}
// This is mandated by JSR-223 (end of section SCR.4.3.4.1.2 - JexlScript Execution)
context.setAttribute(CONTEXT_KEY, context, ScriptContext.ENGINE_SCOPE);
try {
final JexlScript jexlScript = jexlEngine.createScript(script);
final JexlContext ctxt = new JexlContextWrapper(context);
return jexlScript.execute(ctxt);
} catch (final Exception e) {
throw scriptException(e);
}
}
use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.
the class Issues200Test method testTemplate6565a.
@Test
public void testTemplate6565a() throws Exception {
final JexlEngine jexl = new JexlBuilder().create();
final JxltEngine jexlt = jexl.createJxltEngine();
final String source = "$$ var res = '';\n" + "$$ var meta = session.data['METADATA'];\n" + "$$ if (meta) {\n" + "$$ var entry = meta['ID'];\n" + "$$ if (entry) {\n" + "$$ var value = session.data[entry];\n" + "$$ res = value?: '';\n" + "$$ }\n" + "$$ }\n" + "${res}\n";
final JxltEngine.Template script = jexlt.createTemplate("$$", new StringReader(source));
Assert.assertNotNull(script);
final TemplateDebugger dbg = new TemplateDebugger();
final String refactored = dbg.debug(script) ? dbg.toString() : "";
Assert.assertNotNull(refactored);
Assert.assertEquals(source, refactored);
}
use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.
the class TemplateDebugger method debug.
/**
* Position the debugger on the root of a template script.
* @param jt the template
* @return true if the template was a {@link TemplateScript} instance, false otherwise
*/
public boolean debug(final JxltEngine.Template jt) {
if (!(jt instanceof TemplateScript)) {
return false;
}
final TemplateScript ts = (TemplateScript) jt;
// ensure expr is not null for templates
this.exprs = ts.getExpressions() == null ? new TemplateExpression[0] : ts.getExpressions();
this.script = ts.getScript();
start = 0;
end = 0;
indentLevel = 0;
builder.setLength(0);
cause = script;
final int num = script.jjtGetNumChildren();
for (int i = 0; i < num; ++i) {
final JexlNode child = script.jjtGetChild(i);
acceptStatement(child, null);
}
// the last line
if (builder.length() > 0 && builder.charAt(builder.length() - 1) != '\n') {
builder.append('\n');
}
end = builder.length();
return end > 0;
}
Aggregations