Search in sources :

Example 11 with DataValueComparator

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

the class BaseRuleParser method handleRelationRightOperand.

/**
 * @param state
 * @param beforeLeft
 * @param left
 * @param beforeOperator
 * @param op
 * @return The {@link Expression} representing the relation.
 * @throws ParseException
 */
protected PredicateType handleRelationRightOperand(final ParseState state, final int beforeLeft, final Expression left, final int beforeOperator, final Operators op) throws ParseException {
    switch(op) {
        case IN:
            {
                state.skipWS();
                int afterOperator = state.getPosition();
                Expression right = parseList(state);
                try {
                    return m_factoryPred.in(left, right);
                } catch (IllegalStateException e) {
                    throw new ParseException(e.getMessage(), afterOperator);
                }
            }
        case MISSING:
            throw new ParseException("Invalid logical predicate: " + op, beforeOperator);
        case NOT:
        case AND:
        case OR:
        case XOR:
            if (BooleanCell.TYPE.isASuperTypeOf(left.getOutputType())) {
                state.setPosition(beforeOperator);
                return m_factoryPred.boolAsPredicate(left);
            }
            throw new ParseException("Invalid logical connective: " + op, beforeOperator);
        case EQ:
            if (state.peekChar() == '>') {
                throw new ParseException("Expected a relation to complete condition, but got =>", beforeOperator);
            }
        case GE:
        case GT:
        case LE:
        case LIKE:
        case LT:
        case MATCHES:
            // single argument
            break;
        default:
            throw new ParseException("Not supported operator: " + op, beforeOperator);
    }
    switch(op) {
        case LIKE:
        case MATCHES:
            state.expectWS();
            break;
        default:
            break;
    }
    int beforeRight = state.getPosition();
    state.skipWS();
    Expression right = parseOperand(state, BooleanCell.TYPE.isASuperTypeOf(left.getOutputType()), Operators.MISSING == op);
    DataValueComparator cmp = DataType.getCommonSuperType(left.getOutputType(), right.getOutputType()).getComparator();
    switch(op) {
        case IN:
        case MISSING:
        case NOT:
        case AND:
        case OR:
        case XOR:
            throw new IllegalStateException("Already handled: " + op);
        case EQ:
            return m_factoryPred.compare(left, right, cmp, 0);
        case LE:
            return m_factoryPred.compare(left, right, cmp, -1, 0);
        case LT:
            return m_factoryPred.compare(left, right, cmp, -1);
        case GE:
            return m_factoryPred.compare(left, right, cmp, 0, 1);
        case GT:
            return m_factoryPred.compare(left, right, cmp, 1);
        case LIKE:
            reportNotString(beforeLeft, left, op, beforeRight, right);
            try {
                // add keys when parsed
                return m_factoryPred.like(left, right, null);
            } catch (IllegalStateException ex) {
                throw new ParseException("Invalid pattern: " + right.toString(), beforeRight);
            }
        case MATCHES:
            reportNotString(beforeLeft, left, op, beforeRight, right);
            try {
                // add keys when parsed
                return m_factoryPred.matches(left, right, null);
            } catch (IllegalStateException ex) {
                throw new ParseException("Invalid pattern: " + right.toString(), beforeRight);
            }
        default:
            throw new ParseException("Not supported operator: " + op, beforeOperator);
    }
}
Also used : ParseException(java.text.ParseException) DataValueComparator(org.knime.core.data.DataValueComparator)

Example 12 with DataValueComparator

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

the class ExpressionFactory method in.

/**
 * {@inheritDoc}
 */
