Search in sources :

Example 21 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class ExtensionPointTest method testDataCellToJava.

/**
 * Test whether {@link StringCellToIntTestConverterFactory} was correctly registered at the
 * "org.knime.core.DataCellToJavaConverter" extension point and can be used for conversion.
 *
 * @throws Exception
 */
@Test
public void testDataCellToJava() throws Exception {
    final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(StringCell.TYPE, Integer.class).stream().findFirst();
    assertTrue(factory.isPresent());
    final DataCellToJavaConverter<StringValue, Integer> converter = (DataCellToJavaConverter<StringValue, Integer>) factory.get().create();
    assertNotNull(converter);
    final Integer convert = converter.convert(new StringCell("Answer to Life, the Universe, and Everything"));
    assertEquals(convert, new Integer(42));
}
Also used : DataCellToJavaConverter(org.knime.core.data.convert.java.DataCellToJavaConverter) StringCell(org.knime.core.data.def.StringCell) StringValue(org.knime.core.data.StringValue) Test(org.junit.Test)

Example 22 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class CellSplitterByPosCellFactory method getCells.

/**
 * {@inheritDoc}
 */
public DataCell[] getCells(final DataRow row) {
    DataCell[] result = new DataCell[m_splitPoints.length + 1];
    // preset the result to empty strings
    StringCell e = new StringCell("");
    for (int r = 0; r < result.length; r++) {
        result[r] = e;
    }
    if (row.getCell(m_colIdx).isMissing()) {
        // split string is missing - all result cells will be empty
        return result;
    }
    String splitString = ((StringValue) row.getCell(m_colIdx)).getStringValue();
    int lastSplit = 0;
    int s = 0;
    while (s < m_splitPoints.length) {
        int endIdx = m_splitPoints[s];
        if (endIdx > splitString.length()) {
            // string is shorter than the rest of the splits - done.
            break;
        }
        result[s] = new StringCell(splitString.substring(lastSplit, endIdx));
        lastSplit = endIdx;
        s++;
    }
    // put the rest of the string in the last result cell
    if (lastSplit < splitString.length()) {
        result[s] = new StringCell(splitString.substring(lastSplit));
    }
    return result;
}
Also used : StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue)

Example 23 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class ColCombine2NodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) {
    ColumnRearranger result = new ColumnRearranger(spec);
    DataColumnSpec append = new DataColumnSpecCreator(m_newColName, StringCell.TYPE).createSpec();
    final int[] indices = new int[m_included.length];
    int j = 0;
    for (int k = 0; k < spec.getNumColumns() && j < m_included.length; k++) {
        DataColumnSpec cs = spec.getColumnSpec(k);
        if (m_included[j].equals(cs.getName())) {
            indices[j++] = k;
        }
    }
    // ", " -> ","
    // "  " -> "  " (do not let the resulting string be empty)
    // " bla bla " -> "bla bla"
    final String delimTrim = trimDelimString(m_delimString);
    result.append(new SingleCellFactory(append) {

        @Override
        public DataCell getCell(final DataRow row) {
            String[] cellContents = new String[indices.length];
            for (int i = 0; i < indices.length; i++) {
                DataCell c = row.getCell(indices[i]);
                String s = c instanceof StringValue ? ((StringValue) c).getStringValue() : c.toString();
                cellContents[i] = s;
            }
            return new StringCell(handleContent(cellContents, delimTrim));
        }
    });
    return result;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 24 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class CategoryToNumberApplyCellFactory method getCells.

/**
 * {@inheritDoc}
 */
@Override
public DataCell[] getCells(final DataRow row) {
    DataCell cell = row.getCell(m_index);
    if (cell.isMissing()) {
        return m_mapMissingTo;
    }
    String inValue = ((StringValue) cell).getStringValue();
    DataCell[] value = m_categories.get(inValue);
    if (value == null) {
        return m_defaultValue;
    }
    return value;
}
Also used : DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue)

Example 25 with StringValue

use of org.knime.core.data.StringValue in project knime-core by knime.

