Search in sources :

Example 1 with ConfigRO

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

the class HistogramColumn method loadNominalHistogramsPrivate.

private static Map<Integer, HistogramNominalModel> loadNominalHistogramsPrivate(final File histogramsGz, final int[] nominalKeysSize) throws IOException, InvalidSettingsException {
    final FileInputStream is = new FileInputStream(histogramsGz);
    final GZIPInputStream inData = new GZIPInputStream(is);
    final ConfigRO config = NodeSettings.loadFromXML(inData);
    Map<Integer, HistogramNominalModel> histograms = new HashMap<Integer, HistogramNominalModel>();
    // .getConfig(HISTOGRAMS);
    ConfigRO hs = config;
    int[] nomColumnIndices = config.getIntArray(NOMINAL_COLUMNS);
    for (int colIdx : nomColumnIndices) {
        Config h = hs.getConfig(HISTOGRAM + colIdx);
        int maxCount = h.getInt(MAX_COUNT);
        int rowCount = h.getInt(ROW_COUNT);
        String colName = h.getString(COL_NAME);
        String[] values = h.getStringArray(BIN_VALUES);
        int[] binCounts = h.getIntArray(BIN_COUNTS);
        Map<DataValue, Integer> bins = new HashMap<DataValue, Integer>();
        for (int i = binCounts.length; i-- > 0; ) {
            if (values[i] == "?") {
                bins.put(new MissingCell(null), binCounts[i]);
            } else {
                bins.put(new StringCell(values[i]), binCounts[i]);
            }
        }
        HistogramNominalModel histogramData = new HistogramNominalModel(bins, colIdx, colName, rowCount);
        histogramData.setMaxCount(maxCount);
        histogramData.setRowCount(rowCount);
        // assert Math.abs(histogramData.m_width - width) < 1e-9: "histogram data width: " + histogramData.m_width + " width: " + width;
        assert nominalKeysSize[colIdx] == bins.size() : "Saved size of nominal bins: " + nominalKeysSize[colIdx] + ", restored from the file: " + bins.size();
        histograms.put(colIdx, histogramData);
    }
    return histograms;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataValue(org.knime.core.data.DataValue) Config(org.knime.core.node.config.Config) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) MissingCell(org.knime.core.data.MissingCell) StringCell(org.knime.core.data.def.StringCell) ConfigRO(org.knime.core.node.config.ConfigRO)

Example 2 with ConfigRO

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

the class VirtualParallelizedChunkPortObjectInNodeFactory method loadPortTypeList.

/**
 * @param config
 * @return TODO
 * @throws InvalidSettingsException
 */
