Search in sources :

Example 16 with DataRow

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

the class ExpressionFactory method or.

/**
 * {@inheritDoc}
 */
@Override
public Expression or(final List<Expression> boolExpressions) {
    final boolean allIsConstant = checkBooleansAndConstant(boolExpressions);
    return new Expression.Base(boolExpressions) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataType getOutputType() {
            return BooleanCell.TYPE;
        }

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

        /**
         * {@inheritDoc}
         */
        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            DataCell ret = BooleanCell.FALSE;
            Map<String, Map<String, String>> matchedObjects = new HashMap<String, Map<String, String>>();
            for (Expression boolExpression : boolExpressions) {
                ExpressionValue v = boolExpression.evaluate(row, provider);
                DataCell cell = v.getValue();
                assert !cell.isMissing();
                if (cell instanceof BooleanValue) {
                    BooleanValue bool = (BooleanValue) cell;
                    if (!bool.getBooleanValue()) {
                        matchedObjects = Util.mergeObjects(matchedObjects, v.getMatchedObjects());
                    } else {
                        return new ExpressionValue(BooleanCell.TRUE, matchedObjects);
                    }
                } else if (cell.isMissing()) {
                    ret = DataType.getMissingCell();
                } else {
                    throw new IllegalStateException("Not boolean: " + v.getValue());
                }
            }
            return new ExpressionValue(ret, matchedObjects);
        }

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

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

        /**
         * {@inheritDoc}
         */
        @Override
        public ASTType getTreeType() {
            return ASTType.Or;
        }
    };
}
Also used : HashMap(java.util.HashMap) DataRow(org.knime.core.data.DataRow) BooleanValue(org.knime.core.data.BooleanValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with DataRow

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

the class ExpressionFactory method xor.

/**
 * {@inheritDoc}
 */
@Override
public Expression xor(final List<Expression> boolExpressions) {
    if (boolExpressions.size() < 1) {
        throw new IllegalStateException("xor requires at least one argument.");
    }
    final boolean allIsConstant = checkBooleansAndConstant(boolExpressions);
    return new Expression.Base(boolExpressions) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataType getOutputType() {
            return BooleanCell.TYPE;
        }

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

        /**
         * {@inheritDoc}
         */
        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            ExpressionValue val = boolExpressions.get(0).evaluate(row, provider);
            DataCell valCell = val.getValue();
            assert !valCell.isMissing();
            if (valCell.isMissing()) {
                return new ExpressionValue(valCell, EMPTY_MAP);
            } else if (!(valCell instanceof BooleanValue)) {
                throw new IllegalStateException("Not a boolean value in row '" + row.getKey() + "': " + valCell.getType());
            }
            BooleanCell ret = (BooleanCell) BooleanCellFactory.create(((BooleanValue) valCell).getBooleanValue());
            Map<String, Map<String, String>> matchedObjects = val.getMatchedObjects();
            for (int i = 1; i < boolExpressions.size(); ++i) {
                Expression boolExpression = boolExpressions.get(i);
                ExpressionValue v = boolExpression.evaluate(row, provider);
                DataCell cell = v.getValue();
                if (cell.isMissing()) {
                    return new ExpressionValue(cell, EMPTY_MAP);
                } else if (cell instanceof BooleanValue) {
                    BooleanValue bool = (BooleanValue) cell;
                    matchedObjects = Util.mergeObjects(matchedObjects, v.getMatchedObjects());
                    ret = (BooleanCell) BooleanCellFactory.create(ret.getBooleanValue() ^ bool.getBooleanValue());
                } else {
                    throw new IllegalStateException("Not a boolean value: " + v.getValue());
                }
            }
            return new ExpressionValue(ret, matchedObjects);
        }

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

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

        /**
         * {@inheritDoc}
         */
        @Override
        public ASTType getTreeType() {
            return ASTType.Xor;
        }
    };
}
Also used : BooleanCell(org.knime.core.data.def.BooleanCell) DataRow(org.knime.core.data.DataRow) BooleanValue(org.knime.core.data.BooleanValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with DataRow

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

the class ExpressionFactory method missingBoolean.

/**
 * {@inheritDoc}
 */
@Override
public Expression missingBoolean(final Expression reference) {
    return new Expression.Base(reference) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataType getOutputType() {
            return BooleanCell.TYPE;
        }

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

        /**
         * {@inheritDoc}
         */
        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            ExpressionValue val = reference.evaluate(row, provider);
            DataCell valCell = val.getValue();
            return new ExpressionValue(BooleanCellFactory.create(valCell.isMissing()), EMPTY_MAP);
        }

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

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

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

Example 19 with DataRow

use of org.knime.core.data.DataRow 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 20 with DataRow

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

Aggregations

DataRow (org.knime.core.data.DataRow)482 DataCell (org.knime.core.data.DataCell)268 DataTableSpec (org.knime.core.data.DataTableSpec)159 BufferedDataTable (org.knime.core.node.BufferedDataTable)125 DataColumnSpec (org.knime.core.data.DataColumnSpec)109 RowKey (org.knime.core.data.RowKey)88 DefaultRow (org.knime.core.data.def.DefaultRow)88 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)80 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)76 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)73 DoubleValue (org.knime.core.data.DoubleValue)72 ArrayList (java.util.ArrayList)65 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)65 RowIterator (org.knime.core.data.RowIterator)62 DataType (org.knime.core.data.DataType)61 DoubleCell (org.knime.core.data.def.DoubleCell)57 StringCell (org.knime.core.data.def.StringCell)53 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)48 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)44 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)43