Search in sources :

Example 1 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class RuleEngine2PortsSimpleSettings method asString.

/**
 * Converts a {@link DataCell} to {@link String} for rules.
 *
 * @param cell A {@link DataCell}.
 * @return The value of {@code cell} as a {@link String}, properly escaped.
 */
protected String asString(final DataCell cell) {
    if (cell.isMissing()) {
        return "\"?\"";
    }
    if (cell instanceof StringValue) {
        StringValue sv = (StringValue) cell;
        String s = sv.getStringValue();
        if (isTreatOutcomesAsReferences() && s.startsWith("$")) {
            return s;
        }
        return escapedText(s);
    }
    if (cell instanceof BooleanValue) {
        return Boolean.toString(((BooleanValue) cell).getBooleanValue()).toUpperCase();
    }
    String string = cell.toString();
    if (cell instanceof DoubleValue) {
        return string;
    }
    return escapedText(string);
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) BooleanValue(org.knime.core.data.BooleanValue) StringValue(org.knime.core.data.StringValue)

Example 2 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class RuleEngineVariable2PortsNodeModel method performExecute.

/**
 * Creates the flow variable according to the computed value.
 *
 * @param rules The rules to check for match.
 * @throws InvalidSettingsException When there is an error in the settings.
 */
private void performExecute(final List<Rule> rules, final DataType outcomeColumnType) throws InvalidSettingsException {
    String newFlowVar = m_newVariableName;
    if (newFlowVar == null || newFlowVar.isEmpty()) {
        newFlowVar = DEFAULT_VARIABLE_NAME;
    }
    final DataType outType = computeOutputType(rules, outcomeColumnType);
    final VariableProvider provider = new VariableProvider() {

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            return RuleEngineVariable2PortsNodeModel.this.readVariable(name, type);
        }

        @Override
        @Deprecated
        public int getRowCount() {
            throw new IllegalStateException("Row count is not available.");
        }

        @Override
        public long getRowCountLong() {
            throw new IllegalStateException("Row count is not available.");
        }

        @Override
        @Deprecated
        public int getRowIndex() {
            throw new IllegalStateException("Row index is not available.");
        }

        @Override
        public long getRowIndexLong() {
            throw new IllegalStateException("Row index is not available.");
        }
    };
    boolean wasMatch = false;
    for (Rule r : rules) {
        if (r.getCondition().matches(null, provider).getOutcome() == MatchState.matchedAndStop) {
            Outcome outcome2 = r.getOutcome();
            // r.getSideEffect().perform(row, this);
            final DataCell cell = (DataCell) outcome2.getComputedResult(null, provider);
            wasMatch = true;
            if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
                pushFlowVariableString(newFlowVar, cell.toString());
                break;
            } else {
                if (cell.isMissing()) {
                    throw new UnsupportedOperationException("Missing result, TODO");
                }
                if (outType.equals(IntCell.TYPE)) {
                    pushFlowVariableInt(newFlowVar, ((IntValue) cell).getIntValue());
                    break;
                } else if (outType.equals(DoubleCell.TYPE)) {
                    pushFlowVariableDouble(newFlowVar, ((DoubleValue) cell).getDoubleValue());
                    break;
                } else if (outType.equals(StringCell.TYPE)) {
                    pushFlowVariableString(newFlowVar, ((StringValue) cell).getStringValue());
                    break;
                } else {
                    // TODO
                    throw new UnsupportedOperationException("Wrong type: " + cell.getClass());
                }
            }
        }
    }
    if (!wasMatch) {
        if (outType.equals(StringCell.TYPE)) {
            pushFlowVariableString(newFlowVar, "");
        } else if (outType.equals(IntCell.TYPE)) {
            pushFlowVariableInt(newFlowVar, 0);
        } else {
            pushFlowVariableDouble(newFlowVar, 0.0);
        }
    }
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) VariableProvider(org.knime.base.node.rules.engine.VariableProvider) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) Outcome(org.knime.base.node.rules.engine.Rule.Outcome) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) Rule(org.knime.base.node.rules.engine.Rule)

Example 3 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class TSAverageHandler method getMean.

private DataCell getMean() {
    if (m_previous instanceof IntValue) {
        // get an int, create an int
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new IntCell((int) Math.round(mean));
    }
    if (m_previous instanceof LongValue) {
        // get an int, create an int
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new LongCell(Math.round(mean));
    }
    if (m_previous instanceof DoubleValue) {
        // get an double, create an double
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new DoubleCell(mean);
    }
    if (m_previous instanceof DateAndTimeValue) {
        // get an int, create an int
        DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
        DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
        boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
        boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
        boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
        double d = dataCell1.getUTCTimeInMillis() + dataCell2.getUTCTimeInMillis();
        d *= 0.5;
        return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
    }
    return DataType.getMissingCell();
}
Also used : LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) LongValue(org.knime.core.data.LongValue) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue) IntCell(org.knime.core.data.def.IntCell)

