Search in sources :

Example 1 with ExpressionField

use of org.knime.ext.sun.nodes.script.expression.Expression.ExpressionField in project knime-core by knime.

the class ColumnCalculator method calculate.

/**
 * Performs the calculation.
 *
 * @param row the row to process
 * @return the resulting cell
 */
public DataCell calculate(final DataRow row) {
    if (m_flowVarAssignmentMap == null) {
        m_flowVarAssignmentMap = new HashMap<InputField, Object>();
        for (Map.Entry<InputField, ExpressionField> e : m_expression.getFieldMap().entrySet()) {
            InputField f = e.getKey();
            if (f.getFieldType().equals(FieldType.Variable)) {
                Class<?> c = e.getValue().getFieldClass();
                m_flowVarAssignmentMap.put(f, m_flowVarProvider.readVariable(f.getColOrVarName(), c));
            }
        }
    }
    DataTableSpec spec = m_settings.getInputSpec();
    Class<?> returnType = m_settings.getReturnType();
    boolean isArrayReturn = m_settings.isArrayReturn();
    Map<InputField, Object> nameValueMap = new HashMap<InputField, Object>();
    nameValueMap.put(new InputField(Expression.ROWINDEX, FieldType.TableConstant), m_lastProcessedRow++);
    nameValueMap.put(new InputField(Expression.ROWID, FieldType.TableConstant), row.getKey().getString());
    nameValueMap.put(new InputField(Expression.ROWCOUNT, FieldType.TableConstant), m_flowVarProvider.getRowCount());
    nameValueMap.putAll(m_flowVarAssignmentMap);
    for (int i = 0; i < row.getNumCells(); i++) {
        DataColumnSpec columnSpec = spec.getColumnSpec(i);
        InputField inputField = new InputField(columnSpec.getName(), FieldType.Column);
        if (!m_expression.needsInputField(inputField)) {
            continue;
        }
        DataCell cell = row.getCell(i);
        DataType cellType = columnSpec.getType();
        boolean isArray = cellType.isCollectionType();
        if (isArray) {
            cellType = cellType.getCollectionElementType();
        }
        Object cellVal = null;
        if (cell.isMissing()) {
            if (m_settings.isInsertMissingAsNull()) {
            // leave value as null
            } else {
                String message = "Row \"" + row.getKey() + "\" " + "contains missing value in column \"" + columnSpec.getName() + "\" - returning missing";
                if (!m_hasReportedMissing) {
                    m_hasReportedMissing = true;
                    LOGGER.warn(message + " (omitting further warnings)");
                } else {
                    LOGGER.debug(message);
                }
                return DataType.getMissingCell();
            }
        } else {
            for (JavaSnippetType<?, ?, ?> t : JavaSnippetType.TYPES) {
                if (t.checkCompatibility(cellType)) {
                    if (isArray) {
                        cellVal = t.asJavaArray((CollectionDataValue) cell);
                    } else {
                        cellVal = t.asJavaObject(cell);
                    }
                    break;
                }
            }
        }
        nameValueMap.put(inputField, cellVal);
    }
    Object o = null;
    try {
        m_expression.set(nameValueMap);
        o = m_expression.evaluate();
    // class correctness is asserted by compiler
    } catch (Abort ee) {
        StringBuilder builder = new StringBuilder("Calculation aborted: ");
        String message = ee.getMessage();
        builder.append(message == null ? "<no details>" : message);
        throw new RuntimeException(builder.toString(), ee);
    } catch (EvaluationFailedException ee) {
        Throwable cause = ee.getCause();
        if (cause instanceof InvocationTargetException) {
            cause = ((InvocationTargetException) cause).getCause();
        }
        String message = cause != null ? cause.getMessage() : ee.getMessage();
        LOGGER.warn("Evaluation of expression failed for row \"" + row.getKey() + "\": " + message, ee);
    } catch (IllegalPropertyException ipe) {
        LOGGER.warn("Evaluation of expression failed for row \"" + row.getKey() + "\": " + ipe.getMessage(), ipe);
    }
    DataCell result = null;
    for (JavaSnippetType<?, ?, ?> t : JavaSnippetType.TYPES) {
        if (returnType.equals(t.getJavaClass(false))) {
            if (o == null) {
                result = DataType.getMissingCell();
            } else if (isArrayReturn) {
                result = t.asKNIMEListCell((Object[]) o);
            } else {
                result = t.asKNIMECell(o);
            }
            break;
        }
    }
    if (result == null) {
        throw new InternalError("No mapping for objects of class " + o.getClass().getName());
    }
    return result;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) HashMap(java.util.HashMap) ExpressionField(org.knime.ext.sun.nodes.script.expression.Expression.ExpressionField) Abort(org.knime.ext.sun.nodes.script.expression.Abort) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) InputField(org.knime.ext.sun.nodes.script.expression.Expression.InputField) EvaluationFailedException(org.knime.ext.sun.nodes.script.expression.EvaluationFailedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IllegalPropertyException(org.knime.ext.sun.nodes.script.expression.IllegalPropertyException) DataCell(org.knime.core.data.DataCell) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ExpressionField

use of org.knime.ext.sun.nodes.script.expression.Expression.ExpressionField in project knime-core by knime.

the class ExpressionInstance method set.

/**
 * Sets field values.
 *
 * @param property2ValueMap containing properties -&gt; values
 * @throws IllegalPropertyException if a field is unkown or a value is
 *             incompatible
 */
public final void set(final Map<InputField, Object> property2ValueMap) throws IllegalPropertyException {
    // specified in the constructor
    for (Map.Entry<InputField, ExpressionField> entry : m_fieldMap.entrySet()) {
        InputField field = entry.getKey();
        ExpressionField expressionField = entry.getValue();
        Object value = property2ValueMap.get(field);
        if (value == null) {
            // this could also be an assertion
            if (property2ValueMap.containsKey(value)) {
                throw new IllegalPropertyException("No value for field " + field);
            } else {
            // null represents missing value
            }
        }
        if (value != null && !expressionField.getFieldClass().isInstance(value)) {
            throw new IllegalPropertyException("Type for field \"" + field + "\" not matched: got " + value.getClass().getName() + " but expected " + expressionField.getFieldClass().getName());
        }
        setField(expressionField.getFieldNameInJava(), value);
    }
}
Also used : InputField(org.knime.ext.sun.nodes.script.expression.Expression.InputField) ExpressionField(org.knime.ext.sun.nodes.script.expression.Expression.ExpressionField) Map(java.util.Map)

Aggregations

Map (java.util.Map)2 ExpressionField (org.knime.ext.sun.nodes.script.expression.Expression.ExpressionField)2 InputField (org.knime.ext.sun.nodes.script.expression.Expression.InputField)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 DataType (org.knime.core.data.DataType)1 CollectionDataValue (org.knime.core.data.collection.CollectionDataValue)1 Abort (org.knime.ext.sun.nodes.script.expression.Abort)1 EvaluationFailedException (org.knime.ext.sun.nodes.script.expression.EvaluationFailedException)1 IllegalPropertyException (org.knime.ext.sun.nodes.script.expression.IllegalPropertyException)1