use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class CellReplacerNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final DataTableSpec spec, final DataTableSpec dictSpec, final BufferedDataTable dictTable, final ExecutionMonitor dictionaryInitExec) throws InvalidSettingsException {
String targetCol = m_targetColModel.getStringValue();
if (targetCol == null || targetCol.length() == 0) {
throw new InvalidSettingsException("No target column selected");
}
final int targetColIndex = spec.findColumnIndex(targetCol);
if (targetColIndex < 0) {
throw new InvalidSettingsException("No such column \"" + targetCol + "\"");
}
final DataColumnSpec targetColSpec = spec.getColumnSpec(targetColIndex);
final int dictInputColIndex = dictSpec.findColumnIndex(m_dictInputColModel.getStringValue());
final boolean dictInputIsCollection;
if (m_dictInputColModel.useRowID()) {
dictInputIsCollection = false;
} else if (dictInputColIndex < 0) {
throw new InvalidSettingsException("No such column \"" + m_dictInputColModel.getStringValue() + "\"");
} else {
DataColumnSpec inS = dictSpec.getColumnSpec(dictInputColIndex);
dictInputIsCollection = inS.getType().isCollectionType();
}
final int dictOutputColIndex = dictSpec.findColumnIndex(m_dictOutputColModel.getStringValue());
final DataType dictOutputColType;
if (m_dictOutputColModel.useRowID()) {
dictOutputColType = StringCell.TYPE;
} else {
if (dictOutputColIndex < 0) {
throw new InvalidSettingsException("No such column \"" + m_dictOutputColModel.getStringValue() + "\"");
}
dictOutputColType = dictSpec.getColumnSpec(dictOutputColIndex).getType();
}
final NoMatchPolicy noMatchPolicy = getNoMatchPolicy();
DataType outputType;
switch(noMatchPolicy) {
case Input:
outputType = DataType.getCommonSuperType(dictOutputColType, targetColSpec.getType());
break;
default:
outputType = dictOutputColType;
}
String newColName;
if (m_appendColumnModel.getBooleanValue()) {
String newName = m_appendColumnNameModel.getStringValue();
if (newName == null || newName.length() == 0) {
throw new InvalidSettingsException("No new column name given");
}
newColName = DataTableSpec.getUniqueColumnName(spec, newName);
} else {
newColName = targetColSpec.getName();
}
DataColumnSpecCreator replaceSpecCreator = new DataColumnSpecCreator(newColName, outputType);
CellFactory c = new SingleCellFactory(replaceSpecCreator.createSpec()) {
private Map<DataCell, DataCell> m_dictionaryMap;
@Override
public DataCell getCell(final DataRow row) {
try {
ensureInitDictionaryMap();
} catch (CanceledExecutionException e) {
// cancellation done by the framework
return DataType.getMissingCell();
}
DataCell cell = row.getCell(targetColIndex);
DataCell output = m_dictionaryMap.get(cell);
if (output == null) {
switch(noMatchPolicy) {
case Input:
return cell;
default:
return DataType.getMissingCell();
}
}
return output;
}
private void ensureInitDictionaryMap() throws CanceledExecutionException {
if (m_dictionaryMap == null) {
m_dictionaryMap = new HashMap<DataCell, DataCell>();
int i = 0;
double rowCount = dictTable.size();
for (DataRow r : dictTable) {
dictionaryInitExec.setProgress((i++) / rowCount, "Reading dictionary into memory, row " + i);
dictionaryInitExec.checkCanceled();
DataCell output = dictOutputColIndex < 0 ? new StringCell(r.getKey().getString()) : r.getCell(dictOutputColIndex);
DataCell input = dictInputColIndex < 0 ? new StringCell(r.getKey().getString()) : r.getCell(dictInputColIndex);
if (input.isMissing()) {
addSearchPair(input, output);
} else if (dictInputIsCollection) {
CollectionDataValue v = (CollectionDataValue) input;
for (DataCell element : v) {
addSearchPair(element, output);
}
} else {
addSearchPair(input, output);
}
}
}
}
private void addSearchPair(final DataCell input, final DataCell output) {
if (m_dictionaryMap.put(input, output) != null) {
setWarningMessage("Duplicate search key \"" + input + "\"");
}
}
};
ColumnRearranger result = new ColumnRearranger(spec);
if (m_appendColumnModel.getBooleanValue()) {
result.append(c);
} else {
result.replace(c, targetColIndex);
}
return result;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class ColumnComparatorNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
final ComparatorMethod method = ComparatorMethod.getMethod(m_operator.getStringValue());
final int idx1 = spec.findColumnIndex(m_firstColumn.getStringValue());
final int idx2 = spec.findColumnIndex(m_secondColumn.getStringValue());
DataColumnSpec leftSpec = spec.getColumnSpec(idx1);
DataColumnSpec rightSpec = spec.getColumnSpec(idx2);
ColumnRearranger colRe = new ColumnRearranger(spec);
colRe.append(new SingleCellFactory(createSpec(leftSpec, rightSpec)) {
private final StringCell m_matchRepl = new StringCell(m_matchValue.getStringValue());
private final StringCell m_mismatchRepl = new StringCell(m_mismatchValue.getStringValue());
@Override
public DataCell getCell(final DataRow row) {
DataCell cell1 = row.getCell(idx1);
DataCell cell2 = row.getCell(idx2);
if (method.compare(cell1, cell2)) {
String strMatch = m_matchOption.getStringValue();
if (strMatch.equals(REPL_OPTIONS[0])) {
return covertMatch(cell1);
} else if (strMatch.equals(REPL_OPTIONS[1])) {
return covertMatch(cell2);
} else if (strMatch.equals(REPL_OPTIONS[2])) {
return DataType.getMissingCell();
} else {
return m_matchRepl;
}
} else {
String strMismatch = m_mismatchOption.getStringValue();
if (strMismatch.equals(REPL_OPTIONS[0])) {
return covertMismatch(cell1);
} else if (strMismatch.equals(REPL_OPTIONS[1])) {
return covertMismatch(cell2);
} else if (strMismatch.equals(REPL_OPTIONS[2])) {
return DataType.getMissingCell();
} else {
return m_mismatchRepl;
}
}
}
});
return colRe;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class XML2PMMLNodeModel method createColRearranger.
private ColumnRearranger createColRearranger(final DataTableSpec spec) throws InvalidSettingsException {
if (m_xmlColumnName.getStringValue() == null) {
guessDefaultXMLColumn(spec);
}
String xmlColName = m_xmlColumnName.getStringValue();
String newColName = m_newColumnName.getStringValue();
final int colIndex = spec.findColumnIndex(xmlColName);
CheckUtils.checkSetting(colIndex >= 0, "Column: '%s' does not exist anymore.", xmlColName);
final DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
CheckUtils.checkSetting(colSpec.getType().isCompatible(StringValue.class), "Selected column '%s' is not string/xml-compatible", xmlColName);
DataColumnSpecCreator colSpecCreator;
if (newColName != null && !m_replaceColumn.getBooleanValue()) {
String newName = DataTableSpec.getUniqueColumnName(spec, newColName);
colSpecCreator = new DataColumnSpecCreator(newName, PMMLCell.TYPE);
} else {
colSpecCreator = new DataColumnSpecCreator(colSpec);
colSpecCreator.setType(PMMLCell.TYPE);
colSpecCreator.removeAllHandlers();
colSpecCreator.setDomain(null);
}
DataColumnSpec outColumnSpec = colSpecCreator.createSpec();
ColumnRearranger rearranger = new ColumnRearranger(spec);
CellFactory fac = new SingleCellFactory(outColumnSpec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIndex);
if (cell.isMissing()) {
return DataType.getMissingCell();
} else {
PMMLDocument pmmlDoc = null;
String failure = null;
XmlObject xmlDoc;
try (LockedSupplier<Document> supplier = ((XMLValue<Document>) cell).getDocumentSupplier()) {
xmlDoc = XmlObject.Factory.parse(supplier.get().cloneNode(true));
if (xmlDoc instanceof PMMLDocument) {
pmmlDoc = (PMMLDocument) xmlDoc;
} else if (PMMLUtils.isOldKNIMEPMML(xmlDoc) || PMMLUtils.is4_1PMML(xmlDoc)) {
String updatedPMML = PMMLUtils.getUpdatedVersionAndNamespace(xmlDoc);
/* Parse the modified document and assign it to a
* PMMLDocument.*/
pmmlDoc = PMMLDocument.Factory.parse(updatedPMML);
} else {
failure = "No valid PMML v 3.x/4.0/4.1 document";
}
} catch (XmlException e) {
if (!m_failOnInvalid.getBooleanValue()) {
LOGGER.error("Invalid PMML in row " + row.getKey() + ": " + e.getMessage(), e);
}
failure = e.getMessage();
}
if (failure != null) {
m_failCounter.incrementAndGet();
if (m_failOnInvalid.getBooleanValue()) {
throw new RuntimeException("Invalid PMML in row " + row.getKey() + ": " + failure);
} else {
return new MissingCell(failure);
}
} else {
try {
return PMMLCellFactory.create(pmmlDoc.toString());
} catch (Exception e) {
return new MissingCell(e.getMessage());
}
}
}
}
};
if (m_replaceColumn.getBooleanValue()) {
rearranger.replace(fac, colIndex);
} else {
rearranger.append(fac);
}
return rearranger;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class GroupByTable method appendOrderColumn.
/**
* @param exec the {@link ExecutionContext}
* @param dataTable the {@link BufferedDataTable} to add the order column to
* @param workingCols the names of all columns needed for grouping
* @param retainOrderCol the name of the order column
* @return the given table with the appended order column
* @throws CanceledExecutionException if the operation has been canceled
*/
public static BufferedDataTable appendOrderColumn(final ExecutionContext exec, final BufferedDataTable dataTable, final Set<String> workingCols, final String retainOrderCol) throws CanceledExecutionException {
final ColumnRearranger rearranger = new ColumnRearranger(dataTable.getSpec());
rearranger.append(new SingleCellFactory(new DataColumnSpecCreator(retainOrderCol, IntCell.TYPE).createSpec()) {
private int m_id = 0;
@Override
public DataCell getCell(final DataRow row) {
return new IntCell(m_id++);
}
});
final String[] workingColsArray = workingCols.toArray(new String[0]);
rearranger.keepOnly(workingColsArray);
rearranger.permute(workingColsArray);
return exec.createColumnRearrangeTable(dataTable, rearranger, exec);
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class ConstantValueColumnNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec in) throws InvalidSettingsException {
final String value = m_config.getValue();
checkSetting(value != null, "Configuration missing.");
checkSetting(!(m_config.getReplacedColumn() == null && m_config.getNewColumnName() == null), "Either a replacing column or a new column name must be specified");
String colName = m_config.getReplacedColumn();
final int replacedColumn = in.findColumnIndex(m_config.getReplacedColumn());
checkSetting(!(colName != null && replacedColumn < 0), "Column to replace: '%s' does not exist in input table", colName);
String newName = replacedColumn >= 0 ? colName : DataTableSpec.getUniqueColumnName(in, m_config.getNewColumnName());
DataColumnSpec outColumnSpec = new DataColumnSpecCreator(newName, m_config.getCellFactory().getDataType()).createSpec();
final DataCell constantCell = m_config.getCellFactory().createCell(value, m_config.getDateFormat());
ColumnRearranger rearranger = new ColumnRearranger(in);
CellFactory fac = new SingleCellFactory(outColumnSpec) {
@Override
public DataCell getCell(final DataRow row) {
return constantCell;
}
};
if (replacedColumn >= 0) {
rearranger.replace(fac, replacedColumn);
} else {
rearranger.append(fac);
}
return rearranger;
}
Aggregations