Search in sources :

Example 6 with Script

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);
}
Also used : JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlEngine(org.apache.commons.jexl3.JexlEngine) UDFInputSeriesDataTypeNotValidException(org.apache.iotdb.commons.udf.api.exception.UDFInputSeriesDataTypeNotValidException) RowByRowAccessStrategy(org.apache.iotdb.commons.udf.api.customizer.strategy.RowByRowAccessStrategy)

Example 7 with Script

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);
            }
        }
    }
}
Also used : ASTJexlLambda(org.apache.commons.jexl3.parser.ASTJexlLambda)

Example 8 with Script

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);
    }
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) JexlScript(org.apache.commons.jexl3.JexlScript) IOException(java.io.IOException) JexlException(org.apache.commons.jexl3.JexlException) ScriptException(javax.script.ScriptException)

Example 9 with Script

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);
}
Also used : StringReader(java.io.StringReader) TemplateDebugger(org.apache.commons.jexl3.internal.TemplateDebugger) Test(org.junit.Test)

Example 10 with Script

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;
}
Also used : TemplateExpression(org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Aggregations

Test (org.junit.Test)30 JexlScript (org.apache.commons.jexl3.JexlScript)26 JexlEngine (org.apache.commons.jexl3.JexlEngine)20 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)18 JexlException (org.apache.commons.jexl3.JexlException)18 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)13 JexlContext (org.apache.commons.jexl3.JexlContext)9 JexlNode (org.apache.commons.jexl3.parser.JexlNode)8 MapContext (org.apache.commons.jexl3.MapContext)7 JexlInfo (org.apache.commons.jexl3.JexlInfo)4 TemplateDebugger (org.apache.commons.jexl3.internal.TemplateDebugger)4 ASTJexlLambda (org.apache.commons.jexl3.parser.ASTJexlLambda)4 JexlFeatures (org.apache.commons.jexl3.JexlFeatures)3 JexlOptions (org.apache.commons.jexl3.JexlOptions)3 JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)3 JexlUberspect (org.apache.commons.jexl3.introspection.JexlUberspect)3 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)3 StringReader (java.io.StringReader)2 Map (java.util.Map)2 ScriptException (javax.script.ScriptException)2