use of org.knime.core.data.convert.java.DataCellToJavaConverterFactory in project knime-core by knime.
the class JavaSnippetCellFactory method getCells.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public DataCell[] getCells(final DataRow row) {
try {
if (null == m_jsnippet) {
m_jsnippet = m_snippet.createSnippetInstance();
// populate the fields in the m_jsnippet that are constant
// across the rows.
Field[] fs = m_jsnippet.getClass().getSuperclass().getDeclaredFields();
for (Field field : fs) {
if (field.getName().equals("m_flowVars")) {
field.setAccessible(true);
field.set(m_jsnippet, m_flowVars);
}
if (field.getName().equals(JavaSnippet.ROWCOUNT)) {
field.setAccessible(true);
field.set(m_jsnippet, m_rowCount);
}
}
}
// populate data structure with the input cells
Map<String, Cell> cellsMap = createCellsMap(row);
if (null == m_columns) {
m_columns = new ArrayList<>();
m_columns.addAll(cellsMap.keySet());
}
Field[] fs = m_jsnippet.getClass().getSuperclass().getDeclaredFields();
for (Field field : fs) {
if (field.getName().equals("m_cellsMap")) {
field.setAccessible(true);
field.set(m_jsnippet, cellsMap);
}
if (field.getName().equals("m_cells")) {
field.setAccessible(true);
List<Cell> cells = new ArrayList<>();
cells.addAll(cellsMap.values());
field.set(m_jsnippet, cells);
}
if (field.getName().equals("m_columns")) {
field.setAccessible(true);
field.set(m_jsnippet, m_columns);
}
if (field.getName().equals("m_inSpec")) {
field.setAccessible(true);
field.set(m_jsnippet, m_spec);
}
if (field.getName().equals(JavaSnippet.ROWID)) {
field.setAccessible(true);
field.set(m_jsnippet, row.getKey().getString());
}
if (field.getName().equals(JavaSnippet.ROWINDEX)) {
field.setAccessible(true);
field.set(m_jsnippet, m_rowIndex);
}
}
// populate the system input column fields with data
for (InCol inCol : m_snippet.getSystemFields().getInColFields()) {
Field field = m_jsnippet.getClass().getField(inCol.getJavaName());
final DataCell cell = row.getCell(m_spec.findColumnIndex(inCol.getKnimeName()));
if (cell.isMissing()) {
field.set(m_jsnippet, null);
continue;
}
// Get the converter factory for this column
final Optional<DataCellToJavaConverterFactory<?, ?>> factory = ConverterUtil.getDataCellToJavaConverterFactory(inCol.getConverterFactoryId());
if (!factory.isPresent()) {
throw new RuntimeException("Missing converter factory with ID: " + inCol.getConverterFactoryId());
}
final Object converted = factory.get().create().convertUnsafe(cell);
field.set(m_jsnippet, converted);
}
// reset the system output fields to null (see also bug 3781)
for (OutCol outCol : m_snippet.getSystemFields().getOutColFields()) {
Field field = m_jsnippet.getClass().getField(outCol.getJavaName());
field.set(m_jsnippet, null);
}
// populate the system input flow variable fields with data
for (InVar inCol : m_snippet.getSystemFields().getInVarFields()) {
Field field = m_jsnippet.getClass().getField(inCol.getJavaName());
Object v = m_flowVars.getValueOfType(inCol.getKnimeName(), inCol.getJavaType());
field.set(m_jsnippet, v);
}
} catch (Exception e) {
// re-throw exception
throw new RuntimeException(e);
}
try {
// evaluate user script
m_jsnippet.snippet();
} catch (Throwable thr) {
if (thr instanceof Abort) {
StringBuilder builder = new StringBuilder("Calculation aborted: ");
String message = thr.getMessage();
builder.append(message == null ? "<no details>" : message);
throw new RuntimeException(builder.toString(), thr);
} else {
Integer lineNumber = null;
for (StackTraceElement ste : thr.getStackTrace()) {
if (ste.getClassName().equals("JSnippet")) {
lineNumber = ste.getLineNumber();
}
}
StringBuilder msg = new StringBuilder();
msg.append("Evaluation of java snippet failed for row \"");
msg.append(row.getKey());
msg.append("\". ");
if (lineNumber != null) {
msg.append("The exception is caused by line ");
msg.append(lineNumber);
msg.append(" of the snippet. ");
}
if (thr.getMessage() != null) {
msg.append("Exception message:");
msg.append(thr.getMessage());
}
LOGGER.warn(msg.toString(), thr);
OutVarList outVars = m_snippet.getSystemFields().getOutVarFields();
if (outVars.size() > 0) {
// Abort if flow variables are defined
throw new RuntimeException("An error occured in an " + "expression with output flow variables.", thr);
}
OutColList outFields = m_snippet.getSystemFields().getOutColFields();
DataCell[] out = new DataCell[outFields.size()];
for (int i = 0; i < out.length; i++) {
// Return missing values for output fields
out[i] = DataType.getMissingCell();
}
m_rowIndex++;
return out;
}
}
try {
// update m_flowVars with output flow variable fields.
for (OutVar var : m_snippet.getSystemFields().getOutVarFields()) {
Field field = m_jsnippet.getClass().getField(var.getJavaName());
Object value = field.get(m_jsnippet);
if (null != value) {
Type type = var.getFlowVarType();
FlowVariable flowVar = null;
if (type.equals(Type.INTEGER)) {
flowVar = new FlowVariable(var.getKnimeName(), (Integer) value);
} else if (type.equals(Type.DOUBLE)) {
flowVar = new FlowVariable(var.getKnimeName(), (Double) value);
} else {
// case type.equals(Type.String)
flowVar = new FlowVariable(var.getKnimeName(), (String) value);
}
m_flowVars.put(flowVar);
} else {
throw new RuntimeException("Flow variable \"" + var.getKnimeName() + "\" has no value.");
}
}
// get output column fields
OutColList outFields = m_snippet.getSystemFields().getOutColFields();
DataCell[] out = new DataCell[outFields.size()];
for (int i = 0; i < out.length; i++) {
OutCol outField = outFields.get(i);
Field field = m_jsnippet.getClass().getField(outField.getJavaName());
Object value = field.get(m_jsnippet);
if (null == value) {
out[i] = DataType.getMissingCell();
} else {
final String id = outField.getConverterFactoryId();
Optional<JavaToDataCellConverterFactory<?>> factory = ConverterUtil.getJavaToDataCellConverterFactory(id);
if (!factory.isPresent()) {
throw new RuntimeException("Missing converter factory with ID: " + id);
}
out[i] = ((JavaToDataCellConverterFactory<Object>) factory.get()).create(m_context).convert(value);
}
}
// Cleanup Closeable inputs
for (final OutCol outCol : m_snippet.getSystemFields().getOutColFields()) {
final Field field = m_jsnippet.getClass().getField(outCol.getJavaName());
final Object value = field.get(m_jsnippet);
if (value instanceof Closeable) {
((Closeable) value).close();
}
if (value instanceof AutoCloseable) {
// From the doc: Calling close more than once *can* have visible side effects!
((AutoCloseable) value).close();
}
}
m_rowIndex++;
return out;
} catch (Exception e) {
// but in case re-throw exception
throw new RuntimeException(e);
}
}
use of org.knime.core.data.convert.java.DataCellToJavaConverterFactory in project knime-core by knime.
the class ConverterFactoryDataTypeListCellRenderer method getListCellRendererComponent.
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
// The super method will reset the icon if we call this method
// last. So we let super do its job first and then we take care
// that everything is properly set.
final Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
assert c == this;
if (value instanceof JavaToDataCellConverterFactory<?>) {
final JavaToDataCellConverterFactory<?> factory = (JavaToDataCellConverterFactory<?>) value;
setText(factory.getName());
setIcon(factory.getDestinationType().getIcon());
} else if (value instanceof DataCellToJavaConverterFactory) {
final DataCellToJavaConverterFactory<?, ?> factory = (DataCellToJavaConverterFactory<?, ?>) value;
setText(factory.getName());
}
setToolTipText(getText());
return this;
}
use of org.knime.core.data.convert.java.DataCellToJavaConverterFactory in project knime-core by knime.
the class InFieldsTableModel method getAllowedJavaTypes.
/**
* {@inheritDoc}
*/
@Override
public Object[] getAllowedJavaTypes(final int row) {
Object input = getValueAt(row, Column.COLUMN);
if (input instanceof DataColumnSpec) {
DataColumnSpec colSpec = (DataColumnSpec) input;
final Collection<DataCellToJavaConverterFactory<?, ?>> factories = ConverterUtil.getFactoriesForSourceType(colSpec.getType()).stream().filter(factory -> JavaSnippet.getBuildPathFromCache(factory.getIdentifier()) != null).sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).collect(Collectors.toList());
final Object[] array = factories.toArray(new Object[factories.size()]);
return array;
} else if (input instanceof FlowVariable) {
FlowVariable flowVar = (FlowVariable) input;
TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
return typeConversion.canProvideJavaTypes();
} else {
return new Class[0];
}
}
use of org.knime.core.data.convert.java.DataCellToJavaConverterFactory in project knime-core by knime.
the class InFieldsTableModel method validateJavaTypeColumn.
/**
* Validate java type in the given row.
* @param row the row to check
* @return error message if error occurs
*/
private String validateJavaTypeColumn(final int row) {
Object value = this.getValueAt(row, Column.JAVA_TYPE);
if (null == value) {
return "Please select a value";
}
Object input = this.getValueAt(row, Column.COLUMN);
if (value instanceof DataCellToJavaConverterFactory) {
if (input instanceof FlowVariable) {
return "DataCell converters do not work for flow variables.";
} else if (input instanceof DataColumnSpec) {
final DataType dataType = ((DataColumnSpec) input).getType();
if (!dataType.getValueClasses().contains(((DataCellToJavaConverterFactory<?, ?>) value).getSourceType())) {
return "Input DataCell is incompatible with this java type.";
}
}
// otherwise user has to fix column/flowvar first.
} else if (value instanceof String) {
// converter factory id
final String id = (String) value;
if (input instanceof DataColumnSpec) {
final DataColumnSpec colSpec = (DataColumnSpec) input;
final Optional<DataCellToJavaConverterFactory<?, ?>> factory = ConverterUtil.getDataCellToJavaConverterFactory(id);
if (!factory.isPresent()) {
return "Converter with id " + id + " could not be found.";
}
if (!colSpec.getType().getValueClasses().contains(factory.get().getSourceType())) {
// But just in case it does, this will make sure we know.
return "Selected factory does not support the input type.";
}
return null;
} else if (input instanceof FlowVariable) {
return "DataCell converters do not work for flow variables.";
}
} else if (value instanceof Class) {
Class<?> javaType = (Class<?>) value;
if (input instanceof DataColumnSpec) {
DataColumnSpec colSpec = (DataColumnSpec) input;
final Collection<?> factories = ConverterUtil.getFactoriesForSourceType(colSpec.getType());
return factories.isEmpty() ? "The java type \"" + javaType.getSimpleName() + "\" is not supported." : null;
} else if (input instanceof FlowVariable) {
FlowVariable flowVar = (FlowVariable) input;
TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
return typeConversion.canProvideJavaType(javaType) ? null : "The java type \"" + javaType.getSimpleName() + "\" is not supported.";
}
} else {
return "Cannot find type or converter " + value.toString();
}
return null;
}
use of org.knime.core.data.convert.java.DataCellToJavaConverterFactory in project knime-core by knime.
the class InFieldsTable method getInColFields.
/**
* Get the field definitions representing input columns.
*
* @return fields representing input columns.
*/
public InColList getInColFields() {
InColList inCols = new InColList();
for (int r = 0; r < m_model.getRowCount(); r++) {
if (!m_model.validateValues(r)) {
// there are errors in this row
continue;
}
Object value = m_model.getValueAt(r, Column.COLUMN);
if (value instanceof DataColumnSpec) {
DataColumnSpec colSpec = (DataColumnSpec) value;
InCol inCol = new InCol();
inCol.setKnimeName(colSpec.getName());
inCol.setJavaName((String) m_model.getValueAt(r, Column.JAVA_FIELD));
Object javaTypeObject = m_model.getValueAt(r, Column.JAVA_TYPE);
if (javaTypeObject instanceof DataCellToJavaConverterFactory) {
inCol.setConverterFactory(colSpec.getType(), (DataCellToJavaConverterFactory) javaTypeObject);
} else {
throw new IllegalStateException("Contents of JavaType column need to be DataCellToJavaConverterFactory instances.");
}
inCols.add(inCol);
}
}
return inCols;
}
Aggregations