use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class ReadPNGFromURLNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final DataTableSpec in, final AtomicLong failCounter) throws InvalidSettingsException {
String colName = m_config.getUrlColName();
if (colName == null) {
// throws ISE
m_config.guessDefaults(in);
colName = m_config.getUrlColName();
setWarningMessage("Auto-configuration: Guessing column \"" + colName + "\" to contain locations");
}
final int colIndex = in.findColumnIndex(colName);
if (colIndex < 0) {
throw new InvalidSettingsException("No such column in input: " + colName);
}
DataColumnSpec colSpec = in.getColumnSpec(colIndex);
if (!colSpec.getType().isCompatible(StringValue.class)) {
throw new InvalidSettingsException("Selected column \"" + colName + "\" is not string-compatible");
}
final String newColName = m_config.getNewColumnName();
DataColumnSpecCreator colSpecCreator;
if (newColName != null) {
String newName = DataTableSpec.getUniqueColumnName(in, newColName);
colSpecCreator = new DataColumnSpecCreator(newName, PNGImageContent.TYPE);
} else {
colSpecCreator = new DataColumnSpecCreator(colSpec);
colSpecCreator.setType(PNGImageContent.TYPE);
colSpecCreator.removeAllHandlers();
colSpecCreator.setDomain(null);
}
DataColumnSpec outColumnSpec = colSpecCreator.createSpec();
ColumnRearranger rearranger = new ColumnRearranger(in);
CellFactory fac = new SingleCellFactory(outColumnSpec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIndex);
if (cell.isMissing()) {
return DataType.getMissingCell();
} else {
String url = ((StringValue) cell).getStringValue();
try {
return toPNGCell(url);
} catch (Exception e) {
if (m_config.isFailOnInvalid()) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e.getMessage(), e);
}
} else {
String message = "Failed to read png content from " + "\"" + url + "\": " + e.getMessage();
LOGGER.warn(message, e);
failCounter.incrementAndGet();
return DataType.getMissingCell();
}
}
}
}
};
if (newColName == null) {
rearranger.replace(fac, colIndex);
} else {
rearranger.append(fac);
}
return rearranger;
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class AppendVariableToTableNodeModel method createColumnRearranger.
private 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;
if (m_settings.getIncludeAll()) {
vars = getAllVariables();
} else {
vars = m_settings.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();
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;
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class VariableToTableNodeModel method createOutSpec.
private DataTableSpec createOutSpec() throws InvalidSettingsException {
List<Pair<String, FlowVariable.Type>> vars;
if (m_settings.getIncludeAll()) {
vars = getAllVariables();
} else {
vars = m_settings.getVariablesOfInterest();
}
if (vars.isEmpty()) {
throw new InvalidSettingsException("No variables selected");
}
DataColumnSpec[] specs = new DataColumnSpec[vars.size()];
for (int i = 0; i < vars.size(); i++) {
Pair<String, FlowVariable.Type> c = vars.get(i);
DataType type;
switch(c.getSecond()) {
case DOUBLE:
type = DoubleCell.TYPE;
break;
case INTEGER:
type = IntCell.TYPE;
break;
case STRING:
type = StringCell.TYPE;
break;
default:
throw new InvalidSettingsException("Unsupported variable type: " + c.getSecond());
}
specs[i] = new DataColumnSpecCreator(c.getFirst(), type).createSpec();
}
return new DataTableSpec(specs);
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class ReadPNGFromURLConfig method loadInDialog.
/**
* Load config in dialog.
* @param settings To load from
* @param in Current input spec
* @throws NotConfigurableException If no configuration possible, e.g.
*/
void loadInDialog(final NodeSettingsRO settings, final DataTableSpec in) throws NotConfigurableException {
m_urlColName = settings.getString("urlColumn", null);
DataColumnSpec col = in.getColumnSpec(m_urlColName);
if (col == null || !col.getType().isCompatible(StringValue.class)) {
try {
guessDefaults(in);
} catch (InvalidSettingsException e) {
throw new NotConfigurableException("No valid input column available");
}
}
// guessDefaults inits default values, so we can use them as fallback
m_failOnInvalid = settings.getBoolean("failIfInvalid", m_failOnInvalid);
m_newColumnName = settings.getString("newColumnName", m_newColumnName);
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class DBGroupByNodeModel method createOutSpec.
/**
* @param inSpec Spec of the input table
* @param checkRetrieveMetadata
* @return Spec of the output table
* @throws InvalidSettingsException if settings do not match the input specification
*/
private DataTableSpec createOutSpec(final DataTableSpec inSpec, final DatabaseConnectionSettings settings, final String query, final boolean ignoreExceptions) throws InvalidSettingsException {
// Try get spec from database
try {
DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(settings, query);
DatabaseReaderConnection conn = new DatabaseReaderConnection(querySettings);
return conn.getDataTableSpec(getCredentialsProvider());
} catch (SQLException e) {
NodeLogger.getLogger(getClass()).info("Could not determine table spec from database, trying to guess now", e);
if (!ignoreExceptions) {
throw new InvalidSettingsException("Error in automatically build sql statement: " + e.getMessage());
}
// Otherwise guess spec
}
List<DataColumnSpec> colSpecs = new ArrayList<>();
// Add all group by columns
for (String col : m_groupByCols.getIncludeList()) {
colSpecs.add(inSpec.getColumnSpec(col));
}
// Add aggregated columns
for (int i = 0; i < m_aggregatedColumns.length; i++) {
String col = m_aggregatedColumns[i];
String method = m_aggregationMethods[i];
if (inSpec.getColumnSpec(col) == null) {
throw new InvalidSettingsException("Column '" + col + "' in aggregation " + method + " does not exist");
}
final DatabaseUtility databaseUtility = settings.getUtility();
final DBAggregationFunction function = databaseUtility.getAggregationFunction(method);
// Get type of column after aggregation
DataType type = function.getType(inSpec.getColumnSpec(col).getType());
colSpecs.add(new DataColumnSpecCreator(generateColumnName(col, method), type).createSpec());
}
return new DataTableSpec(colSpecs.toArray(new DataColumnSpec[colSpecs.size()]));
}
Aggregations