use of org.knime.core.node.config.Config in project knime-core by knime.
the class OldToNewTimeNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
SimpleStreamableOperatorInternals m_internals = new SimpleStreamableOperatorInternals();
/**
* {@inheritDoc}
*/
@Override
public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
if (partitionInfo.getPartitionIndex() == 0) {
final RowInput rowInput = (RowInput) inputs[0];
final DataRow row = rowInput.poll();
if (row != null) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells()];
final DataTableSpec inSpec = rowInput.getDataTableSpec();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
for (int i = 0; i < inSpec.getNumColumns(); i++) {
final int searchIdx = Arrays.binarySearch(includeIndexes, i);
if (searchIdx < 0) {
colSpecs[i] = inSpec.getColumnSpec(i);
} else {
colSpecs[i] = newColumnSpecs[searchIdx];
}
}
final Config config = m_internals.getConfig();
config.addBoolean("hasIterated", false);
for (int i = 0; i < inSpec.getNumColumns(); i++) {
config.addDataType("type" + i, colSpecs[i].getType());
config.addString("colname" + i, colSpecs[i].getName());
}
config.addInt("sizeRow", colSpecs.length);
} else {
final DataTableSpec inSpec = rowInput.getDataTableSpec();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells() + includeIndexes.length];
for (int i = 0; i < inSpec.getNumColumns(); i++) {
colSpecs[i] = inSpec.getColumnSpec(i);
}
for (int i = 0; i < newColumnSpecs.length; i++) {
colSpecs[i + inSpec.getNumColumns()] = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
}
final Config config = m_internals.getConfig();
config.addBoolean("hasIterated", false);
for (int i = 0; i < colSpecs.length; i++) {
config.addDataType("type" + i, colSpecs[i].getType());
config.addString("colname" + i, colSpecs[i].getName());
}
config.addInt("sizeRow", colSpecs.length);
}
} else {
m_internals.getConfig().addInt("sizeRow", 0);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return m_internals;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
final RowInput in = (RowInput) inputs[0];
final RowOutput out = (RowOutput) outputs[0];
final DataTableSpec inSpec = in.getDataTableSpec();
final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
DataRow row;
while ((row = in.poll()) != null) {
exec.checkCanceled();
final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
DataCell[] datacells = new DataCell[includeIndexes.length];
for (int i = 0; i < includeIndexes.length; i++) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(newColumnSpecs[i], i, includeIndexes[i]);
datacells[i] = cellFac.getCells(row)[0];
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(dataColSpec, i, includeIndexes[i]);
datacells[i] = cellFac.getCells(row)[0];
}
}
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
out.push(new ReplacedColumnsDataRow(row, datacells, includeIndexes));
} else {
out.push(new AppendedColumnRow(row, datacells));
}
}
in.close();
out.close();
}
};
}
use of org.knime.core.node.config.Config in project knime-core by knime.
the class SettingsModelCalendar method validateSettingsForModel.
/**
* {@inheritDoc}
*/
@Override
protected void validateSettingsForModel(final NodeSettingsRO settings) throws InvalidSettingsException {
// guess nothing else to do here than to check for existence of
// the fields
Config internals = settings.getConfig(m_key);
boolean useDate = internals.getBoolean(KEY_USE_DATE);
boolean useTime = internals.getBoolean(KEY_USE_TIME);
if (!useDate && !useTime) {
throw new InvalidSettingsException("Must use date, time or both!");
}
internals.getBoolean(KEY_USE_MILLIS);
// this probably throws an exception if there is no value for the key
// since every possible long is a valid one (even negatives = in order
// to be able to store dates before 1970)
internals.getLong(KEY_TIME);
}
use of org.knime.core.node.config.Config in project knime-core by knime.
the class NodeContainerProperties method getDescriptors.
/**
* @return see {@link #getPropertyDescriptors()}
*/
protected IPropertyDescriptor[] getDescriptors() {
ArrayList<IPropertyDescriptor> descriptors = new ArrayList<IPropertyDescriptor>();
// iterate through all settings in the config
for (Enumeration<TreeNode> it = m_settings.children(); it.hasMoreElements(); ) {
AbstractConfigEntry prop = (AbstractConfigEntry) it.nextElement();
// the id should be globally unique
String hierID = m_prefix.isEmpty() ? prop.getKey() : m_prefix + CONFIG_SEPARATOR + prop.getKey();
if (prop instanceof Config) {
// sub-config
descriptors.add(new PropertyDescriptor(hierID, prop.getKey()));
} else {
// all settings are displayed as string
String typeName = prop.getType().name().substring(1);
// we don't have a label yet
String label = prop.getKey() + " (" + typeName + ")";
switch(prop.getType()) {
// if cases are changed here, setPropertyValue must be adapted
case xboolean:
case xbyte:
case xchar:
case xdouble:
case xfloat:
case xint:
case xlong:
case xshort:
case xstring:
// editable types
descriptors.add(new TextPropertyDescriptor(hierID, label));
break;
default:
descriptors.add(new PropertyDescriptor(hierID, label));
break;
}
}
}
return descriptors.toArray(new IPropertyDescriptor[descriptors.size()]);
}
use of org.knime.core.node.config.Config in project knime-core by knime.
the class NodeContainerProperties method getPropertyValue.
/**
* {@inheritDoc}
*/
@Override
public Object getPropertyValue(final Object id) {
if (id instanceof String) {
String hierID = (String) id;
// cut off our prefix from the ID
if (!hierID.startsWith(m_prefix)) {
return "ERROR: Unexpected property id: " + hierID + " (while in sub-config " + m_prefix + ")";
}
String ourID = hierID;
if (!m_prefix.isEmpty()) {
assert hierID.charAt(m_prefix.length()) == CONFIG_SEPARATOR.charAt(0);
// + 1 for removing the separator
ourID = hierID.substring(m_prefix.length() + 1);
}
AbstractConfigEntry entry = m_settings.getEntry(ourID);
if (entry instanceof Config) {
return new NodeContainerProperties(getNode(), (Config) entry, m_prefix.isEmpty() ? ourID : m_prefix + CONFIG_SEPARATOR + ourID);
} else {
if (entry == null) {
return "ERROR: No value for key " + ourID;
}
return TokenizerSettings.printableStr(entry.toStringValue());
}
}
return null;
}
Aggregations