Search in sources :

Example 21 with SingleCellFactory

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

the class CellReplacerNodeModel method createColumnRearranger.

private ColumnRearranger createColumnRearranger(final DataTableSpec spec, final DataTableSpec dictSpec, final BufferedDataTable dictTable, final ExecutionMonitor dictionaryInitExec) throws InvalidSettingsException {
    String targetCol = m_targetColModel.getStringValue();
    if (targetCol == null || targetCol.length() == 0) {
        throw new InvalidSettingsException("No target column selected");
    }
    final int targetColIndex = spec.findColumnIndex(targetCol);
    if (targetColIndex < 0) {
        throw new InvalidSettingsException("No such column \"" + targetCol + "\"");
    }
    final DataColumnSpec targetColSpec = spec.getColumnSpec(targetColIndex);
    final int dictInputColIndex = dictSpec.findColumnIndex(m_dictInputColModel.getStringValue());
    final boolean dictInputIsCollection;
    if (m_dictInputColModel.useRowID()) {
        dictInputIsCollection = false;
    } else if (dictInputColIndex < 0) {
        throw new InvalidSettingsException("No such column \"" + m_dictInputColModel.getStringValue() + "\"");
    } else {
        DataColumnSpec inS = dictSpec.getColumnSpec(dictInputColIndex);
        dictInputIsCollection = inS.getType().isCollectionType();
    }
    final int dictOutputColIndex = dictSpec.findColumnIndex(m_dictOutputColModel.getStringValue());
    final DataType dictOutputColType;
    if (m_dictOutputColModel.useRowID()) {
        dictOutputColType = StringCell.TYPE;
    } else {
        if (dictOutputColIndex < 0) {
            throw new InvalidSettingsException("No such column \"" + m_dictOutputColModel.getStringValue() + "\"");
        }
        dictOutputColType = dictSpec.getColumnSpec(dictOutputColIndex).getType();
    }
    final NoMatchPolicy noMatchPolicy = getNoMatchPolicy();
    DataType outputType;
    switch(noMatchPolicy) {
        case Input:
            outputType = DataType.getCommonSuperType(dictOutputColType, targetColSpec.getType());
            break;
        default:
            outputType = dictOutputColType;
    }
    String newColName;
    if (m_appendColumnModel.getBooleanValue()) {
        String newName = m_appendColumnNameModel.getStringValue();
        if (newName == null || newName.length() == 0) {
            throw new InvalidSettingsException("No new column name given");
        }
        newColName = DataTableSpec.getUniqueColumnName(spec, newName);
    } else {
        newColName = targetColSpec.getName();
    }
    DataColumnSpecCreator replaceSpecCreator = new DataColumnSpecCreator(newColName, outputType);
    CellFactory c = new SingleCellFactory(replaceSpecCreator.createSpec()) {

        private Map<DataCell, DataCell> m_dictionaryMap;

        @Override
        public DataCell getCell(final DataRow row) {
            try {
                ensureInitDictionaryMap();
            } catch (CanceledExecutionException e) {
                // cancellation done by the framework
                return DataType.getMissingCell();
            }
            DataCell cell = row.getCell(targetColIndex);
            DataCell output = m_dictionaryMap.get(cell);
            if (output == null) {
                switch(noMatchPolicy) {
                    case Input:
                        return cell;
                    default:
                        return DataType.getMissingCell();
                }
            }
            return output;
        }

        private void ensureInitDictionaryMap() throws CanceledExecutionException {
            if (m_dictionaryMap == null) {
                m_dictionaryMap = new HashMap<DataCell, DataCell>();
                int i = 0;
                double rowCount = dictTable.size();
                for (DataRow r : dictTable) {
                    dictionaryInitExec.setProgress((i++) / rowCount, "Reading dictionary into memory, row " + i);
                    dictionaryInitExec.checkCanceled();
                    DataCell output = dictOutputColIndex < 0 ? new StringCell(r.getKey().getString()) : r.getCell(dictOutputColIndex);
                    DataCell input = dictInputColIndex < 0 ? new StringCell(r.getKey().getString()) : r.getCell(dictInputColIndex);
                    if (input.isMissing()) {
                        addSearchPair(input, output);
                    } else if (dictInputIsCollection) {
                        CollectionDataValue v = (CollectionDataValue) input;
                        for (DataCell element : v) {
                            addSearchPair(element, output);
                        }
                    } else {
                        addSearchPair(input, output);
                    }
                }
            }
        }

        private void addSearchPair(final DataCell input, final DataCell output) {
            if (m_dictionaryMap.put(input, output) != null) {
                setWarningMessage("Duplicate search key \"" + input + "\"");
            }
        }
    };
    ColumnRearranger result = new ColumnRearranger(spec);
    if (m_appendColumnModel.getBooleanValue()) {
        result.append(c);
    } else {
        result.replace(c, targetColIndex);
    }
    return result;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) HashMap(java.util.HashMap) Map(java.util.Map) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue)

