use of org.knime.core.data.collection.CollectionDataValue in project knime-core by knime.
the class ElementCountOperator method computeInternal.
/**
* {@inheritDoc}
*/
@Override
protected boolean computeInternal(final DataCell cell) {
if (cell instanceof CollectionDataValue) {
// missing cells are skipped
final CollectionDataValue collectionCell = (CollectionDataValue) cell;
m_counter += collectionCell.size();
}
// has no limit that's why we always return false
return false;
}
use of org.knime.core.data.collection.CollectionDataValue in project knime-core by knime.
the class AndElementOperator method computeInternal.
/**
* {@inheritDoc}
*/
@Override
protected boolean computeInternal(final DataCell cell) {
if (cell instanceof CollectionDataValue) {
// missing cells are skipped
final CollectionDataValue collectionCell = (CollectionDataValue) cell;
final Set<DataCell> valCells = new HashSet<>(collectionCell.size());
for (final DataCell valCell : collectionCell) {
valCells.add(valCell);
}
if (m_first) {
// can't get bigger
if (valCells.size() >= getMaxUniqueValues()) {
setSkipMessage("Group contains too many unique values");
return true;
}
m_vals.addAll(valCells);
m_first = false;
} else {
// keep only the matching ones
m_vals.retainAll(valCells);
}
}
return false;
}
use of org.knime.core.data.collection.CollectionDataValue in project knime-core by knime.
the class ColumnCalculator method calculate.
/**
* Performs the calculation.
*
* @param row the row to process
* @return the resulting cell
*/
public DataCell calculate(final DataRow row) {
if (m_flowVarAssignmentMap == null) {
m_flowVarAssignmentMap = new HashMap<InputField, Object>();
for (Map.Entry<InputField, ExpressionField> e : m_expression.getFieldMap().entrySet()) {
InputField f = e.getKey();
if (f.getFieldType().equals(FieldType.Variable)) {
Class<?> c = e.getValue().getFieldClass();
m_flowVarAssignmentMap.put(f, m_flowVarProvider.readVariable(f.getColOrVarName(), c));
}
}
}
DataTableSpec spec = m_settings.getInputSpec();
Class<?> returnType = m_settings.getReturnType();
boolean isArrayReturn = m_settings.isArrayReturn();
Map<InputField, Object> nameValueMap = new HashMap<InputField, Object>();
nameValueMap.put(new InputField(Expression.ROWINDEX, FieldType.TableConstant), m_lastProcessedRow++);
nameValueMap.put(new InputField(Expression.ROWID, FieldType.TableConstant), row.getKey().getString());
nameValueMap.put(new InputField(Expression.ROWCOUNT, FieldType.TableConstant), m_flowVarProvider.getRowCount());
nameValueMap.putAll(m_flowVarAssignmentMap);
for (int i = 0; i < row.getNumCells(); i++) {
DataColumnSpec columnSpec = spec.getColumnSpec(i);
InputField inputField = new InputField(columnSpec.getName(), FieldType.Column);
if (!m_expression.needsInputField(inputField)) {
continue;
}
DataCell cell = row.getCell(i);
DataType cellType = columnSpec.getType();
boolean isArray = cellType.isCollectionType();
if (isArray) {
cellType = cellType.getCollectionElementType();
}
Object cellVal = null;
if (cell.isMissing()) {
if (m_settings.isInsertMissingAsNull()) {
// leave value as null
} else {
String message = "Row \"" + row.getKey() + "\" " + "contains missing value in column \"" + columnSpec.getName() + "\" - returning missing";
if (!m_hasReportedMissing) {
m_hasReportedMissing = true;
LOGGER.warn(message + " (omitting further warnings)");
} else {
LOGGER.debug(message);
}
return DataType.getMissingCell();
}
} else {
for (JavaSnippetType<?, ?, ?> t : JavaSnippetType.TYPES) {
if (t.checkCompatibility(cellType)) {
if (isArray) {
cellVal = t.asJavaArray((CollectionDataValue) cell);
} else {
cellVal = t.asJavaObject(cell);
}
break;
}
}
}
nameValueMap.put(inputField, cellVal);
}
Object o = null;
try {
m_expression.set(nameValueMap);
o = m_expression.evaluate();
// class correctness is asserted by compiler
} catch (Abort ee) {
StringBuilder builder = new StringBuilder("Calculation aborted: ");
String message = ee.getMessage();
builder.append(message == null ? "<no details>" : message);
throw new RuntimeException(builder.toString(), ee);
} catch (EvaluationFailedException ee) {
Throwable cause = ee.getCause();
if (cause instanceof InvocationTargetException) {
cause = ((InvocationTargetException) cause).getCause();
}
String message = cause != null ? cause.getMessage() : ee.getMessage();
LOGGER.warn("Evaluation of expression failed for row \"" + row.getKey() + "\": " + message, ee);
} catch (IllegalPropertyException ipe) {
LOGGER.warn("Evaluation of expression failed for row \"" + row.getKey() + "\": " + ipe.getMessage(), ipe);
}
DataCell result = null;
for (JavaSnippetType<?, ?, ?> t : JavaSnippetType.TYPES) {
if (returnType.equals(t.getJavaClass(false))) {
if (o == null) {
result = DataType.getMissingCell();
} else if (isArrayReturn) {
result = t.asKNIMEListCell((Object[]) o);
} else {
result = t.asKNIMECell(o);
}
break;
}
}
if (result == null) {
throw new InternalError("No mapping for objects of class " + o.getClass().getName());
}
return result;
}
use of org.knime.core.data.collection.CollectionDataValue in project knime-core by knime.
the class SplitCellFactory method getCells.
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
DataCell inCell = row.getCell(m_colIndex);
DataCell[] result = new DataCell[m_colSpecs.length];
Arrays.fill(result, DataType.getMissingCell());
if (inCell.isMissing()) {
if (m_warnMessage == null) {
m_warnMessage = "Some rows contain missing values";
}
return result;
}
CollectionDataValue v = (CollectionDataValue) inCell;
Iterator<DataCell> it = v.iterator();
for (int i = 0; i < m_colSpecs.length && it.hasNext(); i++) {
DataCell next;
DataType type;
if (it instanceof BlobSupportDataCellIterator) {
next = ((BlobSupportDataCellIterator) it).nextWithBlobSupport();
if (next instanceof BlobWrapperDataCell) {
// try to not access the cell (will get deserialized)
BlobWrapperDataCell bw = (BlobWrapperDataCell) next;
type = DataType.getType(bw.getBlobClass());
} else {
type = next.getType();
}
} else {
next = it.next();
type = next.getType();
}
if (m_commonTypes[i] == null) {
m_commonTypes[i] = type;
} else {
m_commonTypes[i] = DataType.getCommonSuperType(m_commonTypes[i], type);
}
result[i] = next;
}
if (it.hasNext()) {
m_warnMessage = "At least one row had more elements than " + "specified; row was truncated.";
}
m_domainCreator.updateDomain(new DefaultRow(row.getKey(), result));
return result;
}
use of org.knime.core.data.collection.CollectionDataValue in project knime-core by knime.
the class SubgroupMinerModel2 method preprocessCollCells.
/**
*The preprocessing of the cells, if the selected column is a collection.
* the collection values are saved internally, and a bitvector is
* created for each transaction.
*
* @param input the data table.
* @param exec the execution context.
* @return the list of bitvectors
*/
private List<BitVectorValue> preprocessCollCells(final BufferedDataTable inData, final ExecutionMonitor exec, final List<DataCell> nameMapping, final Map<Integer, RowKey> tidRowKeyMapping, final AtomicInteger maxBitsetLength) throws CanceledExecutionException {
final Map<DataCell, Integer> cell2ItemMap = new HashMap<DataCell, Integer>();
int transIndex = inData.getDataTableSpec().findColumnIndex(m_transactionColumn.getStringValue());
for (final DataRow row : inData) {
final DataCell cell = row.getCell(transIndex);
if (!cell.isMissing()) {
final CollectionDataValue colCell = (CollectionDataValue) cell;
for (final DataCell valCell : colCell) {
exec.checkCanceled();
if (!cell2ItemMap.containsKey(valCell)) {
cell2ItemMap.put(valCell, cell2ItemMap.size());
nameMapping.add(valCell);
}
}
}
}
// afterwards create the bitvectors
int nrOfRows = 0;
int totalNrRows = inData.getRowCount();
List<BitVectorValue> bitSets = new ArrayList<BitVectorValue>();
for (final DataRow row : inData) {
exec.checkCanceled();
DataCell dc = row.getCell(transIndex);
if (dc.isMissing()) {
continue;
}
CollectionDataValue currCell = ((CollectionDataValue) row.getCell(transIndex));
SparseBitVector bitvec = new SparseBitVector(nameMapping.size());
for (final DataCell valCell : currCell) {
exec.checkCanceled();
Integer itemID = cell2ItemMap.get(valCell);
assert (itemID != null);
bitvec.set(itemID.intValue(), true);
}
if (currCell.size() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("bit vector in row " + row.getKey().getString() + " is too long: " + currCell.size() + ". Only bit vectors up to " + Integer.MAX_VALUE + " are supported by this node.");
}
bitSets.add(new SparseBitVectorCellFactory(bitvec).createDataCell());
tidRowKeyMapping.put(nrOfRows, row.getKey());
nrOfRows++;
exec.setProgress((double) nrOfRows / (double) totalNrRows, "preprocessing..." + nrOfRows);
}
maxBitsetLength.set(nameMapping.size());
LOGGER.debug("max length: " + maxBitsetLength.get());
return bitSets;
}
Aggregations