Example 4 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class TSLinearHandler method getLinearInterpolation.

private DataCell getLinearInterpolation(final double stepnumber, final double stepcount) {
    if (m_previous instanceof DoubleValue || m_previous instanceof IntValue || m_previous instanceof LongValue) {
        double prev = ((DoubleValue) m_previous).getDoubleValue();
        double next = ((DoubleValue) m_next).getDoubleValue();
        double lin = prev + 1.0 * stepnumber / (1.0 * stepcount) * (next - prev);
        if (m_previous instanceof IntValue) {
            // get an int, create an int
            return new IntCell((int) Math.round(lin));
        }
        if (m_previous instanceof LongValue) {
            // get an long, create an long
            return new LongCell(Math.round(lin));
        }
        return new DoubleCell(lin);
    }
    if (m_previous instanceof DateAndTimeValue) {
        // get an int, create an int
        DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
        DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
        boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
        boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
        boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
        double prev = dataCell1.getUTCTimeInMillis();
        double next = dataCell2.getUTCTimeInMillis();
        double d = prev + stepnumber / stepcount * (next - prev);
        return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
    }
    return DataType.getMissingCell();
}
Also used : LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) LongValue(org.knime.core.data.LongValue) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue) IntCell(org.knime.core.data.def.IntCell)

Example 5 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class RuleEngineVariableNodeModel method performExecute.

/**
 * Creates the flow variable according to the computed value.
 *
 * @param rules The rules to check for match.
 * @throws InvalidSettingsException When there is an error in the settings.
 */
private void performExecute(final List<Rule> rules) throws InvalidSettingsException {
    String newFlowVar = m_settings.getNewColName();
    final DataType outType = computeOutputType(rules);
    final VariableProvider provider = new VariableProvider() {

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            return RuleEngineVariableNodeModel.this.readVariable(name, type);
        }

        @Override
        @Deprecated
        public int getRowCount() {
            throw new IllegalStateException("Row count is not available.");
        }

        @Override
        @Deprecated
        public int getRowIndex() {
            throw new IllegalStateException("Row index is not available.");
        }

        @Override
        public long getRowCountLong() {
            throw new IllegalStateException("Row count is not available.");
        }

        @Override
        public long getRowIndexLong() {
            throw new IllegalStateException("Row index is not available.");
        }
    };
    boolean wasMatch = false;
    for (Rule r : rules) {
        if (r.getCondition().matches(null, provider).getOutcome() == MatchState.matchedAndStop) {
            Outcome outcome2 = r.getOutcome();
            // r.getSideEffect().perform(row, this);
            final DataCell cell = (DataCell) outcome2.getComputedResult(null, provider);
            wasMatch = true;
            if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
                pushFlowVariableString(newFlowVar, cell.toString());
                break;
            } else {
                if (cell.isMissing()) {
                    throw new UnsupportedOperationException("Missing result, TODO");
                }
                if (outType.equals(IntCell.TYPE)) {
                    pushFlowVariableInt(newFlowVar, ((IntValue) cell).getIntValue());
                    break;
                } else if (outType.equals(DoubleCell.TYPE)) {
                    pushFlowVariableDouble(newFlowVar, ((DoubleValue) cell).getDoubleValue());
                    break;
                } else if (outType.equals(StringCell.TYPE)) {
                    pushFlowVariableString(newFlowVar, ((StringValue) cell).getStringValue());
                    break;
                } else {
                    // TODO
                    throw new UnsupportedOperationException("Wrong type: " + cell.getClass());
                }
            }
        }
    }
    if (!wasMatch) {
        if (outType.equals(StringCell.TYPE)) {
            pushFlowVariableString(newFlowVar, "");
        } else if (outType.equals(IntCell.TYPE)) {
            pushFlowVariableInt(newFlowVar, 0);
        } else {
            pushFlowVariableDouble(newFlowVar, 0.0);
        }
    }
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) Outcome(org.knime.base.node.rules.engine.Rule.Outcome) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell)

Aggregations

DoubleValue (org.knime.core.data.DoubleValue)154 DataCell (org.knime.core.data.DataCell)103 DataRow (org.knime.core.data.DataRow)71 DataColumnSpec (org.knime.core.data.DataColumnSpec)38 DataTableSpec (org.knime.core.data.DataTableSpec)38 DoubleCell (org.knime.core.data.def.DoubleCell)32 ArrayList (java.util.ArrayList)26 BufferedDataTable (org.knime.core.node.BufferedDataTable)26 DataType (org.knime.core.data.DataType)23 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)21 LinkedHashMap (java.util.LinkedHashMap)18 IntValue (org.knime.core.data.IntValue)15 HashMap (java.util.HashMap)14 RowIterator (org.knime.core.data.RowIterator)14 RowKey (org.knime.core.data.RowKey)13 DefaultRow (org.knime.core.data.def.DefaultRow)13 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 LongValue (org.knime.core.data.LongValue)10 StringValue (org.knime.core.data.StringValue)10 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)10