Search in sources :

Example 81 with Config

use of org.knime.core.node.config.Config in project knime-core by knime.

the class OldToNewTimeNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        SimpleStreamableOperatorInternals m_internals = new SimpleStreamableOperatorInternals();

        /**
         * {@inheritDoc}
         */
        @Override
        public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
            if (partitionInfo.getPartitionIndex() == 0) {
                final RowInput rowInput = (RowInput) inputs[0];
                final DataRow row = rowInput.poll();
                if (row != null) {
                    if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
                        final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells()];
                        final DataTableSpec inSpec = rowInput.getDataTableSpec();
                        final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
                        final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
                        for (int i = 0; i < inSpec.getNumColumns(); i++) {
                            final int searchIdx = Arrays.binarySearch(includeIndexes, i);
                            if (searchIdx < 0) {
                                colSpecs[i] = inSpec.getColumnSpec(i);
                            } else {
                                colSpecs[i] = newColumnSpecs[searchIdx];
                            }
                        }
                        final Config config = m_internals.getConfig();
                        config.addBoolean("hasIterated", false);
                        for (int i = 0; i < inSpec.getNumColumns(); i++) {
                            config.addDataType("type" + i, colSpecs[i].getType());
                            config.addString("colname" + i, colSpecs[i].getName());
                        }
                        config.addInt("sizeRow", colSpecs.length);
                    } else {
                        final DataTableSpec inSpec = rowInput.getDataTableSpec();
                        final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
                        final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
                        final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells() + includeIndexes.length];
                        for (int i = 0; i < inSpec.getNumColumns(); i++) {
                            colSpecs[i] = inSpec.getColumnSpec(i);
                        }
                        for (int i = 0; i < newColumnSpecs.length; i++) {
                            colSpecs[i + inSpec.getNumColumns()] = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
                        }
                        final Config config = m_internals.getConfig();
                        config.addBoolean("hasIterated", false);
                        for (int i = 0; i < colSpecs.length; i++) {
                            config.addDataType("type" + i, colSpecs[i].getType());
                            config.addString("colname" + i, colSpecs[i].getName());
                        }
                        config.addInt("sizeRow", colSpecs.length);
                    }
                } else {
                    m_internals.getConfig().addInt("sizeRow", 0);
                }
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals saveInternals() {
            return m_internals;
        }

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            final RowInput in = (RowInput) inputs[0];
            final RowOutput out = (RowOutput) outputs[0];
            final DataTableSpec inSpec = in.getDataTableSpec();
            final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
            DataRow row;
            while ((row = in.poll()) != null) {
                exec.checkCanceled();
                final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
                DataCell[] datacells = new DataCell[includeIndexes.length];
                for (int i = 0; i < includeIndexes.length; i++) {
                    if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
                        ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(newColumnSpecs[i], i, includeIndexes[i]);
                        datacells[i] = cellFac.getCells(row)[0];
                    } else {
                        final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
                        ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(dataColSpec, i, includeIndexes[i]);
                        datacells[i] = cellFac.getCells(row)[0];
                    }
                }
                if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
                    out.push(new ReplacedColumnsDataRow(row, datacells, includeIndexes));
                } else {
                    out.push(new AppendedColumnRow(row, datacells));
                }
            }
            in.close();
            out.close();
        }
    };
}
Also used : Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ZonedDateTime(java.time.ZonedDateTime) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) LocalDateTimeCellFactory(org.knime.core.data.time.localdatetime.LocalDateTimeCellFactory) DataColumnSpec(org.knime.core.data.DataColumnSpec) LocalTime(java.time.LocalTime) PortInput(org.knime.core.node.streamable.PortInput) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) PartitionInfo(org.knime.core.node.streamable.PartitionInfo) SettingsModelColumnFilter2(org.knime.core.node.defaultnodesettings.SettingsModelColumnFilter2) NodeModel(org.knime.core.node.NodeModel) LocalDateCell(org.knime.core.data.time.localdate.LocalDateCell) ZoneId(java.time.ZoneId) BufferedDataTable(org.knime.core.node.BufferedDataTable) InputPortRole(org.knime.core.node.streamable.InputPortRole) LocalDate(java.time.LocalDate) Config(org.knime.core.node.config.Config) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) LocalDateTimeCell(org.knime.core.data.time.localdatetime.LocalDateTimeCell) RowOutput(org.knime.core.node.streamable.RowOutput) DataTableSpec(org.knime.core.data.DataTableSpec) ZonedDateTimeCell(org.knime.core.data.time.zoneddatetime.ZonedDateTimeCell) LocalDateTime(java.time.LocalDateTime) AppendedColumnRow(org.knime.core.data.append.AppendedColumnRow) ExecutionContext(org.knime.core.node.ExecutionContext) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) ZonedDateTimeCellFactory(org.knime.core.data.time.zoneddatetime.ZonedDateTimeCellFactory) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DateTimeType(org.knime.time.util.DateTimeType) DataCell(org.knime.core.data.DataCell) LocalDateCellFactory(org.knime.core.data.time.localdate.LocalDateCellFactory) LocalTimeCellFactory(org.knime.core.data.time.localtime.LocalTimeCellFactory) RowInput(org.knime.core.node.streamable.RowInput) LocalTimeCell(org.knime.core.data.time.localtime.LocalTimeCell) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) OutputPortRole(org.knime.core.node.streamable.OutputPortRole) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) File(java.io.File) DataRow(org.knime.core.data.DataRow) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PortOutput(org.knime.core.node.streamable.PortOutput) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) TimeUnit(java.util.concurrent.TimeUnit) MissingCell(org.knime.core.data.MissingCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataType(org.knime.core.data.DataType) MergeOperator(org.knime.core.node.streamable.MergeOperator) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) DataTableSpec(org.knime.core.data.DataTableSpec) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) Config(org.knime.core.node.config.Config) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) RowInput(org.knime.core.node.streamable.RowInput) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) DataRow(org.knime.core.data.DataRow) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) RowOutput(org.knime.core.node.streamable.RowOutput) ExecutionContext(org.knime.core.node.ExecutionContext) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataCell(org.knime.core.data.DataCell) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) AppendedColumnRow(org.knime.core.data.append.AppendedColumnRow)