the class ColumnHeaderInsertNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // init name map
    LinkedHashMap<String, String> dictionaryMap = new LinkedHashMap<String, String>();
    DataTableSpec dataSpec = inData[0].getDataTableSpec();
    for (DataColumnSpec dataCol : dataSpec) {
        dictionaryMap.put(dataCol.getName(), null);
    }
    // read dictionary
    BufferedDataTable dictionaryTable = inData[1];
    DataTableSpec dictionaryTableSpec = dictionaryTable.getDataTableSpec();
    String lookupColumn = m_config.getLookupColumn();
    int lookupColIdx = lookupColumn == null ? -1 : dictionaryTableSpec.findColumnIndex(lookupColumn);
    String valueColumnIdx = m_config.getValueColumn();
    int valueColIndex = dictionaryTableSpec.findColumnIndex(valueColumnIdx);
    int rowIndex = 0;
    final long rowCount = dictionaryTable.size();
    for (DataRow row : dictionaryTable) {
        RowKey key = row.getKey();
        exec.setProgress(rowIndex / (double) rowCount, "Reading dictionary, " + "row \"" + key + "\" (" + rowIndex + "/" + rowCount + ")");
        rowIndex += 1;
        String lookup;
        if (lookupColIdx < 0) {
            lookup = row.getKey().getString();
        } else {
            DataCell c = row.getCell(lookupColIdx);
            lookup = c.isMissing() ? null : ((StringValue) c).getStringValue();
        }
        if (!dictionaryMap.containsKey(lookup)) {
            continue;
        }
        DataCell valueCell = row.getCell(valueColIndex);
        // if missing, assign original column name
        String value = valueCell.isMissing() ? lookup : ((StringValue) valueCell).getStringValue();
        if (dictionaryMap.put(lookup, value) != null) {
            throw new Exception("Multiple occurrences of lookup key \"" + lookup + "\" in dictionary table; consider to remove " + "duplicates using, e.g. the GroupBy node.");
        }
    }
    // check consistency in new column name values
    HashSet<String> uniqNames = new HashSet<String>();
    for (Map.Entry<String, String> e : dictionaryMap.entrySet()) {
        String value = e.getValue();
        if (value == null) {
            if (m_config.isFailIfNoMatch()) {
                throw new Exception("No name assignment for column \"" + e.getKey() + "\" -- set the appropriate option " + "in the configuration dialog to keep the " + "original column name.");
            } else {
                // (try to) keep original name
                value = e.getKey();
            }
        }
        String newName = value;
        int unifier = 1;
        while (!uniqNames.add(newName)) {
            newName = value + " (#" + (unifier++) + ")";
        }
        e.setValue(newName);
    }
    // assign new names
    DataColumnSpec[] cols = new DataColumnSpec[dataSpec.getNumColumns()];
    for (int i = 0; i < cols.length; i++) {
        DataColumnSpec c = dataSpec.getColumnSpec(i);
        DataColumnSpecCreator creator = new DataColumnSpecCreator(c);
        creator.setName(dictionaryMap.get(c.getName()));
        cols[i] = creator.createSpec();
    }
    DataTableSpec outSpec = new DataTableSpec(dataSpec.getName(), cols);
    BufferedDataTable outTable = exec.createSpecReplacerTable(inData[0], outSpec);
    return new BufferedDataTable[] { outTable };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

StringValue (org.knime.core.data.StringValue)33 DataCell (org.knime.core.data.DataCell)25 DataRow (org.knime.core.data.DataRow)22 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)10 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)10 DoubleValue (org.knime.core.data.DoubleValue)8 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)8 ArrayList (java.util.ArrayList)7 DataTableSpec (org.knime.core.data.DataTableSpec)7 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)7 StringCell (org.knime.core.data.def.StringCell)7 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)6 ParseException (java.text.ParseException)5 DataType (org.knime.core.data.DataType)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 PortObject (org.knime.core.node.port.PortObject)5 IOException (java.io.IOException)4 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)4 BitVectorType (org.knime.core.data.vector.bitvector.BitVectorType)3