Search in sources :

Example 16 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.

the class BootstrapNodeModel method getSamplesSpec.

/**
 * Generates the specs for the bootstrap samples table.
 *
 * @param inSpec Specs of the input table
 * @return Specs of the bootstrap samples table
 */
private DataTableSpec getSamplesSpec(final DataTableSpec inSpec) {
    DataTableSpec outSpec;
    if (m_configuration != null && (m_configuration.getAppendOccurrences() || m_configuration.getAppendOriginalRowId())) {
        UniqueNameGenerator generator = new UniqueNameGenerator(inSpec);
        int appendedColumns = 0;
        if (m_configuration.getAppendOccurrences()) {
            appendedColumns++;
        }
        if (m_configuration.getAppendOriginalRowId()) {
            appendedColumns++;
        }
        // Output spec is spec from input table + count of occurrences column
        DataColumnSpec[] columnSpecs = new DataColumnSpec[inSpec.getNumColumns() + appendedColumns];
        int i;
        for (i = 0; i < inSpec.getNumColumns(); i++) {
            columnSpecs[i] = inSpec.getColumnSpec(i);
        }
        if (m_configuration.getAppendOccurrences()) {
            columnSpecs[i++] = generator.newColumn("Count of occurrences", IntCell.TYPE);
        }
        if (m_configuration.getAppendOriginalRowId()) {
            columnSpecs[i++] = generator.newColumn("Original RowID", StringCell.TYPE);
        }
        outSpec = new DataTableSpec(columnSpecs);
    } else {
        // Output spec is the same as spec from input table
        outSpec = inSpec;
    }
    return outSpec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator)

Example 17 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.

the class ColumnAppenderNodeModel method createOutSpec.

private static DataTableSpec createOutSpec(final DataTableSpec[] inSpecs) {
    DataColumnSpec[] cspecs = new DataColumnSpec[inSpecs[1].getNumColumns()];
    // Look in the bottom input table spec and uniquify column names
    UniqueNameGenerator nameGenerator = new UniqueNameGenerator(inSpecs[0]);
    for (int i = 0; i < inSpecs[1].getNumColumns(); i++) {
        DataColumnSpec oldSpec = inSpecs[1].getColumnSpec(i);
        cspecs[i] = nameGenerator.newCreator(oldSpec).createSpec();
    }
    DataTableSpec outSpec = new DataTableSpec(cspecs);
    return outSpec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator)

Example 18 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.

the class Joiner method createSpec.

/**
 * Creates a spec for the output table by taking care of duplicate columns.
 *
 * @param specs the specs of the two input tables
 * @return the spec of the output table
 * @throws InvalidSettingsException when settings are not supported
 */
