Search in sources :

Example 11 with DataType

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

the class ExpressionFactory method columnRefImpl.

/**
 * @param spec The {@link DataTableSpec}.
 * @param columnRef Name of the column.
 * @param booleanArgumentOfMissing When this value is {@code true} and the value is a boolean missing value, the
 *            result is {@code false}, else missing.
 * @return The {@link Expression} computing the value of the column.
 * @see #columnRef(DataTableSpec, String)
 * @see #columnRefForMissing(DataTableSpec, String)
 */
private Expression columnRefImpl(final DataTableSpec spec, final String columnRef, final boolean booleanArgumentOfMissing) {
    if (!spec.containsName(columnRef)) {
        throw new IllegalStateException("Not a column: " + columnRef);
    }
    final int position = spec.findColumnIndex(columnRef);
    final DataType type = spec.getColumnSpec(position).getType();
    final boolean isBoolean = type.isCompatible(BooleanValue.class);
    assert (!booleanArgumentOfMissing || isBoolean) : type;
    return new Expression.Base() {

        /**
         * {@inheritDoc}
         */
        @Override
        public List<DataType> getInputArgs() {
            return Collections.emptyList();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public DataType getOutputType() {
            return spec.getColumnSpec(columnRef).getType();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            if (booleanArgumentOfMissing) {
                return new ExpressionValue(row.getCell(position), EMPTY_MAP);
            }
            final DataCell cell = row.getCell(position);
            if (isBoolean && cell.isMissing()) {
                return new ExpressionValue(BooleanCell.FALSE, EMPTY_MAP);
            }
            return new ExpressionValue(row.getCell(position), EMPTY_MAP);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isConstant() {
            return false;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String toString() {
            return "$" + columnRef + "$";
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ASTType getTreeType() {
            return ASTType.ColRef;
        }
    };
}
Also used : DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 12 with DataType

use of org.knime.core.data.DataType 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)

Example 13 with DataType

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

the class RuleEngineNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec, final List<Rule> rules, final long rowCount, final boolean updateColSpec) throws InvalidSettingsException {
    if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
        throw new InvalidSettingsException("No name for prediction column provided");
    }
    ColumnRearranger crea = new ColumnRearranger(inSpec);
    String newColName = m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(inSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn();
    final DataType outType = computeOutputType(rules, RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
    DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(newColName, outType);
    if (updateColSpec) {
        // only update in configure, execute will compute properly
        updateColSpec(rules, outType, colSpecCreator, this);
    }
    DataColumnSpec cs = colSpecCreator.createSpec();
    final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
    VariableProvider.SingleCellFactoryProto cellFactory = new VariableProvider.SingleCellFactoryProto(cs) {

        private long m_rowIndex = -1L;

        @Override
        public DataCell getCell(final DataRow row) {
            m_rowIndex++;
            return getRulesOutcome(outType, row, rules, disallowLongOutputForCompatibility, this);
        }

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

        @Deprecated
        @Override
        public int getRowIndex() {
            return (int) m_rowIndex;
        }

        @Override
        public long getRowIndexLong() {
            return m_rowIndex;
        }

        @Deprecated
        @Override
        public int getRowCount() {
            return (int) rowCount;
        }

        @Override
        public long getRowCountLong() {
            return rowCount;
        }
    };
    if (m_settings.isAppendColumn()) {
        crea.append(cellFactory);
    } else {
        crea.replace(cellFactory, m_settings.getReplaceColumn());
    }
    return crea;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) DataType(org.knime.core.data.DataType) DataRow(org.knime.core.data.DataRow)

Example 14 with DataType

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

the class RuleEngineNodeModel method computeOutputType.

/**
 * Computes the output's type based on the types of the outcomes and on the option of allowed boolean outcome. In
 * case there are no enabled (non-commented out) rules, the type {@code outcomeType} will be used.
 *
 * @param types The type of outcomes.
 * @param outcomeType The default outcome type if no enabled rules were present.
 * @param allowBooleanOutcome Allow or not boolean results in outcome?
 * @param disallowLongOutputForCompatibility see {@link RuleEngineSettings#isDisallowLongOutputForCompatibility()}
 * @return The {@link DataType} specifying the result's type.
 * @since 2.12
 */
public static DataType computeOutputType(final List<DataType> types, final DataType outcomeType, final boolean allowBooleanOutcome, final boolean disallowLongOutputForCompatibility) {
    final DataType outType;
    if (disallowLongOutputForCompatibility) {
        types.replaceAll(t -> LongCell.TYPE.equals(t) ? IntCell.TYPE : t);
    }
    if (types.size() > 0) {
        DataType temp = types.get(0);
        for (int i = 1; i < types.size(); i++) {
            temp = DataType.getCommonSuperType(temp, types.get(i));
        }
        if ((temp.getValueClasses().size() == 1) && temp.getValueClasses().contains(DataValue.class)) {
            // a non-native type, we replace it with string
            temp = StringCell.TYPE;
        }
        outType = allowBooleanOutcome ? temp : BooleanCell.TYPE.isASuperTypeOf(temp) ? IntCell.TYPE : temp;
    } else {
        outType = outcomeType;
    }
    return outType;
}
Also used : DataValue(org.knime.core.data.DataValue) DataType(org.knime.core.data.DataType)

Example 15 with DataType

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

the class RulePanel method updateOutputType.

/**
 * Refreshes the outcome's type.
 */
protected void updateOutputType() {
    if (!m_nodeType.hasOutput()) {
        return;
    }
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            final long startDate = new Date().getTime();
            try {
                final List<Rule> rules = computeRules();
                final DataType outputType = RuleEngineNodeModel.computeOutputType(rules, m_nodeType, m_disallowLongOutputForCompatibility);
                ViewUtils.invokeLaterInEDT(new Runnable() {

                    @Override
                    public void run() {
                        setOutputType(startDate, outputType);
                    }
                });
            } catch (ParseException e) {
                setOutputType(startDate, DataType.getMissingCell().getType());
            }
        }
    };
    threadPool.execute(runnable);
}
Also used : DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) Date(java.util.Date)

Aggregations

DataType (org.knime.core.data.DataType)330 DataColumnSpec (org.knime.core.data.DataColumnSpec)142 DataTableSpec (org.knime.core.data.DataTableSpec)101 DataCell (org.knime.core.data.DataCell)96 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)95 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)71 DoubleValue (org.knime.core.data.DoubleValue)67 DataRow (org.knime.core.data.DataRow)61 ArrayList (java.util.ArrayList)55 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)34 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)32 DefaultRow (org.knime.core.data.def.DefaultRow)24 HashSet (java.util.HashSet)23 HashMap (java.util.HashMap)20 StringCell (org.knime.core.data.def.StringCell)20 NominalValue (org.knime.core.data.NominalValue)18 DoubleCell (org.knime.core.data.def.DoubleCell)18 IntCell (org.knime.core.data.def.IntCell)18 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)18 ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)18