Search in sources :

Example 1 with ColumnCalculator

use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.

the class StringManipulationVariableNodeModel method calculate.

/**
 * @throws CompilationFailedException
 * @throws InstantiationException
 * @throws Exception
 */
private void calculate() throws InvalidSettingsException, CompilationFailedException, InstantiationException {
    if (m_settings == null || m_settings.getExpression() == null) {
        throw new InvalidSettingsException("No expression has been set.");
    }
    JavaScriptingSettings settings = m_settings.createJavaScriptingSettings();
    settings.setInputAndCompile(new DataTableSpec());
    // calculate the result
    ColumnCalculator cc = new ColumnCalculator(settings, this);
    DataCell calculate = null;
    try {
        calculate = cc.calculate(new DefaultRow(new RowKey(""), new DataCell[] {}));
    } catch (NoSuchElementException e) {
        throw new InvalidSettingsException(e.getMessage());
    }
    String newVariableName;
    Map<String, FlowVariable> inputFlowVariables = getAvailableInputFlowVariables();
    if (m_settings.isReplace()) {
        newVariableName = m_settings.getColName();
        CheckUtils.checkSettingNotNull(inputFlowVariables.get(newVariableName), "Can't replace input variable '%s' -- it does not exist in the input", newVariableName);
    } else {
        newVariableName = new UniqueNameGenerator(inputFlowVariables.keySet()).newName(m_settings.getColName());
    }
    // convert and push result as flow variable
    CheckUtils.checkSetting(!calculate.isMissing(), "Calculation returned missing value");
    Class<? extends DataCell> cellType = calculate.getClass();
    if (cellType.equals(IntCell.class)) {
        pushFlowVariableInt(newVariableName, ((IntCell) calculate).getIntValue());
    } else if (cellType.equals(DoubleCell.class)) {
        pushFlowVariableDouble(newVariableName, ((DoubleCell) calculate).getDoubleValue());
    } else if (cellType.equals(StringCell.class)) {
        pushFlowVariableString(newVariableName, ((StringCell) calculate).getStringValue());
    } else {
        throw new RuntimeException("Invalid variable class: " + cellType);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) JavaScriptingSettings(org.knime.ext.sun.nodes.script.settings.JavaScriptingSettings) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColumnCalculator(org.knime.ext.sun.nodes.script.calculator.ColumnCalculator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 2 with ColumnCalculator

use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.

the class JavaIfSwitchNodeModel method calculate.

/**
 */
private int calculate() throws InvalidSettingsException {
    if (m_settings == null || m_settings.getExpression() == null) {
        throw new InvalidSettingsException("No expression has been set.");
    }
    try {
        m_settings.setInputAndCompile(EMPTY_SPEC);
        ColumnCalculator cc = new ColumnCalculator(m_settings, this);
        DataCell result = cc.calculate(EMPTY_ROW);
        if (result.isMissing()) {
            throw new InvalidSettingsException("Calculation returned missing value");
        }
        Class<?> ret = m_settings.getReturnType();
        if (!JavaSnippetIntType.INSTANCE.getJavaClass(false).equals(ret)) {
            throw new InvalidSettingsException("Return type must be int: " + ret != null ? ret.getName() : "<null>");
        }
        Integer i = JavaSnippetIntType.INSTANCE.asJavaObject(result);
        return i;
    } catch (InvalidSettingsException e) {
        throw e;
    } catch (Exception e) {
        throw new InvalidSettingsException(e.getMessage(), e);
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColumnCalculator(org.knime.ext.sun.nodes.script.calculator.ColumnCalculator) DataCell(org.knime.core.data.DataCell) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 3 with ColumnCalculator

use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.

the class JavaRowSplitterNodeModel method execute.

private void execute(final RowInput inData, final RowOutput[] outputs, final ExecutionContext exec) throws Exception {
    DataTableSpec spec = inData.getDataTableSpec();
    m_settings.setInputAndCompile(spec);
    ColumnCalculator cc = new ColumnCalculator(m_settings, this);
    int rowIndex = 0;
    DataRow r;
    RowOutput trueMatch = outputs[0];
    RowOutput falseMatch = outputs.length > 1 ? outputs[1] : null;
    while ((r = inData.poll()) != null) {
        cc.setProgress(rowIndex, m_rowCount, r.getKey(), exec);
        DataCell result = cc.calculate(r);
        boolean b;
        if (result.isMissing()) {
            b = false;
            setWarningMessage("Expression returned missing value for some rows (interpreted as no match)");
        } else {
            b = ((BooleanValue) result).getBooleanValue();
        }
        if (b) {
            trueMatch.push(r);
        } else if (falseMatch != null) {
            falseMatch.push(r);
        }
        exec.checkCanceled();
        rowIndex++;
    }
    trueMatch.close();
    if (falseMatch != null) {
        falseMatch.close();
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput) RowOutput(org.knime.core.node.streamable.RowOutput) ColumnCalculator(org.knime.ext.sun.nodes.script.calculator.ColumnCalculator) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 4 with ColumnCalculator

use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.

the class JavaScriptingNodeModel method createColumnRearranger.

private ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
    if (m_settings == null || m_settings.getExpression() == null) {
        throw new InvalidSettingsException("No expression has been set.");
    }
    boolean isReplace = m_settings.isReplace();
    String colName = m_settings.getColName();
    try {
        m_settings.setInputAndCompile(spec);
        ColumnCalculator cc = new ColumnCalculator(m_settings, this);
        ColumnRearranger result = new ColumnRearranger(spec);
        if (isReplace) {
            result.replace(cc, colName);
        } else {
            if (spec.containsName(colName)) {
                throw new InvalidSettingsException("Can't create new column \"" + colName + "\" as input spec already contains such column");
            }
            result.append(cc);
        }
        return result;
    } catch (Exception e) {
        throw new InvalidSettingsException(e.getMessage(), e);
    }
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColumnCalculator(org.knime.ext.sun.nodes.script.calculator.ColumnCalculator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 5 with ColumnCalculator

use of org.knime.ext.sun.nodes.script.calculator.ColumnCalculator in project knime-core by knime.

the class JavaEditVariableNodeModel method calculate.

/**
 */
private void calculate() throws InvalidSettingsException {
    if (m_settings == null || m_settings.getExpression() == null) {
        throw new InvalidSettingsException("No expression has been set.");
    }
    try {
        m_settings.setInputAndCompile(EMPTY_SPEC);
        ColumnCalculator cc = new ColumnCalculator(m_settings, this);
        DataCell result = cc.calculate(EMPTY_ROW);
        if (result.isMissing()) {
            throw new InvalidSettingsException("Calculation returned missing value");
        }
        Class<?> ret = m_settings.getReturnType();
        if (ret.equals(JavaSnippetIntType.INSTANCE.getJavaClass(false))) {
            Integer i = JavaSnippetIntType.INSTANCE.asJavaObject(result);
            pushFlowVariableInt(m_settings.getColName(), i);
        } else if (ret.equals(JavaSnippetDoubleType.INSTANCE.getJavaClass(false))) {
            Double d = JavaSnippetDoubleType.INSTANCE.asJavaObject(result);
            pushFlowVariableDouble(m_settings.getColName(), d);
        } else {
            String s = JavaSnippetStringType.INSTANCE.asJavaObject(result);
            pushFlowVariableString(m_settings.getColName(), s);
        }
    } catch (InvalidSettingsException e) {
        throw e;
    } catch (Exception e) {
        throw new InvalidSettingsException(e.getMessage(), e);
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColumnCalculator(org.knime.ext.sun.nodes.script.calculator.ColumnCalculator) DataCell(org.knime.core.data.DataCell) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Aggregations

ColumnCalculator (org.knime.ext.sun.nodes.script.calculator.ColumnCalculator)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)5 IOException (java.io.IOException)4 DataCell (org.knime.core.data.DataCell)4 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)4 DataTableSpec (org.knime.core.data.DataTableSpec)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 JavaScriptingSettings (org.knime.ext.sun.nodes.script.settings.JavaScriptingSettings)2 NoSuchElementException (java.util.NoSuchElementException)1 DataRow (org.knime.core.data.DataRow)1 RowKey (org.knime.core.data.RowKey)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 DoubleCell (org.knime.core.data.def.DoubleCell)1 BufferedDataTableRowOutput (org.knime.core.node.streamable.BufferedDataTableRowOutput)1 RowOutput (org.knime.core.node.streamable.RowOutput)1 FlowVariable (org.knime.core.node.workflow.FlowVariable)1 UniqueNameGenerator (org.knime.core.util.UniqueNameGenerator)1