Search in sources :

Example 16 with CollectionDataValue

use of org.knime.core.data.collection.CollectionDataValue in project knime-core by knime.

the class CreateBitVectorNodeModel method getSingleColFactory.

private BitVectorCellFactory getSingleColFactory(final ExecutionMonitor exec, final int colIdx, final DataTableSpec spec, final BufferedDataTable data, final ColumnType columnType, final BitVectorType vectorType) throws InvalidSettingsException, CanceledExecutionException {
    final String outColName = m_outputColumn.getStringValue();
    final DataColumnSpecCreator creator = new DataColumnSpecCreator(outColName, vectorType.getCellDataType());
    final BitVectorCellFactory factory;
    if (ColumnType.SINGLE_STRING.equals(columnType)) {
        final StringType singleStringColType = StringType.getType(m_singleStringColumnType.getStringValue());
        final DataColumnSpec colSpec = creator.createSpec();
        switch(singleStringColType) {
            case BIT:
                factory = new BitString2BitVectorCellFactory(vectorType, colSpec, colIdx);
                break;
            case HEX:
                factory = new Hex2BitVectorCellFactory(vectorType, colSpec, colIdx);
                break;
            case ID:
                final int maxPosition;
                if (data != null) {
                    final ExecutionMonitor scanExec = exec.createSubProgress(0.5);
                    exec.setMessage("preparing");
                    maxPosition = scanMaxPos(data, scanExec);
                } else {
                    maxPosition = 0;
                }
                factory = new IdString2BitVectorCellFactory(vectorType, colSpec, colIdx, maxPosition);
                break;
            default:
                throw new InvalidSettingsException("String type to parse bit vectors from unknown type " + singleStringColType.getActionCommand());
        }
    } else if (ColumnType.SINGLE_COLLECTION.equals(columnType)) {
        final Map<String, Integer> idxMap;
        if (data != null) {
            final ExecutionMonitor scanExec = exec.createSubProgress(0.5);
            scanExec.setMessage("preparing");
            final List<String> elementNames = new ArrayList<>();
            idxMap = new HashMap<>();
            long nrRows = data.size();
            long currRow = 0;
            for (DataRow row : data) {
                currRow++;
                scanExec.setProgress((double) currRow / (double) nrRows, "Counting uniqe elements. Processing row " + currRow + " of " + nrRows);
                scanExec.checkCanceled();
                final DataCell cell = row.getCell(colIdx);
                if (cell.isMissing()) {
                    // ignore missing cells
                    continue;
                }
                if (cell instanceof CollectionDataValue) {
                    final CollectionDataValue collCell = (CollectionDataValue) cell;
                    for (DataCell collVal : collCell) {
                        String stringRep = collVal.toString();
                        Integer idx = idxMap.get(stringRep);
                        if (idx == null) {
                            idx = Integer.valueOf(idxMap.size());
                            idxMap.put(stringRep, idx);
                            elementNames.add(stringRep);
                        }
                    }
                } else {
                    throw new RuntimeException("Found incompatible type in row " + row.getKey().getString());
                }
            }
            creator.setElementNames(elementNames.toArray(new String[0]));
        } else {
            idxMap = Collections.EMPTY_MAP;
        }
        factory = new Collection2BitVectorCellFactory(vectorType, creator.createSpec(), colIdx, idxMap);
    } else {
        throw new java.lang.IllegalStateException("Single column type not implemented: " + columnType);
    }
    return factory;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) HashMap(java.util.HashMap) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) Collection2BitVectorCellFactory(org.knime.base.data.bitvector.Collection2BitVectorCellFactory) DataRow(org.knime.core.data.DataRow) SettingsModelInteger(org.knime.core.node.defaultnodesettings.SettingsModelInteger) Hex2BitVectorCellFactory(org.knime.base.data.bitvector.Hex2BitVectorCellFactory) DataColumnSpec(org.knime.core.data.DataColumnSpec) BitVectorCellFactory(org.knime.base.data.bitvector.BitVectorCellFactory) Collection2BitVectorCellFactory(org.knime.base.data.bitvector.Collection2BitVectorCellFactory) Hex2BitVectorCellFactory(org.knime.base.data.bitvector.Hex2BitVectorCellFactory) IdString2BitVectorCellFactory(org.knime.base.data.bitvector.IdString2BitVectorCellFactory) BitString2BitVectorCellFactory(org.knime.base.data.bitvector.BitString2BitVectorCellFactory) MultiString2BitVectorCellFactory(org.knime.base.data.bitvector.MultiString2BitVectorCellFactory) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) List(java.util.List) ArrayList(java.util.ArrayList) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) IdString2BitVectorCellFactory(org.knime.base.data.bitvector.IdString2BitVectorCellFactory) Map(java.util.Map) HashMap(java.util.HashMap) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) BitString2BitVectorCellFactory(org.knime.base.data.bitvector.BitString2BitVectorCellFactory)

