Search in sources :

Example 1 with ExpressionEvaluator

use of org.codehaus.janino.ExpressionEvaluator in project pentaho-kettle by pentaho.

the class JavaFilter method calcFields.

private boolean calcFields(RowMetaInterface rowMeta, Object[] r) throws KettleValueException {
    try {
        // 
        if (data.expressionEvaluator == null) {
            String realCondition = environmentSubstitute(meta.getCondition());
            data.argumentIndexes = new ArrayList<Integer>();
            List<String> parameterNames = new ArrayList<String>();
            List<Class<?>> parameterTypes = new ArrayList<Class<?>>();
            for (int i = 0; i < data.outputRowMeta.size(); i++) {
                ValueMetaInterface valueMeta = data.outputRowMeta.getValueMeta(i);
                // 
                if (realCondition.contains(valueMeta.getName())) {
                    // If so, add it to the indexes...
                    data.argumentIndexes.add(i);
                    Class<?> parameterType;
                    switch(valueMeta.getType()) {
                        case ValueMetaInterface.TYPE_STRING:
                            parameterType = String.class;
                            break;
                        case ValueMetaInterface.TYPE_NUMBER:
                            parameterType = Double.class;
                            break;
                        case ValueMetaInterface.TYPE_INTEGER:
                            parameterType = Long.class;
                            break;
                        case ValueMetaInterface.TYPE_DATE:
                            parameterType = Date.class;
                            break;
                        case ValueMetaInterface.TYPE_BIGNUMBER:
                            parameterType = BigDecimal.class;
                            break;
                        case ValueMetaInterface.TYPE_BOOLEAN:
                            parameterType = Boolean.class;
                            break;
                        case ValueMetaInterface.TYPE_BINARY:
                            parameterType = byte[].class;
                            break;
                        default:
                            parameterType = String.class;
                            break;
                    }
                    parameterTypes.add(parameterType);
                    parameterNames.add(valueMeta.getName());
                }
            }
            // Create the expression evaluator: is relatively slow so we do it only for the first row...
            // 
            data.expressionEvaluator = new ExpressionEvaluator();
            data.expressionEvaluator.setParameters(parameterNames.toArray(new String[parameterNames.size()]), parameterTypes.toArray(new Class<?>[parameterTypes.size()]));
            data.expressionEvaluator.setReturnType(Object.class);
            data.expressionEvaluator.setThrownExceptions(new Class<?>[] { Exception.class });
            data.expressionEvaluator.cook(realCondition);
            // Also create the argument data structure once...
            // 
            data.argumentData = new Object[data.argumentIndexes.size()];
        }
        // 
        for (int x = 0; x < data.argumentIndexes.size(); x++) {
            int index = data.argumentIndexes.get(x);
            ValueMetaInterface outputValueMeta = data.outputRowMeta.getValueMeta(index);
            data.argumentData[x] = outputValueMeta.convertToNormalStorageType(r[index]);
        }
        Object formulaResult = data.expressionEvaluator.evaluate(data.argumentData);
        if (formulaResult instanceof Boolean) {
            return (Boolean) formulaResult;
        } else {
            throw new KettleException("The result of the filter expression must be a boolean and we got back : " + formulaResult.getClass().getName());
        }
    } catch (Exception e) {
        throw new KettleValueException(e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) ArrayList(java.util.ArrayList) ExpressionEvaluator(org.codehaus.janino.ExpressionEvaluator) KettleException(org.pentaho.di.core.exception.KettleException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 2 with ExpressionEvaluator

use of org.codehaus.janino.ExpressionEvaluator in project jaffa-framework by jaffa-projects.

the class MDCFilter method getExpressionEvaluator.

/**
 * Create "ExpressionEvaluator" object.
 *
 * @param types
 *          <code>List</code>
 * @param values
 *          <code>String</code>
 * @return <code>ExpressionEvaluator</code>
 */
private ExpressionEvaluator getExpressionEvaluator(final List<Class<String>> types, final String[] values) {
    ExpressionEvaluator evaluator = ExpressionCache.getInstance().get(expression);
    if (evaluator == null) {
        evaluator = new ExpressionEvaluator();
        evaluator.setParameters(values, types.toArray(new Class[0]));
        try {
            evaluator.cook(expression);
            ExpressionCache.getInstance().put(expression, evaluator);
        } catch (CompileException e) {
            LOG.error(e);
        } catch (ParseException e) {
            LOG.error(e);
        } catch (ScanException e) {
            LOG.error(e);
        }
    }
    return evaluator;
}
Also used : ScanException(org.codehaus.janino.Scanner.ScanException) CompileException(org.codehaus.janino.CompileException) ParseException(org.codehaus.janino.Parser.ParseException) ExpressionEvaluator(org.codehaus.janino.ExpressionEvaluator)

Example 3 with ExpressionEvaluator

use of org.codehaus.janino.ExpressionEvaluator in project flink by apache.

the class CompileUtilsTest method testExpressionCacheReuse.

@Test
public void testExpressionCacheReuse() {
    String code = "a + b";
    ExpressionEvaluator evaluator1 = CompileUtils.compileExpression(code, Arrays.asList("a", "b"), Arrays.asList(Integer.class, Integer.class), Integer.class);
    ExpressionEvaluator evaluator2 = CompileUtils.compileExpression(code, Arrays.asList("a", "b"), Arrays.asList(Integer.class, Integer.class), Integer.class);
    ExpressionEvaluator evaluator3 = CompileUtils.compileExpression(code, Arrays.asList("a", "b"), Arrays.asList(String.class, String.class), String.class);
    assertSame(evaluator1, evaluator2);
    assertNotSame(evaluator1, evaluator3);
}
Also used : ExpressionEvaluator(org.codehaus.janino.ExpressionEvaluator) Test(org.junit.Test)

Example 4 with ExpressionEvaluator

use of org.codehaus.janino.ExpressionEvaluator in project jaffa-framework by jaffa-projects.

the class ExpressionCacheTest method testDecide.

@Test
public void testDecide() {
    final String keys = "LoggedBy,MessageId";
    final String expression = "LoggedBy != null || MessageId != null";
    final List<Object> mdcValues = new ArrayList<Object>();
    final List<Class<String>> keyTypes = new ArrayList<Class<String>>();
    final String[] keyValues = keys.split(",");
    if (keyValues != null) {
        for (final String keyValue : keyValues) {
            final Object mdcValue = MDC.get(keyValue);
            keyTypes.add((Class<String>) keyValue.getClass());
            mdcValues.add(mdcValue);
        }
    }
    final ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.setParameters(keyValues, keyTypes.toArray(new Class[0]));
    try {
        evaluator.cook(expression);
    } catch (Exception e) {
        LOG.error(e);
        fail(e.getMessage());
    }
    assertNotNull(evaluator);
    ExpressionCache.getInstance().put(expression, evaluator);
    ExpressionEvaluator testEvaluator = ExpressionCache.getInstance().get(expression);
    assertNotNull(testEvaluator);
    assertTrue(testEvaluator.equals(evaluator));
    ExpressionCache.getInstance().put(expression, evaluator);
}
Also used : ArrayList(java.util.ArrayList) ExpressionEvaluator(org.codehaus.janino.ExpressionEvaluator) Test(org.junit.Test)

Example 5 with ExpressionEvaluator

use of org.codehaus.janino.ExpressionEvaluator in project pentaho-kettle by pentaho.

the class Janino method calcFields.

private Object[] calcFields(RowMetaInterface rowMeta, Object[] r) throws KettleValueException {
    try {
        Object[] outputRowData = RowDataUtil.createResizedCopy(r, data.outputRowMeta.size());
        int tempIndex = rowMeta.size();
        // 
        if (data.expressionEvaluators == null) {
            data.expressionEvaluators = new ExpressionEvaluator[meta.getFormula().length];
            data.argumentIndexes = new ArrayList<List<Integer>>();
            for (int i = 0; i < meta.getFormula().length; i++) {
                List<Integer> argIndexes = new ArrayList<Integer>();
                data.argumentIndexes.add(argIndexes);
            }
            for (int m = 0; m < meta.getFormula().length; m++) {
                List<Integer> argIndexes = data.argumentIndexes.get(m);
                List<String> parameterNames = new ArrayList<String>();
                List<Class<?>> parameterTypes = new ArrayList<Class<?>>();
                for (int i = 0; i < data.outputRowMeta.size(); i++) {
                    ValueMetaInterface valueMeta = data.outputRowMeta.getValueMeta(i);
                    // 
                    if (meta.getFormula()[m].getFormula().contains(valueMeta.getName())) {
                        // If so, add it to the indexes...
                        argIndexes.add(i);
                        parameterTypes.add(valueMeta.getNativeDataTypeClass());
                        parameterNames.add(valueMeta.getName());
                    }
                }
                JaninoMetaFunction fn = meta.getFormula()[m];
                if (!Utils.isEmpty(fn.getFieldName())) {
                    // Create the expression evaluator: is relatively slow so we do it only for the first row...
                    // 
                    data.expressionEvaluators[m] = new ExpressionEvaluator();
                    data.expressionEvaluators[m].setParameters(parameterNames.toArray(new String[parameterNames.size()]), parameterTypes.toArray(new Class<?>[parameterTypes.size()]));
                    data.expressionEvaluators[m].setReturnType(Object.class);
                    data.expressionEvaluators[m].setThrownExceptions(new Class<?>[] { Exception.class });
                    data.expressionEvaluators[m].cook(fn.getFormula());
                } else {
                    throw new KettleException("Unable to find field name for formula [" + Const.NVL(fn.getFormula(), "") + "]");
                }
            }
        }
        for (int i = 0; i < meta.getFormula().length; i++) {
            List<Integer> argumentIndexes = data.argumentIndexes.get(i);
            // This method can only accept the specified number of values...
            // 
            Object[] argumentData = new Object[argumentIndexes.size()];
            for (int x = 0; x < argumentIndexes.size(); x++) {
                int index = argumentIndexes.get(x);
                ValueMetaInterface outputValueMeta = data.outputRowMeta.getValueMeta(index);
                argumentData[x] = outputValueMeta.convertToNormalStorageType(outputRowData[index]);
            }
            Object formulaResult = data.expressionEvaluators[i].evaluate(argumentData);
            Object value = null;
            if (formulaResult == null) {
                value = null;
            } else {
                ValueMetaInterface valueMeta = data.returnType[i];
                if (valueMeta.getNativeDataTypeClass().isAssignableFrom(formulaResult.getClass())) {
                    value = formulaResult;
                } else if (formulaResult instanceof Integer && valueMeta.getType() == ValueMetaInterface.TYPE_INTEGER) {
                    value = ((Integer) formulaResult).longValue();
                } else {
                    throw new KettleValueException(BaseMessages.getString(PKG, "Janino.Error.ValueTypeMismatch", valueMeta.getTypeDesc(), meta.getFormula()[i].getFieldName(), formulaResult.getClass(), meta.getFormula()[i].getFormula()));
                }
            }
            // 
            if (data.replaceIndex[i] < 0) {
                outputRowData[tempIndex++] = value;
            } else {
                outputRowData[data.replaceIndex[i]] = value;
            }
        }
        return outputRowData;
    } catch (Exception e) {
        throw new KettleValueException(e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) ArrayList(java.util.ArrayList) ExpressionEvaluator(org.codehaus.janino.ExpressionEvaluator) KettleException(org.pentaho.di.core.exception.KettleException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) ArrayList(java.util.ArrayList) List(java.util.List) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Aggregations

ExpressionEvaluator (org.codehaus.janino.ExpressionEvaluator)6 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)2 KettleException (org.pentaho.di.core.exception.KettleException)2 KettleValueException (org.pentaho.di.core.exception.KettleValueException)2 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)2 List (java.util.List)1 InvalidProgramException (org.apache.flink.api.common.InvalidProgramException)1 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)1 CompileException (org.codehaus.commons.compiler.CompileException)1 CompileException (org.codehaus.janino.CompileException)1 ParseException (org.codehaus.janino.Parser.ParseException)1 ScanException (org.codehaus.janino.Scanner.ScanException)1