Search in sources :

Example 6 with DataType

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

the class BaseRuleParser method parseColumnExpression.

/**
 * Creates an {@link Expression} to compute a column.
 *
 * @param state The {@link ParseState}.
 * @param fromMissing This {@link Expression} is created within a {@link Operators#MISSING}.
 * @return The {@link Expression} parsed representing a column.
 * @throws ParseException Problem during parsing.
 */
private Expression parseColumnExpression(final ParseState state, final boolean fromMissing) throws ParseException {
    state.skipWS();
    int startPos = state.getPosition();
    if (state.isColumnRef()) {
        String columnRef = state.readColumnRef();
        if (!m_checkColumns) {
            // Create a dummy expression
            return new Expression() {

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

                @Override
                public ASTType getTreeType() {
                    return ASTType.ColRef;
                }

                @Override
                public DataType getOutputType() {
                    return DataType.getMissingCell().getType();
                }

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

                @Override
                public List<Expression> getChildren() {
                    return Collections.emptyList();
                }

                @Override
                public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
                    throw new IllegalStateException("This expression is only for configuration.");
                }
            };
        }
        if (m_spec == null) {
            throw new ParseException("No columns present.", startPos);
        }
        try {
            Expression expr = m_factoryRef.columnRef(m_spec, columnRef);
            if (BooleanCell.TYPE.isASuperTypeOf(expr.getOutputType()) && fromMissing) {
                expr = m_factoryRef.columnRefForMissing(m_spec, columnRef);
            }
            return expr;
        } catch (IllegalStateException e) {
            throw new ParseException(e.getMessage(), startPos);
        }
    }
    throw new ParseException("Expected a column reference", state.getPosition());
}
Also used : DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) DataRow(org.knime.core.data.DataRow)

Example 7 with DataType

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

the class BaseRuleParser method parseFlowVariableExpression.

/**
 * Creates an {@link Expression} to compute a {@link FlowVariable}.
 *
 * @param state The {@link ParseState}.
 * @return The {@link Expression} parsed representing a {@link FlowVariable}.
 * @throws ParseException Problem during parsing.
 */
private Expression parseFlowVariableExpression(final ParseState state) throws ParseException {
    state.skipWS();
    int startPos = state.getPosition();
    if (state.isFlowVariableRef()) {
        String flowVarRef = state.readFlowVariable();
        if (!m_checkFlowVars) {
            // Create a dummy expression
            return new Expression() {

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

                @Override
                public ASTType getTreeType() {
                    return ASTType.FlowVarRef;
                }

                @Override
                public DataType getOutputType() {
                    return DataType.getMissingCell().getType();
                }

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

                @Override
                public List<Expression> getChildren() {
                    return Collections.emptyList();
                }

                @Override
                public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
                    throw new IllegalStateException("This expression is only for configuration.");
                }
            };
        }
        try {
            return m_factoryRef.flowVarRef(m_flowVariables, flowVarRef);
        } catch (IllegalStateException e) {
            throw new ParseException(e.getMessage(), startPos);
        }
    }
    throw new ParseException("Expected a flow variable reference", state.getPosition());
}
Also used : DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) DataRow(org.knime.core.data.DataRow)

Example 8 with DataType

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

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

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

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