use of org.knime.ext.sun.nodes.script.expression.Expression.InputField 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;
}
use of org.knime.ext.sun.nodes.script.expression.Expression.InputField in project knime-core by knime.
the class ExpressionInstance method set.
/**
* Sets field values.
*
* @param property2ValueMap containing properties -> 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);
}
}
Aggregations