use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class DataColumnPropertiesView method createPropsTable.
private DataTable createPropsTable(final DataTableSpec tableSpec) {
// output has as many cols
int numOfCols = tableSpec.getNumColumns();
String[] colNames = new String[numOfCols];
DataType[] colTypes = new DataType[numOfCols];
// colnames are the same as incoming, types are all StringTypes
for (int c = 0; c < numOfCols; c++) {
colNames[c] = tableSpec.getColumnSpec(c).getName();
colTypes[c] = StringCell.TYPE;
}
// get keys for ALL props in the table. Each will show in one row.
HashSet<String> allKeys = new LinkedHashSet<String>();
for (int c = 0; c < numOfCols; c++) {
Enumeration<String> props = tableSpec.getColumnSpec(c).getProperties().properties();
while (props.hasMoreElements()) {
allKeys.add(props.nextElement());
}
}
DataContainer result = new DataContainer(new DataTableSpec(colNames, colTypes));
// now construct the rows we wanna display
for (String key : allKeys) {
DataCell[] cells = new DataCell[numOfCols];
for (int c = 0; c < numOfCols; c++) {
String cellValue = "";
if (tableSpec.getColumnSpec(c).getProperties().containsProperty(key)) {
cellValue = tableSpec.getColumnSpec(c).getProperties().getProperty(key);
}
cells[c] = new StringCell(cellValue);
}
result.addRowToTable(new DefaultRow(key, cells));
}
result.close();
return result.getTable();
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class ARFFRowIterator method createNewDataCellOfType.
/*
* The function creates a default <code> DataCell </code> of a type
* depending on the <code> type </code> passed in, and initializes the value
* of this data cell from the <code> data </code> string (converting the
* string to the corresponding type). It will create a missing cell and
* print a warning if it couldn't convert the string into the appropreate
* format (to int or double). It throws a <code> IllegalStateException
* </code> if the <code> type </code> passed in is not supported. @param
* type Specifies the type of DataCell that is to be created, supported are
* DoubleCell, IntCell, and StringCell. @param data the string
* representation of the value that will be set in the DataCell created. It
* gets trimmed before it's converted into a number. @param
* createMissingCell If set true the default ' <code> missing </code> '
* value of that cell type will be set indicating that the data in that cell
* was not specified. The <code> data </code> parameter is ignored then.
*
* @return <code> DataCell </code> of the type specified in <code> type
* </code> .
*/
private DataCell createNewDataCellOfType(final DataType type, final String data, final boolean createMissingCell) {
if (type.equals(StringCell.TYPE)) {
if (createMissingCell) {
return DataType.getMissingCell();
} else {
return new StringCell(data);
}
} else if (type.equals(IntCell.TYPE)) {
if (createMissingCell) {
return DataType.getMissingCell();
} else {
try {
int val = Integer.parseInt(data.trim());
return new IntCell(val);
} catch (NumberFormatException nfe) {
if (m_numMsgWrongFormat < MAX_ERR_MSG) {
LOGGER.warn("ARFF reader WARNING: Wrong data " + "format. In line " + m_tokenizer.getLineNumber() + " read '" + data + "' for an integer.");
LOGGER.warn(" Creating missing cell for it.");
m_numMsgWrongFormat++;
}
if (m_numMsgWrongFormat == MAX_ERR_MSG) {
LOGGER.warn(" (last message of " + "this kind.)");
m_numMsgWrongFormat++;
}
return DataType.getMissingCell();
}
}
} else if (type.equals(DoubleCell.TYPE)) {
if (createMissingCell) {
return DataType.getMissingCell();
} else {
try {
double val = Double.parseDouble(data.trim());
return new DoubleCell(val);
} catch (NumberFormatException nfe) {
if (m_numMsgWrongFormat < MAX_ERR_MSG) {
LOGGER.warn("ARFF reader WARNING: Wrong data " + "format. In line " + m_tokenizer.getLineNumber() + " read '" + data + "' for a floating point.");
LOGGER.warn(" Creating missing cell for it.");
m_numMsgWrongFormat++;
}
if (m_numMsgWrongFormat == MAX_ERR_MSG) {
m_numMsgWrongFormat++;
LOGGER.warn(" (last message of this kind.)");
}
return DataType.getMissingCell();
}
}
} else {
throw new IllegalStateException("Cannot create DataCell of type" + type.toString());
}
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class ARFFTable method extractNominalVals.
/*
* expects the list of nominal values (in curely braces and comma separated)
* from the "@attribute" line to be next in the tokenizer (including the
* beginning of the list with the iopening brace). Will return an array of
* StringsCells with the different values extracted (and removed) from the
* tokenizer. It will leave the EOL at the end of the list in the tokenizer.
* Pass in also file name for nice error messages.
*/
private static DataCell[] extractNominalVals(final String valList, final String fileName, final int lineNo) throws InvalidSettingsException {
Collection<DataCell> vals = new LinkedHashSet<DataCell>();
// we must support quotes and stuff - let's use another tokenizer.
StringReader strReader = new StringReader(valList);
Tokenizer tokizer = new Tokenizer(strReader);
TokenizerSettings tokSets = new TokenizerSettings();
tokSets.addDelimiterPattern(",", false, false, false);
tokSets.addQuotePattern("'", "'");
tokSets.addQuotePattern("\"", "\"");
tokizer.setSettings(tokSets);
for (String val = tokizer.nextToken(); val != null; val = tokizer.nextToken()) {
String newval = val;
// trimm off any whitespaces.
if (!tokizer.lastTokenWasQuoted()) {
newval = val.trim();
}
// make sure we don't add the same value twice.
StringCell newValCell = new StringCell(newval);
if (!vals.contains(newValCell)) {
vals.add(newValCell);
} else {
LOGGER.warn("ARFF reader WARNING: The list of nominal " + "values in the header of file '" + fileName + "' line " + lineNo + " contains the value '" + newval + "' twice. Ignoring one appearance.");
}
}
return vals.toArray(new DataCell[vals.size()]);
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class DatabaseLoopingNodeModel method aggregate.
private void aggregate(final DataTable table, final MutableInteger rowCnt, final BufferedDataContainer buf, final DataCell gridValue) {
final DataTableSpec spec = table.getDataTableSpec();
@SuppressWarnings("unchecked") Set<DataCell>[] values = new LinkedHashSet[spec.getNumColumns()];
for (final DataRow resRow : table) {
for (int i = 0; i < values.length; i++) {
if (values[i] == null) {
values[i] = new LinkedHashSet<>(1);
}
values[i].add(resRow.getCell(i));
}
}
DataCell[] cells;
if (m_appendGridColumn.getBooleanValue()) {
cells = new DataCell[values.length + 1];
cells[cells.length - 1] = gridValue;
} else {
cells = new DataCell[values.length];
}
for (int i = 0; i < values.length; i++) {
if (values[i] == null) {
cells[i] = DataType.getMissingCell();
} else {
StringBuilder builder = new StringBuilder();
for (DataCell cell : values[i]) {
if (builder.length() > 0) {
builder.append(",");
}
builder.append(cell.toString());
}
cells[i] = new StringCell(builder.toString());
}
}
rowCnt.inc();
final RowKey rowKey = RowKey.createRowKey(rowCnt.intValue());
buf.addRowToTable(new DefaultRow(rowKey, cells));
}
use of org.knime.core.data.def.StringCell in project knime-core by knime.
the class SQLExtractNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
DatabasePortObject dbIn = (DatabasePortObject) inData[0];
String query = dbIn.getConnectionSettings(getCredentialsProvider()).getQuery();
final String flowVarName = m_flowVariableName.getStringValue();
pushFlowVariableString(flowVarName, query);
// Create a table with the SQL query in a StringCell
DataContainer container = exec.createDataContainer(createSpec(flowVarName));
container.addRowToTable(new DefaultRow(RowKey.createRowKey(0), new StringCell(query)));
container.close();
BufferedDataTable outTable = (BufferedDataTable) container.getTable();
return new PortObject[] { FlowVariablePortObject.INSTANCE, outTable };
}
Aggregations