Search in sources :

Example 21 with DataType

use of org.knime.core.data.DataType 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 22 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class OutFieldsTableModel method setValueAt.

/**
 * {@inheritDoc}
 */
@Override
public void setValueAt(final Object aValue, final int row, final int column) {
    // make sure setValue(Object, int, Column) is always called.
    Column col = getColumnForIndex(column);
    final FieldType fieldType = (m_flowVarsOnly) ? FieldType.FlowVariable : (FieldType) getValueAt(row, Column.FIELD_TYPE);
    if (col == Column.COLUMN) {
        if (aValue instanceof FlowVariable) {
            // make sure we do not keep a ConverterFactory in the JavaType column
            // when changing from DataColumnSpec.
            Object type = getValueAt(row, Column.JAVA_TYPE);
            if (type instanceof JavaToDataCellConverterFactory) {
                // set java type to dest type of converter factory
                setValueAt(((JavaToDataCellConverterFactory<?>) type).getSourceType(), row, Column.JAVA_TYPE);
            }
        } else if (aValue instanceof DataColumnSpec) {
            // make sure we do not keep a Java class in the JavaType column for Columns
            final Object type = getValueAt(row, Column.JAVA_TYPE);
            final DataType dataType = (DataType) getValueAt(row, Column.DATA_TYPE);
            if (type instanceof Class) {
                // find a DataCell converter which is able to convert from the new column type to the current java type
                final Optional<?> factory = ConverterUtil.getConverterFactory((Class<?>) type, dataType);
                if (factory.isPresent()) {
                    setValueAt(factory.get(), row, Column.JAVA_TYPE);
                }
            }
        }
    } else if (fieldType == FieldType.Column && col == Column.DATA_TYPE) {
        Object type = getValueAt(row, Column.JAVA_TYPE);
        Boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
        if (isCollection != null && type instanceof JavaToDataCellConverterFactory && aValue instanceof DataType) {
            final JavaToDataCellConverterFactory<?> converterFactory = (JavaToDataCellConverterFactory<?>) type;
            DataType dataType = (DataType) aValue;
            Class<?> javaType = converterFactory.getSourceType();
            if (isCollection && !dataType.isCollectionType()) {
                dataType = ListCell.getCollectionType(dataType);
                if (!(converterFactory instanceof ArrayToCollectionConverterFactory)) {
                    javaType = Array.newInstance(javaType, 0).getClass();
                }
            } else if (!isCollection && javaType.isArray()) {
                if (converterFactory instanceof ArrayToCollectionConverterFactory) {
                    javaType = javaType.getComponentType();
                }
            }
            // Try to find a converter factory which converts the newly selected DataType to the selected JavaType.
            final Optional<?> factory = ConverterUtil.getConverterFactory(javaType, dataType);
            if (factory.isPresent()) {
                setValueAt(factory.get(), row, Column.JAVA_TYPE);
            } else {
                // If there is no way to convert to the existing Java type, use the preferred type instead.
                final Optional<JavaToDataCellConverterFactory<?>> preferred = ConverterUtil.getPreferredFactoryForDestinationType(dataType);
                if (preferred.isPresent()) {
                    setValueAt(preferred.get(), row, Column.JAVA_TYPE);
                }
            }
        }
    }
    super.setValueAt(aValue, row, column);
    /* If this was the collection check box, post process to set the appropriate converter factory. */
    if (col == Column.IS_COLLECTION) {
        Object type = getValueAt(row, Column.DATA_TYPE);
        if (type != null) {
            // make sure DataType is updated to a Collection type or back to a single element type
            setValueAt(type, row, getIndex(Column.DATA_TYPE));
        }
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ArrayToCollectionConverterFactory(org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) FlowVariable(org.knime.core.node.workflow.FlowVariable) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 23 with DataType

use of org.knime.core.data.DataType 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 24 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class ExtractDateTimeFieldsNodeDialog method refreshFieldsSelectionsEnabled.

private void refreshFieldsSelectionsEnabled() {
    if (m_dialogCompColSelect.getSelectedAsSpec() != null) {
        final DataType type = m_dialogCompColSelect.getSelectedAsSpec().getType();
        final boolean isDate = ExtractDateTimeFieldsNodeModel.isDateType(type);
        for (final DialogComponentBoolean dc : m_dialogCompDateFields) {
            dc.getModel().setEnabled(isDate);
        }
        final boolean isTime = ExtractDateTimeFieldsNodeModel.isTimeType(type);
        for (final DialogComponentBoolean dc : m_dialogCompTimeFields) {
            dc.getModel().setEnabled(isTime);
        }
        final boolean isZoned = ExtractDateTimeFieldsNodeModel.isZonedType(type);
        for (final DialogComponentBoolean dc : m_dialogCompTimeZoneFields) {
            dc.getModel().setEnabled(isZoned);
        }
    }
}
Also used : DataType(org.knime.core.data.DataType) DialogComponentBoolean(org.knime.core.node.defaultnodesettings.DialogComponentBoolean)

Example 25 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class DateTimeBasedRowFilterNodeDialog method updateEnableStatusOfDateTimeComponents.

private void updateEnableStatusOfDateTimeComponents(final SettingsModelDateTime startDateTimeModel, final SettingsModelDateTime endDateTimeModel) {
    try {
        final DataType type = m_dialogCompColSelection.getSelectedAsSpec().getType();
        startDateTimeModel.setEnabled(!((SettingsModelBoolean) m_dialogCompStartAlwaysNow.getModel()).getBooleanValue() && ((SettingsModelBoolean) m_dialogCompStartBoolean.getModel()).getBooleanValue());
        startDateTimeModel.setUseDate(!type.isCompatible(LocalTimeValue.class));
        startDateTimeModel.setUseTime(!type.isCompatible(LocalDateValue.class));
        startDateTimeModel.setUseZone(type.isCompatible(ZonedDateTimeValue.class));
        endDateTimeModel.setEnabled(!((SettingsModelBoolean) m_dialogCompEndAlwaysNow.getModel()).getBooleanValue() && ((SettingsModelBoolean) m_dialogCompEndBoolean.getModel()).getBooleanValue());
        endDateTimeModel.setUseDate(!type.isCompatible(LocalTimeValue.class));
        endDateTimeModel.setUseTime(!type.isCompatible(LocalDateValue.class));
        endDateTimeModel.setUseZone(type.isCompatible(ZonedDateTimeValue.class));
    } catch (NullPointerException e) {
    }
}
Also used : SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) DataType(org.knime.core.data.DataType) ZonedDateTimeValue(org.knime.core.data.time.zoneddatetime.ZonedDateTimeValue)

Aggregations

DataType (org.knime.core.data.DataType)330 DataColumnSpec (org.knime.core.data.DataColumnSpec)142 DataTableSpec (org.knime.core.data.DataTableSpec)101 DataCell (org.knime.core.data.DataCell)96 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)95 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)71 DoubleValue (org.knime.core.data.DoubleValue)67 DataRow (org.knime.core.data.DataRow)61 ArrayList (java.util.ArrayList)55 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)34 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)32 DefaultRow (org.knime.core.data.def.DefaultRow)24 HashSet (java.util.HashSet)23 HashMap (java.util.HashMap)20 StringCell (org.knime.core.data.def.StringCell)20 NominalValue (org.knime.core.data.NominalValue)18 DoubleCell (org.knime.core.data.def.DoubleCell)18 IntCell (org.knime.core.data.def.IntCell)18 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)18 ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)18