Search in sources :

Example 1 with BooleanValue

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

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

the class RuleEngineFilter2PortsNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        private SimpleStreamableOperatorInternals m_internals;

        /**
         * {@inheritDoc}
         */
        @Override
        public void loadInternals(final StreamableOperatorInternals internals) {
            m_internals = (SimpleStreamableOperatorInternals) internals;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
            // count number of rows
            long count = 0;
            RowInput rowInput = (RowInput) inputs[DATA_PORT];
            while (rowInput.poll() != null) {
                count++;
            }
            m_internals.getConfig().addLong(CFG_ROW_COUNT, count);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals saveInternals() {
            return m_internals;
        }

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            long rowCount = -1L;
            if (m_internals.getConfig().containsKey(CFG_ROW_COUNT)) {
                rowCount = m_internals.getConfig().getLong(CFG_ROW_COUNT);
            }
            m_rulesList.clear();
            final PortInput rulePort = inputs[RULE_PORT];
            if (rulePort instanceof PortObjectInput) {
                PortObjectInput poRule = (PortObjectInput) rulePort;
                m_rulesList.addAll(RuleEngineVariable2PortsNodeModel.rules((BufferedDataTable) poRule.getPortObject(), m_settings, RuleNodeSettings.RuleFilter));
            } else if (rulePort instanceof RowInput) {
                RowInput riRule = (RowInput) rulePort;
                m_rulesList.addAll(RuleEngineVariable2PortsNodeModel.rules(riRule, m_settings, RuleNodeSettings.RuleFilter));
            }
            final DataTableSpec spec = (DataTableSpec) inSpecs[DATA_PORT];
            try {
                parseRules(spec, RuleNodeSettings.RuleSplitter);
            } catch (final ParseException e) {
                throw new InvalidSettingsException(e);
            }
            final RowInput inputPartitions = (RowInput) inputs[DATA_PORT];
            final List<Rule> rules = parseRules(inputPartitions.getDataTableSpec(), RuleNodeSettings.RuleFilter);
            final RowOutput first = (RowOutput) outputs[0];
            final int nrOutPorts = getNrOutPorts();
            final RowOutput second = nrOutPorts > 1 ? (RowOutput) outputs[1] : new RowOutput() {

                @Override
                public void push(final DataRow row) throws InterruptedException {
                // do nothing
                }

                @Override
                public void close() throws InterruptedException {
                // do nothing
                }
            };
            final RowOutput[] containers = new RowOutput[] { first, second };
            final int matchIndex = m_includeOnMatch ? 0 : 1;
            final int otherIndex = 1 - matchIndex;
            try {
                final MutableLong rowIdx = new MutableLong(0L);
                final long rows = rowCount;
                final VariableProvider provider = new VariableProvider() {

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

                    @Override
                    @Deprecated
                    public int getRowCount() {
                        throw new UnsupportedOperationException();
                    }

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

                    @Override
                    @Deprecated
                    public int getRowIndex() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getRowIndexLong() {
                        return rowIdx.longValue();
                    }
                };
                DataRow row;
                while ((row = inputPartitions.poll()) != null) {
                    rowIdx.increment();
                    if (rows > 0) {
                        exec.setProgress(rowIdx.longValue() / (double) rows, () -> "Adding row " + rowIdx.longValue() + " of " + rows);
                    } else {
                        exec.setMessage(() -> "Adding row " + rowIdx.longValue() + " 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);
                            if (value instanceof BooleanValue) {
                                final BooleanValue bv = (BooleanValue) value;
                                containers[bv.getBooleanValue() ? matchIndex : otherIndex].push(row);
                            } else {
                                containers[matchIndex].push(row);
                            }
                            wasMatch = true;
                            break;
                        }
                    }
                    if (!wasMatch) {
                        containers[otherIndex].push(row);
                    }
                }
            } finally {
                try {
                    second.close();
                } finally {
                    first.close();
                }
            }
        }
    };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataValue(org.knime.core.data.DataValue) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) RowInput(org.knime.core.node.streamable.RowInput) DataRow(org.knime.core.data.DataRow) PortObjectInput(org.knime.core.node.streamable.PortObjectInput) RowAppenderRowOutput(org.knime.base.node.rules.engine.RowAppenderRowOutput) BufferedDataTableRowOutput(org.knime.core.node.streamable.BufferedDataTableRowOutput) RowOutput(org.knime.core.node.streamable.RowOutput) VariableProvider(org.knime.base.node.rules.engine.VariableProvider) BooleanValue(org.knime.core.data.BooleanValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortInput(org.knime.core.node.streamable.PortInput) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) MutableLong(org.apache.commons.lang3.mutable.MutableLong) ExecutionContext(org.knime.core.node.ExecutionContext) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ParseException(java.text.ParseException) Rule(org.knime.base.node.rules.engine.Rule)

Example 3 with BooleanValue

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

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

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

the class RuleSetToTable method toString.

/**
 * 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.
 */
public static String toString(final DataCell cell) {
    if (cell.isMissing()) {
        return "\"?\"";
    }
    if (cell instanceof StringValue) {
        StringValue sv = (StringValue) cell;
        String s = sv.getStringValue();
        return escapedText(s);
    }
    if (cell instanceof BooleanValue) {
        return Boolean.toString(((BooleanValue) cell).getBooleanValue()).toUpperCase();
    }
    if (cell instanceof DoubleValue) {
        return cell.toString();
    }
    return escapedText(cell.toString());
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) BooleanValue(org.knime.core.data.BooleanValue) StringValue(org.knime.core.data.StringValue)

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