private DataTableSpec createSpec(final DataTableSpec[] specs) throws InvalidSettingsException {
    validateSettings(m_settings);
    m_configWarnings.clear();
    List<String> leftCols = getLeftIncluded(specs[0]);
    List<String> rightCols = getRightIncluded(specs[1]);
    List<String> duplicates = new ArrayList<String>();
    duplicates.addAll(leftCols);
    duplicates.retainAll(rightCols);
    if (m_settings.getDuplicateHandling().equals(DuplicateHandling.DontExecute) && !duplicates.isEmpty()) {
        throw new InvalidSettingsException("Found duplicate columns, won't execute. Fix it in " + "\"Column Selection\" tab");
    }
    if (m_settings.getDuplicateHandling().equals(DuplicateHandling.Filter)) {
        for (String duplicate : duplicates) {
            DataType leftType = specs[0].getColumnSpec(duplicate).getType();
            DataType rightType = specs[1].getColumnSpec(duplicate).getType();
            if (!leftType.equals(rightType)) {
                m_configWarnings.add("The column \"" + duplicate + "\" can be found in " + "both input tables but with different data type. " + "Only the one in the top input table will show " + "up in the output table. Please change the " + "Duplicate Column Handling if both columns " + "should show up in the output table.");
            }
        }
        rightCols.removeAll(leftCols);
    }
    if ((!duplicates.isEmpty()) && m_settings.getDuplicateHandling().equals(DuplicateHandling.AppendSuffix) && (m_settings.getDuplicateColumnSuffix() == null || m_settings.getDuplicateColumnSuffix().equals(""))) {
        throw new InvalidSettingsException("No suffix for duplicate columns provided.");
    }
    // check if data types of joining columns do match
    for (int i = 0; i < m_settings.getLeftJoinColumns().length; i++) {
        String leftJoinAttr = m_settings.getLeftJoinColumns()[i];
        boolean leftJoinAttrIsRowKey = Joiner2Settings.ROW_KEY_IDENTIFIER.equals(leftJoinAttr);
        DataType leftType = leftJoinAttrIsRowKey ? StringCell.TYPE : specs[0].getColumnSpec(leftJoinAttr).getType();
        String rightJoinAttr = m_settings.getRightJoinColumns()[i];
        boolean rightJoinAttrIsRowKey = Joiner2Settings.ROW_KEY_IDENTIFIER.equals(rightJoinAttr);
        DataType rightType = rightJoinAttrIsRowKey ? StringCell.TYPE : specs[1].getColumnSpec(rightJoinAttr).getType();
        if (!leftType.equals(rightType)) {
            String left = leftJoinAttrIsRowKey ? "Row ID" : leftJoinAttr;
            String right = rightJoinAttrIsRowKey ? "Row ID" : rightJoinAttr;
            // check different cases here to give meaningful error messages
            if (leftType.equals(DoubleCell.TYPE) && rightType.equals(IntCell.TYPE)) {
                throw new InvalidSettingsException("Type mismatch found of " + "Joining Column Pair \"" + left + "\" and \"" + right + "\"." + " Use \"Double to Int node\" to " + "convert the type of \"" + left + "\" to integer.");
            } else if (leftType.equals(IntCell.TYPE) && rightType.equals(DoubleCell.TYPE)) {
                throw new InvalidSettingsException("Type mismatch found of " + "Joining Column Pair \"" + left + "\" and \"" + right + "\"." + " se \"Double to Int node\" to " + "convert the type of \"" + right + "\" to integer.");
            } else if (leftType.isCompatible(DoubleValue.class) && rightType.equals(StringCell.TYPE)) {
                throw new InvalidSettingsException("Type mismatch found of " + "Joining Column Pair \"" + left + "\" and \"" + right + "\"." + " Use \"Number to String node\" to " + "convert the type of \"" + left + "\" to string.");
            } else if (leftType.equals(StringCell.TYPE) && rightType.isCompatible(DoubleValue.class)) {
                throw new InvalidSettingsException("Type mismatch found of " + "Joining Column Pair \"" + left + "\" and \"" + right + "\"." + " Use \"Number to String node\" to " + "convert the type of \"" + right + "\" to string.");
            } else if (leftType.getPreferredValueClass() != rightType.getPreferredValueClass()) {
                throw new InvalidSettingsException("Type mismatch found of " + "Joining Column Pair \"" + left + "\" and \"" + right + "\"." + "This causes an empty output table.");
            }
        }
    }
    @SuppressWarnings("unchecked") UniqueNameGenerator nameGen = new UniqueNameGenerator(Collections.EMPTY_SET);
    m_leftSurvivors = new ArrayList<String>();
    List<DataColumnSpec> outColSpecs = new ArrayList<DataColumnSpec>();
    for (int i = 0; i < specs[0].getNumColumns(); i++) {
        DataColumnSpec columnSpec = specs[0].getColumnSpec(i);
        if (leftCols.contains(columnSpec.getName())) {
            outColSpecs.add(columnSpec);
            nameGen.newName(columnSpec.getName());
            m_leftSurvivors.add(columnSpec.getName());
        }
    }
    m_rightSurvivors = new ArrayList<String>();
    for (int i = 0; i < specs[1].getNumColumns(); i++) {
        DataColumnSpec columnSpec = specs[1].getColumnSpec(i);
        if (rightCols.contains(columnSpec.getName())) {
            if (m_settings.getDuplicateHandling().equals(DuplicateHandling.AppendSuffix)) {
                if (m_leftSurvivors.contains(columnSpec.getName()) || m_rightSurvivors.contains(columnSpec.getName())) {
                    String newName = columnSpec.getName();
                    do {
                        newName += m_settings.getDuplicateColumnSuffix();
                    } while (m_leftSurvivors.contains(newName) || m_rightSurvivors.contains(newName));
                    DataColumnSpecCreator dcsc = new DataColumnSpecCreator(columnSpec);
                    dcsc.removeAllHandlers();
                    dcsc.setName(newName);
                    outColSpecs.add(dcsc.createSpec());
                    rightCols.add(newName);
                } else {
                    outColSpecs.add(columnSpec);
                }
            } else {
                String newName = nameGen.newName(columnSpec.getName());
                if (newName.equals(columnSpec.getName())) {
                    outColSpecs.add(columnSpec);
                } else {
                    DataColumnSpecCreator dcsc = new DataColumnSpecCreator(columnSpec);
                    dcsc.removeAllHandlers();
                    dcsc.setName(newName);
                    outColSpecs.add(dcsc.createSpec());
                }
            }
            m_rightSurvivors.add(columnSpec.getName());
        }
    }
    return new DataTableSpec(outColSpecs.toArray(new DataColumnSpec[outColSpecs.size()]));
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType)

