use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class ModifyTimeZoneNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) throws InvalidSettingsException {
CheckUtils.checkSetting(m_hasValidatedConfiguration, "Node must be configured!");
final String modification = CheckUtils.checkSettingNotNull(m_modifyAction.getStringValue(), "must not be null");
CheckUtils.checkSetting(modification.equals(MODIFY_OPTION_SET) || modification.equals(MODIFY_OPTION_SHIFT) || modification.equals(MODIFY_OPTION_REMOVE), "Unknow modification operation '%s'. Most likely the parameter '%s' was controlled by " + "an invalid flow variable.", CFG_KEY_MODIFY_SELECT, m_modifyAction.getStringValue());
final ColumnRearranger rearranger = new ColumnRearranger(inSpec);
final String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
final int[] includeIndeces = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
int i = 0;
final ZoneId zone = m_timeZone.getZone();
DataType dataType;
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_REMOVE)) {
dataType = LocalDateTimeCellFactory.TYPE;
} else {
dataType = ZonedDateTimeCellFactory.TYPE;
}
for (String includedCol : includeList) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includedCol, dataType);
final SingleCellFactory cellFac;
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SET)) {
cellFac = new SetTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++], zone);
} else if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SHIFT)) {
cellFac = new ShiftTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++], zone);
} else {
cellFac = new RemoveTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++]);
}
rearranger.replace(cellFac, includedCol);
} else {
DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(includedCol + m_suffix.getStringValue(), dataType);
final SingleCellFactory cellFac;
if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SET)) {
cellFac = new SetTimeZoneCellFactory(dataColSpec, includeIndeces[i++], zone);
} else if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SHIFT)) {
cellFac = new ShiftTimeZoneCellFactory(dataColSpec, includeIndeces[i++], zone);
} else {
cellFac = new RemoveTimeZoneCellFactory(dataColSpec, includeIndeces[i++]);
}
rearranger.append(cellFac);
}
}
return rearranger;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class DateShiftConfigure method getTimeBasedCellFactory.
/**
* @param spec the output column spec
* @param col1Idx the column index of the numerical column to add
* @param g the time field to modify (as defined by calendar constants)
* @param conf the configuration object
* @param time the configured time as Calendar
* @return the cell factory
*/
public static SingleCellFactory getTimeBasedCellFactory(final DataColumnSpec spec, final int col1Idx, final int g, final DateShiftConfigure conf, final Calendar time) {
return new SingleCellFactory(spec) {
/**
* Value for the new column is based on the values of two column of the row (first and second date column),
* the selected granularity, and the fraction digits for rounding.
*
* @param row the current row
* @return the difference between the two date values with the given granularity and rounding
*/
@Override
public DataCell getCell(final DataRow row) {
final int value;
String typeofshift = conf.gettypeofshift().getStringValue();
if (typeofshift.equals(DateShiftNodeDialog.CFG_COLUMN_SHIFT)) {
DataCell cell1 = row.getCell(col1Idx);
if ((cell1.isMissing())) {
return DataType.getMissingCell();
}
value = ((IntValue) cell1).getIntValue();
} else {
value = conf.getvalueofshift().getIntValue();
}
Calendar c = (Calendar) time.clone();
c.add(g, value);
return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
}
};
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class DateShiftConfigure method getColumnValuebasedCellFactory.
/**
* @param spec the previous data table spec
* @param col1Idx the column index of the numerical column to add
* @param g the time field to modify (as defined by calendar constants)
* @param conf the configuration object
* @param col2Idx the time column
* @return the cell factory
*/
public static SingleCellFactory getColumnValuebasedCellFactory(final DataTableSpec spec, final int col1Idx, final int col2Idx, final int g, final DateShiftConfigure conf) {
return new SingleCellFactory(createOutputColumnSpec(spec, conf.getNewColumnName().getStringValue())) {
/**
* Value for the new column is based on the values of two column of the row (first and second date column),
* the selected granularity, and the fraction digits for rounding.
*
* @param row the current row
* @return the difference between the two date values with the given granularity and rounding
*/
@Override
public DataCell getCell(final DataRow row) {
DataCell cell1 = row.getCell(col1Idx);
DataCell cell2 = row.getCell(col2Idx);
if ((cell1.isMissing()) || (cell2.isMissing())) {
return DataType.getMissingCell();
}
Calendar c = ((DateAndTimeValue) cell2).getUTCCalendarClone();
c.add(g, ((IntValue) cell1).getIntValue());
return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
}
};
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class TimePresetNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable input = inData[0];
DataTableSpec spec = input.getDataTableSpec();
String selectedColName = m_selectedCol.getStringValue();
final int colIdx = spec.findColumnIndex(selectedColName);
if (colIdx < 0) {
throw new IllegalArgumentException("Column " + selectedColName + " not found in input table!");
}
DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(spec.getColumnSpec(selectedColName));
/*
* Reset the domain here. Had problems when time was there and a date is
* added, then the time without the date will stay the lower bound of
* the domain.
*/
colSpecCreator.setDomain(null);
ColumnRearranger rearranger = new ColumnRearranger(spec);
final Calendar preset = m_presetCalendar.getCalendar();
rearranger.replace(new SingleCellFactory(colSpecCreator.createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
DataCell dc = row.getCell(colIdx);
if (dc.isMissing()) {
if (m_replaceMissingValues.getBooleanValue()) {
return new DateAndTimeCell(preset.getTimeInMillis(), m_presetCalendar.useDate(), m_presetCalendar.useTime(), m_presetCalendar.useMilliseconds());
}
return DataType.getMissingCell();
}
if (dc.getType().isCompatible(DateAndTimeValue.class)) {
DateAndTimeValue v = (DateAndTimeValue) dc;
Calendar existing = v.getUTCCalendarClone();
// look at the date
if (m_presetCalendar.useDate() && !v.hasDate()) {
// set it
existing.set(Calendar.YEAR, preset.get(Calendar.YEAR));
existing.set(Calendar.MONTH, preset.get(Calendar.MONTH));
existing.set(Calendar.DAY_OF_MONTH, preset.get(Calendar.DAY_OF_MONTH));
}
if (m_presetCalendar.useTime() && !v.hasTime()) {
// set it
existing.set(Calendar.HOUR_OF_DAY, preset.get(Calendar.HOUR_OF_DAY));
existing.set(Calendar.MINUTE, preset.get(Calendar.MINUTE));
existing.set(Calendar.SECOND, preset.get(Calendar.SECOND));
if (m_presetCalendar.useMilliseconds()) {
existing.set(Calendar.MILLISECOND, preset.get(Calendar.MILLISECOND));
}
}
return new DateAndTimeCell(existing.getTimeInMillis(), m_presetCalendar.useDate() || v.hasDate(), m_presetCalendar.useTime() || v.hasTime(), m_presetCalendar.useMilliseconds() || v.hasMillis());
}
LOGGER.error("Unsupported type " + dc.getType() + " found in row " + row.getKey() + "!");
return DataType.getMissingCell();
}
}, colIdx);
BufferedDataTable out = exec.createColumnRearrangeTable(input, rearranger, exec);
return new BufferedDataTable[] { out };
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class MaskTimeNodeModel method createRearranger.
private ColumnRearranger createRearranger(final DataTableSpec inSpec) {
// get the selected column index
final int colIdx = inSpec.findColumnIndex(m_selectedColumn.getStringValue());
if (colIdx < 0) {
throw new IllegalArgumentException("Column " + m_selectedColumn.getStringValue() + " not found in input table!");
}
ColumnRearranger rearranger = new ColumnRearranger(inSpec);
final String maskMode = m_maskSelection.getStringValue();
m_nrInvalids = 0;
m_onlyInvalids = true;
DataColumnSpec existing = inSpec.getColumnSpec(colIdx);
SingleCellFactory fac = createCellFactory(createNewColumnSpec(existing), colIdx, maskMode);
if (m_replaceColumn.getBooleanValue()) {
rearranger.replace(fac, colIdx);
} else {
rearranger.append(fac);
}
return rearranger;
}
Aggregations