Search in sources :

Example 16 with StringValue

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

the class TableColumnToVariableNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    if (inData[0] instanceof BufferedDataTable) {
        final BufferedDataTable table = (BufferedDataTable) inData[0];
        final int colIndex = table.getSpec().findColumnIndex(m_column.getStringValue());
        assert colIndex >= 0 : colIndex;
        for (DataRow dataRow : table) {
            DataCell cell = dataRow.getCell(colIndex);
            if (cell.isMissing()) {
                if (m_ignoreMissing.getBooleanValue()) {
                    continue;
                }
                throw new Exception("Missing value in column (" + m_column.getColumnName() + ") in row: " + dataRow.getKey());
            }
            if (cell instanceof IntValue) {
                final IntValue iv = (IntValue) cell;
                pushFlowVariableInt(dataRow.getKey().getString(), iv.getIntValue());
            } else if (cell instanceof DoubleValue) {
                final DoubleValue dv = (DoubleValue) cell;
                pushFlowVariableDouble(dataRow.getKey().getString(), dv.getDoubleValue());
            } else if (cell instanceof StringValue) {
                final StringValue sv = (StringValue) cell;
                pushFlowVariableString(dataRow.getKey().getString(), sv.getStringValue());
            }
        }
    }
    return new FlowVariablePortObject[] { FlowVariablePortObject.INSTANCE };
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) DataRow(org.knime.core.data.DataRow) IntValue(org.knime.core.data.IntValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 17 with StringValue

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

the class BitVectorGeneratorNodeModel method scanMaxPos.

private int scanMaxPos(final BufferedDataTable data, final ExecutionMonitor exec) {
    int maxPos = Integer.MIN_VALUE;
    int cellIdx = data.getDataTableSpec().findColumnIndex(m_stringColumn.getStringValue());
    long nrRows = data.size();
    long currRow = 0;
    for (DataRow row : data) {
        currRow++;
        exec.setProgress((double) currRow / (double) nrRows, "scanning row " + currRow);
        DataCell cell = row.getCell(cellIdx);
        if (cell.isMissing()) {
            continue;
        }
        if (!cell.getType().isCompatible(StringValue.class)) {
            throw new RuntimeException("Found incompatible type in row " + row.getKey().getString());
        }
        String toParse = ((StringValue) cell).getStringValue();
        String[] numbers = toParse.split("\\s");
        for (int i = 0; i < numbers.length; i++) {
            int pos = -1;
            try {
                pos = Integer.parseInt(numbers[i].trim());
                maxPos = Math.max(maxPos, pos);
            } catch (NumberFormatException nfe) {
            // nothing to do here
            // same exception will be logged from cell factory
            }
        }
    }
    return maxPos + 1;
}
Also used : DataCell(org.knime.core.data.DataCell) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StringValue(org.knime.core.data.StringValue) DataRow(org.knime.core.data.DataRow)

Example 18 with StringValue

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

the class IdString2BitVectorCellFactory method getCell.

/**
 * {@inheritDoc}
 */
@Override
public DataCell getCell(final DataRow row) {
    incrementNrOfRows();
    if (!row.getCell(getColumnIndex()).getType().isCompatible(StringValue.class)) {
        printError(LOGGER, row, "Cell in column " + getColumnIndex() + " is not a string value!");
        return DataType.getMissingCell();
    }
    if (row.getCell(getColumnIndex()).isMissing()) {
        return DataType.getMissingCell();
    }
    String toParse = ((StringValue) row.getCell(getColumnIndex())).getStringValue().trim();
    toParse = toParse.trim();
    try {
        int newlySetBits = 0;
        final BitVectorType type = getVectorType();
        final BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(m_maxPos);
        if (!toParse.isEmpty()) {
            final String[] numbers = toParse.split("\\s");
            for (int i = 0; i < numbers.length; i++) {
                int pos = Integer.parseInt(numbers[i].trim());
                if (pos < 0) {
                    printError(LOGGER, row, "Invalid negative index in index string: " + toParse);
                    return DataType.getMissingCell();
                }
                if (!factory.get(pos)) {
                    factory.set(pos);
                    newlySetBits++;
                }
            }
        }
        m_nrOfSetBits += newlySetBits;
        return factory.createDataCell();
    } catch (NumberFormatException nfe) {
        String nfeMsg = nfe.getMessage();
        if (nfeMsg == null) {
            nfeMsg = "<sorry, no further details>";
        }
        printError(LOGGER, row, "Unable to convert \"" + toParse + "\" to " + "bit vector: " + nfeMsg);
        return DataType.getMissingCell();
    }
}
Also used : BitVectorType(org.knime.core.data.vector.bitvector.BitVectorType) StringValue(org.knime.core.data.StringValue)

Example 19 with StringValue

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

the class BitString2BitVectorCellFactory method getCell.

/**
 * {@inheritDoc}
 */
@Override
public DataCell getCell(final DataRow row) {
    incrementNrOfRows();
    String bitString;
    DataCell cell = row.getCell(getColumnIndex());
    if (cell.isMissing()) {
        return DataType.getMissingCell();
    }
    if (!cell.getType().isCompatible(StringValue.class)) {
        printError(LOGGER, row, "Cell in column " + getColumnIndex() + " is not of type string.");
        return DataType.getMissingCell();
    }
    bitString = ((StringValue) cell).getStringValue().trim();
    final BitVectorType type = getVectorType();
    final BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(bitString.length());
    int pos = 0;
    int numberOf0s = 0;
    int numberOf1s = 0;
    for (int i = bitString.length() - 1; i >= 0; i--) {
        char c = bitString.charAt(i);
        if (c == '0') {
            pos++;
            numberOf0s++;
        } else if (c == '1') {
            factory.set(pos++);
            numberOf1s++;
        } else {
            printError(LOGGER, row, "Invalid character ('" + c + "') in bitvector string");
            return DataType.getMissingCell();
        }
    }
    m_nrOfNotSetBits += numberOf0s;
    m_nrOfSetBits += numberOf1s;
    return factory.createDataCell();
}
Also used : DataCell(org.knime.core.data.DataCell) BitVectorType(org.knime.core.data.vector.bitvector.BitVectorType) StringValue(org.knime.core.data.StringValue)

Example 20 with StringValue

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

the class Hex2BitVectorCellFactory method getCell.

/**
 * {@inheritDoc}
 */
@Override
public DataCell getCell(final DataRow row) {
    incrementNrOfRows();
    final DataCell old = row.getCell(getColumnIndex());
    if (old.isMissing()) {
        return DataType.getMissingCell();
    }
    if (old instanceof StringValue) {
        String val = ((StringValue) old).getStringValue();
        DataCell newCell;
        try {
            String hexString = val.trim();
            BitVectorType type = getVectorType();
            BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(hexString);
            // hopefully int does it
            int card = (int) factory.cardinality();
            m_nrOfSetBits += card;
            m_nrOfNotSetBits += factory.length() - card;
            newCell = factory.createDataCell();
        } catch (IllegalArgumentException nfe) {
            String nfeMsg = nfe.getMessage();
            if (nfeMsg == null) {
                nfeMsg = "<sorry, no further details>";
            }
            printError(LOGGER, row, "Unable to convert \"" + val + "\" to bit vector: " + nfeMsg);
            newCell = DataType.getMissingCell();
        }
        return newCell;
    } else {
        printError(LOGGER, row, "Unable to convert \"" + old + "\" to bit vector, not a string value cell.");
        return DataType.getMissingCell();
    }
}
Also used : DataCell(org.knime.core.data.DataCell) BitVectorType(org.knime.core.data.vector.bitvector.BitVectorType) StringValue(org.knime.core.data.StringValue)

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