use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class MaskTimeNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec inSpec = inSpecs[0];
// check if there is a date and tme column in niput spec
if (!inSpec.containsCompatibleType(DateAndTimeValue.class)) {
throw new InvalidSettingsException("Input table must contain at least one column " + "containing time!");
}
// do we have a selected column?
String selectedCol = m_selectedColumn.getStringValue();
if (selectedCol != null && !selectedCol.isEmpty()) {
// if yes -> exists in input spec?
if (!inSpec.containsName(selectedCol)) {
throw new InvalidSettingsException("Selected column " + selectedCol + "not found in input table!");
}
} else {
// if no -> auto-configure: select first date and time column
for (DataColumnSpec colSpec : inSpec) {
if (colSpec.getType().isCompatible(DateAndTimeValue.class)) {
String colName = colSpec.getName();
m_selectedColumn.setStringValue(colName);
setWarningMessage("Auto-configure: selected column " + colName + "!");
}
}
}
return new DataTableSpec[] { createOutputSpec(inSpec) };
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class String2DateNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
* @throws InvalidSettingsException
* @since 2.6
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec, final SimpleStreamableOperatorInternals emptyInternals) throws InvalidSettingsException {
if (m_formatModel.getStringValue() == null) {
throw new InvalidSettingsException("No format selected.");
}
try {
m_dateFormat = new SimpleDateFormat(m_formatModel.getStringValue());
} catch (IllegalArgumentException ex) {
throw new InvalidSettingsException("Invalid format: " + m_formatModel.getStringValue(), ex);
}
m_dateFormat.setTimeZone(DateAndTimeCell.UTC_TIMEZONE);
String selectedCol = m_selectedColModel.getStringValue();
if (selectedCol == null || selectedCol.isEmpty()) {
// try to find first String compatible one and auto-guess it
for (DataColumnSpec cs : spec) {
if (cs.getType().isCompatible(StringValue.class)) {
m_selectedColModel.setStringValue(cs.getName());
setWarningMessage("Auto-guessing first String compatible column: " + cs.getName());
break;
}
}
}
// if still null -> no String compatible column at all
if (selectedCol == null || selectedCol.isEmpty()) {
throw new InvalidSettingsException("No String compatible column found!");
}
final int colIndex = spec.findColumnIndex(selectedCol);
if (colIndex < 0) {
throw new InvalidSettingsException("No such column: " + selectedCol);
}
DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
if (!colSpec.getType().isCompatible(StringValue.class)) {
throw new InvalidSettingsException("Column \"" + selectedCol + "\" does not contain string values: " + colSpec.getType().toString());
}
ColumnRearranger result = new ColumnRearranger(spec);
String uniqueColName = selectedCol;
if (!m_replace.getBooleanValue()) {
// if we do not have a default new column name yet
// create one as done in
// check whether the new column name is unique...
uniqueColName = DataTableSpec.getUniqueColumnName(spec, m_newColNameModel.getStringValue());
m_newColNameModel.setStringValue(uniqueColName);
}
DataColumnSpec newColSpec = new DataColumnSpecCreator(uniqueColName, DateAndTimeCell.TYPE).createSpec();
m_dateFormat = new SimpleDateFormat(m_formatModel.getStringValue());
m_dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
SingleCellFactory c = new SingleCellFactory(newColSpec) {
private int m_failCounter = 0;
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIndex);
if (cell.isMissing() || !(cell instanceof StringValue)) {
return DataType.getMissingCell();
}
try {
String source = ((StringValue) cell).getStringValue();
Date date = m_dateFormat.parse(source);
Calendar calendar = DateAndTimeCell.getUTCCalendar();
calendar.setTimeInMillis(date.getTime());
// dependent on the type create the referring cell
return new DateAndTimeCell(calendar.getTimeInMillis(), m_useDate, m_useTime, m_useMillis);
} catch (ParseException pe) {
m_failCounter++;
if (m_cancelOnFail.getBooleanValue() && m_failCounter >= m_failNumberModel.getIntValue()) {
throw new IllegalArgumentException("Maximum number of fails reached: " + m_failNumberModel.getIntValue());
}
return DataType.getMissingCell();
}
}
@Override
public void afterProcessing() {
setFailMessage(m_failCounter);
emptyInternals.getConfig().addLong(INTERNALS_KEY_FAIL_COUNT, m_failCounter);
}
};
if (m_replace.getBooleanValue()) {
result.replace(c, colIndex);
} else {
result.append(c);
}
return result;
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class Time2StringNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
* @since 2.6
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) throws InvalidSettingsException {
// check if input has dateandtime column
if (!inSpec.containsCompatibleType(DateAndTimeValue.class)) {
throw new InvalidSettingsException("Input table must contain at least timestamp column!");
}
// currently selected column still there?
String selectedColName = m_selectedCol.getStringValue();
if (selectedColName != null && !selectedColName.isEmpty()) {
if (!inSpec.containsName(selectedColName)) {
throw new InvalidSettingsException("Column " + selectedColName + " not found in input spec!");
}
} else {
// no value set: auto-configure -> choose first timeseries
for (DataColumnSpec colSpec : inSpec) {
if (colSpec.getType().isCompatible(DateAndTimeValue.class)) {
String colName = colSpec.getName();
m_selectedCol.setStringValue(colName);
m_newColName.setStringValue(colName + "_" + COL_NAME_SUFFIX);
setWarningMessage("Auto-selected column: '" + colName + "'");
break;
}
}
}
ColumnRearranger rearranger = new ColumnRearranger(inSpec);
// if replace -> use original column name
final boolean replace = m_replaceCol.getBooleanValue();
String colName = DataTableSpec.getUniqueColumnName(inSpec, m_newColName.getStringValue());
if (replace) {
colName = m_selectedCol.getStringValue();
}
DataColumnSpecCreator specCreator = new DataColumnSpecCreator(colName, StringCell.TYPE);
final SimpleDateFormat dateFormat = new SimpleDateFormat(m_pattern.getStringValue());
dateFormat.setTimeZone(DateAndTimeCell.UTC_TIMEZONE);
final int colIdx = inSpec.findColumnIndex(m_selectedCol.getStringValue());
SingleCellFactory factory = new SingleCellFactory(specCreator.createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
DataCell dc = row.getCell(colIdx);
if (dc.isMissing()) {
return DataType.getMissingCell();
}
if (dc.getType().isCompatible(DateAndTimeValue.class)) {
DateAndTimeValue v = (DateAndTimeValue) dc;
String result = dateFormat.format(v.getUTCCalendarClone().getTime());
return new StringCell(result);
}
LOGGER.error("Encountered unsupported data type: " + dc.getType() + " in row: " + row.getKey());
return DataType.getMissingCell();
}
};
if (!replace) {
rearranger.append(factory);
} else {
rearranger.replace(factory, m_selectedCol.getStringValue());
}
return rearranger;
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class TimeMissingValueHandlingNodeDialogPane method loadSettingsFrom.
/**
* {@inheritDoc}
*/
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final DataTableSpec[] specs) throws NotConfigurableException {
m_searchableListModifier = m_searchableListPanel.update(specs[0]);
TimeMissingValueHandlingColSetting[] defaults = TimeMissingValueHandlingColSetting.loadMetaColSettings(settings, specs[0]);
TimeMissingValueHandlingColSetting[] individuals = TimeMissingValueHandlingColSetting.loadIndividualColSettings(settings, specs[0]);
m_defaultsPanel.removeAll();
for (int i = 0; i < defaults.length; i++) {
final TimeMissingValueHandlingPanel p = new TimeMissingValueHandlingPanel(defaults[i], (DataColumnSpec) null);
m_defaultsPanel.add(p);
}
m_individualsPanel.removeAll();
Set<String> invalidColumns = new LinkedHashSet<String>();
for (int i = 0; i < individuals.length; i++) {
String[] names = individuals[i].getNames();
ArrayList<DataColumnSpec> colSpecs = new ArrayList<DataColumnSpec>();
for (int j = 0; j < names.length; j++) {
final DataColumnSpec cspec = specs[0].getColumnSpec(names[j]);
if (cspec == null) {
LOGGER.debug("No such column in spec: " + names[j]);
DataColumnSpec createUnkownSpec = createUnkownSpec(names[j], individuals[i]);
colSpecs.add(createUnkownSpec);
invalidColumns.add(names[j]);
} else {
colSpecs.add(cspec);
}
}
if (!colSpecs.isEmpty()) {
names = new String[colSpecs.size()];
for (int j = 0; j < names.length; j++) {
names[j] = colSpecs.get(j).getName();
}
individuals[i].setNames(names);
markIncompatibleTypedColumns(individuals[i].getType(), colSpecs);
final TimeMissingValueHandlingPanel p = new TimeMissingValueHandlingPanel(individuals[i], colSpecs.toArray(new DataColumnSpec[0]));
p.registerMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void mouseClicked(final MouseEvent me) {
selectColumns(p.getSettings());
}
});
addToIndividualPanel(p);
}
}
m_searchableListModifier.addInvalidColumns(invalidColumns.toArray(new String[invalidColumns.size()]));
m_individualsPanel.setPreferredSize(m_defaultsPanel.getPreferredSize());
checkButtonStatus();
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class TimeMissingValueHandlingNodeDialogPane method createAsIncompatibleMarkedColumnSpec.
private static DataColumnSpec createAsIncompatibleMarkedColumnSpec(final DataColumnSpec originalSpec) {
DataColumnSpecCreator creator = new DataColumnSpecCreator(originalSpec);
final DataColumnProperties origProps = originalSpec.getProperties();
final Map<String, String> map = createIncompatiblePropertiesMap();
final DataColumnProperties props;
if (origProps != null) {
props = origProps.cloneAndOverwrite(map);
} else {
props = new DataColumnProperties(map);
}
creator.setProperties(props);
final DataColumnSpec invalidSpec = creator.createSpec();
return invalidSpec;
}
Aggregations