Search in sources :

Example 1 with TypeConverter

use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.

the class OutFieldsTableModel method validateJavaTypeColumn.

/**
 * Validate java type in the given row.
 * @param row the row to check
 * @return error message if error occurs
 */
@SuppressWarnings("rawtypes")
private String validateJavaTypeColumn(final int row) {
    Object value = this.getValueAt(row, Column.JAVA_TYPE);
    if (null == value) {
        return "Please select a value";
    }
    if (value instanceof JavaToDataCellConverterFactory) {
        if (getValueAt(row, Column.FIELD_TYPE) == FieldType.FlowVariable) {
            return "Cannot use DataCell converters for flow variables.";
        }
    } else if (value instanceof String) {
        final String id = (String) value;
        final Optional<JavaToDataCellConverterFactory<?>> factory = ConverterUtil.getJavaToDataCellConverterFactory(id);
        return factory.isPresent() ? null : "Converter factory " + id + " was not found.";
    } else if (value instanceof Class) {
        Class javaType = (Class) value;
        Object outType = getValueAt(row, Column.DATA_TYPE);
        if (outType instanceof DataType) {
            DataType elemType = (DataType) outType;
            boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
            DataType dataType = isCollection ? ListCell.getCollectionType(elemType) : elemType;
            final Collection<JavaToDataCellConverterFactory<?>> factories = ConverterUtil.getFactoriesForDestinationType(dataType);
            if (factories.isEmpty()) {
                return "The java type \"" + javaType.getSimpleName() + "\" is not supported for output columns.";
            }
            if (dataType.isCollectionType() && !javaType.isArray()) {
                return "Please choose an java array for collection types.";
            }
            if (!dataType.isCollectionType() && javaType.isArray()) {
                return "An array cannot be written to a non collection column";
            }
        } else if (outType instanceof Type) {
            Type type = (Type) outType;
            TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(type);
            if (!typeConversion.canProvideJavaType(javaType)) {
                return "The java type \"" + javaType.getSimpleName() + "\" is not supported.";
            }
        }
    } else {
        return "Cannot find class " + value.toString();
    }
    // no error found
    return null;
}
Also used : TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) Type(org.knime.core.node.workflow.FlowVariable.Type) DataType(org.knime.core.data.DataType) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 2 with TypeConverter

use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.

the class OutFieldsTableModel method getAllowedJavaTypes.

/**
 * {@inheritDoc}
 */
@Override
public Object[] getAllowedJavaTypes(final int row) {
    Object input = getValueAt(row, Column.DATA_TYPE);
    if (input instanceof DataType) {
        DataType elemType = (DataType) input;
        boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
        final DataType dataType = isCollection ? ListCell.getCollectionType(elemType) : elemType;
        final Collection<JavaToDataCellConverterFactory<?>> factories = ConverterUtil.getFactoriesForDestinationType(dataType).stream().filter(factory -> JavaSnippet.getBuildPathFromCache(factory.getIdentifier()) != null).collect(Collectors.toList());
        return factories.toArray(new Object[factories.size()]);
    } else if (input instanceof Type) {
        Type type = (Type) input;
        TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(type);
        return typeConversion.canCreatedFromJavaTypes();
    } else {
        return new Class[] { String.class };
    }
}
Also used : Array(java.lang.reflect.Array) ListCell(org.knime.core.data.collection.ListCell) Collection(java.util.Collection) FlowVariable(org.knime.core.node.workflow.FlowVariable) TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayToCollectionConverterFactory(org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) TypeProvider(org.knime.base.node.jsnippet.type.TypeProvider) Type(org.knime.core.node.workflow.FlowVariable.Type) JavaSnippet(org.knime.base.node.jsnippet.JavaSnippet) ConverterUtil(org.knime.base.node.jsnippet.type.ConverterUtil) DataColumnSpec(org.knime.core.data.DataColumnSpec) Map(java.util.Map) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) Type(org.knime.core.node.workflow.FlowVariable.Type) DataType(org.knime.core.data.DataType) DataType(org.knime.core.data.DataType) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 3 with TypeConverter

use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.

the class FlowVariableRepository method getValueOfType.

