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()]));
}
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 };
}
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 };
}
Aggregations