Example 17 with CollectionDataValue

use of org.knime.core.data.collection.CollectionDataValue in project knime-core by knime.

the class AttrValueRowFilter method performDeepFiltering.

/**
 * @param theCell {@link CollectionDataValue} to process
 * @return <code>true</code> if one of the elements of the given {@link CollectionDataValue} matches the
 * filter criterion otherwise it returns <code>false</code>
 * @since 2.10
 */
protected boolean performDeepFiltering(final CollectionDataValue theCell) {
    // use a LIFO queue to perform a depth first search through all elements of the collections
    final Stack<CollectionDataValue> collCells = new Stack<CollectionDataValue>();
    collCells.push(theCell);
    while (!collCells.isEmpty()) {
        final CollectionDataValue collCell = collCells.pop();
        for (final DataCell cell : collCell) {
            if (cell instanceof CollectionDataValue) {
                collCells.push((CollectionDataValue) cell);
            } else if (matches(cell)) {
                // if we find a match we can return otherwise we have to keep on comparing till the end
                return true;
            }
        }
    }
    return false;
}
Also used : DataCell(org.knime.core.data.DataCell) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) Stack(java.util.Stack)

Example 18 with CollectionDataValue

use of org.knime.core.data.collection.CollectionDataValue in project knime-core by knime.

the class Buffer method mustBeFlushedPriorSave.

private boolean mustBeFlushedPriorSave(final DataCell cell) {
    if (cell instanceof FileStoreCell) {
        FileStore fileStore = FileStoreUtil.getFileStore((FileStoreCell) cell);
        return ((IWriteFileStoreHandler) m_fileStoreHandler).mustBeFlushedPriorSave(fileStore);
    } else if (cell instanceof CollectionDataValue) {
        for (DataCell c : (CollectionDataValue) cell) {
            if (mustBeFlushedPriorSave(c)) {
                return true;
            }
        }
    } else if (cell instanceof BlobWrapperDataCell) {
        final BlobWrapperDataCell blobWrapperCell = (BlobWrapperDataCell) cell;
        Class<? extends BlobDataCell> blobClass = blobWrapperCell.getBlobClass();
        if (CollectionDataValue.class.isAssignableFrom(blobClass)) {
            return mustBeFlushedPriorSave(blobWrapperCell.getCell());
        }
    }
    return false;
}
Also used : FileStore(org.knime.core.data.filestore.FileStore) IWriteFileStoreHandler(org.knime.core.data.filestore.internal.IWriteFileStoreHandler) DataCell(org.knime.core.data.DataCell) FileStoreCell(org.knime.core.data.filestore.FileStoreCell) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue)

Example 19 with CollectionDataValue

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

Aggregations

CollectionDataValue (org.knime.core.data.collection.CollectionDataValue)19 DataCell (org.knime.core.data.DataCell)18 DataRow (org.knime.core.data.DataRow)9 HashMap (java.util.HashMap)7 DataColumnSpec (org.knime.core.data.DataColumnSpec)5 DataType (org.knime.core.data.DataType)5 HashSet (java.util.HashSet)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)3 DataTableSpec (org.knime.core.data.DataTableSpec)3 RowKey (org.knime.core.data.RowKey)3 DefaultRow (org.knime.core.data.def.DefaultRow)3 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2 Set (java.util.Set)2 StringCell (org.knime.core.data.def.StringCell)2 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2