/**
 * Get value of a flow variable. The type of the returned object is equal
 * to the given className.
 * @param name the name of the flow variable
 * @param className the type of the returned object
 * @return the value of the flow variable
 */
@SuppressWarnings("rawtypes")
public Object getValueOfType(final String name, final Class className) {
    FlowVariable flowVar = getFlowVariable(name);
    if (null == flowVar) {
        throw new FlowVariableException("The flow variable with name \"" + name + "\" does not exist.");
    }
    TypeConverter converter = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
    return converter.getValue(flowVar, className);
}
Also used : TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) FlowVariable(org.knime.core.node.workflow.FlowVariable) FlowVariableException(org.knime.base.node.jsnippet.expression.FlowVariableException)

Example 4 with TypeConverter

use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.

the class FlowVariableRepository method isOfType.

/**
 * Returns true when getValueOfType(String, Class) does not throw
 * an TypeException when called with the given flow variable and the given
 * class name.
 * @param name the name of the flow variable
 * @param className the type
 * @return true when flow variable is of type.
 */
@SuppressWarnings("rawtypes")
public boolean isOfType(final String name, final Class className) {
    FlowVariable flowVar = getFlowVariable(name);
    if (null == flowVar) {
        throw new FlowVariableException("The flow variable with name \"" + name + "\" does not exist.");
    }
    TypeConverter converter = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
    return converter.canProvideJavaType(className);
}
Also used : TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) FlowVariable(org.knime.core.node.workflow.FlowVariable) FlowVariableException(org.knime.base.node.jsnippet.expression.FlowVariableException)

Example 5 with TypeConverter

use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.

the class InFieldsTableModel method getAllowedJavaTypes.

/**
 * {@inheritDoc}
 */
@Override
public Object[] getAllowedJavaTypes(final int row) {
    Object input = getValueAt(row, Column.COLUMN);
    if (input instanceof DataColumnSpec) {
        DataColumnSpec colSpec = (DataColumnSpec) input;
        final Collection<DataCellToJavaConverterFactory<?, ?>> factories = ConverterUtil.getFactoriesForSourceType(colSpec.getType()).stream().filter(factory -> JavaSnippet.getBuildPathFromCache(factory.getIdentifier()) != null).sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).collect(Collectors.toList());
        final Object[] array = factories.toArray(new Object[factories.size()]);
        return array;
    } else if (input instanceof FlowVariable) {
        FlowVariable flowVar = (FlowVariable) input;
        TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
        return typeConversion.canProvideJavaTypes();
    } else {
        return new Class[0];
    }
}
Also used : Collection(java.util.Collection) FlowVariable(org.knime.core.node.workflow.FlowVariable) TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) TypeProvider(org.knime.base.node.jsnippet.type.TypeProvider) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) JavaSnippet(org.knime.base.node.jsnippet.JavaSnippet) ConverterUtil(org.knime.base.node.jsnippet.type.ConverterUtil) DataColumnSpec(org.knime.core.data.DataColumnSpec) Map(java.util.Map) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Aggregations

TypeConverter (org.knime.base.node.jsnippet.type.flowvar.TypeConverter)6 FlowVariable (org.knime.core.node.workflow.FlowVariable)5 Optional (java.util.Optional)4 DataType (org.knime.core.data.DataType)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 JavaSnippet (org.knime.base.node.jsnippet.JavaSnippet)2 FlowVariableException (org.knime.base.node.jsnippet.expression.FlowVariableException)2 ConverterUtil (org.knime.base.node.jsnippet.type.ConverterUtil)2 TypeProvider (org.knime.base.node.jsnippet.type.TypeProvider)2 FieldType (org.knime.base.node.jsnippet.util.field.JavaField.FieldType)2 JavaToDataCellConverterFactory (org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)2 DataCellToJavaConverterFactory (org.knime.core.data.convert.java.DataCellToJavaConverterFactory)2 Type (org.knime.core.node.workflow.FlowVariable.Type)2 Array (java.lang.reflect.Array)1 ListCell (org.knime.core.data.collection.ListCell)1 ArrayToCollectionConverterFactory (org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory)1