Example 82 with Config

use of org.knime.core.node.config.Config in project knime-core by knime.

the class SettingsModelCalendar method validateSettingsForModel.

/**
 * {@inheritDoc}
 */
@Override
protected void validateSettingsForModel(final NodeSettingsRO settings) throws InvalidSettingsException {
    // guess nothing else to do here than to check for existence of
    // the fields
    Config internals = settings.getConfig(m_key);
    boolean useDate = internals.getBoolean(KEY_USE_DATE);
    boolean useTime = internals.getBoolean(KEY_USE_TIME);
    if (!useDate && !useTime) {
        throw new InvalidSettingsException("Must use date, time or both!");
    }
    internals.getBoolean(KEY_USE_MILLIS);
    // this probably throws an exception if there is no value for the key
    // since every possible long is a valid one (even negatives = in order
    // to be able to store dates before 1970)
    internals.getLong(KEY_TIME);
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Config(org.knime.core.node.config.Config)

Example 83 with Config

use of org.knime.core.node.config.Config in project knime-core by knime.

the class NodeContainerProperties method getDescriptors.

/**
 * @return see {@link #getPropertyDescriptors()}
 */
protected IPropertyDescriptor[] getDescriptors() {
    ArrayList<IPropertyDescriptor> descriptors = new ArrayList<IPropertyDescriptor>();
    // iterate through all settings in the config
    for (Enumeration<TreeNode> it = m_settings.children(); it.hasMoreElements(); ) {
        AbstractConfigEntry prop = (AbstractConfigEntry) it.nextElement();
        // the id should be globally unique
        String hierID = m_prefix.isEmpty() ? prop.getKey() : m_prefix + CONFIG_SEPARATOR + prop.getKey();
        if (prop instanceof Config) {
            // sub-config
            descriptors.add(new PropertyDescriptor(hierID, prop.getKey()));
        } else {
            // all settings are displayed as string
            String typeName = prop.getType().name().substring(1);
            // we don't have a label yet
            String label = prop.getKey() + " (" + typeName + ")";
            switch(prop.getType()) {
                // if cases are changed here, setPropertyValue must be adapted
                case xboolean:
                case xbyte:
                case xchar:
                case xdouble:
                case xfloat:
                case xint:
                case xlong:
                case xshort:
                case xstring:
                    // editable types
                    descriptors.add(new TextPropertyDescriptor(hierID, label));
                    break;
                default:
                    descriptors.add(new PropertyDescriptor(hierID, label));
                    break;
            }
        }
    }
    return descriptors.toArray(new IPropertyDescriptor[descriptors.size()]);
}
Also used : AbstractConfigEntry(org.knime.core.node.config.base.AbstractConfigEntry) PropertyDescriptor(org.eclipse.ui.views.properties.PropertyDescriptor) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) TreeNode(javax.swing.tree.TreeNode) Config(org.knime.core.node.config.Config) ArrayList(java.util.ArrayList) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor)

Example 84 with Config

use of org.knime.core.node.config.Config in project knime-core by knime.

the class NodeContainerProperties method getPropertyValue.

/**
 * {@inheritDoc}
 */
@Override
public Object getPropertyValue(final Object id) {
    if (id instanceof String) {
        String hierID = (String) id;
        // cut off our prefix from the ID
        if (!hierID.startsWith(m_prefix)) {
            return "ERROR: Unexpected property id: " + hierID + " (while in sub-config " + m_prefix + ")";
        }
        String ourID = hierID;
        if (!m_prefix.isEmpty()) {
            assert hierID.charAt(m_prefix.length()) == CONFIG_SEPARATOR.charAt(0);
            // + 1 for removing the separator
            ourID = hierID.substring(m_prefix.length() + 1);
        }
        AbstractConfigEntry entry = m_settings.getEntry(ourID);
        if (entry instanceof Config) {
            return new NodeContainerProperties(getNode(), (Config) entry, m_prefix.isEmpty() ? ourID : m_prefix + CONFIG_SEPARATOR + ourID);
        } else {
            if (entry == null) {
                return "ERROR: No value for key " + ourID;
            }
            return TokenizerSettings.printableStr(entry.toStringValue());
        }
    }
    return null;
}
Also used : AbstractConfigEntry(org.knime.core.node.config.base.AbstractConfigEntry) Config(org.knime.core.node.config.Config)

Aggregations

Config (org.knime.core.node.config.Config)84 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)25 Color (java.awt.Color)10 File (java.io.File)10 FileOutputStream (java.io.FileOutputStream)8 NodeSettings (org.knime.core.node.NodeSettings)8 ArrayList (java.util.ArrayList)7 DataColumnSpec (org.knime.core.data.DataColumnSpec)7 ConfigRO (org.knime.core.node.config.ConfigRO)7 HashMap (java.util.HashMap)6 GZIPOutputStream (java.util.zip.GZIPOutputStream)6 LinkedHashMap (java.util.LinkedHashMap)5 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 SimpleStreamableOperatorInternals (org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals)5 FileInputStream (java.io.FileInputStream)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 DataCell (org.knime.core.data.DataCell)4