@Override
public Expression in(final Expression left, final Expression right) {
    if (!right.getOutputType().isCollectionType()) {
        throw new IllegalStateException("The m_right operand of operator 'IN' is not a collection: " + right.getOutputType());
    }
    ExpressionValue constantTmp = null;
    if (left.isConstant() && right.isConstant()) {
        ExpressionValue leftValue = left.evaluate(null, null);
        ExpressionValue rightValue = right.evaluate(null, null);
        DataCell l = leftValue.getValue();
        final DataCell r = rightValue.getValue();
        if (r.isMissing()) {
            constantTmp = new ExpressionValue(DataType.getMissingCell(), EMPTY_MAP);
        } else if (r instanceof CollectionDataValue) {
            CollectionDataValue rightValues = (CollectionDataValue) r;
            for (DataCell dataCell : rightValues) {
                DataValueComparator cmp = DataType.getCommonSuperType(l.getType(), dataCell.getType()).getComparator();
                if (cmp.compare(l, dataCell) == 0) {
                    constantTmp = new ExpressionValue(BooleanCell.TRUE, Util.mergeObjects(leftValue.getMatchedObjects(), rightValue.getMatchedObjects()));
                }
            }
            if (constantTmp == null) {
                constantTmp = new ExpressionValue(BooleanCell.FALSE, EMPTY_MAP);
            }
        }
        if (constantTmp == null) {
            throw new IllegalStateException("Right operand of the 'IN' operator is not a collection.");
        }
    }
    final ExpressionValue constant = constantTmp;
    return new Expression.Base(left, right) {

        /**
         * {@inheritDoc}
         */
        @Override
        public List<DataType> getInputArgs() {
            return Arrays.asList(DataType.getMissingCell().getType(), ListCell.getCollectionType(DataType.getMissingCell().getType()));
        }

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

        /**
         * {@inheritDoc}
         */
        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            if (constant != null) {
                return constant;
            }
            ExpressionValue leftValue = left.evaluate(row, provider);
            ExpressionValue rightValue = right.evaluate(row, provider);
            DataCell l = leftValue.getValue();
            final DataCell r = rightValue.getValue();
            if (r.isMissing()) {
                return new ExpressionValue(DataType.getMissingCell(), EMPTY_MAP);
            }
            if (r instanceof CollectionDataValue) {
                CollectionDataValue rightValues = (CollectionDataValue) r;
                for (DataCell dataCell : rightValues) {
                    DataValueComparator cmp = DataType.getCommonSuperType(l.getType(), dataCell.getType()).getComparator();
                    if (cmp.compare(l, dataCell) == 0) {
                        return new ExpressionValue(BooleanCell.TRUE, Util.mergeObjects(leftValue.getMatchedObjects(), rightValue.getMatchedObjects()));
                    }
                }
                return new ExpressionValue(BooleanCell.FALSE, EMPTY_MAP);
            }
            throw new IllegalStateException("Right operand of the 'IN' operator is not a collection.");
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isConstant() {
            return left.isConstant() && right.isConstant();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String toString() {
            return left + " in " + right;
        }

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

Example 13 with DataValueComparator

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

the class Rule method parseArithmeticExpression.

/**
 * Parses an arithmetic expression (<tt>AEXPR</tt>).
 *
 * @param spec the table spec
 * @return a rule node for the parsed expression
 * @throws ParseException if rule contains a syntax error
 */
private RuleNode parseArithmeticExpression(final DataTableSpec spec) throws ParseException {
    skipWS();
    if (s[p] == '$') {
        int start = p;
        int leftColIndex = parseColumn(spec);
        Operators op = parseOperator();
        skipWS();
        if (op == Operators.IN) {
            return RuleNodeFactory.in(leftColIndex, parseStringList());
        } else if (s[p] == '$') {
            int rightColIndex = parseColumn(spec);
            DataColumnSpec leftSpec = spec.getColumnSpec(leftColIndex);
            DataColumnSpec rightSpec = spec.getColumnSpec(rightColIndex);
            DataType commonType = DataType.getCommonSuperType(leftSpec.getType(), rightSpec.getType());
            DataValueComparator comp = commonType.getComparator();
            switch(op) {
                case EQ:
                    return RuleNodeFactory.eq(leftColIndex, rightColIndex);
                case GE:
                    return RuleNodeFactory.ge(leftColIndex, rightColIndex, comp);
                case GT:
                    return RuleNodeFactory.gt(leftColIndex, rightColIndex, comp);
                case LE:
                    return RuleNodeFactory.le(leftColIndex, rightColIndex, comp);
                case LT:
                    return RuleNodeFactory.lt(leftColIndex, rightColIndex, comp);
                case LIKE:
                    return RuleNodeFactory.like(leftColIndex, rightColIndex);
                default:
                    throw new ParseException("Unhandeled operator " + op, start);
            }
        } else if (s[p] == '"') {
            String t = parseString();
            switch(op) {
                case EQ:
                    return RuleNodeFactory.eq(leftColIndex, t);
                case GE:
                    return RuleNodeFactory.ge(leftColIndex, t);
                case GT:
                    return RuleNodeFactory.gt(leftColIndex, t);
                case LE:
                    return RuleNodeFactory.le(leftColIndex, t);
                case LT:
                    return RuleNodeFactory.lt(leftColIndex, t);
                case LIKE:
                    return RuleNodeFactory.like(leftColIndex, t);
                default:
                    throw new ParseException("Unhandeled operator " + op, start);
            }
        } else {
            Number n = parseNumber();
            DataType leftType = spec.getColumnSpec(leftColIndex).getType();
            if (!leftType.isCompatible(DoubleValue.class)) {
                throw new ParseException(spec.getColumnSpec(leftColIndex).getName() + " is not a numeric column", start);
            }
            if ((n instanceof Integer) && !leftType.isCompatible(IntValue.class)) {
                n = new Double(n.doubleValue());
            }
            switch(op) {
                case EQ:
                    return RuleNodeFactory.eq(leftColIndex, n);
                case GE:
                    return RuleNodeFactory.ge(leftColIndex, n);
                case GT:
                    return RuleNodeFactory.gt(leftColIndex, n);
                case LE:
                    return RuleNodeFactory.le(leftColIndex, n);
                case LT:
                    return RuleNodeFactory.lt(leftColIndex, n);
                default:
                    throw new ParseException("Unhandeled operator " + op, start);
            }
        }
    } else if (s[p] == '"') {
        String t = parseString();
        Operators op = parseOperator();
        int rightColIndex = parseColumn(spec);
        switch(op) {
            case EQ:
                return RuleNodeFactory.eq(rightColIndex, t);
            case GE:
                return RuleNodeFactory.lt(rightColIndex, t);
            case GT:
                return RuleNodeFactory.le(rightColIndex, t);
            case LE:
                return RuleNodeFactory.gt(rightColIndex, t);
            case LT:
                return RuleNodeFactory.ge(rightColIndex, t);
            case LIKE:
                return RuleNodeFactory.like(t, rightColIndex);
            default:
                throw new ParseException("Unhandeled operator " + op, p);
        }
    } else if ((s[p] >= '0') && (s[p] <= '9')) {
        Number n = parseNumber();
        Operators op = parseOperator();
        int start = p;
        int rightColIndex = parseColumn(spec);
        DataType rightType = spec.getColumnSpec(rightColIndex).getType();
        if (!rightType.isCompatible(DoubleValue.class)) {
            throw new ParseException(spec.getColumnSpec(rightColIndex).getName() + " is not a numeric column", start);
        }
        if ((n instanceof Integer) && !rightType.isCompatible(IntValue.class)) {
            n = new Double(n.doubleValue());
        }
        switch(op) {
            case EQ:
                return RuleNodeFactory.eq(rightColIndex, n);
            case GE:
                return RuleNodeFactory.lt(rightColIndex, n);
            case GT:
                return RuleNodeFactory.le(rightColIndex, n);
            case LE:
                return RuleNodeFactory.gt(rightColIndex, n);
            case LT:
                return RuleNodeFactory.ge(rightColIndex, n);
            default:
                throw new ParseException("Unhandeled operator " + op, start);
        }
    } else {
        throw new ParseException("Expected a column name, a string or a number", p);
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) DataValueComparator(org.knime.core.data.DataValueComparator)

Example 14 with DataValueComparator

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

the class MedianTable method sortOnDisk.

/**
 * Sorts the data on the disk, it moves the missing values to the end.
 *
 * @param context An {@link ExecutionContext}.
 * @param k The indices to read from the different columns
 *        (first dim: length 2 (above & below median indices), second dim: columns)
 * @throws CanceledExecutionException Execution was cancelled.
 */
private void sortOnDisk(final ExecutionContext context, final long[][] k) throws CanceledExecutionException {
    final SortingDescription[] sorting = new SortingDescription[m_indices.length];
    final DataTableSpec spec = m_table.getSpec();
    for (int i = 0; i < m_indices.length; i++) {
        final DataColumnSpec columnSpec = spec.getColumnSpec(m_indices[i]);
        final DataValueComparator comparator = columnSpec.getType().getComparator();
        sorting[i] = new SortingDescription(columnSpec.getName()) {

            @Override
            public int compare(final DataRow o1, final DataRow o2) {
                // Move missing values to the end.
                final DataCell c1 = o1.getCell(0);
                final DataCell c2 = o2.getCell(0);
                if (c1.isMissing()) {
                    return c2.isMissing() ? 0 : 1;
                }
                if (c2.isMissing()) {
                    return -1;
                }
                return comparator.compare(c1, c2);
            }
        };
    }
    final ColumnBufferedDataTableSorter tableSorter;
    try {
        tableSorter = new ColumnBufferedDataTableSorter(m_table.getSpec(), m_table.size(), sorting);
    } catch (InvalidSettingsException e) {
        throw new IllegalStateException(e);
    }
    final MutableLong counter = new MutableLong();
    final DoubleValue[][] cells = new DoubleValue[2][m_indices.length];
    tableSorter.sort(m_table, context, new SortingConsumer() {

        @Override
        public void consume(final DataRow row) {
            for (int kindex = 0; kindex < 2; kindex++) {
                for (int i = 0; i < m_indices.length; i++) {
                    if (counter.longValue() == k[kindex][i]) {
                        DataCell cell = row.getCell(i);
                        if (cell instanceof DoubleValue) {
                            DoubleValue dv = (DoubleValue) cell;
                            cells[kindex][i] = dv;
                        } else {
                            cells[kindex][i] = new DoubleCell(Double.NaN);
                        }
                    }
                }
            }
            counter.increment();
        }
    });
    for (int index = m_indices.length; index-- > 0; ) {
        if (cells[0][index] == null || cells[1][index] == null) {
            // No non-missing rows
            m_medians[index] = Double.NaN;
        } else {
            m_medians[index] = (cells[0][index].getDoubleValue() + cells[1][index].getDoubleValue()) / 2;
        }
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DoubleCell(org.knime.core.data.def.DoubleCell) SortingDescription(org.knime.core.data.sort.SortingDescription) DataValueComparator(org.knime.core.data.DataValueComparator) DataRow(org.knime.core.data.DataRow) ColumnBufferedDataTableSorter(org.knime.core.data.sort.ColumnBufferedDataTableSorter) MutableLong(org.apache.commons.lang.mutable.MutableLong) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) SortingConsumer(org.knime.core.data.sort.SortingConsumer) DataCell(org.knime.core.data.DataCell)

Example 15 with DataValueComparator

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

the class ValueCounterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    final int colIndex = inData[0].getDataTableSpec().findColumnIndex(m_settings.columnName());
    final double max = inData[0].getRowCount();
    int rowCount = 0;
    Map<DataCell, Set<RowKey>> hlMap = new HashMap<DataCell, Set<RowKey>>();
    Map<DataCell, MutableInteger> countMap = new HashMap<DataCell, MutableInteger>();
    for (DataRow row : inData[0]) {
        exec.checkCanceled();
        exec.setProgress(rowCount++ / max, countMap.size() + " different values found");
        DataCell cell = row.getCell(colIndex);
        MutableInteger count = countMap.get(cell);
        if (count == null) {
            count = new MutableInteger(0);
            countMap.put(cell, count);
        }
        count.inc();
        if (m_settings.hiliting()) {
            Set<RowKey> s = hlMap.get(cell);
            if (s == null) {
                s = new HashSet<RowKey>();
                hlMap.put(cell, s);
            }
            s.add(row.getKey());
        }
    }
    final DataValueComparator comp = inData[0].getDataTableSpec().getColumnSpec(colIndex).getType().getComparator();
    List<Map.Entry<DataCell, MutableInteger>> sorted = new ArrayList<Map.Entry<DataCell, MutableInteger>>(countMap.entrySet());
    Collections.sort(sorted, new Comparator<Map.Entry<DataCell, MutableInteger>>() {

        public int compare(final Map.Entry<DataCell, MutableInteger> o1, final Entry<DataCell, MutableInteger> o2) {
            return comp.compare(o1.getKey(), o2.getKey());
        }
    });
    BufferedDataContainer cont = exec.createDataContainer(TABLE_SPEC);
    for (Map.Entry<DataCell, MutableInteger> entry : sorted) {
        RowKey newKey = new RowKey(entry.getKey().toString());
        cont.addRowToTable(new DefaultRow(newKey, new int[] { entry.getValue().intValue() }));
    }
    cont.close();
    if (m_settings.hiliting()) {
        Map<RowKey, Set<RowKey>> temp = new HashMap<RowKey, Set<RowKey>>();
        for (Map.Entry<DataCell, Set<RowKey>> entry : hlMap.entrySet()) {
            RowKey newKey = new RowKey(entry.getKey().toString());
            temp.put(newKey, entry.getValue());
        }
        m_translator.setMapper(new DefaultHiLiteMapper(temp));
    }
    return new BufferedDataTable[] { cont.getTable() };
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) RowKey(org.knime.core.data.RowKey) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) DataValueComparator(org.knime.core.data.DataValueComparator) Entry(java.util.Map.Entry) BufferedDataTable(org.knime.core.node.BufferedDataTable) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) MutableInteger(org.knime.core.util.MutableInteger) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DataValueComparator (org.knime.core.data.DataValueComparator)15 DataCell (org.knime.core.data.DataCell)12 DataRow (org.knime.core.data.DataRow)9 DataColumnSpec (org.knime.core.data.DataColumnSpec)6 DataTableSpec (org.knime.core.data.DataTableSpec)5 DataType (org.knime.core.data.DataType)5 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)3 DoubleValue (org.knime.core.data.DoubleValue)3 RowKey (org.knime.core.data.RowKey)3 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 DefaultRow (org.knime.core.data.def.DefaultRow)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 MutableInteger (org.knime.core.util.MutableInteger)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1