Search in sources :

Example 11 with BooleanValue

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

the class ExpressionFactory method and.

/**
 * {@inheritDoc}
 */
@Override
public Expression and(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.TRUE;
            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.FALSE, EMPTY_MAP);
                    }
                } 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 "and(" + boolExpressions + ")";
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ASTType getTreeType() {
            return ASTType.And;
        }
    };
}
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 12 with BooleanValue

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

the class RuleEngineFilterNodeModel method execute.

/**
 * The real worker.
 * @param inData The input data as {@link RowInput}.
 * @param outputs The output tables.
 * @param rowCount The row count (if available, else {@code -1} is fine).
 * @param exec The {@link ExecutionContext}.
 * @throws ParseException Parsing of rules failed.
 * @throws CanceledExecutionException Execution cancelled.
 * @throws InterruptedException Streaming failed.
 */
private void execute(final RowInput inData, final RowOutput[] outputs, final long rowCount, final ExecutionContext exec) throws ParseException, CanceledExecutionException, InterruptedException {
    final List<Rule> rules = parseRules(inData.getDataTableSpec(), RuleNodeSettings.RuleFilter);
    final int matchIndex = m_includeOnMatch.getBooleanValue() ? 0 : 1;
    final int otherIndex = 1 - matchIndex;
    try {
        final long[] rowIdx = new long[] { 0L };
        final long rows = rowCount;
        final VariableProvider provider = new VariableProvider() {

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

            @Override
            public long getRowCountLong() {
                return rows;
            }

            @Deprecated
            @Override
            public int getRowCount() {
                return KnowsRowCountTable.checkRowCount(rows);
            }

            @Override
            @Deprecated
            public int getRowIndex() {
                return (int) rowIdx[0];
            }

            @Override
            public long getRowIndexLong() {
                return rowIdx[0];
            }
        };
        DataRow row;
        while ((row = inData.poll()) != null) {
            rowIdx[0]++;
            exec.setProgress(rowIdx[0] / (double) rows, () -> "Adding row " + rowIdx[0] + " of " + rows);
            exec.checkCanceled();
            boolean wasMatch = false;
            for (Rule r : rules) {
                if (r.getCondition().matches(row, provider).getOutcome() == MatchState.matchedAndStop) {
                    // r.getSideEffect().perform(row, provider);
                    DataValue value = r.getOutcome().getComputedResult(row, provider);
                    final int index;
                    if (value instanceof BooleanValue) {
                        final BooleanValue bv = (BooleanValue) value;
                        index = bv.getBooleanValue() ? matchIndex : otherIndex;
                    } else {
                        index = matchIndex;
                    }
                    if (index < outputs.length) {
                        outputs[index].push(row);
                    }
                    wasMatch = true;
                    break;
                }
            }
            if (!wasMatch) {
                if (otherIndex < outputs.length) {
                    outputs[otherIndex].push(row);
                }
            }
        }
    } finally {
        outputs[0].close();
        if (outputs.length > 1) {
            outputs[1].close();
        }
    }
}
Also used : DataValue(org.knime.core.data.DataValue) BooleanValue(org.knime.core.data.BooleanValue) DataRow(org.knime.core.data.DataRow)

Aggregations

BooleanValue (org.knime.core.data.BooleanValue)12 DataRow (org.knime.core.data.DataRow)8 DataCell (org.knime.core.data.DataCell)6 DoubleValue (org.knime.core.data.DoubleValue)6 SQLException (java.sql.SQLException)4 DataType (org.knime.core.data.DataType)4 IntValue (org.knime.core.data.IntValue)4 LongValue (org.knime.core.data.LongValue)4 BinaryObjectDataValue (org.knime.core.data.blob.BinaryObjectDataValue)4 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DataTableSpec (org.knime.core.data.DataTableSpec)3 StringValue (org.knime.core.data.StringValue)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Statement (java.sql.Statement)2