Search in sources :

Example 91 with StringCell

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataTableSpec(org.knime.core.data.DataTableSpec) DataContainer(org.knime.core.data.container.DataContainer) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 92 with StringCell

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());
    }
}
Also used : StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) IntCell(org.knime.core.data.def.IntCell)

Example 93 with StringCell

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()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TokenizerSettings(org.knime.core.util.tokenizer.TokenizerSettings) StringCell(org.knime.core.data.def.StringCell) StringReader(java.io.StringReader) DataCell(org.knime.core.data.DataCell) Tokenizer(org.knime.core.util.tokenizer.Tokenizer)

Example 94 with StringCell

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));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataTableSpec(org.knime.core.data.DataTableSpec) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 95 with StringCell

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 };
}
Also used : DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) DataContainer(org.knime.core.data.container.DataContainer) StringCell(org.knime.core.data.def.StringCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DefaultRow(org.knime.core.data.def.DefaultRow) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) PortObject(org.knime.core.node.port.PortObject)

Aggregations

StringCell (org.knime.core.data.def.StringCell)176 DataCell (org.knime.core.data.DataCell)130 DoubleCell (org.knime.core.data.def.DoubleCell)67 DefaultRow (org.knime.core.data.def.DefaultRow)65 IntCell (org.knime.core.data.def.IntCell)55 DataRow (org.knime.core.data.DataRow)52 DataTableSpec (org.knime.core.data.DataTableSpec)49 ArrayList (java.util.ArrayList)41 DataColumnSpec (org.knime.core.data.DataColumnSpec)37 RowKey (org.knime.core.data.RowKey)36 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)26 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)26 DataType (org.knime.core.data.DataType)22 LinkedHashSet (java.util.LinkedHashSet)21 BufferedDataTable (org.knime.core.node.BufferedDataTable)20 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)19 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)16 LinkedHashMap (java.util.LinkedHashMap)15 Test (org.junit.Test)15 HashMap (java.util.HashMap)11