Example 22 with SingleCellFactory

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

the class ColumnComparatorNodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
    final ComparatorMethod method = ComparatorMethod.getMethod(m_operator.getStringValue());
    final int idx1 = spec.findColumnIndex(m_firstColumn.getStringValue());
    final int idx2 = spec.findColumnIndex(m_secondColumn.getStringValue());
    DataColumnSpec leftSpec = spec.getColumnSpec(idx1);
    DataColumnSpec rightSpec = spec.getColumnSpec(idx2);
    ColumnRearranger colRe = new ColumnRearranger(spec);
    colRe.append(new SingleCellFactory(createSpec(leftSpec, rightSpec)) {

        private final StringCell m_matchRepl = new StringCell(m_matchValue.getStringValue());

        private final StringCell m_mismatchRepl = new StringCell(m_mismatchValue.getStringValue());

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell1 = row.getCell(idx1);
            DataCell cell2 = row.getCell(idx2);
            if (method.compare(cell1, cell2)) {
                String strMatch = m_matchOption.getStringValue();
                if (strMatch.equals(REPL_OPTIONS[0])) {
                    return covertMatch(cell1);
                } else if (strMatch.equals(REPL_OPTIONS[1])) {
                    return covertMatch(cell2);
                } else if (strMatch.equals(REPL_OPTIONS[2])) {
                    return DataType.getMissingCell();
                } else {
                    return m_matchRepl;
                }
            } else {
                String strMismatch = m_mismatchOption.getStringValue();
                if (strMismatch.equals(REPL_OPTIONS[0])) {
                    return covertMismatch(cell1);
                } else if (strMismatch.equals(REPL_OPTIONS[1])) {
                    return covertMismatch(cell2);
                } else if (strMismatch.equals(REPL_OPTIONS[2])) {
                    return DataType.getMissingCell();
                } else {
                    return m_mismatchRepl;
                }
            }
        }
    });
    return colRe;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) ColumnComparatorNodeDialogPane.createComparatorMethod(org.knime.base.node.preproc.colcompare.ColumnComparatorNodeDialogPane.createComparatorMethod) ComparatorMethod(org.knime.base.node.preproc.colcompare.ColumnComparatorNodeDialogPane.ComparatorMethod) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 23 with SingleCellFactory

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

the class XML2PMMLNodeModel method createColRearranger.

