Search in sources :

Example 1 with OutColList

use of org.knime.base.node.jsnippet.util.JavaFieldList.OutColList in project knime-core by knime.

the class JavaSnippet method execute.

/**
 * Execute the snippet.
 *
 * @param table the data table at the inport
 * @param flowVariableRepository the flow variables at the inport
 * @param exec the execution context to report progress
 * @return the table for the output
 * @throws InvalidSettingsException when settings are inconsistent with the table or the flow variables at the input
 * @throws CanceledExecutionException when execution is canceled by the user
 */
public BufferedDataTable execute(final BufferedDataTable table, final FlowVariableRepository flowVariableRepository, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
    final OutColList outFields = m_fields.getOutColFields();
    if (outFields.size() > 0) {
        final ColumnRearranger rearranger = createRearranger(table.getDataTableSpec(), flowVariableRepository, table.getRowCount(), exec);
        return exec.createColumnRearrangeTable(table, rearranger, exec);
    } else {
        final JavaSnippetCellFactory factory = new JavaSnippetCellFactory(this, table.getDataTableSpec(), flowVariableRepository, table.getRowCount(), exec);
        try {
            for (final DataRow row : table) {
                exec.checkCanceled();
                factory.getCells(row);
            }
        } finally {
            factory.afterProcessing();
        }
        return table;
    }
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList) DataRow(org.knime.core.data.DataRow)

Example 2 with OutColList

use of org.knime.base.node.jsnippet.util.JavaFieldList.OutColList 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);
    }
}
Also used : OutCol(org.knime.base.node.jsnippet.util.field.OutCol) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList) InCol(org.knime.base.node.jsnippet.util.field.InCol) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) Field(java.lang.reflect.Field) Abort(org.knime.base.node.jsnippet.expression.Abort) OutVarList(org.knime.base.node.jsnippet.util.JavaFieldList.OutVarList) InVar(org.knime.base.node.jsnippet.util.field.InVar) Cell(org.knime.base.node.jsnippet.expression.Cell) DataCell(org.knime.core.data.DataCell) OutVar(org.knime.base.node.jsnippet.util.field.OutVar) TypeException(org.knime.base.node.jsnippet.expression.TypeException) Type(org.knime.core.node.workflow.FlowVariable.Type) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) FlowVariable(org.knime.core.node.workflow.FlowVariable) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 3 with OutColList

use of org.knime.base.node.jsnippet.util.JavaFieldList.OutColList in project knime-core by knime.

the class JavaSnippet method createRearranger.

/**
 * The rearranger is the working horse for creating the output table.
 */
ColumnRearranger createRearranger(final DataTableSpec spec, final FlowVariableRepository flowVariableRepository, final int rowCount, final ExecutionContext context) throws InvalidSettingsException {
    int offset = spec.getNumColumns();
    CellFactory factory = new JavaSnippetCellFactory(this, spec, flowVariableRepository, rowCount, context);
    ColumnRearranger c = new ColumnRearranger(spec);
    // add factory to the column rearranger
    c.append(factory);
    // define which new columns do replace others
    OutColList outFields = m_fields.getOutColFields();
    for (int i = outFields.size() - 1; i >= 0; i--) {
        OutCol field = outFields.get(i);
        int index = spec.findColumnIndex(field.getKnimeName());
        if (index >= 0) {
            if (field.getReplaceExisting()) {
                c.remove(index);
                c.move(offset + i - 1, index);
            } else {
                throw new InvalidSettingsException("Field \"" + field.getJavaName() + "\" is configured to " + "replace no existing columns.");
            }
        }
    }
    return c;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList) CellFactory(org.knime.core.data.container.CellFactory)

Example 4 with OutColList

use of org.knime.base.node.jsnippet.util.JavaFieldList.OutColList in project knime-core by knime.

the class JavaSnippetCellFactory method getColumnSpecs.

/**
 * {@inheritDoc}
 */
@Override
public DataColumnSpec[] getColumnSpecs() {
    OutColList outFields = m_snippet.getSystemFields().getOutColFields();
    DataColumnSpec[] cols = new DataColumnSpec[outFields.size()];
    for (int i = 0; i < cols.length; i++) {
        OutCol field = outFields.get(i);
        cols[i] = new DataColumnSpecCreator(field.getKnimeName(), field.getDataType()).createSpec();
    }
    return cols;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList)

Example 5 with OutColList

use of org.knime.base.node.jsnippet.util.JavaFieldList.OutColList in project knime-core by knime.

the class OutFieldsTable method getOutColFields.

/**
 * Get the field definitions representing output columns.
 *
 * @return fields representing output columns.
 */
public OutColList getOutColFields() {
    OutColList outCols = new OutColList();
    for (int r = 0; r < m_model.getRowCount(); r++) {
        if (!m_model.validateValues(r)) {
            // there are errors in this row
            continue;
        }
        Object fieldTypeValue = getFieldType(r);
        if (null == fieldTypeValue) {
            continue;
        }
        boolean isColumn = fieldTypeValue.equals(FieldType.Column);
        if (isColumn) {
            OutCol outCol = new OutCol();
            outCol.setReplaceExisting((Boolean) m_model.getValueAt(r, Column.REPLACE_EXISTING));
            Object colColValue = m_model.getValueAt(r, Column.COLUMN);
            if (colColValue instanceof DataColumnSpec) {
                DataColumnSpec colSpec = (DataColumnSpec) colColValue;
                outCol.setKnimeName(colSpec.getName());
            } else if (colColValue instanceof String) {
                outCol.setKnimeName(colColValue.toString());
            } else {
                continue;
            }
            final Object dataTypeValue = m_model.getValueAt(r, Column.DATA_TYPE);
            if (!(dataTypeValue instanceof DataType)) {
                continue;
            }
            outCol.setJavaName((String) m_model.getValueAt(r, Column.JAVA_FIELD));
            Object javaTypeObject = m_model.getValueAt(r, Column.JAVA_TYPE);
            if (javaTypeObject instanceof JavaToDataCellConverterFactory) {
                outCol.setConverterFactory((JavaToDataCellConverterFactory) javaTypeObject);
            } else {
                continue;
            }
            outCols.add(outCol);
        }
    }
    return outCols;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) DataType(org.knime.core.data.DataType) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Aggregations

OutColList (org.knime.base.node.jsnippet.util.JavaFieldList.OutColList)5 OutCol (org.knime.base.node.jsnippet.util.field.OutCol)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)2 DataType (org.knime.core.data.DataType)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 JavaToDataCellConverterFactory (org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)2 Closeable (java.io.Closeable)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Abort (org.knime.base.node.jsnippet.expression.Abort)1 Cell (org.knime.base.node.jsnippet.expression.Cell)1 TypeException (org.knime.base.node.jsnippet.expression.TypeException)1 OutVarList (org.knime.base.node.jsnippet.util.JavaFieldList.OutVarList)1 InCol (org.knime.base.node.jsnippet.util.field.InCol)1 InVar (org.knime.base.node.jsnippet.util.field.InVar)1 OutVar (org.knime.base.node.jsnippet.util.field.OutVar)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1 DataRow (org.knime.core.data.DataRow)1 CellFactory (org.knime.core.data.container.CellFactory)1