use of org.knime.core.data.container.AbstractCellFactory in project knime-core by knime.
the class ExpandBitVectorNodeModel method createCellFactory.
/**
* {@inheritDoc}
*/
@Override
protected AbstractCellFactory createCellFactory(final String[] colNames, final DataColumnSpec[] outputColumns, final int inputIndex) {
return new AbstractCellFactory(outputColumns) {
@Override
public DataCell[] getCells(final DataRow row) {
DataCell[] vs = new DataCell[colNames.length];
DataCell cell = row.getCell(inputIndex);
if (cell instanceof BitVectorValue) {
BitVectorValue bvv = (BitVectorValue) cell;
int length = Math.min(vs.length, (int) bvv.length());
for (int i = length; i-- > 0; ) {
vs[i] = VALUES[bvv.get(i) ? 1 : 0];
}
for (int i = vs.length; i-- > length; ) {
vs[i] = DataType.getMissingCell();
}
} else {
for (int i = 0; i < vs.length; i++) {
vs[i] = DataType.getMissingCell();
}
}
return vs;
}
};
}
use of org.knime.core.data.container.AbstractCellFactory in project knime-core by knime.
the class RegexSplitNodeModel method createColumnRearranger.
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec, final SimpleStreamableOperatorInternals internals) throws InvalidSettingsException {
AtomicInteger errorCounter = new AtomicInteger();
if (m_settings == null) {
throw new InvalidSettingsException("Not configuration available.");
}
final int colIndex = spec.findColumnIndex(m_settings.getColumn());
if (colIndex < 0) {
throw new InvalidSettingsException("No such column in input table: " + m_settings.getColumn());
}
DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
if (!colSpec.getType().isCompatible(StringValue.class)) {
throw new InvalidSettingsException("Selected column does not " + "contain strings");
}
final Pattern p = m_settings.compile();
int count = 0;
String patternS = p.pattern();
boolean isNextSpecial = false;
boolean isPreviousAParenthesis = false;
// escaped parentheses "\(" or non-capturing groups "(?"
for (int i = 0; i < patternS.length(); i++) {
switch(patternS.charAt(i)) {
case '\\':
isNextSpecial = !isNextSpecial;
isPreviousAParenthesis = false;
break;
case '(':
count += isNextSpecial ? 0 : 1;
isPreviousAParenthesis = !isNextSpecial;
isNextSpecial = false;
break;
case '?':
if (isPreviousAParenthesis) {
count -= 1;
}
// no break;
default:
isNextSpecial = false;
isPreviousAParenthesis = false;
}
}
final int newColCount = count;
final DataColumnSpec[] newColSpecs = new DataColumnSpec[count];
for (int i = 0; i < newColCount; i++) {
String name = DataTableSpec.getUniqueColumnName(spec, "split_" + i);
newColSpecs[i] = new DataColumnSpecCreator(name, StringCell.TYPE).createSpec();
}
ColumnRearranger rearranger = new ColumnRearranger(spec);
rearranger.append(new AbstractCellFactory(newColSpecs) {
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
DataCell[] result = new DataCell[newColCount];
Arrays.fill(result, DataType.getMissingCell());
DataCell c = row.getCell(colIndex);
if (c.isMissing()) {
return result;
}
String s = ((StringValue) c).getStringValue();
Matcher m = p.matcher(s);
if (m.matches()) {
int max = m.groupCount();
if (m.groupCount() > newColCount) {
errorCounter.incrementAndGet();
max = newColCount;
}
for (int i = 0; i < max; i++) {
// group(0) will return the entire string and is not
// included in groupCount, see Matcher API for details
String str = m.group(i + 1);
if (str != null) {
// null for optional groups "(...)?"
result[i] = new StringCell(str);
}
}
return result;
} else {
errorCounter.incrementAndGet();
return result;
}
}
/**
* {@inheritDoc}
*/
@Override
public void afterProcessing() {
// propagate error count
internals.getConfig().addInt(CONFIG_KEY_ERRORCOUNT, errorCounter.get());
}
});
return rearranger;
}
use of org.knime.core.data.container.AbstractCellFactory in project knime-core by knime.
the class ExpandByteVectorNodeModel method createCellFactory.
/**
* {@inheritDoc}
*/
@Override
protected AbstractCellFactory createCellFactory(final String[] colNames, final DataColumnSpec[] outputColumns, final int inputIndex) {
return new AbstractCellFactory(outputColumns) {
@Override
public DataCell[] getCells(final DataRow row) {
DataCell[] vs = new DataCell[colNames.length];
DataCell cell = row.getCell(inputIndex);
if (cell instanceof ByteVectorValue) {
ByteVectorValue bvv = (ByteVectorValue) cell;
int length = Math.min(vs.length, (int) bvv.length());
for (int i = length; i-- > 0; ) {
vs[i] = VALUES[bvv.get(i)];
}
for (int i = vs.length; i-- > length; ) {
vs[i] = DataType.getMissingCell();
}
} else {
for (int i = 0; i < vs.length; i++) {
vs[i] = DataType.getMissingCell();
}
}
return vs;
}
};
}
use of org.knime.core.data.container.AbstractCellFactory in project knime-core by knime.
the class AppendVariableToTable2NodeModel method createColumnRearranger.
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
ColumnRearranger arranger = new ColumnRearranger(spec);
Set<String> nameHash = new HashSet<String>();
for (DataColumnSpec c : spec) {
nameHash.add(c.getName());
}
List<Pair<String, FlowVariable.Type>> vars = getVariablesOfInterest();
if (vars.isEmpty()) {
throw new InvalidSettingsException("No variables selected");
}
DataColumnSpec[] specs = new DataColumnSpec[vars.size()];
final DataCell[] values = new DataCell[vars.size()];
for (int i = 0; i < vars.size(); i++) {
Pair<String, FlowVariable.Type> c = vars.get(i);
String name = c.getFirst();
final DataType type;
switch(c.getSecond()) {
case DOUBLE:
type = DoubleCell.TYPE;
try {
double dValue = peekFlowVariableDouble(name);
values[i] = new DoubleCell(dValue);
} catch (NoSuchElementException e) {
throw new InvalidSettingsException("No such flow variable (of type double): " + name);
}
break;
case INTEGER:
type = IntCell.TYPE;
try {
int iValue = peekFlowVariableInt(name);
values[i] = new IntCell(iValue);
} catch (NoSuchElementException e) {
throw new InvalidSettingsException("No such flow variable (of type int): " + name);
}
break;
case STRING:
type = StringCell.TYPE;
try {
String sValue = peekFlowVariableString(name);
sValue = sValue == null ? "" : sValue;
values[i] = new StringCell(sValue);
} catch (NoSuchElementException e) {
throw new InvalidSettingsException("No such flow variable (of type String): " + name);
}
break;
default:
throw new InvalidSettingsException("Unsupported variable type: " + c.getSecond());
}
if (nameHash.contains(name) && !name.toLowerCase().endsWith("(variable)")) {
name = name.concat(" (variable)");
}
String newName = name;
int uniquifier = 1;
while (!nameHash.add(newName)) {
newName = name + " (#" + (uniquifier++) + ")";
}
specs[i] = new DataColumnSpecCreator(newName, type).createSpec();
}
arranger.append(new AbstractCellFactory(specs) {
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
return values;
}
});
return arranger;
}
Aggregations