use of org.knime.ext.sun.nodes.script.expression.EvaluationFailedException 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;
}
Aggregations