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;
}
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;
}
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;
}
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;
}
};
}
Aggregations