private ColumnRearranger createColRearranger(final DataTableSpec spec) throws InvalidSettingsException {
    if (m_xmlColumnName.getStringValue() == null) {
        guessDefaultXMLColumn(spec);
    }
    String xmlColName = m_xmlColumnName.getStringValue();
    String newColName = m_newColumnName.getStringValue();
    final int colIndex = spec.findColumnIndex(xmlColName);
    CheckUtils.checkSetting(colIndex >= 0, "Column: '%s' does not exist anymore.", xmlColName);
    final DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
    CheckUtils.checkSetting(colSpec.getType().isCompatible(StringValue.class), "Selected column '%s' is not string/xml-compatible", xmlColName);
    DataColumnSpecCreator colSpecCreator;
    if (newColName != null && !m_replaceColumn.getBooleanValue()) {
        String newName = DataTableSpec.getUniqueColumnName(spec, newColName);
        colSpecCreator = new DataColumnSpecCreator(newName, PMMLCell.TYPE);
    } else {
        colSpecCreator = new DataColumnSpecCreator(colSpec);
        colSpecCreator.setType(PMMLCell.TYPE);
        colSpecCreator.removeAllHandlers();
        colSpecCreator.setDomain(null);
    }
    DataColumnSpec outColumnSpec = colSpecCreator.createSpec();
    ColumnRearranger rearranger = new ColumnRearranger(spec);
    CellFactory fac = new SingleCellFactory(outColumnSpec) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell = row.getCell(colIndex);
            if (cell.isMissing()) {
                return DataType.getMissingCell();
            } else {
                PMMLDocument pmmlDoc = null;
                String failure = null;
                XmlObject xmlDoc;
                try (LockedSupplier<Document> supplier = ((XMLValue<Document>) cell).getDocumentSupplier()) {
                    xmlDoc = XmlObject.Factory.parse(supplier.get().cloneNode(true));
                    if (xmlDoc instanceof PMMLDocument) {
                        pmmlDoc = (PMMLDocument) xmlDoc;
                    } else if (PMMLUtils.isOldKNIMEPMML(xmlDoc) || PMMLUtils.is4_1PMML(xmlDoc)) {
                        String updatedPMML = PMMLUtils.getUpdatedVersionAndNamespace(xmlDoc);
                        /* Parse the modified document and assign it to a
                                 * PMMLDocument.*/
                        pmmlDoc = PMMLDocument.Factory.parse(updatedPMML);
                    } else {
                        failure = "No valid PMML v 3.x/4.0/4.1 document";
                    }
                } catch (XmlException e) {
                    if (!m_failOnInvalid.getBooleanValue()) {
                        LOGGER.error("Invalid PMML in row " + row.getKey() + ": " + e.getMessage(), e);
                    }
                    failure = e.getMessage();
                }
                if (failure != null) {
                    m_failCounter.incrementAndGet();
                    if (m_failOnInvalid.getBooleanValue()) {
                        throw new RuntimeException("Invalid PMML in row " + row.getKey() + ": " + failure);
                    } else {
                        return new MissingCell(failure);
                    }
                } else {
                    try {
                        return PMMLCellFactory.create(pmmlDoc.toString());
                    } catch (Exception e) {
                        return new MissingCell(e.getMessage());
                    }
                }
            }
        }
    };
    if (m_replaceColumn.getBooleanValue()) {
        rearranger.replace(fac, colIndex);
    } else {
        rearranger.append(fac);
    }
    return rearranger;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PMMLDocument(org.dmg.pmml.PMMLDocument) Document(org.w3c.dom.Document) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) MissingCell(org.knime.core.data.MissingCell) XmlException(org.apache.xmlbeans.XmlException) DataCell(org.knime.core.data.DataCell) XmlObject(org.apache.xmlbeans.XmlObject) PMMLDocument(org.dmg.pmml.PMMLDocument) XMLValue(org.knime.core.data.xml.XMLValue) StringValue(org.knime.core.data.StringValue) PMMLCellFactory(org.knime.core.data.xml.PMMLCellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 24 with SingleCellFactory

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

the class GroupByTable method appendOrderColumn.

/**
 * @param exec the {@link ExecutionContext}
 * @param dataTable the {@link BufferedDataTable} to add the order column to
 * @param workingCols the names of all columns needed for grouping
 * @param retainOrderCol the name of the order column
 * @return the given table with the appended order column
 * @throws CanceledExecutionException if the operation has been canceled
 */
public static BufferedDataTable appendOrderColumn(final ExecutionContext exec, final BufferedDataTable dataTable, final Set<String> workingCols, final String retainOrderCol) throws CanceledExecutionException {
    final ColumnRearranger rearranger = new ColumnRearranger(dataTable.getSpec());
    rearranger.append(new SingleCellFactory(new DataColumnSpecCreator(retainOrderCol, IntCell.TYPE).createSpec()) {

        private int m_id = 0;

        @Override
        public DataCell getCell(final DataRow row) {
            return new IntCell(m_id++);
        }
    });
    final String[] workingColsArray = workingCols.toArray(new String[0]);
    rearranger.keepOnly(workingColsArray);
    rearranger.permute(workingColsArray);
    return exec.createColumnRearrangeTable(dataTable, rearranger, exec);
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell)

Example 25 with SingleCellFactory

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

the class ConstantValueColumnNodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec in) throws InvalidSettingsException {
    final String value = m_config.getValue();
    checkSetting(value != null, "Configuration missing.");
    checkSetting(!(m_config.getReplacedColumn() == null && m_config.getNewColumnName() == null), "Either a replacing column or a new column name must be specified");
    String colName = m_config.getReplacedColumn();
    final int replacedColumn = in.findColumnIndex(m_config.getReplacedColumn());
    checkSetting(!(colName != null && replacedColumn < 0), "Column to replace: '%s' does not exist in input table", colName);
    String newName = replacedColumn >= 0 ? colName : DataTableSpec.getUniqueColumnName(in, m_config.getNewColumnName());
    DataColumnSpec outColumnSpec = new DataColumnSpecCreator(newName, m_config.getCellFactory().getDataType()).createSpec();
    final DataCell constantCell = m_config.getCellFactory().createCell(value, m_config.getDateFormat());
    ColumnRearranger rearranger = new ColumnRearranger(in);
    CellFactory fac = new SingleCellFactory(outColumnSpec) {

        @Override
        public DataCell getCell(final DataRow row) {
            return constantCell;
        }
    };
    if (replacedColumn >= 0) {
        rearranger.replace(fac, replacedColumn);
    } else {
        rearranger.append(fac);
    }
    return rearranger;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Aggregations

SingleCellFactory (org.knime.core.data.container.SingleCellFactory)48 DataRow (org.knime.core.data.DataRow)47 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)41 DataCell (org.knime.core.data.DataCell)40 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)35 DataColumnSpec (org.knime.core.data.DataColumnSpec)34 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)19 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)19 DataType (org.knime.core.data.DataType)12 CellFactory (org.knime.core.data.container.CellFactory)12 StringCell (org.knime.core.data.def.StringCell)10 Calendar (java.util.Calendar)8 DataTableSpec (org.knime.core.data.DataTableSpec)8 StringValue (org.knime.core.data.StringValue)8 DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)8 ArrayList (java.util.ArrayList)7 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)6 SettingsModelCalendar (org.knime.timeseries.util.SettingsModelCalendar)5 ParseException (java.text.ParseException)4 ZonedDateTime (java.time.ZonedDateTime)4