Example 19 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.

the class JavaScriptingSettings method getNewColSpec.

/**
 * The column spec of the generated column.
 * @return The col spec.
 * @throws InvalidSettingsException If settings are inconsistent.
 */
public DataColumnSpec getNewColSpec() throws InvalidSettingsException {
    Class<?> returnType = getReturnType();
    String colName = getColName();
    boolean isArrayReturn = isArrayReturn();
    DataType type = null;
    for (JavaSnippetType<?, ?, ?> t : JavaSnippetType.TYPES) {
        if (t.getJavaClass(false).equals(returnType)) {
            type = t.getKNIMEDataType(isArrayReturn);
        }
    }
    CheckUtils.checkSettingNotNull(type, "Illegal return type: %s", returnType.getName());
    if (isReplace()) {
        return new DataColumnSpecCreator(colName, type).createSpec();
    } else {
        return new UniqueNameGenerator(m_inputSpec).newColumn(colName, type);
    }
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataType(org.knime.core.data.DataType) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator)

Example 20 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.

the class CreateFilenameNodeModel method verifyAndPushOutputVar.

private void verifyAndPushOutputVar() throws InvalidSettingsException {
    String outputFlowVar;
    if (!m_outputModel.getStringValue().isEmpty()) {
        outputFlowVar = m_outputModel.getStringValue();
    } else {
        outputFlowVar = DEFAULT_OUTPUT_NAME;
    }
    String baseDir = m_pathModel.getStringValue();
    String ext = m_extModel.getStringValue();
    String name = m_nameModel.getStringValue();
    if (!verifyBaseDir(baseDir)) {
        throw new InvalidSettingsException("Invalid base directory name!");
    }
    if (name.isEmpty()) {
        throw new InvalidSettingsException("Invalid file name: Filename cannot be empty.");
    }
    int invalidCharIdx = findInvalidChar(name);
    if (invalidCharIdx != -1) {
        throw new InvalidSettingsException("Invalid file name: " + name.charAt(invalidCharIdx) + " at position " + invalidCharIdx + ".");
    }
    if (IS_WINDOWS && checkForbiddenWindowsName(name)) {
        throw new InvalidSettingsException("Invalid file name: Filename might contain names that are forbidden in Windows platform.");
    }
    if (!checkDotsAndSpaces(name)) {
        throw new InvalidSettingsException("Invalid file name: Filename cannot contain only dot(s) or space(s).");
    }
    if (!checkLeadingWhitespaces(name)) {
        name = name.replaceAll("^\\s+", "");
        LOGGER.warn("Filename contains leading whitespace(s). It will be removed.");
    }
    ext = verifyExtension(ext);
    if (ext == "-1") {
        throw new InvalidSettingsException("Invalid file extension: Only alphanumeric characters are allowed.");
    }
    try {
        new File(name).getCanonicalPath();
    } catch (IOException | NullPointerException e) {
        throw new InvalidSettingsException("Invalid file name!");
    }
    String output = handleSlash(baseDir, name, ext);
    if (output == "-1") {
        throw new InvalidSettingsException("Invalid file name!");
    }
    if (!m_overwriteModel.getBooleanValue()) {
        Set<String> flowVars = getAvailableFlowVariables().keySet();
        outputFlowVar = new UniqueNameGenerator(flowVars).newName(outputFlowVar);
    }
    pushFlowVariableString(outputFlowVar, output);
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) IOException(java.io.IOException) File(java.io.File) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator)

Aggregations

UniqueNameGenerator (org.knime.core.util.UniqueNameGenerator)36 DataColumnSpec (org.knime.core.data.DataColumnSpec)33 DataTableSpec (org.knime.core.data.DataTableSpec)31 DataCell (org.knime.core.data.DataCell)24 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)22 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)19 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)18 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)18 DataRow (org.knime.core.data.DataRow)17 Arrays (java.util.Arrays)16 DataType (org.knime.core.data.DataType)16 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)16 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)16 NodeSettingsWO (org.knime.core.node.NodeSettingsWO)16 SettingsModelColumnFilter2 (org.knime.core.node.defaultnodesettings.SettingsModelColumnFilter2)13 File (java.io.File)10 IOException (java.io.IOException)10 ZonedDateTime (java.time.ZonedDateTime)10 ArrayList (java.util.ArrayList)10 AppendedColumnRow (org.knime.core.data.append.AppendedColumnRow)10