Search in sources :

Example 6 with AbstractCellFactory

use of org.knime.core.data.container.AbstractCellFactory in project knime-core by knime.

the class ExpandBitVectorNodeModel method createCellFactory.

/**
 * {@inheritDoc}
 */
@Override
protected AbstractCellFactory createCellFactory(final String[] colNames, final DataColumnSpec[] outputColumns, final int inputIndex) {
    return new AbstractCellFactory(outputColumns) {

        @Override
        public DataCell[] getCells(final DataRow row) {
            DataCell[] vs = new DataCell[colNames.length];
            DataCell cell = row.getCell(inputIndex);
            if (cell instanceof BitVectorValue) {
                BitVectorValue bvv = (BitVectorValue) cell;
                int length = Math.min(vs.length, (int) bvv.length());
                for (int i = length; i-- > 0; ) {
                    vs[i] = VALUES[bvv.get(i) ? 1 : 0];
                }
                for (int i = vs.length; i-- > length; ) {
                    vs[i] = DataType.getMissingCell();
                }
            } else {
                for (int i = 0; i < vs.length; i++) {
                    vs[i] = DataType.getMissingCell();
                }
            }
            return vs;
        }
    };
}
Also used : AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) DataCell(org.knime.core.data.DataCell) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue) DataRow(org.knime.core.data.DataRow)

Example 7 with AbstractCellFactory

use of org.knime.core.data.container.AbstractCellFactory in project knime-core by knime.

the class RegexSplitNodeModel method createColumnRearranger.

@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec, final SimpleStreamableOperatorInternals internals) throws InvalidSettingsException {
    AtomicInteger errorCounter = new AtomicInteger();
    if (m_settings == null) {
        throw new InvalidSettingsException("Not configuration available.");
    }
    final int colIndex = spec.findColumnIndex(m_settings.getColumn());
    if (colIndex < 0) {
        throw new InvalidSettingsException("No such column in input table: " + m_settings.getColumn());
    }
    DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
    if (!colSpec.getType().isCompatible(StringValue.class)) {
        throw new InvalidSettingsException("Selected column does not " + "contain strings");
    }
    final Pattern p = m_settings.compile();
    int count = 0;
    String patternS = p.pattern();
    boolean isNextSpecial = false;
    boolean isPreviousAParenthesis = false;
    // escaped parentheses "\(" or non-capturing groups "(?"
    for (int i = 0; i < patternS.length(); i++) {
        switch(patternS.charAt(i)) {
            case '\\':
                isNextSpecial = !isNextSpecial;
                isPreviousAParenthesis = false;
                break;
            case '(':
                count += isNextSpecial ? 0 : 1;
                isPreviousAParenthesis = !isNextSpecial;
                isNextSpecial = false;
                break;
            case '?':
                if (isPreviousAParenthesis) {
                    count -= 1;
                }
            // no break;
            default:
                isNextSpecial = false;
                isPreviousAParenthesis = false;
        }
    }
    final int newColCount = count;
    final DataColumnSpec[] newColSpecs = new DataColumnSpec[count];
    for (int i = 0; i < newColCount; i++) {
        String name = DataTableSpec.getUniqueColumnName(spec, "split_" + i);
        newColSpecs[i] = new DataColumnSpecCreator(name, StringCell.TYPE).createSpec();
    }
    ColumnRearranger rearranger = new ColumnRearranger(spec);
    rearranger.append(new AbstractCellFactory(newColSpecs) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataCell[] getCells(final DataRow row) {
            DataCell[] result = new DataCell[newColCount];
            Arrays.fill(result, DataType.getMissingCell());
            DataCell c = row.getCell(colIndex);
            if (c.isMissing()) {
                return result;
            }
            String s = ((StringValue) c).getStringValue();
            Matcher m = p.matcher(s);
            if (m.matches()) {
                int max = m.groupCount();
                if (m.groupCount() > newColCount) {
                    errorCounter.incrementAndGet();
                    max = newColCount;
                }
                for (int i = 0; i < max; i++) {
                    // group(0) will return the entire string and is not
                    // included in groupCount, see Matcher API for details
                    String str = m.group(i + 1);
                    if (str != null) {
                        // null for optional groups "(...)?"
                        result[i] = new StringCell(str);
                    }
                }
                return result;
            } else {
                errorCounter.incrementAndGet();
                return result;
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void afterProcessing() {
            // propagate error count
            internals.getConfig().addInt(CONFIG_KEY_ERRORCOUNT, errorCounter.get());
        }
    });
    return rearranger;
}
Also used : Pattern(java.util.regex.Pattern) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Matcher(java.util.regex.Matcher) AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue)

Example 8 with AbstractCellFactory

use of org.knime.core.data.container.AbstractCellFactory in project knime-core by knime.

the class ExpandByteVectorNodeModel method createCellFactory.

/**
 * {@inheritDoc}
 */
@Override
protected AbstractCellFactory createCellFactory(final String[] colNames, final DataColumnSpec[] outputColumns, final int inputIndex) {
    return new AbstractCellFactory(outputColumns) {

        @Override
        public DataCell[] getCells(final DataRow row) {
            DataCell[] vs = new DataCell[colNames.length];
            DataCell cell = row.getCell(inputIndex);
            if (cell instanceof ByteVectorValue) {
                ByteVectorValue bvv = (ByteVectorValue) cell;
                int length = Math.min(vs.length, (int) bvv.length());
                for (int i = length; i-- > 0; ) {
                    vs[i] = VALUES[bvv.get(i)];
                }
                for (int i = vs.length; i-- > length; ) {
                    vs[i] = DataType.getMissingCell();
                }
            } else {
                for (int i = 0; i < vs.length; i++) {
                    vs[i] = DataType.getMissingCell();
                }
            }
            return vs;
        }
    };
}
Also used : AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) DataCell(org.knime.core.data.DataCell) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) DataRow(org.knime.core.data.DataRow)

Example 9 with AbstractCellFactory

use of org.knime.core.data.container.AbstractCellFactory in project knime-core by knime.

the class AppendVariableToTable2NodeModel method createColumnRearranger.

@Override
protected 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 = 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();
        final 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;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType) HashSet(java.util.HashSet) Pair(org.knime.core.util.Pair) AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) PortType(org.knime.core.node.port.PortType) DataType(org.knime.core.data.DataType) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) NoSuchElementException(java.util.NoSuchElementException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Aggregations

AbstractCellFactory (org.knime.core.data.container.AbstractCellFactory)9 DataCell (org.knime.core.data.DataCell)8 DataRow (org.knime.core.data.DataRow)8 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)7 DataColumnSpec (org.knime.core.data.DataColumnSpec)6 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)5 DataType (org.knime.core.data.DataType)4 StringCell (org.knime.core.data.def.StringCell)4 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 DoubleCell (org.knime.core.data.def.DoubleCell)3 IntCell (org.knime.core.data.def.IntCell)3 Pair (org.knime.core.util.Pair)3 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1