use of org.knime.base.node.jsnippet.util.field.OutCol in project knime-core by knime.
the class JavaSnippetTest method testEncoding.
/**
* Test encoding.
* @throws Exception
*/
@Test
public void testEncoding() throws Exception {
final JavaSnippetSettings settings = new JavaSnippetSettings("outString = \"���\";");
final OutCol outCol = new OutCol();
outCol.setJavaName("outString");
outCol.setConverterFactory(ConverterUtil.getConverterFactory(String.class, StringCell.TYPE).get());
settings.getJavaSnippetFields().getOutColFields().add(outCol);
snippet.setSettings(settings);
final AbstractJSnippet s = snippet.createSnippetInstance();
assertNotNull(s);
s.snippet();
final Field outStringField = s.getClass().getField("outString");
final String string = (String) outStringField.get(s);
assertEquals(string, "���");
}
use of org.knime.base.node.jsnippet.util.field.OutCol in project knime-core by knime.
the class JavaSnippet method createRearranger.
/**
* The rearranger is the working horse for creating the output table.
*/
ColumnRearranger createRearranger(final DataTableSpec spec, final FlowVariableRepository flowVariableRepository, final int rowCount, final ExecutionContext context) throws InvalidSettingsException {
int offset = spec.getNumColumns();
CellFactory factory = new JavaSnippetCellFactory(this, spec, flowVariableRepository, rowCount, context);
ColumnRearranger c = new ColumnRearranger(spec);
// add factory to the column rearranger
c.append(factory);
// define which new columns do replace others
OutColList outFields = m_fields.getOutColFields();
for (int i = outFields.size() - 1; i >= 0; i--) {
OutCol field = outFields.get(i);
int index = spec.findColumnIndex(field.getKnimeName());
if (index >= 0) {
if (field.getReplaceExisting()) {
c.remove(index);
c.move(offset + i - 1, index);
} else {
throw new InvalidSettingsException("Field \"" + field.getJavaName() + "\" is configured to " + "replace no existing columns.");
}
}
}
return c;
}
use of org.knime.base.node.jsnippet.util.field.OutCol in project knime-core by knime.
the class JavaSnippetCellFactory method getColumnSpecs.
/**
* {@inheritDoc}
*/
@Override
public DataColumnSpec[] getColumnSpecs() {
OutColList outFields = m_snippet.getSystemFields().getOutColFields();
DataColumnSpec[] cols = new DataColumnSpec[outFields.size()];
for (int i = 0; i < cols.length; i++) {
OutCol field = outFields.get(i);
cols[i] = new DataColumnSpecCreator(field.getKnimeName(), field.getDataType()).createSpec();
}
return cols;
}
use of org.knime.base.node.jsnippet.util.field.OutCol in project knime-core by knime.
the class AddOutFieldDialog method takeOverSettings.
/**
* Save settings in field m_result.
*/
@SuppressWarnings("rawtypes")
private JavaField takeOverSettings() {
if (m_fieldType.getSelectedItem().equals(FieldType.Column)) {
OutCol outCol = new OutCol();
boolean isReplacing = m_replace.isSelected();
outCol.setReplaceExisting(isReplacing);
String colName = isReplacing ? ((DataColumnSpec) m_replacedKnimeName.getSelectedItem()).getName() : m_knimeName.getText();
outCol.setKnimeName(colName);
Set<String> taken = new HashSet<>();
for (int i = 0; i < m_model.getRowCount(); i++) {
taken.add((String) m_model.getValueAt(i, Column.JAVA_FIELD));
}
// collection is now done by a separate checkbox
// DataType dataType = (DataType)m_knimeType.getSelectedItem();
// DataType elemType =
// dataType.isCollectionType() ? dataType
// .getCollectionElementType() : dataType;
// boolean isCollection = dataType.isCollectionType();
DataType dataType = (DataType) m_knimeType.getSelectedItem();
if (m_isArray.isSelected()) {
dataType = ListCell.getCollectionType(dataType);
}
final Optional<JavaToDataCellConverterFactory<?>> selectedFactory = ConverterUtil.getPreferredFactoryForDestinationType(dataType);
if (!selectedFactory.isPresent()) {
// Default to string converter, which always exists. Should not happen, though.
outCol.setConverterFactory(ConverterUtil.getPreferredFactoryForDestinationType(StringCell.TYPE).get());
} else {
outCol.setConverterFactory(selectedFactory.get());
}
final String javaName = FieldsTableUtil.createUniqueJavaIdentifier(colName, taken, "out_");
outCol.setJavaName(javaName);
return outCol;
} else {
// flow variable
OutVar outVar = new OutVar();
boolean isReplacing = m_replace.isSelected();
outVar.setReplaceExisting(isReplacing);
String colName = isReplacing ? ((FlowVariable) m_replacedKnimeName.getSelectedItem()).getName() : m_knimeName.getText();
outVar.setKnimeName(colName);
Set<String> taken = new HashSet<>();
for (int i = 0; i < m_model.getRowCount(); i++) {
taken.add((String) m_model.getValueAt(i, Column.JAVA_FIELD));
}
String javaName = FieldsTableUtil.createUniqueJavaIdentifier(colName, taken, "out_");
FlowVariable.Type type = (FlowVariable.Type) m_knimeType.getSelectedItem();
TypeProvider typeProvider = TypeProvider.getDefault();
Class javaType = typeProvider.getTypeConverter(type).getPreferredJavaType();
outVar.setFlowVarType(type);
outVar.setJavaName(javaName);
outVar.setJavaType(javaType);
return outVar;
}
}
use of org.knime.base.node.jsnippet.util.field.OutCol in project knime-core by knime.
the class OutFieldsTable method getOutColFields.
/**
* Get the field definitions representing output columns.
*
* @return fields representing output columns.
*/
public OutColList getOutColFields() {
OutColList outCols = new OutColList();
for (int r = 0; r < m_model.getRowCount(); r++) {
if (!m_model.validateValues(r)) {
// there are errors in this row
continue;
}
Object fieldTypeValue = getFieldType(r);
if (null == fieldTypeValue) {
continue;
}
boolean isColumn = fieldTypeValue.equals(FieldType.Column);
if (isColumn) {
OutCol outCol = new OutCol();
outCol.setReplaceExisting((Boolean) m_model.getValueAt(r, Column.REPLACE_EXISTING));
Object colColValue = m_model.getValueAt(r, Column.COLUMN);
if (colColValue instanceof DataColumnSpec) {
DataColumnSpec colSpec = (DataColumnSpec) colColValue;
outCol.setKnimeName(colSpec.getName());
} else if (colColValue instanceof String) {
outCol.setKnimeName(colColValue.toString());
} else {
continue;
}
final Object dataTypeValue = m_model.getValueAt(r, Column.DATA_TYPE);
if (!(dataTypeValue instanceof DataType)) {
continue;
}
outCol.setJavaName((String) m_model.getValueAt(r, Column.JAVA_FIELD));
Object javaTypeObject = m_model.getValueAt(r, Column.JAVA_TYPE);
if (javaTypeObject instanceof JavaToDataCellConverterFactory) {
outCol.setConverterFactory((JavaToDataCellConverterFactory) javaTypeObject);
} else {
continue;
}
outCols.add(outCol);
}
}
return outCols;
}
Aggregations