static PortType[] loadPortTypeList(final ConfigRO config) throws InvalidSettingsException {
    Set<String> keySet = config.keySet();
    PortType[] outTypes = new PortType[keySet.size()];
    for (String s : keySet) {
        ConfigRO portConfig = config.getConfig(s);
        int index = portConfig.getInt("index");
        CheckUtils.checkSetting(index >= 0 && index < outTypes.length, "Invalid port index must be in [0, %d]: %d", keySet.size() - 1, index);
        Config portTypeConfig = portConfig.getConfig("type");
        PortType type = PortType.load(portTypeConfig);
        outTypes[index] = type;
    }
    int invalidIndex = Arrays.asList(outTypes).indexOf(null);
    if (invalidIndex >= 0) {
        throw new InvalidSettingsException("Unassigned port type at index " + invalidIndex);
    }
    return outTypes;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Config(org.knime.core.node.config.Config) ConfigRO(org.knime.core.node.config.ConfigRO) PortType(org.knime.core.node.port.PortType)

Example 3 with ConfigRO

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

the class ColorModelNominal method load.

/**
 * Read color settings from given <code>Config</code> and returns a new
 * <code>ColorModelNominal</code> object.
 * @param config Reads color model from.
 * @return A new <code>ColorModelNominal</code> object.
 * @throws InvalidSettingsException If the color model settings could not
 *         be read.
 * @throws NullPointerException If the <i>config</i> is <code>null</code>.
 */
public static ColorModelNominal load(final ConfigRO config) throws InvalidSettingsException {
    Map<DataCell, ColorAttr> map = new LinkedHashMap<>();
    ConfigRO keyConfig = config.getConfig(CFG_KEYS);
    for (String key : keyConfig.keySet()) {
        Color color;
        try {
            // load color components before 2.0
            int[] v = config.getIntArray(key.toString());
            color = new Color(v[0], v[1], v[2], v[3]);
        } catch (InvalidSettingsException ise) {
            color = new Color(config.getInt(key.toString()), true);
        }
        DataCell cell = keyConfig.getDataCell(key);
        map.put(cell, ColorAttr.getInstance(color));
    }
    return new ColorModelNominal(map);
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Color(java.awt.Color) DataCell(org.knime.core.data.DataCell) ConfigRO(org.knime.core.node.config.ConfigRO) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with ConfigRO

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

the class DefaultHiLiteMapper method load.

/**
 * Restores the mapper from the config object that has been written using
 * the save method.
 * @param config To read from
 * @return A new mapper based on the settings.
 * @throws InvalidSettingsException If that fails.
 */
public static DefaultHiLiteMapper load(final ConfigRO config) throws InvalidSettingsException {
    // load hilite mapping
    LinkedHashMap<RowKey, Set<RowKey>> mapping = new LinkedHashMap<RowKey, Set<RowKey>>();
    for (String key : config.keySet()) {
        ConfigRO keySettings = config.getConfig(key);
        String cellKey;
        try {
            // load keys before 2.0
            cellKey = keySettings.getDataCell(key).toString();
        } catch (InvalidSettingsException ise) {
            cellKey = keySettings.getString(key);
        }
        Set<RowKey> keySet;
        try {
            // load mapping before 2.0
            DataCell[] mappedKeys = keySettings.getDataCellArray(CFG_MAPPED_KEYS);
            keySet = new LinkedHashSet<RowKey>();
            for (DataCell dc : mappedKeys) {
                keySet.add(new RowKey(dc.toString()));
            }
        } catch (InvalidSettingsException ise) {
            RowKey[] mappedKeys = keySettings.getRowKeyArray(CFG_MAPPED_KEYS);
            keySet = new LinkedHashSet<RowKey>(Arrays.asList(mappedKeys));
        }
        mapping.put(new RowKey(cellKey), keySet);
    }
    return new DefaultHiLiteMapper(mapping);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) RowKey(org.knime.core.data.RowKey) LinkedHashMap(java.util.LinkedHashMap) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ConfigRO(org.knime.core.node.config.ConfigRO) DataCell(org.knime.core.data.DataCell)

Example 5 with ConfigRO

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

the class DataTableSpec method load.

/**
 * Reads all {@link DataColumnSpec} objects from the given {@link ConfigRO}
 * and returns a new <code>DataTableSpec</code> object containing them.
 *
 * @param config object to read column specs from
 * @return a new table spec object containing the just read columns
 * @throws InvalidSettingsException if the name, number of columns, or a
 *             column spec could not be read
 */
public static DataTableSpec load(final ConfigRO config) throws InvalidSettingsException {
    String name = config.getString(CFG_SPEC_NAME);
    int ncols = config.getInt(CFG_NR_COLUMNS);
    DataColumnSpec[] specs = new DataColumnSpec[ncols];
    for (int i = 0; i < ncols; i++) {
        ConfigRO column = config.getConfig(CFG_COLUMN_SPEC + i);
        specs[i] = DataColumnSpec.load(column);
    }
    Map<String, String> map;
    if (config.containsKey(CFG_PROPERTIES)) {
        map = new LinkedHashMap<String, String>();
        ConfigRO propertiesConfig = config.getConfig(CFG_PROPERTIES);
        for (String s : propertiesConfig.keySet()) {
            ConfigRO entryConfig = propertiesConfig.getConfig(s);
            String key = entryConfig.getString(CFG_PROPERTY_KEY);
            if (key == null || key.length() == 0) {
                throw new InvalidSettingsException("Invalid property key (empty or null)");
            }
            String value = entryConfig.getString(CFG_PROPERTY_VALUE);
            if (map.put(key, value) != null) {
                throw new InvalidSettingsException("Duplicate key identifier \"" + key + "\"");
            }
        }
    } else {
        map = Collections.emptyMap();
    }
    return new DataTableSpec(name, specs, map);
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ConfigRO(org.knime.core.node.config.ConfigRO)

Aggregations

ConfigRO (org.knime.core.node.config.ConfigRO)16 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 FileInputStream (java.io.FileInputStream)7 Config (org.knime.core.node.config.Config)7 GZIPInputStream (java.util.zip.GZIPInputStream)6 File (java.io.File)5 LinkedHashMap (java.util.LinkedHashMap)5 Color (java.awt.Color)4 HashMap (java.util.HashMap)4 DataCell (org.knime.core.data.DataCell)4 ArrayList (java.util.ArrayList)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Set (java.util.Set)2 DefaultDataArray (org.knime.base.node.util.DefaultDataArray)2 ContainerTable (org.knime.core.data.container.ContainerTable)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 PortType (org.knime.core.node.port.PortType)2 Point (java.awt.Point)1 HashSet (java.util.HashSet)1