Search in sources :

Example 1 with ValidationReport

use of org.knime.base.node.jsnippet.util.ValidationReport in project knime-core by knime.

the class JavaSnippet method validateSettings.

/**
 * Validate settings which is typically called in the configure method of a node.
 *
 * What is checked:
 * <ul>
 * <li>Whether converter factories matching the ids from the settings exist</li>
 * <li>Whether the code compiles</li>
 * <li>Whether columns required by input mappings still exist.</li>
 * </ul>
 *
 * @param spec the spec of the data table at the inport
 * @param flowVariableRepository the flow variables at the inport
 * @return the validation results
 */
public ValidationReport validateSettings(final DataTableSpec spec, final FlowVariableRepository flowVariableRepository) {
    List<String> errors = new ArrayList<>();
    List<String> warnings = new ArrayList<>();
    // check input fields
    for (final InCol field : m_fields.getInColFields()) {
        final int index = spec.findColumnIndex(field.getKnimeName());
        if (index < 0) {
            errors.add("The column \"" + field.getKnimeName() + "\" is not found in the input table.");
        } else {
            final DataColumnSpec colSpec = spec.getColumnSpec(index);
            final DataType type = colSpec.getType();
            if (!type.equals(field.getDataType())) {
                // Input column type changed, try to find new converter
                final Optional<?> factory = ConverterUtil.getConverterFactory(type, field.getJavaType());
                if (factory.isPresent()) {
                    warnings.add("The type of the column \"" + field.getKnimeName() + "\" has changed but is compatible.");
                    field.setConverterFactory(type, (DataCellToJavaConverterFactory<?, ?>) factory.get());
                } else {
                    errors.add("The type of the column \"" + field.getKnimeName() + "\" has changed.");
                }
            }
        }
        if (!field.getConverterFactory().isPresent()) {
            errors.add(String.format("Missing converter for column '%s' to java field '%s' (converter id: '%s')", field.getKnimeName(), field.getJavaName(), field.getConverterFactoryId()));
        }
    }
    // check input variables
    for (InVar field : m_fields.getInVarFields()) {
        FlowVariable var = flowVariableRepository.getFlowVariable(field.getKnimeName());
        if (var != null) {
            if (!var.getType().equals(field.getFlowVarType())) {
                errors.add("The type of the flow variable \"" + field.getKnimeName() + "\" has changed.");
            }
        } else {
            errors.add("The flow variable \"" + field.getKnimeName() + "\" is not found in the input.");
        }
    }
    // check output fields
    for (OutCol field : m_fields.getOutColFields()) {
        if (field.getJavaType() == null) {
            errors.add("Java type could not be loaded. Providing plugin may be missing.");
        }
        int index = spec.findColumnIndex(field.getKnimeName());
        if (field.getReplaceExisting() && index < 0) {
            errors.add("The output column \"" + field.getKnimeName() + "\" is marked to be a replacement, " + "but an input with this name does not exist.");
        }
        if (!field.getReplaceExisting() && index > 0) {
            errors.add("The output column \"" + field.getKnimeName() + "\" is marked to be new, " + "but an input with this name does exist.");
        }
        if (!field.getConverterFactory().isPresent()) {
            errors.add(String.format("Missing converter for java field '%s' to column '%s' (converter id: '%s')", field.getJavaName(), field.getKnimeName(), field.getConverterFactoryId()));
        }
    }
    // check output variables
    for (OutVar field : m_fields.getOutVarFields()) {
        FlowVariable var = flowVariableRepository.getFlowVariable(field.getKnimeName());
        if (field.getReplaceExisting() && var == null) {
            errors.add("The output flow variable \"" + field.getKnimeName() + "\" is marked to be a replacement, " + "but an input with this name does not exist.");
        }
        if (!field.getReplaceExisting() && var != null) {
            errors.add("The output flow variable \"" + field.getKnimeName() + "\" is marked to be new, " + "but an input with this name does exist.");
        }
    }
    // Check additional bundles
    for (final String bundleName : m_settings.getBundles()) {
        final Bundle bundle = Platform.getBundle(bundleName);
        if (bundle == null) {
            errors.add("Bundle \"" + bundleName + "\" required by this snippet was not found.");
        } else {
        // TODO Version warning?
        }
    }
    try {
        // test if snippet compiles and if the file can be created
        createSnippetClass();
    } catch (Exception e) {
        errors.add(e.getMessage());
    } finally {
        close();
    }
    return new ValidationReport(errors.toArray(new String[errors.size()]), warnings.toArray(new String[warnings.size()]));
}
Also used : Bundle(org.osgi.framework.Bundle) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) ArrayList(java.util.ArrayList) InCol(org.knime.base.node.jsnippet.util.field.InCol) OutVar(org.knime.base.node.jsnippet.util.field.OutVar) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BadLocationException(javax.swing.text.BadLocationException) IOException(java.io.IOException) FlowVariableException(org.knime.base.node.jsnippet.expression.FlowVariableException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColumnException(org.knime.base.node.jsnippet.expression.ColumnException) TypeException(org.knime.base.node.jsnippet.expression.TypeException) MalformedURLException(java.net.MalformedURLException) DataColumnSpec(org.knime.core.data.DataColumnSpec) ValidationReport(org.knime.base.node.jsnippet.util.ValidationReport) DataType(org.knime.core.data.DataType) InVar(org.knime.base.node.jsnippet.util.field.InVar) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 2 with ValidationReport

use of org.knime.base.node.jsnippet.util.ValidationReport in project knime-core by knime.

the class JavaEditVarNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    m_snippet.setSettings(m_settings);
    FlowVariableRepository flowVarRepository = new FlowVariableRepository(getAvailableInputFlowVariables());
    ValidationReport report = m_snippet.validateSettings(new DataTableSpec(), flowVarRepository);
    if (report.hasWarnings()) {
        setWarningMessage(StringUtils.join(report.getWarnings(), "\n"));
    }
    if (report.hasErrors()) {
        throw new InvalidSettingsException(StringUtils.join(report.getErrors(), "\n"));
    }
    m_snippet.configure(new DataTableSpec(), flowVarRepository);
    for (FlowVariable flowVar : flowVarRepository.getModified()) {
        if (flowVar.getType().equals(Type.INTEGER)) {
            pushFlowVariableInt(flowVar.getName(), flowVar.getIntValue());
        } else if (flowVar.getType().equals(Type.DOUBLE)) {
            pushFlowVariableDouble(flowVar.getName(), flowVar.getDoubleValue());
        } else {
            pushFlowVariableString(flowVar.getName(), flowVar.getStringValue());
        }
    }
    // execute if execute on config flag is set
    if (!m_settings.isRunOnExecute()) {
        performExecute(null);
    }
    return new PortObjectSpec[] { FlowVariablePortObjectSpec.INSTANCE };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) FlowVariableRepository(org.knime.base.node.jsnippet.util.FlowVariableRepository) ValidationReport(org.knime.base.node.jsnippet.util.ValidationReport) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) FlowVariablePortObjectSpec(org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 3 with ValidationReport

use of org.knime.base.node.jsnippet.util.ValidationReport in project knime-core by knime.

the class JavaSnippetNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    m_snippet.setSettings(m_settings);
    FlowVariableRepository flowVarRepository = new FlowVariableRepository(getAvailableInputFlowVariables());
    // The following method also compile-checks the code and checks for missing converter factories
    ValidationReport report = m_snippet.validateSettings(inSpecs[0], flowVarRepository);
    if (report.hasWarnings()) {
        setWarningMessage(StringUtils.join(report.getWarnings(), "\n"));
    }
    if (report.hasErrors()) {
        throw new InvalidSettingsException(StringUtils.join(report.getErrors(), "\n"));
    }
    DataTableSpec outSpec = m_snippet.configure(inSpecs[0], flowVarRepository);
    for (FlowVariable flowVar : flowVarRepository.getModified()) {
        if (flowVar.getType().equals(Type.INTEGER)) {
            pushFlowVariableInt(flowVar.getName(), flowVar.getIntValue());
        } else if (flowVar.getType().equals(Type.DOUBLE)) {
            pushFlowVariableDouble(flowVar.getName(), flowVar.getDoubleValue());
        } else {
            pushFlowVariableString(flowVar.getName(), flowVar.getStringValue());
        }
    }
    return new DataTableSpec[] { outSpec };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) FlowVariableRepository(org.knime.base.node.jsnippet.util.FlowVariableRepository) ValidationReport(org.knime.base.node.jsnippet.util.ValidationReport) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Aggregations

ValidationReport (org.knime.base.node.jsnippet.util.ValidationReport)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 FlowVariable (org.knime.core.node.workflow.FlowVariable)3 FlowVariableRepository (org.knime.base.node.jsnippet.util.FlowVariableRepository)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 BadLocationException (javax.swing.text.BadLocationException)1 ColumnException (org.knime.base.node.jsnippet.expression.ColumnException)1 FlowVariableException (org.knime.base.node.jsnippet.expression.FlowVariableException)1 TypeException (org.knime.base.node.jsnippet.expression.TypeException)1 InCol (org.knime.base.node.jsnippet.util.field.InCol)1 InVar (org.knime.base.node.jsnippet.util.field.InVar)1 OutCol (org.knime.base.node.jsnippet.util.field.OutCol)1 OutVar (org.knime.base.node.jsnippet.util.field.OutVar)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataType (org.knime.core.data.DataType)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)1